Skip to content

[FIX] is_contiguous returns True for empty tensors (numel() == 0) - #610

Closed
tqchen wants to merge 1 commit into
apache:mainfrom
tqchen:tvm-ffi-is-contiguous-returns-true-early-when-numel-0-cxx-python
Closed

[FIX] is_contiguous returns True for empty tensors (numel() == 0)#610
tqchen wants to merge 1 commit into
apache:mainfrom
tqchen:tvm-ffi-is-contiguous-returns-true-early-when-numel-0-cxx-python

Conversation

@tqchen

@tqchen tqchen commented Jun 6, 2026

Copy link
Copy Markdown
Member

[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:

  • 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 #607 (comment)

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)

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@tqchen

tqchen commented Jun 6, 2026

Copy link
Copy Markdown
Member Author

Folding this into #607. Closing in favor of that PR.

@tqchen tqchen closed this Jun 6, 2026
@tqchen
tqchen deleted the tvm-ffi-is-contiguous-returns-true-early-when-numel-0-cxx-python branch June 6, 2026 00:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant