-
Notifications
You must be signed in to change notification settings - Fork 26
Support buffer protocol objects as advanced index keys #2889
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: fix_inplace_indexind_4d
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,8 +52,9 @@ def _unwrap_index_element(x): | |
| """ | ||
| Unwrap a single index element for the tensor indexing layer. | ||
|
|
||
| Converts dpnp arrays to usm_ndarray and array-like objects (range, list) | ||
| to numpy arrays with intp dtype for NumPy-compatible advanced indexing. | ||
| Converts dpnp arrays to usm_ndarray and array-like objects (range, list, | ||
| buffer protocol objects) to numpy arrays for NumPy-compatible advanced | ||
| indexing. | ||
|
|
||
| """ | ||
|
|
||
|
|
@@ -71,6 +72,16 @@ def _unwrap_index_element(x): | |
| if arr.size == 0: | ||
| arr = arr.astype(numpy.intp) | ||
| return arr | ||
| if isinstance(x, numpy.ndarray): | ||
| return x | ||
| # convert buffer protocol objects (array.array, memoryview, etc.) | ||
| try: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seems we should probably start to support it as well. Maybe add a TODO to check for
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| mv = memoryview(x) | ||
| except TypeError: | ||
| return x | ||
| # 0-d buffers are handled by the tensor layer | ||
| if mv.ndim > 0: | ||
| return numpy.asarray(x) | ||
| return x | ||
|
|
||
|
|
||
|
|
||
There was a problem hiding this comment.
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
xto numpy.ndarray?Do we need so complicated if-logic?
There was a problem hiding this comment.
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_ndarrayto the tensor layer, though, for queue propagation reasons)There was a problem hiding this comment.
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:
There was a problem hiding this comment.
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:
which is in cupy implementation. We could handle
range,list,tuplethrough numpy though