jax0.8.0
Published
Differentiate, compile, and transform Numpy code.
pip install jax
Package Downloads
Authors
Project URLs
Requires Python
>=3.11
Dependencies
- jaxlib
<=0.8.0,>=0.8.0 - ml_dtypes
>=0.5.0 - numpy
>=2.0 - opt_einsum
- scipy
>=1.13 - jaxlib
==0.8.0; extra == "minimum-jaxlib" - jaxlib
==0.7.2; extra == "ci" - jaxlib
<=0.8.0,>=0.8.0; extra == "tpu" - libtpu
==0.0.24.*; extra == "tpu" - requests
; extra == "tpu" - jaxlib
<=0.8.0,>=0.8.0; extra == "cuda" - jax-cuda12-plugin
[with-cuda]<=0.8.0,>=0.8.0; extra == "cuda" - jaxlib
<=0.8.0,>=0.8.0; extra == "cuda12" - jax-cuda12-plugin
[with-cuda]<=0.8.0,>=0.8.0; extra == "cuda12" - jaxlib
<=0.8.0,>=0.8.0; extra == "cuda13" - jax-cuda13-plugin
[with-cuda]<=0.8.0,>=0.8.0; extra == "cuda13" - jaxlib
<=0.8.0,>=0.8.0; extra == "cuda12-local" - jax-cuda12-plugin
<=0.8.0,>=0.8.0; extra == "cuda12-local" - jaxlib
<=0.8.0,>=0.8.0; extra == "cuda13-local" - jax-cuda13-plugin
<=0.8.0,>=0.8.0; extra == "cuda13-local" - jaxlib
<=0.8.0,>=0.8.0; extra == "rocm" - jax-rocm60-plugin
<=0.8.0,>=0.8.0; extra == "rocm" - kubernetes
; extra == "k8s" - xprof
; extra == "xprof"
Transformable numerical computing at scale
Transformations | Scaling | Install guide | Change logs | Reference docs
What is JAX?
JAX is a Python library for accelerator-oriented array computation and program transformation, designed for high-performance numerical computing and large-scale machine learning.
JAX can automatically differentiate native
Python and NumPy functions. It can differentiate through loops, branches,
recursion, and closures, and it can take derivatives of derivatives of
derivatives. It supports reverse-mode differentiation (a.k.a. backpropagation)
via jax.grad as well as forward-mode differentiation,
and the two can be composed arbitrarily to any order.
JAX uses XLA
to compile and scale your NumPy programs on TPUs, GPUs, and other hardware accelerators.
You can compile your own pure functions with jax.jit.
Compilation and automatic differentiation can be composed arbitrarily.
Dig a little deeper, and you'll see that JAX is really an extensible system for composable function transformations at scale.
This is a research project, not an official Google product. Expect sharp edges. Please help by trying it out, reporting bugs, and letting us know what you think!
import jax
import jax.numpy as jnp
def predict(params, inputs):
for W, b in params:
outputs = jnp.dot(inputs, W) + b
inputs = jnp.tanh(outputs) # inputs to the next layer
return outputs # no activation on last layer
def loss(params, inputs, targets):
preds = predict(params, inputs)
return jnp.sum((preds - targets)**2)
grad_loss = jax.jit(jax.grad(loss)) # compiled gradient evaluation function
perex_grads = jax.jit(jax.vmap(grad_loss, in_axes=(None, 0, 0))) # fast per-example grads
Contents
Transformations
At its core, JAX is an extensible system for transforming numerical functions.
Here are three: jax.grad, jax.jit, and jax.vmap.
Automatic differentiation with grad
Use jax.grad
to efficiently compute reverse-mode gradients:
import jax
import jax.numpy as jnp
def tanh(x):
y = jnp.exp(-2.0 * x)
return (1.0 - y) / (1.0 + y)
grad_tanh = jax.grad(tanh)
print(grad_tanh(1.0))
# prints 0.4199743
You can differentiate to any order with grad:
print(jax.grad(jax.grad(jax.grad(tanh)))(1.0))
# prints 0.62162673
You're free to use differentiation with Python control flow:
def abs_val(x):
if x > 0:
return x
else:
return -x
abs_val_grad = jax.grad(abs_val)
print(abs_val_grad(1.0)) # prints 1.0
print(abs_val_grad(-1.0)) # prints -1.0 (abs_val is re-evaluated)
See the JAX Autodiff Cookbook and the reference docs on automatic differentiation for more.
Compilation with jit
Use XLA to compile your functions end-to-end with
jit,
used either as an @jit decorator or as a higher-order function.
import jax
import jax.numpy as jnp
def slow_f(x):
# Element-wise ops see a large benefit from fusion
return x * x + x * 2.0
x = jnp.ones((5000, 5000))
fast_f = jax.jit(slow_f)
%timeit -n10 -r3 fast_f(x)
%timeit -n10 -r3 slow_f(x)
Using jax.jit constrains the kind of Python control flow
the function can use; see
the tutorial on Control Flow and Logical Operators with JIT
for more.
Auto-vectorization with vmap
vmap maps
a function along array axes.
But instead of just looping over function applications, it pushes the loop down
onto the function’s primitive operations, e.g. turning matrix-vector multiplies into
matrix-matrix multiplies for better performance.
Using vmap can save you from having to carry around batch dimensions in your
code:
import jax
import jax.numpy as jnp
def l1_distance(x, y):
assert x.ndim == y.ndim == 1 # only works on 1D inputs
return jnp.sum(jnp.abs(x - y))
def pairwise_distances(dist1D, xs):
return jax.vmap(jax.vmap(dist1D, (0, None)), (None, 0))(xs, xs)
xs = jax.random.normal(jax.random.key(0), (100, 3))
dists = pairwise_distances(l1_distance, xs)
dists.shape # (100, 100)
By composing jax.vmap with jax.grad and jax.jit, we can get efficient
Jacobian matrices, or per-example gradients:
per_example_grads = jax.jit(jax.vmap(jax.grad(loss), in_axes=(None, 0, 0)))
Scaling
To scale your computations across thousands of devices, you can use any composition of these:
- Compiler-based automatic parallelization where you program as if using a single global machine, and the compiler chooses how to shard data and partition computation (with some user-provided constraints);
- Explicit sharding and automatic partitioning
where you still have a global view but data shardings are
explicit in JAX types, inspectable using
jax.typeof; - Manual per-device programming where you have a per-device view of data and computation, and can communicate with explicit collectives.
| Mode | View? | Explicit sharding? | Explicit Collectives? |
|---|---|---|---|
| Auto | Global | ❌ | ❌ |
| Explicit | Global | ✅ |