Skip to content

Support buffer protocol objects as advanced index keys#2889

Open
vlad-perevezentsev wants to merge 15 commits intomasterfrom
support_buffer_protocol_indexing
Open

Support buffer protocol objects as advanced index keys#2889
vlad-perevezentsev wants to merge 15 commits intomasterfrom
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_25 ran successfully.
Passed: 1358
Failed: 2
Skipped: 16

Comment thread dpnp/dpnp_array.py Outdated
Comment thread dpnp/dpnp_array.py Outdated
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)

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.

I don't think we need to fully follow the same CuPy implementation in _prepare_slice_list, because it seems CuPy mimics faulty NumPy behavior sometimes.

Not sure if I got the comment fully correctly, but in general I meant something like:

    if x is None or x is Ellipsis or isinstance(x, (dpt.usm_ndarray, slice, numpy.ndarray)):
        return x
    if isinstance(x, dpnp_array):
        return x.get_array()

    try:
        x = numpy.asarray(x)
    except ... :
        ...
        raise ...
    ...
    return x

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.

yep, agreed mostly, though problem is, we don't really want to send integers or boolean Python scalars to numpy arrays, which is why we may need something like:

        elif numpy.isscalar(s):
            if not isinstance(s, (bool, numpy.bool_)):
                # keep scalar int
                continue

which is in cupy implementation. We could handle range, list, tuple through numpy though

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.

I don not think try/except around numpy.asarray is necessary because invalid index types either raise naturally or produce arrays with unsupported dtypes that the tensor layer rejects with IndexError

Regarding elif numpy.isscalar(s) I replaced it with isinstance(x, (int, numpy.generic)) because np.isscalar(memoryview(array.array("l", [0, 1, 2]))) returns True
bool check is not needed since bool is subclass of int

Base automatically changed from fix_inplace_indexind_4d to master May 6, 2026 09:41
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 6, 2026

View rendered docs @ https://intelpython.github.io/dpnp/pull/2889/index.html

@antonwolfy antonwolfy added this to the 0.21.0 release milestone May 6, 2026
Comment thread dpnp/dpnp_array.py
arr = numpy.asarray(x)
# cast empty arrays (float64 in NumPy) to intp
# for correct tensor indexing
if arr.size == 0 and arr.dtype.kind == "f":
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.

Why do we need to explicitly check arr.dtype.kind == "f"?

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