[CPU] One compiled module returns different results across calls when the process has more than one thread
Description
On the CPU backend, a single compiled module called repeatedly on the same input buffers returns different results whenever the process has more than one intra-op thread. At one thread it is bit-identical across hundreds of calls.
The spread is small (~1e-9 relative) but it compounds over a training trajectory and eventually produces NaN: in the model this came from, 4/4 runs went non-finite between steps 81 and 893 at 2 threads, while 6/6 runs of the same seeds stayed finite for 2500 steps pinned to 1 thread. Replaying a failing step from its own pre-step state returned 40/40 finite values, which is what pointed at nondeterminism rather than an unstable objective.
Reproducer
Artifacts (exported StableHLO module + the input arrays + a ~40-line runner, jax/numpy only): https://gist.github.com/AJB-ajb/e8dc04d656e16f01d3bd3b94efe568b1
pip install jax==0.11.0 flatbuffers
taskset -c 0 python3 replay.py --calls 200
taskset -c 0,1 python3 replay.py --calls 200
replay.py deserializes the module once, calls it --calls times on identical buffers, and summarises each call as one float64 sum per output array.
Results
AMD Ryzen 7 8845HS (16 cores), Linux 6.17.0 x86_64, glibc 2.39, Python 3.12, jax/jaxlib 0.11.0, CPU backend:
| cores visible to the process | calls | distinct finite results |
|---|---|---|
| 1 | 200 | 1 |
| 2 | 200 | 85 |
| 4 | 200 | 4 |
| 8 | 200 | 98 |
2, with xla_cpu_enable_concurrency_optimized_scheduler=false |
60 | 6 |
Turning the concurrency-optimized scheduler off reduces the NaN rate substantially in the full training loop (300 replays of one step at 2 threads: 56 NaN with the default scheduler, 0/150 with it off) but, as the last row shows, does not restore determinism. --xla_cpu_use_thunk_runtime=false (20 NaN/150) and --xla_cpu_multi_thread_eigen=false (17/150) also only reduce the rate.
What the module is
One training step of a parameter-decomposition model: two decomposed linear layers (x @ V, mask, @ U, plus a residual x @ Δᵀ term), a per-component MLP producing a [B, C, 1] gate that multiplies [B, C, R] activations, a lax.scan of jax.grad (a short inner ascent) inside the forward pass, and jax.vjp + value_and_grad for the outer backward. float32 throughout, batch 4096, ~52 input arrays, 62 outputs. python3 replay.py --dump-mlir step.mlir dumps it readably (~330 KB).
Two things narrowed it, in case they help localise:
- A different gate architecture — one MLP per layer over the layer input, emitting
[B, C]with no[B, C, 1] → [B, C, R]broadcast into the multiply — exports to a module that is deterministic at 2 threads (1 distinct result in 100 calls). That swap changes more than the broadcast, so it is a hint rather than a bisection. - Setting the
lax.scanascent to 0 iterations likewise: 1 distinct result in 100 calls.
I could not reproduce it by rebuilding those ingredients standalone — several hand-written jax-only reconstructions carrying the chained decomposed layers, the broadcast gate multiply, custom-VJP clips, and the scanned jax.grad all stayed at a single distinct value. Only the real module does it, which is why the report is an exported artifact rather than a short script.
Also worth noting: exporting only the loss scalar rather than the whole step output makes the module deterministic — XLA DCEs the state update away and the surviving computation is fine.
System info
jax: 0.11.0
jaxlib: 0.11.0
numpy: 2.4.4
python: 3.12.3
device: CpuDevice(id=0)
OS: Linux-6.17.0-122035-tuxedo-x86_64-with-glibc2.39
CPU: AMD Ryzen 7 8845HS (16 cores)
Hi @AJB-ajb
Thanks for the detailed issue report and the exact reproduction snippet.
Root Cause
The XLA CPU backend thunk runtime and Eigen thread pool execute multithreaded reductions and broadcast operations without guaranteeing deterministic summation order across CPU worker threads. Because floating-point addition is non-associative, out-of-order completion across thread workers introduces small numerical variations (~1e-9 relative) across execution calls. Over training iterations, these non-deterministic reduction order variations accumulate and cause gradient instabilities that degrade into NaNs.
Proposed Solution
- In JAX's CPU configuration and runtime initialization (
jax/_src/config.pyandjax/_src/lib/xla_client.py), enforce deterministic reduction order flags for XLA CPU thunks whenjax_deterministic_opsor dynamic CPU thread scheduling is active. - Ensure that setting
jax.config.update("jax_cpu_enable_concurrency_optimized_scheduler", False)or enabling deterministic execution mode forces fixed-tree reduction ordering across XLA CPU worker pools. - Add a multi-threaded CPU regression unit test in
tests/lax_test.pyverifying bit-identical numerical outputs across repeated executions with--calls 200under 2+ CPU threads.
Would you like me to open a Pull Request with this CPU determinism flag logic and accompanying unit tests?