-
Notifications
You must be signed in to change notification settings - Fork 779
feat: add Array API support via array-api-compat #4179
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
Draft
amalia-k510
wants to merge
43
commits into
scverse:main
Choose a base branch
from
amalia-k510:array-api-compat-integration
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.
Draft
Changes from all commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
a8fc77e
first try at array-api integration
amalia-k510 790ddde
typo + forgot numpy exclusion
amalia-k510 fe9337a
jax/array-api fixes
amalia-k510 5dc2d19
addressing failing tests
amalia-k510 cf39f57
Merge branch 'main' into array-api-compat-integration
amalia-k510 b88b8fd
fixing test errors 2
amalia-k510 ccd7683
cleanup
amalia-k510 9eed5e5
docs/release-notes/4179.feat.md
amalia-k510 4fd8335
dependcies
amalia-k510 c861c56
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 23e02bf
is_array_api missed, removed, fixed
amalia-k510 56206cf
Merge branch 'array-api-compat-integration' of https://github.com/ama…
amalia-k510 7980d7a
fix
amalia-k510 374cac0
anndata version, was installing 11.2
amalia-k510 ef4d3d8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 97e733d
fix deps
flying-sheep b5394f1
Merge branch 'main' into pr/amalia-k510/4179
flying-sheep 4ef21c6
addressing comments
amalia-k510 bab97ee
Merge branch 'array-api-compat-integration' of https://github.com/ama…
amalia-k510 454f62b
Merge branch 'main' into array-api-compat-integration
amalia-k510 1bce79a
ruff check
amalia-k510 68de6d0
Merge branch 'array-api-compat-integration' of https://github.com/ama…
amalia-k510 e3cdf5e
ruff request
amalia-k510 25acbca
anndata version fix
amalia-k510 03c6e5e
comments addressed
amalia-k510 9c0e993
Merge branch 'main' into array-api-compat-integration
amalia-k510 8060092
[autofix.ci] apply automated fixes
autofix-ci[bot] 7ab57b3
array_namespace fix
amalia-k510 5cd5370
Merge branch 'array-api-compat-integration' of https://github.com/ama…
amalia-k510 c0eaa3f
Merge branch 'main' into array-api-compat-integration
flying-sheep 41165bf
[autofix.ci] apply automated fixes
autofix-ci[bot] 9a82ad2
Merge branch 'main' into array-api-compat-integration
amalia-k510 72bb3d5
rank comment addressed.
amalia-k510 7e74847
ruff checks
amalia-k510 0e6aa03
Merge branch 'main' into array-api-compat-integration
amalia-k510 aa58269
[autofix.ci] apply automated fixes
autofix-ci[bot] 150ff64
error fixes
amalia-k510 796f412
tests fix
amalia-k510 02f17c5
tests fixed
amalia-k510 3639aef
ruff
flying-sheep 1fc2ecb
no jax extra
flying-sheep 99d7fad
undo test circumvention
flying-sheep 22c6c6f
Merge branch 'main' into array-api-compat-integration
flying-sheep 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
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
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 |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Add Array-API support, enabling JAX and other array-api backends in `adata.X` {smaller}`A. Karesh` |
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
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 |
|---|---|---|
|
|
@@ -30,6 +30,8 @@ | |
| import numpy as np | ||
| import pandas as pd | ||
| from anndata._core.sparse_dataset import BaseCompressedSparseDataset | ||
| from array_api_compat import array_namespace | ||
| from fast_array_utils.types import HasArrayNamespace | ||
|
|
||
| from .. import logging as logg | ||
| from .._compat import CSBase, DaskArray, SpBase, warn | ||
|
|
@@ -609,6 +611,20 @@ def axis_mul_or_truediv( | |
| *, | ||
| allow_divide_by_zero: bool = True, | ||
| out: ArrayLike | None = None, | ||
| ) -> np.ndarray: | ||
| raise NotImplementedError | ||
|
|
||
|
|
||
| @axis_mul_or_truediv.register(np.ndarray) | ||
| def _( | ||
| x: np.ndarray, | ||
| /, | ||
| scaling_array: np.ndarray, | ||
| axis: Literal[0, 1], | ||
| op: Callable[[Any, Any], Any], | ||
| *, | ||
| allow_divide_by_zero: bool = True, | ||
| out: ArrayLike | None = None, | ||
| ) -> np.ndarray: | ||
| _check_op(op) | ||
| scaling_array = _broadcast_axis(scaling_array, axis) | ||
|
|
@@ -731,8 +747,38 @@ def _[T: (DaskArray, np.ndarray)]( | |
| ) | ||
|
|
||
|
|
||
| @axis_mul_or_truediv.register(HasArrayNamespace) | ||
| def _( | ||
| x: HasArrayNamespace, | ||
| /, | ||
| scaling_array: np.ndarray, | ||
| axis: Literal[0, 1], | ||
| op: Callable[[Any, Any], Any], | ||
| *, | ||
| allow_divide_by_zero: bool = True, | ||
| out: ArrayLike | None = None, | ||
| ) -> Any: | ||
|
|
||
| _check_op(op) | ||
| scaling_array = _broadcast_axis(scaling_array, axis) | ||
| xp = array_namespace(x) | ||
| scaling_array = xp.asarray(scaling_array) | ||
| if op is mul: | ||
| return x * scaling_array | ||
| if not allow_divide_by_zero: | ||
| scaling_array = xp.where( | ||
| scaling_array == 0, xp.ones_like(scaling_array), scaling_array | ||
| ) | ||
| return x / scaling_array | ||
|
|
||
|
|
||
| @singledispatch | ||
| def axis_nnz(x: ArrayLike, /, axis: Literal[0, 1]) -> np.ndarray: | ||
| raise NotImplementedError | ||
|
|
||
|
|
||
| @axis_nnz.register(np.ndarray) | ||
| def _(x: np.ndarray, /, axis: Literal[0, 1]) -> np.ndarray: | ||
| return np.count_nonzero(x, axis=axis) | ||
|
|
||
|
|
||
|
|
@@ -751,6 +797,12 @@ def _(x: DaskArray, /, axis: Literal[0, 1]) -> DaskArray: | |
| ) | ||
|
|
||
|
|
||
| @axis_nnz.register(HasArrayNamespace) | ||
| def _(x: HasArrayNamespace, /, axis: Literal[0, 1]) -> Any: | ||
| xp = array_namespace(x) | ||
| return xp.count_nonzero(x, axis=axis) | ||
|
|
||
|
|
||
| @singledispatch | ||
| def check_nonnegative_integers(x: _SupportedArray, /) -> bool | DaskArray: | ||
| """Check values of X to ensure it is count data.""" | ||
|
|
@@ -772,6 +824,16 @@ def _check_nonnegative_integers_in_mem(x: _MemoryArray, /) -> bool: | |
| return not np.any((data % 1) != 0) | ||
|
|
||
|
|
||
| @check_nonnegative_integers.register(HasArrayNamespace) | ||
| def _check_nonnegative_integers_array_api(x: HasArrayNamespace, /) -> bool: | ||
| xp = array_namespace(x) | ||
| if bool(xp.any(x < 0)): | ||
| return False | ||
| if xp.isdtype(x.dtype, "integral"): | ||
| return True | ||
| return not bool(xp.any((x % 1) != 0)) | ||
|
Comment on lines
+830
to
+834
Member
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. maybe we can add a shortcut branch for “if unsigned integer then return I don’t know why we don’t do that for the numpy case either. |
||
|
|
||
|
|
||
| @check_nonnegative_integers.register(DaskArray) | ||
| def _check_nonnegative_integers_dask(x: DaskArray, /) -> DaskArray: | ||
| return x.map_blocks(check_nonnegative_integers, dtype=bool, drop_axis=(0, 1)) | ||
|
|
||
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
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
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.
please make sure that you mark all functions that are now supported.
also please check in the tests that these functions preserve the data type. you can do that only for jax if it’s too hard, we plan to update the tests with a way to make it easier to get the expected type in the future.