You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[numpy_vs_numba_vs_jax] Fix deprecated device= arg on jax.jit (#563)
JAX has deprecated the device/backend arguments to jax.jit, which emitted
two DeprecationWarnings into the lecture's rendered HTML output:
DeprecationWarning: backend and device argument on jit is deprecated.
You can use jax.device_put(..., jax.local_devices(backend="cpu")[0])
on the inputs to the jitted function to get the same behavior.
Pin the computation to the CPU the recommended way -- commit the input to
the CPU with jax.device_put -- instead of the deprecated decorator
argument. The behaviour (CPU execution for this sequential workload) is
unchanged.
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
# Pin the input to the CPU, which keeps the whole computation there
475
+
x0_cpu = jax.device_put(0.1, cpu)
476
+
477
+
@partial(jax.jit, static_argnames=("n",))
475
478
def qm_jax_fori(x0, n, α=4.0):
476
479
477
480
x = jnp.empty(n + 1).at[0].set(x0)
@@ -485,7 +488,7 @@ def qm_jax_fori(x0, n, α=4.0):
485
488
```
486
489
487
490
* We hold `n` static because it affects array size and hence JAX wants to specialize on its value in the compiled code.
488
-
* We pin to the CPU via `device=cpu` because this sequential workload consists of many small operations, leaving little opportunity for GPU parallelism.
491
+
* We pin the input to the CPU with `jax.device_put` (which keeps the whole computation on the CPU) because this sequential workload consists of many small operations, leaving little opportunity for GPU parallelism.
489
492
490
493
Important: Although `at[t].set` appears to create a new array at each step, inside a JIT-compiled function the compiler detects that the old array is no longer needed and performs the update in place!
491
494
@@ -494,7 +497,7 @@ Let's time it with the same parameters:
494
497
```{code-cell} ipython3
495
498
with qe.Timer():
496
499
# First run
497
-
x_jax = qm_jax_fori(0.1, n)
500
+
x_jax = qm_jax_fori(x0_cpu, n)
498
501
# Hold interpreter
499
502
x_jax.block_until_ready()
500
503
```
@@ -504,7 +507,7 @@ Let's run it again to eliminate compilation overhead:
504
507
```{code-cell} ipython3
505
508
with qe.Timer():
506
509
# Second run
507
-
x_jax = qm_jax_fori(0.1, n)
510
+
x_jax = qm_jax_fori(x0_cpu, n)
508
511
# Hold interpreter
509
512
x_jax.block_until_ready()
510
513
```
@@ -521,7 +524,7 @@ although the syntax is difficult to remember.
0 commit comments