Skip to content

jax on the GPU runner is >2.7x slower than its CPU for a small float64 model (sargent_surico NUTS cell) #1022

Open

Description

@mmcky

Summary

A jax + numpyro cell in lectures/sargent_surico.md could not finish in 2400 seconds on our g4dn.2xlarge (T4) CI runner, and takes 157 seconds on a laptop CPU. Pinning jax to the CPU with jax.config.update('jax_platform_name', 'cpu') made the lecture build, cutting it from a hard timeout to 14m52s on the same runner.

So the GPU is not merely a poor fit for this workload — it is at least 2.7x slower than the CPU sitting next to it in the same box, and that is a floor, since we only ever observed "more than 2400s", never the actual GPU time.

@HumphreyYang — flagging you as the person most likely to know where to look. The pin is already merged into the PR branch as a pragmatic fix, so nothing is blocked; the question is what the underlying cause is and how many other lectures it touches.

The measurements

All from PR #1020, lecture sargent_surico.md, notebook execution step (Build Download Notebooks):

configuration full step that lecture alone the NUTS cell
jax on GPU (default) 58m02s ~47m30s timed out at 2400s
jax pinned to CPU 26m10s 14m52s comfortably inside the cap
local laptop, CPU only — 5m09s (52 cells) 157s

The timeout was confirmed from the execution-reports-html artifact of run 30674947077, which names the cell: CellTimeoutError: A cell timed out while it was being executed, after 2400 seconds with a preview beginning kernel = NUTS(ss_model, target_accept_prob=0.8, dense_mass=True,.

Runner CPU is roughly 5x slower than the laptop on this lecture's non-jax cells, so the comparison is CPU-to-CPU consistent — the GPU is the outlier, not the machine.

The workload

The cell is a numpyro NUTS run over a small linear state-space model:

  • 10x10 matrices, a lax.scan Kalman filter over about 100 quarterly observations
  • jax.config.update('jax_enable_x64', True) — the whole model runs in float64
  • NUTS(..., dense_mass=True, max_tree_depth=8), so up to 256 leapfrog steps per iteration
  • MCMC(..., num_warmup=400, num_samples=400, num_chains=4, chain_method='sequential')

Hypotheses, none confirmed

  1. float64 on a T4. Inference-class GPUs have deliberately crippled FP64 — on a T4 it is a small fraction of FP32 throughput. This is my leading guess but I have not measured it.
  2. Kernel launch overhead. At 10x10, each op is far too small to fill the device, so the run may be dominated by launch latency rather than arithmetic. A lax.scan of tiny ops is close to the worst case.
  3. chain_method='sequential'. Four chains run one after another. 'vectorized' would batch them, and it is worth knowing whether that alone closes the gap on GPU.
  4. Compilation. Possible but unlikely to account for tens of minutes on its own.

These are not mutually exclusive, and 1 and 2 together would be enough to explain the size of the gap.

What would settle it

A profiled run on an actual T4 — something we cannot do from a laptop. Useful experiments, roughly in order of cost:

  • time the cell on GPU with jax_enable_x64 off, to size the FP64 contribution
  • time it on GPU with chain_method='vectorized'
  • capture a jax profile and look at kernel time versus gap time, which separates hypothesis 1 from 2
  • time a single loglik_jax gradient evaluation on CPU and GPU, which isolates the model from the sampler

Why this matters beyond one lecture

If it is FP64 or small-op overhead, the rule generalises: any lecture pairing jax_enable_x64 with a small model may be slower on our GPU runners than on their CPUs. We would want to know which of the existing jax lectures are in that category, and whether the guidance should be to pin small-model lectures to CPU by default and reserve the GPU for lectures with genuinely large arrays.

There is also a CI-hygiene angle. This failure cost about two hours of on-demand g4dn.2xlarge time across two runs, because a failed notebook is not cached and every builder re-executes it — the same cell timed out twice in one job, once in the notebook build and again in the HTML build. Worth considering whether the per-cell timeout: 2400 in lectures/_config.yml should be lower, so a pathological cell fails fast instead of burning 40 minutes per builder.

Activity

  1. added
    maintenance
    on Aug 1, 2026
  2. mmcky commented on Aug 3, 2026

    @mmcky
    ContributorAuthor

    This is now one of two data points — QuantEcon/lecture-python-programming#363 hit a different JAX-on-cloud failure the same week (a GPU preallocation race between concurrently executing notebook kernels, confirmed by an instrumented run and fixed with XLA_PYTHON_CLIENT_PREALLOCATE=false). Different layer, same underlying gap: no tested model of how JAX behaves on our runners.

    Proposed as an investigation project in QuantEcon/meta#350, which folds in this issue's experiment list. Keeping this issue open as the FP64/launch-overhead specific tracker; the project issue covers the general characterisation.

  3. mmcky commented on Nov 1, 2026

    @mmcky
    ContributorAuthor

    Measured: it's launch latency, not float64 (2026-11-01). QuantEcon/benchmarks now has both pieces this issue asked about: a Kalman filter of the same shape, timed on this runner's T4 and on its own CPU, and the lecture itself, run on this runner.

    The filter is the precision suite's kalman workload: a 10-state Kalman filter log-likelihood under lax.scan, the shape of the NUTS cell's model (precision/kernels.py). Warm, at 100,000 steps, on g4dn.2xlarge (results/runson-g4dn.2xlarge/2026-10-25.jsonl):

    T4 The runner's Xeon The Xeon's lead
    float64, steps per second 7.1k 94k 13×
    float32, steps per second 10.2k 83k 8×

    Against the four hypotheses:

    1. float64 on a T4: a small part. On this kernel float64 costs the T4 1.4× over float32, because each step is too small for arithmetic to be the limit. In float32 the T4 is still 8× slower than its CPU.
    2. Kernel launch overhead: the cause. Each step is a chain of tiny matrix operations, and the card pays its launch and synchronisation latency on every one. A faster card doesn't help. Every card measured runs this filter at 6k to 11k steps a second in float64: the T4, a TITAN RTX, an A10, an A100, an RTX PRO 6000 Blackwell and an H100. This runner's Xeon runs it at 94k, and Apple's M5 Pro and M6 CPUs at 570k to 630k.
    3. chain_method='sequential': not measured. 'vectorized' would put the four chains in the same kernels, so the card would pay each step's latency once rather than four times. That is at most about 4×, against a 13× gap.
    4. Compilation: not it. The filter compiles in under 1 s on the T4.

    The lecture on this runner, with the pin, three runs on each device (the table, from QuantEcon/benchmarks#22):

    • On the runner's Xeon it takes 874 s, 406 s of it in the NUTS cell. That matches the 14m52s above.
    • On an M5 Pro it takes 158 s, 67 s of it in that cell.
    • If the card's 13× deficit on the filter carried over to the NUTS cell, it would take roughly 5,300 s on the T4. That is consistent with the timeout at 2,400 s, though the cell itself wasn't timed on the card.

    So the pin is right on any machine with a card, not only the T4: no card measured runs this shape faster than a CPU. The same holds for other lectures built on long chains of small steps. On this runner, endogenous_lake takes 499 s on the T4 against 24 s on its CPU, for the same reason: a Python bisection around a jitted while_loop.

    That answers this issue's question, I think. What's left is the vectorised-chains test, worth running only if someone wants a GPU path for this lecture.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

infrastructuremaintenance

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions