[FIX] is_contiguous returns True for empty tensors (numel() == 0) - #610
Closed
tqchen wants to merge 1 commit into
Closed
Conversation
An empty tensor — one where any shape dimension is 0 — has no elements and its stride values carry no meaning. The previous implementation iterated over strides and returned False when it encountered a stride that did not match the expected row-major stride (e.g. strides all-zero for a shape like (4, 0, 4)), which is incorrect. This commit adds an early return when numel == 0 to both: - C++: `IsContiguous(const DLTensor&)` in `include/tvm/ffi/container/tensor.h` (checks any shape dim == 0 before the stride loop) - Python/Cython: `is_contiguous()` in `python/tvm_ffi/cython/tensor.pxi` (same shape-zero scan before the stride loop) Semantics now match NumPy and PyTorch: an empty tensor is trivially contiguous. Tests added: - C++: `TEST(Tensor, EmptyTensorIsContiguous)` in `tests/cpp/test_tensor.cc` — constructs a strided empty tensor with strides (0,0,0) and asserts `IsContiguous() == true`. - Python: `test_empty_tensor_attributes` in `tests/python/test_tensor.py` — existing test updated to use the correct numpy-reported strides (0,0,0) for shape (4,0,4) and now asserts `is_contiguous() == True`. Closes apache#607 (comment)
Contributor
There was a problem hiding this comment.
Code Review
This pull request ensures that empty tensors (where at least one dimension is 0) are treated as trivially contiguous in both C++ and Cython, matching NumPy and PyTorch semantics. It also adds a C++ test and updates a Python test to verify this behavior. There are no review comments, so I have no feedback to provide.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
Member
Author
|
Folding this into #607. Closing in favor of that PR. |
tqchen
deleted the
tvm-ffi-is-contiguous-returns-true-early-when-numel-0-cxx-python
branch
June 6, 2026 00:54
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
[FIX] is_contiguous returns True for empty tensors (numel() == 0)
An empty tensor — one where any shape dimension is 0 — has no elements
and its stride values carry no meaning. The previous implementation
iterated over strides and returned False when it encountered a stride
that did not match the expected row-major stride (e.g. strides all-zero
for a shape like (4, 0, 4)), which is incorrect.
This commit adds an early return when numel == 0 to both:
IsContiguous(const DLTensor&)ininclude/tvm/ffi/container/tensor.h(checks any shape dim == 0before the stride loop)
is_contiguous()inpython/tvm_ffi/cython/tensor.pxi(same shape-zero scan before thestride loop)
Semantics now match NumPy and PyTorch: an empty tensor is trivially
contiguous.
Tests added:
TEST(Tensor, EmptyTensorIsContiguous)intests/cpp/test_tensor.cc— constructs a strided empty tensor withstrides (0,0,0) and asserts
IsContiguous() == true.test_empty_tensor_attributesintests/python/test_tensor.py— existing test updated to use thecorrect numpy-reported strides (0,0,0) for shape (4,0,4) and now
asserts
is_contiguous() == True.Closes #607 (comment)