Repository navigation
Backend-Agnostic Refactor Using Array API Compatibility - #2019
amalia-k510 wants to merge 6 commits into
Conversation
There was a problem hiding this comment.
3 things to simplify things:
- Ignore the benchmarks for now, including the
gen_adatathere. There is anothergen_adatain https://github.com/scverse/anndata/blob/main/src/anndata/tests/helpers.py that you can add a jax type to much more easily (i.e.,X_typeor orDEFAULT_KEY_TYPES), and then you will get the entire test suit for free running against jax. Specifically, I would add it as a type forobsm,varm,X, andlayerswhile ignoring the otehrs. It doesn't make sense to use it inobsorvar, for example - No need to use the array_api where it's not needed (like for sparse as you point out). I would remove it from all the other subsetting functions and only have it in the default
single_dispatchcase - The test file you have is great! I would start there if you feel overwhelmed by the full test suite
| ) | ||
|
|
||
|
|
||
| # def gen_vstr_recarray( |
There was a problem hiding this comment.
No need to worry bout jax for this! I don't even think strings are in the array api: https://data-apis.org/array-api/latest/API_specification/data_types.html#data-types
…d reverting some files to the original
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2019 +/- ##
==========================================
- Coverage 87.47% 78.09% -9.38%
==========================================
Files 46 47 +1
Lines 7057 7332 +275
==========================================
- Hits 6173 5726 -447
- Misses 884 1606 +722
|
| if all( | ||
| isinstance(x, Iterable) and not isinstance(x, (str, bytes)) for x in subset_idx | ||
| ): | ||
| subset_idx = xp.ix_(*subset_idx) |
There was a problem hiding this comment.
Chekc type of subset_idx against what is required from https://data-apis.org/array-api/latest/API_specification/indexing.html#indexing
This PR refactors the gen_adata function in benchmarks/utils.py to support backend-agnostic generation of AnnData objects, using the array-api-compat interface to infer and operate with the correct array namespace (e.g., NumPy or JAX). The goal is to allow tests and benchmarking utilities to seamlessly run across supported array backends without manual rewrites. The function now detects the namespace from a real array object instead of relying on a global fallback and uses backend-specific RNG to generate observation and variable metadata. I also updated the related test to parameterize over NumPy and JAX backends. However, I'm currently running into an issue where passing a scipy.sparse.csr_matrix into get_namespace() raises a TypeError, since sparse matrices are not supported by array-api-compat. As a workaround, I’m generating a dense array first to infer the namespace, and only falling back to sparse when explicitly requested in the attribute set. This works fine for NumPy but breaks for JAX, as sparse support isn’t available there. Still trying to figure out whether to raise, skip, or handle this more gracefully depending on the backend. Would appreciate any thoughts on best practices here.