Skip to content

Fix for dpnp buffer-arg ndarray ignoring offset - #3068

Open
abagusetty wants to merge 3 commits into
IntelPython:masterfrom
abagusetty:fix-dpnp-bufferarg-offset
Open

Fix for dpnp buffer-arg ndarray ignoring offset#3068
abagusetty wants to merge 3 commits into
IntelPython:masterfrom
abagusetty:fix-dpnp-bufferarg-offset

Conversation

@abagusetty

Copy link
Copy Markdown
Contributor

Fixes: #3067

  • Have you provided a meaningful PR description?
  • Have you added a test, reproducer or referred to an issue with a reproducer?
  • Have you tested your changes locally for CPU and GPU devices?
  • Have you made sure that new changes do not introduce compiler warnings?
  • Have you checked performance impact of proposed changes?
  • Have you added documentation for your changes, if necessary?
  • Have you added your changes to the changelog?

@intel-python-devops

Copy link
Copy Markdown

Can one of the admins verify this patch?

@antonwolfy antonwolfy added this to the 0.21.0 release milestone Sep 11, 2026
@coveralls

coveralls commented Sep 11, 2026

Copy link
Copy Markdown
Collaborator

Coverage Status

coverage: 78.48% (+0.003%) from 78.477% — abagusetty:fix-dpnp-bufferarg-offset into IntelPython:master

Comment thread dpnp/dpnp_array.py Outdated
Comment thread dpnp/dpnp_array.py Outdated
offset += buffer._element_offset

if dtype is None and hasattr(buffer, "dtype"):
if isinstance(buffer, dpt.usm_ndarray):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done!

Comment thread dpnp/dpnp_array.py Outdated
(dpnp.int16, dpnp.int64),
],
)
def test_nonzero_offset_buffer_ctor_dtype_mismatch(self, src_dt, new_dt):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Two small gaps worth filling while we're here (non-blocking):

  1. No test exercises a non-zero incoming offset= arg together with a dtype-mismatched buffer — the offset=1 case uses a same-itemsize dtype, so the offset += add_offset addition is never checked with a non-trivial add_offset. A case like dpnp.ndarray((size,), dtype=new_dt, buffer=sl, offset=1) would cover it.

  2. The misalignment ValueError is only asserted for a dpnp_array buffer (test_misaligned_offset_buffer_ctor_error). Since the fix routes bare usm_ndarray buffers through the same branch, a matching case with buffer=base[3:].get_array() would guard that path too.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Addressed both aspects

(dpnp.int16, dpnp.int64),
],
)
def test_nonzero_offset_buffer_ctor_dtype_mismatch(self, src_dt, new_dt):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This test hard-fails on devices without native fp64/complex128 support.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

guarded it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

dpnp.ndarray ctor returns a view at wrong addr with buffer= has non-zero offset

4 participants