Fix for dpnp buffer-arg ndarray ignoring offset - #3068
Conversation
|
Can one of the admins verify this patch? |
| offset += buffer._element_offset | ||
|
|
||
| if dtype is None and hasattr(buffer, "dtype"): | ||
| if isinstance(buffer, dpt.usm_ndarray): |
There was a problem hiding this comment.
The dtype-defaulting could be hoisted out of the branch so it runs once for any buffer with a .dtype, instead of being duplicated between the inner if dtype is None and the elif. That also makes the ordering explicit: the offset math needs dtype for new_itemsize.
if isinstance(buffer, dpnp_array):
buffer = buffer.get_array()
if dtype is None and hasattr(buffer, "dtype"):
dtype = buffer.dtype
if isinstance(buffer, dpt.usm_ndarray):
byte_offset = buffer._element_offset * buffer.itemsize
new_itemsize = dpnp.dtype(dtype).itemsize
add_offset, rem = divmod(byte_offset, new_itemsize)
if rem != 0:
raise ValueError(...)
offset += add_offset| (dpnp.int16, dpnp.int64), | ||
| ], | ||
| ) | ||
| def test_nonzero_offset_buffer_ctor_dtype_mismatch(self, src_dt, new_dt): |
There was a problem hiding this comment.
Two small gaps worth filling while we're here (non-blocking):
-
No test exercises a non-zero incoming
offset=arg together with a dtype-mismatched buffer — theoffset=1case uses a same-itemsize dtype, so theoffset += add_offsetaddition is never checked with a non-trivialadd_offset. A case likedpnp.ndarray((size,), dtype=new_dt, buffer=sl, offset=1)would cover it. -
The misalignment
ValueErroris only asserted for adpnp_arraybuffer (test_misaligned_offset_buffer_ctor_error). Since the fix routes bareusm_ndarraybuffers through the same branch, a matching case withbuffer=base[3:].get_array()would guard that path too.
There was a problem hiding this comment.
Addressed both aspects
| (dpnp.int16, dpnp.int64), | ||
| ], | ||
| ) | ||
| def test_nonzero_offset_buffer_ctor_dtype_mismatch(self, src_dt, new_dt): |
There was a problem hiding this comment.
This test hard-fails on devices without native fp64/complex128 support.
Fixes: #3067