-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #238 from crusaderky/jax_jit_device
- Loading branch information
Showing
4 changed files
with
61 additions
and
18 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,34 @@ | ||
import jax | ||
import jax.numpy as jnp | ||
from numpy.testing import assert_equal | ||
import pytest | ||
|
||
from array_api_compat import device, to_device | ||
|
||
HAS_JAX_0_4_31 = jax.__version__ >= "0.4.31" | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"func", | ||
[ | ||
lambda x: jnp.zeros(1, device=device(x)), | ||
lambda x: jnp.zeros_like(jnp.ones(1, device=device(x))), | ||
lambda x: jnp.zeros_like(jnp.empty(1, device=device(x))), | ||
lambda x: jnp.full(1, fill_value=0, device=device(x)), | ||
pytest.param( | ||
lambda x: jnp.asarray([0], device=device(x)), | ||
marks=pytest.mark.skipif( | ||
not HAS_JAX_0_4_31, reason="asarray() has no device= parameter" | ||
), | ||
), | ||
lambda x: to_device(jnp.zeros(1), device(x)), | ||
] | ||
) | ||
def test_device_jit(func): | ||
# Test work around to https://github.com/jax-ml/jax/issues/26000 | ||
# Also test missing to_device() method in JAX < 0.4.31 | ||
# when inside jax.jit, even after importing jax.experimental.array_api | ||
|
||
x = jnp.ones(1) | ||
assert_equal(func(x), jnp.asarray([0])) | ||
assert_equal(jax.jit(func)(x), jnp.asarray([0])) |