-
Notifications
You must be signed in to change notification settings - Fork 26
Fix in-place indexing with range/list for 4D arrays #2872
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: master
Are you sure you want to change the base?
Changes from all commits
8d0149e
672a45c
7ef44b7
5f5b0b1
830ef88
18bb358
e64845c
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 |
|---|---|---|
|
|
@@ -38,6 +38,8 @@ | |
|
|
||
| import warnings | ||
|
|
||
| import numpy | ||
|
|
||
| import dpnp | ||
| import dpnp.tensor as dpt | ||
| import dpnp.tensor._type_utils as dtu | ||
|
|
@@ -46,24 +48,45 @@ | |
| from .exceptions import AxisError | ||
|
|
||
|
|
||
| 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. | ||
|
|
||
| """ | ||
|
|
||
| if isinstance(x, dpt.usm_ndarray): | ||
| return x | ||
| if isinstance(x, dpnp_array): | ||
| return x.get_array() | ||
| if isinstance(x, range): | ||
| return numpy.asarray(x, dtype=numpy.intp) | ||
| if isinstance(x, list): | ||
| # keep boolean lists as boolean | ||
| arr = numpy.asarray(x) | ||
| # cast empty lists (float64 in NumPy) to intp | ||
| # for correct tensor indexing | ||
| if arr.size == 0: | ||
| arr = arr.astype(numpy.intp) | ||
| return arr | ||
| return x | ||
|
Comment on lines
+62
to
+74
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. this is good approach, but if we're going to support i.e., with NumPy
Contributor
Author
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. I will implement this in the following PR |
||
|
|
||
|
|
||
| def _get_unwrapped_index_key(key): | ||
| """ | ||
| Get an unwrapped index key. | ||
|
|
||
| Return a key where each nested instance of DPNP array is unwrapped into | ||
| USM ndarray for further processing in DPCTL advanced indexing functions. | ||
| USM ndarray, and array-like objects (range, list) are converted to numpy | ||
| arrays for further processing in advanced indexing functions. | ||
|
|
||
| """ | ||
|
|
||
| if isinstance(key, tuple): | ||
| if any(isinstance(x, dpnp_array) for x in key): | ||
| # create a new tuple from the input key with unwrapped DPNP arrays | ||
| return tuple( | ||
| x.get_array() if isinstance(x, dpnp_array) else x for x in key | ||
| ) | ||
| elif isinstance(key, dpnp_array): | ||
| return key.get_array() | ||
| return key | ||
| return tuple(_unwrap_index_element(x) for x in key) | ||
| return _unwrap_index_element(key) | ||
|
|
||
|
|
||
| # pylint: disable=too-many-public-methods | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -104,12 +104,6 @@ cdef bint _is_boolean(object x) except *: | |
| return f in "?" | ||
| else: | ||
| return False | ||
| if callable(getattr(x, "__bool__", None)): | ||
| try: | ||
| x.__bool__() | ||
| except (TypeError, ValueError): | ||
| return False | ||
| return True | ||
|
Comment on lines
-107
to
-112
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. need to add to the or else it will fail on numpy scalar, where before it would pass
Contributor
Author
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. What do you mean? |
||
| return False | ||
|
|
||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.