JAX configuration: devices, memory and precision¶
geossm runs its computations with JAX. A few JAX settings are process-wide: they affect every JAX computation in your Python process, not only geossm’s. geossm therefore never changes them itself; this page lists the ones that matter and how to set them.
Most of them must be set before JAX starts, i.e. before the first JAX array is created (in practice: at the top of your script, before importing geossm or creating any model). Setting them later has no effect.
Running on the CPU only¶
backend="cpu" makes a model compute on the CPU, but it does not stop JAX
from starting its GPU backend when a GPU is installed: JAX initialises every
available platform the first time a device is requested. With the default
allocator (next section) that alone reserves most of the GPU memory.
To keep the GPU completely untouched, restrict JAX to the CPU:
JAX_PLATFORMS=cpu python my_script.py
or, at the very top of the script:
import os
os.environ["JAX_PLATFORMS"] = "cpu" # before importing jax or geossm
GPU memory preallocation¶
By default JAX reserves 75 % of the GPU memory when its GPU backend starts, whether or not the computation needs it. On a shared GPU this blocks other users and processes. To allocate memory on demand instead:
XLA_PYTHON_CLIENT_PREALLOCATE=false python my_script.py
or limit the reserved fraction, e.g. to 30 %:
XLA_PYTHON_CLIENT_MEM_FRACTION=.30 python my_script.py
The same variables can be set with os.environ at the top of the script,
as above. See JAX’s GPU memory allocation page for
details.
Measured on the Agrimonia example of the Quick Start (107 sites, 60 latent points, 2192 days; float32 on an NVIDIA RTX PRO 6000 with 96 GB; JAX 0.10.1):
Setting |
GPU memory |
EM iteration |
|---|---|---|
default (any |
73.5 GB reserved when the model is created |
0.58 s |
|
1.3 GB after fitting; 9.0 GB peak with standard errors |
0.58–0.62 s |
|
0 GB |
1.9 s (CPU) |
Double precision (float64)¶
Models use float32 by default. dtype=jnp.float64 requires JAX’s 64-bit
mode, which is off by default and is a process-wide setting. Enable it once,
at the start of the program:
import jax
jax.config.update("jax_enable_x64", True)
or set JAX_ENABLE_X64=1 in the environment. Creating a float64 model
without it raises a ValueError. float32 models give the same results
whether 64-bit mode is on or off.
Matrix-multiplication precision¶
On GPUs, JAX may run float32 matrix products in reduced precision
(TensorFloat-32) by default. geossm’s numerical kernels always request full
precision, so you do not need to set JAX_DEFAULT_MATMUL_PRECISION. Without
it, float32 results on the GPU were not reproducible between runs.
Compilation cache¶
The first fit in a Python process spends most of its time compiling the kernels (on a GPU, about 35 s of a 41 s six-iteration fit of the Agrimonia example). Later fits in the same process reuse them. To reuse them across processes, enable JAX’s persistent compilation cache:
JAX_COMPILATION_CACHE_DIR=$HOME/.cache/jax python my_script.py
With the cache populated, the same fit took 10.7 s. The cache is keyed on the JAX version, the device and the array shapes, so a different dataset size or mesh compiles again.
How it works. Every compiled program (XLA executable) gets a key, a hash
of: the program itself (which depends on the array shapes and dtypes, the
model’s block sizes and any fixed parameter values), the jaxlib version, the
backend and driver version (e.g. the CUDA driver and GPU type), the XLA flags
and the compiler options. Before compiling, JAX looks the key up in the cache
directory; on a hit it loads the executable instead of compiling it. The
cache is off unless a directory is set, with the JAX_COMPILATION_CACHE_DIR
environment variable or, before the first computation,
jax.config.update("jax_compilation_cache_dir", "/path/to/cache")
Where it is saved. In that directory only, one file per compiled program
(named after the jitted function and the key, e.g. jit__filter_kernelJAX-
<hash>), plus, on a GPU, a xla_gpu_per_fusion_autotune_cache_dir
subdirectory with XLA’s autotuning results. JAX chooses no default location;
~/.cache/jax above is just a convention. For the Agrimonia example it
holds 14 files, 2.8 MB.
What is cached. Only programs whose compilation took at least
jax_persistent_cache_min_compile_time_secs (1 s by default): geossm’s
filter, smoother, M-step, prediction and Hessian kernels, but not the many
small operations compiled in a fraction of a second. Lowering the threshold
(e.g. to 0) caches those too, at the cost of many small files. Measured on the
Agrimonia example (GPU, float32, 4 EM iterations):
First call in a new process |
|
|
|
|---|---|---|---|
empty cache |
49 s |
55 s |
11 s |
cache filled by an earlier run |
9.4 s |
4.1 s |
2.4 s |
(later call in the same process) |
3.4 s |
1.9 s |
2.0 s |
Lifetime and size. Entries never expire: a file stays until it is
deleted. Entries that no longer match (after upgrading JAX or the GPU driver,
changing XLA flags, or for data of other shapes) are simply never read again.
By default the directory grows without limit
(jax_compilation_cache_max_size = -1); setting a size in bytes, e.g.
jax.config.update("jax_compilation_cache_max_size", 2 * 1024**3),
deletes the least recently used entries beyond it. Deleting the directory is
always safe: the programs are compiled again on the next run.
Within a process, every kernel is compiled once per array shape and dtype and
reused by later fits, predictions and standard errors (compute_cov_params
included).
Memory of the standard errors (Hessian)¶
compute_cov_params differentiates the observed log-likelihood twice. The
reverse (gradient) pass through the Kalman filter keeps, for every one of the
\(T\) time steps, intermediates of size about
\(8\,(4pq + 10q^2)\) bytes in float64 (\(p\) observed sites,
\(q\) latent nodes): the memory grows linearly with \(T\) and roughly
with \(q^2\), and is the same whatever the number \(k\) of free
parameters.
The Hessian is built one column at a time, one Hessian-vector product per free
parameter, so its peak is about two gradients. jax.hessian would instead
push the \(k\) tangents through the reverse pass at once and store the
gradient’s intermediates \(k+1\) times: about 160 GiB for the bivariate
GRINS model (PM10 and PM2.5, about 24 parameters), which does not fit on a
96 GB GPU. Compile-time memory of the two approaches (synthetic problems,
float64, JAX 0.10.1):
\(p, q, T, k\) |
one gradient |
|
one column at a time |
|---|---|---|---|
200, 100, 200, 14 |
0.27 GiB |
4.08 GiB |
0.54 GiB |
200, 100, 200, 24 |
0.27 GiB |
6.81 GiB |
0.54 GiB |
200, 100, 400, 14 |
0.54 GiB |
8.13 GiB |
1.08 GiB |
200, 200, 200, 14 |
0.84 GiB |
12.67 GiB |
1.69 GiB |
The columns are computed one after the other, so on a GPU the Hessian can take
longer than with jax.hessian (which batches them); the result is the same.
Not implemented yet¶
Checkpointing the filter scan. If a model is too large even for the
column-by-column Hessian (long series, many latent nodes), the next step is to
wrap the Kalman step of the log-likelihood in jax.checkpoint
(rematerialisation). The reverse pass would then store only the scan carry
per time step (the state \(x_t\), its covariance \(P_t\) and, as the
filter is written now, the gain \(K_t\) of size \(q \times p\), which
a log-likelihood-only filter does not need), and recompute the other
intermediates of the step while differentiating it: roughly one \(q
\times q\) matrix per step instead of about ten \(q \times q\) and four
\(q \times p\) ones, for about one extra forward pass of compute.
Checkpointing blocks of \(\sqrt{T}\) steps instead (a nested scan with
jax.checkpoint on the inner one) would store only \(\sqrt{T}\)
carries plus the intermediates of one block of \(\sqrt{T}\) steps, at a
similar cost. Either change belongs in a log-likelihood-only variant of
_filter_kernelJAX, so that the filter used by the EM algorithm is not
slowed down.
Two other changes could save time on a GPU but are not implemented:
Keeping the E-step on the device. Every EM iteration copies the smoothed states, their covariances and the EM sufficient statistics to the host and back (about 67 MB per iteration on the Agrimonia example, roughly 5-10 % of an E-step on a GPU; negligible on the CPU).
Fewer synchronisations in the M-step. The M-step waits for each of its small kernels to finish (to time them), which prevents the GPU from overlapping them.
Summary¶
A typical script header:
import os
# CPU only (leaves the GPU untouched); remove to use a GPU.
os.environ.setdefault("JAX_PLATFORMS", "cpu")
# On a GPU: allocate memory on demand instead of reserving 75 %.
os.environ.setdefault("XLA_PYTHON_CLIENT_PREALLOCATE", "false")
# Reuse compiled kernels across runs.
os.environ.setdefault("JAX_COMPILATION_CACHE_DIR", os.path.expanduser("~/.cache/jax"))
import jax
# Only if you use dtype=jnp.float64.
jax.config.update("jax_enable_x64", True)
import geossm