Skip to content

Support buffer protocol objects as advanced index keys#2889

Open
vlad-perevezentsev wants to merge 3 commits intofix_inplace_indexind_4dfrom
support_buffer_protocol_indexing
Open

Support buffer protocol objects as advanced index keys#2889
vlad-perevezentsev wants to merge 3 commits intofix_inplace_indexind_4dfrom
support_buffer_protocol_indexing

Conversation

@vlad-perevezentsev
Copy link
Copy Markdown
Contributor

This PR proposes by adding support for buffer protocol objects (array.array, memoryview etc.) as advanced index keys
These changes were proposed in #2872 as an extension to the advanced indexing support.

  • 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?

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 4, 2026

Array API standard conformance tests for dpnp=0.21.0dev0=py313h509198e_19 ran successfully.
Passed: 1357
Failed: 3
Skipped: 16

Comment thread dpnp/dpnp_array.py
if isinstance(x, numpy.ndarray):
return x
# convert buffer protocol objects (array.array, memoryview, etc.)
try:
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.

seems __buffer__ has been added with 3.12: https://docs.python.org/3/reference/datamodel.html#object.__buffer__
https://docs.python.org/3/library/collections.abc.html#collections.abc.Buffer
https://peps.python.org/pep-0688/

we should probably start to support it as well. Maybe add a TODO to check for __buffer__/use collections.abc.Buffer and then call memoryview if it fails

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.

actually I misunderstood the PEP: this feature is already implemented, we can add a check for Python version and check against __buffer__ or try memoryview only if it's <3.12

Comment thread dpnp/dpnp_array.py
return arr
if isinstance(x, numpy.ndarray):
return x
# convert buffer protocol objects (array.array, memoryview, etc.)
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.

Can all that use cases covered converting x to numpy.ndarray?
Do we need so complicated if-logic?

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.

here is CuPy implementation of a similar idea for a reference:
https://github.com/cupy/cupy/blob/main/cupy/_core/_routines_indexing.pyx#L247

can take a similar approach too in some cases (best to leave converting to usm_ndarray to the tensor layer, though, for queue propagation reasons)

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.

3 participants