From 60e80dc4078f9d1c49b6767d22b97f456bdaf086 Mon Sep 17 00:00:00 2001 From: Vladislav Perevezentsev Date: Wed, 20 May 2026 05:09:51 -0700 Subject: [PATCH 1/3] Add strides validation to usm_ndarray constructor for new allocations --- dpnp/tensor/_usmarray.pyx | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/dpnp/tensor/_usmarray.pyx b/dpnp/tensor/_usmarray.pyx index 7d90ffeb05a..2c2d9becd02 100644 --- a/dpnp/tensor/_usmarray.pyx +++ b/dpnp/tensor/_usmarray.pyx @@ -378,6 +378,20 @@ cdef class usm_ndarray: elif isinstance(buffer, (str, bytes)): if isinstance(buffer, bytes): buffer = buffer.decode("UTF-8") + if strides is not None and ary_min_displacement < 0: + self._cleanup() + raise ValueError( + "strides={} result in a negative memory displacement " + "and are not allowed when allocating new " + "memory".format(strides)) + if strides is not None and ( + (ary_max_displacement - ary_min_displacement + 1) > ary_nelems + ): + self._cleanup() + raise ValueError( + "strides={} is incompatible with shape={} when " + "allocating new memory because the memory footprint " + "exceeds the number of elements".format(strides, shape)) _offset = -ary_min_displacement if (buffer == "shared"): _buffer = dpmem.MemoryUSMShared(ary_nbytes, From 29512748689d4f005e8f554776d22b3b549d288e Mon Sep 17 00:00:00 2001 From: Vladislav Perevezentsev Date: Wed, 20 May 2026 05:10:22 -0700 Subject: [PATCH 2/3] Add test_ctor_invalid_strides --- dpnp/tests/tensor/test_usm_ndarray_ctor.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/dpnp/tests/tensor/test_usm_ndarray_ctor.py b/dpnp/tests/tensor/test_usm_ndarray_ctor.py index b03a01ad370..0ce5557e2c0 100644 --- a/dpnp/tests/tensor/test_usm_ndarray_ctor.py +++ b/dpnp/tests/tensor/test_usm_ndarray_ctor.py @@ -123,9 +123,6 @@ def test_usm_ndarray_flags(): f = dpt.usm_ndarray((5, 0, 1), dtype="i4", strides=(1, 0, 1)).flags assert f.fc assert f.forc - assert not dpt.usm_ndarray( - (5, 1, 1), dtype="i4", strides=(2, 0, 1) - ).flags.forc x = dpt.empty(5, dtype="u2") assert x.flags.writable is True @@ -1146,6 +1143,19 @@ def test_ctor_invalid(): dpt.usm_ndarray((4,), dtype="u1", buffer=m, strides={"not": "valid"}) +def test_ctor_invalid_strides(): + try: + dpt.usm_ndarray((1,), dtype="i4") + except dpctl.SyclDeviceCreationError: + pytest.skip("No SYCL devices available") + # negative displacement + with pytest.raises(ValueError): + dpt.usm_ndarray((2, 3, 4), dtype="i4", strides=(-1, 1, 1)) + # oversized memory footprint + with pytest.raises(ValueError): + dpt.usm_ndarray((2, 3, 4), dtype="i4", strides=(1, 16, 128)) + + def test_reshape(): try: X = dpt.usm_ndarray((5, 5), "i4") From cd3a953b65cc901b4410ed4b5f24af9a139f2c9a Mon Sep 17 00:00:00 2001 From: Vladislav Perevezentsev Date: Wed, 20 May 2026 05:20:49 -0700 Subject: [PATCH 3/3] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a738267346..956055e70e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,7 @@ This release is compatible with NumPy 2.4.5. * Fixed incorrect `dpnp.tensor.acosh` result for `complex(±0, NaN)` special case to match the Python Array API specification [#2914](https://github.com/IntelPython/dpnp/pull/2914) * Fixed fork PR documentation workflow failures by implementing conditional publishing strategy: upstream PRs publish to GitHub Pages with comment, fork PRs upload artifacts [#2910](https://github.com/IntelPython/dpnp/pull/2910) * Fixed missing `libtensor` headers in the installed `dpnp` package [#2915](https://github.com/IntelPython/dpnp/pull/2915) +* Fixed missing strides validation in `dpnp.tensor.usm_ndarray` constructor when allocating new memory [#2927](https://github.com/IntelPython/dpnp/pull/2927) ### Security