JAX was designed to run efficiently on accelerators from day one. Here's what you need to know about running JAX on different hardware.
JAX on GPUs
import jax
# Check available devices
print(jax.devices())
# [CudaDevice(id=0), CudaDevice(id=1), ...]
# JAX automatically uses GPU if available
x = jax.numpy.ones((1000, 1000))
# x is already on GPU — no .to('cuda') needed!
# Multi-GPU: use sharding (see Track 12)
from jax.sharding import Mesh, NamedSharding, PartitionSpec as P
devices = jax.devices() # all GPUs
mesh = Mesh(devices, ('data',))
# Data parallelism: shard batch across GPUs
data_sharding = NamedSharding(mesh, P('data'))
batch = jax.device_put(x, data_sharding)
# Linux NVIDIA GPU: choose a wheel matching the supported driver/hardware.
# python -m pip install -U "jax[cuda13]"
# CUDA 12 wheels remain a documented alternative.
The installation page lists supported operating systems, accelerator wheels and driver requirements. A CUDA build version is not automatically the minimum supported CUDA version: the JAX 0.6.0 release notes distinguish its CUDA 12.8 build from continued support for CUDA 12.1 or later.
JAX on TPUs
# On a supported Google Cloud TPU VM:
# python -m pip install -U "jax[tpu]"
# Inspect the actual runtime; device counts depend on its topology.
print(jax.local_device_count())
print(jax.device_count())
# Choose a dtype explicitly; bfloat16 is not JAX's default float dtype.
x_bf16 = x.astype(jax.numpy.bfloat16)
# Profile batch shapes and precision for the specific workload.
# Configure distributed initialization before device discovery when required.
Performance Tips
# Common performance pitfalls:
# 1. UNNECESSARY RECOMPILATION
# BAD: different shapes cause recompilation
for batch in variable_size_batches:
result = jax.jit(fn)(batch) # recompiles every new shape!
# GOOD: pad to fixed size
max_batch_size = 256
for batch in batches:
padded = pad_to_size(batch, max_batch_size)
result = jax.jit(fn)(padded) # compiled once, reused
# 2. HOST-DEVICE TRANSFER
# BAD: pulling values back to CPU in a loop
for step in range(1000):
loss = train_step(params, batch)
print(float(loss)) # blocks! transfers to CPU every step
# GOOD: only transfer periodically
for step in range(1000):
loss = train_step(params, batch)
if step % 100 == 0:
print(float(loss)) # transfer only every 100 steps
# 3. USE jax.block_until_ready() for timing
import time
x = jax.numpy.ones((1000, 1000))
start = time.time()
y = x @ x
y.block_until_ready() # wait for computation to finish
print(f"Time: {time.time() - start:.4f}s")
💡 Why This Matters
One performance pitfall in JAX is unnecessary recompilation — using different input shapes causes JIT to recompile, which can take seconds. Another is frequent host-device transfers (converting to Python floats in a loop). Both are silent performance killers that can make your JAX code slower than NumPy. Use jax.make_jaxpr to check what gets compiled, and jax.profiler to identify bottlenecks.
GPU memory
The GPU memory guide documents a default preallocation of 75% of total GPU memory at the first JAX operation. Set XLA_PYTHON_CLIENT_PREALLOCATE=false before starting JAX to allocate on demand, or use XLA_CLIENT_MEM_FRACTION to lower the preallocation fraction. Disabling preallocation can increase fragmentation; measure the workload rather than treating either setting as universally better.