cuda-python icon indicating copy to clipboard operation
cuda-python copied to clipboard

Fail on construction of `StridedMemoryView` when unsupported strides are used

Open cpcloud opened this issue 3 weeks ago • 1 comments

Since strides in SMV are defined as counts of itemsize, they are a subset of what Numpy can handle and we should make sure arrays we can't handle are properly rejected. It seems that this is /mostly/ the case, but due to the lazy construction, the exception happens a little later than ideal. For example:

def test_from_array_interface_exception(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"]
    # with pytest.raises(ValueError) as e:
    smv = StridedMemoryView.from_array_interface(x)
    with pytest.raises(ValueError) as e:
        smv.strides
    assert "strides must be divisible by itemsize" in str(e)

In user code, this could make it harder for the user to find the source of the error.

But given this is a latent bug that I think also applies to CAI, and not introduced by this PR, I'd be fine with (a) adding this test above, (b) creating a follow-up issue to detect this case during construction.

Originally posted by @mdboom in https://github.com/NVIDIA/cuda-python/pull/1428#discussion_r2665181777

cpcloud avatar Jan 06 '26 15:01 cpcloud