Add __array_namespace__ to ArrayBox#647
Merged
agriyakhetarpal merged 3 commits intoHIPS:masterfrom Oct 15, 2024
Merged
Conversation
Collaborator
|
Hi @yaugenst-flex, thanks for contributing to autograd! I made the type hints compatible with python<3.10 and now all tests seem to pass 👍 Any objections against merging this @agriyakhetarpal @j-towns ? |
fjosw
approved these changes
Oct 13, 2024
Collaborator
|
No objections from me. |
agriyakhetarpal
approved these changes
Oct 15, 2024
Collaborator
agriyakhetarpal
left a comment
There was a problem hiding this comment.
No objections from me either! Thanks, @yaugenst-flex!
Collaborator
|
I've created https://github.com/HIPS/autograd/milestone/1 to track things we can add to a v1.8.0 release (or to see which things we can split up and add to a v1.7.1 release). This sounds like a useful improvement. |
Contributor
Author
|
This is great, thanks! |
Closed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Small change that adds the
__array_namespace__method from the Array API standard to autograd'sArrayBox.This makes autograd (partially) compatible with libraries that check for array API compatibility, such as xarray, because xarray will coerce types that do not implement the array API into regular numpy arrays, see relevant discussion here.
The following code snippet:
prints
currently, because the traced
ArrayBoxis being coerced into a regular numpy array ofdtype=objectwithArrayBoxelements. Differentiating through this works (with some limitations), but is very inefficient.With the addition suggested in this PR, the snippet instead prints:
i.e.
datais now a "proper"ArrayBox.I might also be interested in working on a full xarray wrapper in the future, but for now just this change would already help a lot.