Skip to content

Future-proof JAX array API import #172

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

Merged
merged 4 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions array_api_compat/common/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,13 @@ def your_function(x, y):
elif use_compat is False:
import jax.numpy as jnp
else:
# jax.experimental.array_api is already an array namespace. We do
# not have a wrapper submodule for it.
import jax.experimental.array_api as jnp
# JAX v0.4.32 and newer implements the array API directly in jax.numpy.
# For older JAX versions, it is available via jax.experimental.array_api.
import jax.numpy
if hasattr(jax.numpy, "__array_api_version__"):
jnp = jax.numpy
else:
import jax.experimental.array_api as jnp
namespaces.add(jnp)
elif is_pydata_sparse_array(x):
if use_compat is True:
Expand Down Expand Up @@ -613,8 +617,9 @@ def to_device(x: Array, device: Device, /, *, stream: Optional[Union[int, Any]]
return x
raise ValueError(f"Unsupported device {device!r}")
elif is_jax_array(x):
# This import adds to_device to x
import jax.experimental.array_api # noqa: F401
if not hasattr(x, "__array_namespace__"):
# In JAX v0.4.31 and older, this import adds to_device method to x.
import jax.experimental.array_api # noqa: F401
return x.to_device(device, stream=stream)
elif is_pydata_sparse_array(x) and device == _device(x):
# Perform trivial check to return the same array if
Expand Down
3 changes: 2 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ import array_api_compat.dask as da
```{note}
There are no `array_api_compat` submodules for JAX, sparse, or ndonnx. These
support for these libraries is contained in the libraries themselves (JAX
support is in the `jax.experimental.array_api` module). The
support is in the `jax.numpy` module in JAX v0.4.32 or newer, and in the
`jax.experimental.array_api` module for older JAX versions). The
array-api-compat support for these libraries consists of supporting them in
the [helper functions](helper-functions).
```
Expand Down
6 changes: 5 additions & 1 deletion tests/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ def import_(library, wrapper=False):
pytest.importorskip(library)
if wrapper:
if 'jax' in library:
library = 'jax.experimental.array_api'
# JAX v0.4.32 implements the array API directly in jax.numpy
# Older jax versions use jax.experimental.array_api
jax_numpy = import_module("jax.numpy")
if not hasattr(jax_numpy, "__array_api_version__"):
library = 'jax.experimental.array_api'
elif library.startswith('sparse'):
library = 'sparse'
else:
Expand Down
17 changes: 13 additions & 4 deletions tests/test_array_namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,14 @@ def test_array_namespace(library, api_version, use_compat):

if use_compat is False or use_compat is None and library not in wrapped_libraries:
if library == "jax.numpy" and use_compat is None:
import jax.experimental.array_api
assert namespace == jax.experimental.array_api
import jax.numpy
if hasattr(jax.numpy, "__array_api_version__"):
# JAX v0.4.32 or later uses jax.numpy directly
assert namespace == jax.numpy
else:
# JAX v0.4.31 or earlier uses jax.experimental.array_api
import jax.experimental.array_api
assert namespace == jax.experimental.array_api
else:
assert namespace == xp
else:
Expand Down Expand Up @@ -58,8 +64,11 @@ def test_array_namespace(library, api_version, use_compat):
assert 'jax.experimental.array_api' not in sys.modules
namespace = array_api_compat.array_namespace(array, api_version={api_version!r})

import jax.experimental.array_api
assert namespace == jax.experimental.array_api
if hasattr(jax.numpy, '__array_api_version__'):
assert namespace == jax.numpy
else:
import jax.experimental.array_api
assert namespace == jax.experimental.array_api
"""
subprocess.run([sys.executable, "-c", code], check=True)

Expand Down
Loading