-
Notifications
You must be signed in to change notification settings - Fork 14
ENH: at
: add __setitem__
fancy indexing fallback
#395
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
Open
amacati
wants to merge
6
commits into
data-apis:main
Choose a base branch
from
amacati:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+138
−4
Open
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7f42065
Add fancy __setitem__ support for array-api-strict
amacati 9df1c65
Remove unnecessary variable assignment
amacati 2c5e0aa
Add docs. Fix 0D and scalar cases. Handle negative indices
amacati 6fe2ffb
Add handling and tests for negative and positive out-of-bounds indices
amacati acd5365
Fix linting
amacati 07e6bda
[wip] Fix _at logic. Update tests
amacati File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,8 @@ | |
from ._utils import _compat | ||
from ._utils._compat import ( | ||
array_namespace, | ||
is_array_api_obj, | ||
is_array_api_strict_namespace, | ||
is_dask_array, | ||
is_jax_array, | ||
is_torch_array, | ||
|
@@ -352,6 +354,33 @@ def _op( | |
if is_torch_array(y): | ||
y = xp.astype(y, x.dtype, copy=False) | ||
|
||
# Work around lack of fancy indexing __setitem__ support in array-api-strict. | ||
if ( | ||
is_array_api_strict_namespace(xp) | ||
and is_array_api_obj(idx) | ||
and xp.isdtype(idx.dtype, "integral") | ||
and out_of_place_op is None # only use for set() | ||
): | ||
# Vectorize the operation using boolean indexing | ||
# For non-unique indices, take the last occurrence. This requires creating | ||
# masks for x and y that create matching shapes. | ||
amacati marked this conversation as resolved.
Show resolved
Hide resolved
|
||
unique_indices, _ = xp.unique_inverse(idx) | ||
x_mask = xp.any(xp.arange(x.shape[0])[..., None] == unique_indices, axis=-1) | ||
# Get last occurrence of each unique index | ||
cmp = unique_indices[:, None] == unique_indices[None, :] | ||
# Ignore later matches | ||
lower_tri_mask = ( | ||
xp.arange(y.shape[0])[:, None] >= xp.arange(y.shape[0])[None, :] | ||
) | ||
masked_cmp = cmp & lower_tri_mask | ||
# For each position i, count how many matches occurred before i | ||
prior_matches = xp.sum(xp.astype(masked_cmp, xp.int32), axis=-1) | ||
# Last occurrence has highest match count | ||
y_mask = prior_matches == xp.max(prior_matches, axis=-1) | ||
# Apply the operation only to last occurrences | ||
x[x_mask] = y[y_mask] | ||
return x | ||
|
||
# Backends without boolean indexing (other than JAX) crash here | ||
if in_place_op: # add(), subtract(), ... | ||
x[idx] = in_place_op(x[idx], y) | ||
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. These remain broken. 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. Yes. I'm not sure if we should attempt to fix them. See my general comment. |
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.