Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 14 additions & 0 deletions dpnp/tensor/_usmarray.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Comment on lines +384 to +386
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
"strides={} result in a negative memory displacement "
"and are not allowed when allocating new "
"memory".format(strides))
f"strides={strides} result in a negative memory displacement "
"and are not allowed when allocating new "
"memory"

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))
Comment on lines +392 to +394
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
"strides={} is incompatible with shape={} when "
"allocating new memory because the memory footprint "
"exceeds the number of elements".format(strides, shape))
f"strides={strides} is incompatible with shape={shape} when "
"allocating new memory because the memory footprint "
"exceeds the number of elements"

_offset = -ary_min_displacement
if (buffer == "shared"):
_buffer = dpmem.MemoryUSMShared(ary_nbytes,
Expand Down
16 changes: 13 additions & 3 deletions dpnp/tests/tensor/test_usm_ndarray_ctor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down
Loading