From 0fe3e5822ff11e4799a2946cd312347f9d0094d5 Mon Sep 17 00:00:00 2001 From: Phillip Cloud <417981+cpcloud@users.noreply.github.com> Date: Fri, 6 Mar 2026 09:24:30 -0500 Subject: [PATCH 1/2] Fail fast on unsupported StridedMemoryView strides. Validate CAI and array-interface layout during StridedMemoryView construction so non-itemsize-divisible strides fail immediately. Add regression coverage to assert constructor-time failures for both interfaces. Made-with: Cursor --- cuda_core/cuda/core/_memoryview.pyx | 4 ++++ cuda_core/tests/test_utils.py | 22 +++++++++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/cuda_core/cuda/core/_memoryview.pyx b/cuda_core/cuda/core/_memoryview.pyx index 94824e1c4d..0e1df726c0 100644 --- a/cuda_core/cuda/core/_memoryview.pyx +++ b/cuda_core/cuda/core/_memoryview.pyx @@ -1096,6 +1096,8 @@ cpdef StridedMemoryView view_as_cai(obj, stream_ptr, view=None): buf.exporting_obj = obj buf.metadata = cai_data buf.dl_tensor = NULL + # Validate shape/strides/typestr eagerly so constructor paths fail fast. + buf.get_layout() buf.ptr, buf.readonly = cai_data["data"] buf.is_device_accessible = True if buf.ptr != 0: @@ -1138,6 +1140,8 @@ cpdef StridedMemoryView view_as_array_interface(obj, view=None): buf.exporting_obj = obj buf.metadata = data buf.dl_tensor = NULL + # Validate shape/strides/typestr eagerly so constructor paths fail fast. + buf.get_layout() buf.ptr, buf.readonly = data["data"] buf.is_device_accessible = False buf.device_id = handle_return(driver.cuCtxGetDevice()) diff --git a/cuda_core/tests/test_utils.py b/cuda_core/tests/test_utils.py index 9e79f48313..e75fa9e669 100644 --- a/cuda_core/tests/test_utils.py +++ b/cuda_core/tests/test_utils.py @@ -582,10 +582,26 @@ def test_from_array_interface_unsupported_strides(init_cuda): # Create an array with strides that aren't a multiple of itemsize x = np.array([(1, 2.0), (3, 4.0)], dtype=[("a", "i4"), ("b", "f8")]) b = x["b"] - smv = StridedMemoryView.from_array_interface(b) with pytest.raises(ValueError, match="strides must be divisible by itemsize"): - # TODO: ideally this would raise on construction - smv.strides # noqa: B018 + StridedMemoryView.from_array_interface(b) + + +def test_from_cuda_array_interface_unsupported_strides(init_cuda): + cai_obj = type( + "UnsupportedStridesCAI", + (), + { + "__cuda_array_interface__": { + "shape": (2,), + "strides": (10,), + "typestr": " Date: Fri, 6 Mar 2026 16:33:50 -0500 Subject: [PATCH 2/2] Expand CAI stride edge-case coverage for StridedMemoryView. Add explicit CAI tests for zero, empty-array, and negative-stride layouts so stride divisibility validation remains covered for uncommon but valid cases. Require `shape` and `strides` as keyword-only args in the synthetic CAI helper to keep test call sites unambiguous. Made-with: Cursor --- cuda_core/tests/test_utils.py | 43 ++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/cuda_core/tests/test_utils.py b/cuda_core/tests/test_utils.py index e75fa9e669..e7ebb5bb52 100644 --- a/cuda_core/tests/test_utils.py +++ b/cuda_core/tests/test_utils.py @@ -586,24 +586,51 @@ def test_from_array_interface_unsupported_strides(init_cuda): StridedMemoryView.from_array_interface(b) -def test_from_cuda_array_interface_unsupported_strides(init_cuda): - cai_obj = type( - "UnsupportedStridesCAI", +def _make_cuda_array_interface_obj(*, shape, strides, typestr="