Skip to content

cuda.core: fix _arr_is_c_contiguous discriminator for numba arrays#1998

Merged
leofang merged 1 commit intoNVIDIA:mainfrom
Andy-Jost:fix-arr-is-c-contiguous-numba
May 1, 2026
Merged

cuda.core: fix _arr_is_c_contiguous discriminator for numba arrays#1998
leofang merged 1 commit intoNVIDIA:mainfrom
Andy-Jost:fix-arr-is-c-contiguous-numba

Conversation

@Andy-Jost
Copy link
Copy Markdown
Contributor

@Andy-Jost Andy-Jost commented Apr 30, 2026

Summary

Fix the discriminator in _arr_is_c_contiguous so the numba-cuda
parametrizations of the StridedMemoryView GPU tests pass instead of
raising AttributeError.

Changes

_arr_is_c_contiguous (in cuda_core/tests/test_utils.py) checked
hasattr(arr, "flags"), which is True for both numpy arrays and
numba DeviceNDArray. For numba arr.flags is a plain dict, so the
truthy branch falls into arr.flags.c_contiguous and raises:

AttributeError: 'dict' object has no attribute 'c_contiguous'

Discriminate on the flags object instead, mirroring the sibling
_arr_is_writeable helper a few lines below:

return arr.flags.c_contiguous if hasattr(arr.flags, "c_contiguous") else arr.flags["C_CONTIGUOUS"]

One-character placement bug; one-line patch.

Test Coverage

Unblocks the six numba-cuda parametrizations that have been failing:

  • TestViewGPU::test_args_viewable_as_strided_memory_gpu[numba-cuda-int8]
  • TestViewGPU::test_args_viewable_as_strided_memory_gpu[numba-cuda-float32]
  • TestViewGPU::test_strided_memory_view_cpu[numba-cuda-int8]
  • TestViewGPU::test_strided_memory_view_cpu[numba-cuda-float32]
  • TestViewGPU::test_strided_memory_view_init[numba-cuda-int8]
  • TestViewGPU::test_strided_memory_view_init[numba-cuda-float32]

Verified locally on a GPU host: the previously failing parametrizations
now pass, no other tests regress.

Pre-commit clean (pre-commit run --all-files).

`_arr_is_c_contiguous` checked `hasattr(arr, "flags")`, which is True
for both numpy arrays and numba `DeviceNDArray`. For numba `arr.flags`
is a plain dict, so the truthy branch falls into `arr.flags.c_contiguous`
and raises `AttributeError: 'dict' object has no attribute
'c_contiguous'`.

Discriminate on the flags object instead, mirroring the sibling
`_arr_is_writeable` helper. Unblocks six numba-cuda parametrizations:

- TestViewGPU::test_args_viewable_as_strided_memory_gpu[numba-cuda-{int8,float32}]
- TestViewGPU::test_strided_memory_view_cpu[numba-cuda-{int8,float32}]
- TestViewGPU::test_strided_memory_view_init[numba-cuda-{int8,float32}]

Made-with: Cursor
@Andy-Jost Andy-Jost added this to the cuda.core v1.0.0 milestone Apr 30, 2026
@Andy-Jost Andy-Jost added bug Something isn't working cuda.core Everything related to the cuda.core module labels Apr 30, 2026
@Andy-Jost Andy-Jost self-assigned this Apr 30, 2026
@Andy-Jost Andy-Jost requested a review from leofang April 30, 2026 20:02
@github-actions

This comment has been minimized.

@leofang leofang added P1 Medium priority - Should do test Improvements or additions to tests labels May 1, 2026
@leofang leofang merged commit 0f85061 into NVIDIA:main May 1, 2026
175 of 197 checks passed
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 1, 2026

Doc Preview CI
Preview removed because the pull request was closed or merged.
@Andy-Jost Andy-Jost deleted the fix-arr-is-c-contiguous-numba branch May 1, 2026 17:18
leofang added a commit to leofang/cuda-python that referenced this pull request May 1, 2026
Per @rwgk's review:
- Flip strides check to branch on view.strides (all 3 _check_view)
- Add _arr_dtype helper using __cuda_array_interface__["typestr"]
  for torch tensors, restore dtype assertion in CAI _check_view
- Merge main to pick up NVIDIA#1998 (numba flags fix)

Verified locally: 76/76 tests pass across all three test classes.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
leofang added a commit that referenced this pull request May 1, 2026
…1999)

* Fix torch-incompatible assertions in TestViewCudaArrayInterfaceGPU

The _check_view method in TestViewCudaArrayInterfaceGPU was missed
during the tensor bridge refactor (#1894) and still used raw numpy
attributes (in_arr.size, in_arr.strides, in_arr.flags, etc.) that
don't work with torch tensors. Use the _arr_* helpers that #1894
added for torch/numpy compatibility.

Caught by the nightly optional-dependency CI (#1987).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* Fix strides assertion for torch CAI: allow explicit C-contiguous strides

torch's __cuda_array_interface__ always reports strides, even for
C-contiguous tensors. Use the same assertion pattern as the other
_check_view methods: allow strides to equal the C-contiguous values
instead of requiring None.

Verified locally: 7/7 torch CAI tests pass.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* Unify strides assertion pattern across all _check_view methods

Use the same if/else pattern with `in (None, strides_in_counts)` in
all three _check_view methods for consistency. Previously TestViewCPU
and TestViewCudaArrayInterfaceGPU used a one-liner that was harder
to read and behaved slightly differently.

Verified locally: 66/66 tests pass across TestViewCPU, TestViewGPU,
and TestViewCudaArrayInterfaceGPU (including all torch variants).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* Address review: flip strides assertion, add _arr_dtype, merge main

Per @rwgk's review:
- Flip strides check to branch on view.strides (all 3 _check_view)
- Add _arr_dtype helper using __cuda_array_interface__["typestr"]
  for torch tensors, restore dtype assertion in CAI _check_view
- Merge main to pick up #1998 (numba flags fix)

Verified locally: 76/76 tests pass across all three test classes.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working cuda.core Everything related to the cuda.core module P1 Medium priority - Should do test Improvements or additions to tests

2 participants