Skip to content

Performance and accelerator guidance

Choose a device and precision

Run jax.devices() to confirm that JAX sees the accelerator. Set JAX_PLATFORMS=cpu before importing JAX to reproduce a CPU run, or use jax.default_device(jax.devices("gpu")[0]) to place a local calculation. Follow the JAX installation guide.

Use JAX_ENABLE_X64=1 and float64 on CPU for reference comparisons, especially when checking convergence near round-off. Exact-equality checks target CPU float64. Use float32 for production workloads once the statistics and precision meet your requirements. TPUs lack native float64 arithmetic.

Measure both throughput and numerical error on the target hardware. Warm up or compile the workloads before timing, and synchronise device execution with block_until_ready().

For GPU memory pressure, set XLA_PYTHON_CLIENT_PREALLOCATE=false before importing JAX to allocate on demand, or limit the pool with XLA_CLIENT_MEM_FRACTION. The older fraction setting is XLA_PYTHON_CLIENT_MEM_FRACTION. JAX GPU memory settings

Reduce working memory

  • batch_size limits how many paths a mapped kernel processes together in stochastic_ensemble_rollout, stochastic_structure_factor and richardson_weak_extrapolation. None uses the full vmap batch. Compare peak memory and throughput on your workload. Batching bounds in-flight work. It leaves the size of the returned trajectories unchanged. Combine batch_size with stride in stochastic_ensemble_rollout to retain fewer states, or use streamed statistics when only a spectrum is needed. The streamed spectrum carries current fields for all paths.
  • Both stochastic_rollout and stochastic_ensemble_rollout accept stride=k to save steps k, 2*k, …. Choose a multiple of k for num_steps. The output contains num_steps // stride saved states, plus step zero if include_init=True. These states use the same keys as the corresponding stride-one states. Upstream exponax's rollout has no stride argument.
  • Use compiled scans for time loops. The Strang scaffold calls Python for each SSA half-step and returns to the host on accelerators.
  • Prefer FFT sizes with small prime factors and benchmark nearby sizes that meet your resolution requirements. The corrected noise model in #20 traces 5 FFTs per additive step and 8 for either multiplicative scheme via the physical-space interface. FFT performance issue #22 · Exponax Performance Hints

Milstein adds elementwise work to the multiplicative step and uses the same FFT count as Euler–Maruyama. At 256 steps the coupled fixed-grid SPDE benchmark gives about 14× smaller strong RMS error with Milstein. This is a pathwise accuracy gain in the commutative, smooth-noise regime. Both methods have weak order one under the usual smoothness assumptions. Noise model · Corrected implementation and evidence #20

Distribute the key batch

This single-host example shards independent keys and their initial fields. Spatial axes stay local. Sharding both inputs gives the scan a per-device state from its first step. The path count is a multiple of the device count.

import jax
import jax.numpy as jnp
import numpy as np
from jax.sharding import Mesh, NamedSharding, PartitionSpec

from exponax_spde import stochastic_rollout
from exponax_spde.stepper import StochasticAllenCahn

devices = np.asarray(jax.local_devices())
mesh = Mesh(devices, ("path",))
paths = PartitionSpec("path")
sharding = NamedSharding(mesh, paths)
keys = jax.device_put(
    jax.random.split(jax.random.key(0, impl="threefry2x32"), 4 * devices.size),
    sharding,
)
stepper = StochasticAllenCahn(1, 1.0, 128, 0.001)
dtype = jnp.float64 if jax.config.jax_enable_x64 else jnp.float32
initial_fields = jax.device_put(
    jnp.zeros((keys.shape[0], 1, 128), dtype=dtype), sharding
)
rollout = stochastic_rollout(stepper, 40, include_init=False, stride=4)


def local_ensemble(fields, local_keys):
    return jax.vmap(rollout)(fields, local_keys)


run = jax.jit(
    jax.shard_map(local_ensemble, mesh=mesh, in_specs=(paths, paths), out_specs=paths)
)
states = run(initial_fields, keys)
states.block_until_ready()

Use typed Threefry keys when comparing across chunk sizes and shardings. Record the seed, RNG implementation, JAX version and dtype with results. Experimental rbg and unsafe_rbg can be faster on some devices, but their vmap batching can generate a full batch from its first key, changing which draws each path receives. JAX PRNG configuration

Reuse compilations

Configure a persistent cache before the first compilation. For example, set JAX_COMPILATION_CACHE_DIR to a user-owned directory. The jax_enable_compilation_cache setting enables caching, and jax_persistent_cache_min_compile_time_secs and jax_persistent_cache_min_entry_size_bytes control which executables are written. Fix shapes, dtypes and rollout parameters where practical. JAX persistent compilation cache

Buffer donation

If a compiled rollout consumes its input state, an outer jax.jit can donate that argument through donate_argnums. Donation lets the compiler reuse the input storage. Supply a fresh state when the caller needs the original buffer for another rollout.