Skip to content

Keep TONN gradients right under torch.compile - #76

Open
raunak2007 wants to merge 1 commit into
exea-labs:nairrfrom
raunak2007:compile-scale-grad
Open

raunak2007 wants to merge 1 commit into
exea-labs:nairrfrom
raunak2007:compile-scale-grad

Conversation

@raunak2007

@raunak2007 raunak2007 commented Nov 25, 2026 •

Copy link
Copy Markdown

Fixes #75. Rebased on nairr at b54cf92, where it replaces the torch.compiler.disable split from fdef3ec.

_unit_scale reads self.scale, divides by it and updates it in the same forward. Written in place, that update breaks compiled training two ways: a stream's next window changes a tensor the compiled backward saved ("modified by an inplace operation", the crash fdef3ec fixed), and a single window can get gradients divided by the updated sizes, because the partitioner recomputes the read in the backward (pytorch/pytorch#201089, not fixed in a release). fdef3ec fixes both by moving the read and the update out of the graph. This PR builds the new scale out of place and rebinds the buffer (self.scale = self.scale.index_copy(...)), so the tensor the forward read is never written and the layer has no graph break. Eager results are the same. experiments/expanse/nine_b.py held a reference to inner.scale across checkpoint recompute and swaps the attribute now.

Both fixes give correct gradients. What differs is how much of the model torch.compile actually compiles. With fdef3ec, the break sits inside ExeaLM.forward's loop over layers, so dynamo skips the whole forward ("graph breaks in loops are not supported") and compiles each layer separately. The 13 attention layers share one frame whose guards depend on len(ctx['stream']), so in the stream phase each layer needs its own compile, the frame reaches dynamo's recompile limit (8), and the remaining attention layers run eager.

pretrain.py at 0.1B with exealm_mid_100m.json (one TONN layer, at layer 7), on one L4, bf16, torch 2.15.0, 400 steps with streams of 4 windows from step 200, synthetic Markov text with a 32,768-token vocabulary. nairr at 59688c3 and this PR's commit on top of it, every configuration of a batch size run back to back in one container. Tokens per second, steps 100-200 (one window per row) and 250-400 (streams):

--batch 8 (32 windows per forward pass, then 8) one window streams graph breaks recompiles
eager 15,700 19,400
compiled, nairr 32,500 (2.07x) 21,900 (1.13x) 2 16, limit hit
compiled, this PR 36,400 (2.32x) 30,900 (1.59x) 0 3
--batch 4 (16, then 4) one window streams
eager 18,300 17,700
compiled, nairr 32,200 (1.75x) 19,600 (1.11x)
compiled, this PR 35,800 (1.95x) 28,200 (1.59x)

Held-out bits per token at step 400 agree within the spread between eager runs (2.395 to 2.418 at batch 8, 2.707 to 2.729 at batch 4).

The cost is compile time. From runs that started with an empty cache, this PR compiles for about 180 s at the start and 240 s when streams begin, against about 95 s and 130 s on nairr, because the whole model is one graph. Over these 400 steps the compiled run took 561 s of wall time at batch 4, against 390 s on nairr and 197 s eager. Extrapolating the batch-8 rates to the plan's 0.1B run (1,468 steps of 262,144 tokens, streams in the last 20%), the extra compile time is repaid: about 3.2 hours on this L4 with this PR, 3.7 with nairr and 6.5 eager. That is arithmetic on short runs, not a measurement. The three recompiles: the first call runs before the TONN plan cache (_plan_key) exists, the stream phase switches from 16 rows to 4, and the stream state is empty for the first window only. nairr has the same three plus 13 more, 8 of them the attention frame reaching the limit.

Tests in tests/test_compile.py: one layer compiled against eager with measured and with zeroed scales (fails on the in-place code from before fdef3ec); three streamed windows of a small ExeaLM with one backward (fails before fdef3ec with the inplace error); a TONN layer compiles with fullgraph=True (fails on fdef3ec). On an L4 the layer comparison for a 6-type and a 14-type layer, fp32 and fp64, train and eval, matches eager to 1.7e-6 on input gradients and 6.1e-5 on parameter gradients; the in-place code gives up to 4.7 and 97. The full suite passes on macOS at 5e9b33c (394 passed, 9 skipped).

Not measured: several GPUs, sizes above 0.1B, an H100. At batch 8 the eager one-window phase uses 19.6 of the L4's 22.5 GB and is slower than at batch 4, which flatters compile there; the stream phase is the closer match to the plan, whose stream passes are 16 windows on an H100.

🤖 Generated with Claude Code

@raunak2007

raunak2007 commented Nov 25, 2026 •

Copy link
Copy Markdown
Author

Correction, 26 November: this was first measured on main's TONN layer (fd70f9f), not on this branch. Rerun on an L4 (torch 2.15.0+cu132) on nairr at 7abbcfb for "before" and on this branch's commit for "after", the maxima are the same: 4.7 and 97 before, 1.7e-6 and 6.1e-5 after.

Same check on an L4 (torch 2.15.0+cu132) with this branch: one layer of 6 types and one of all 14, fp32 and fp64, train and eval, measured and zeroed scales. Inductor now matches eager to 1.7e-6 on input gradients and 6e-5 on parameter gradients in every cell. Before the change, the same matrix gave up to 4.7 and 97.

fdef3ec moved the scale read and update into a
torch.compiler.disable function to stop compiled stream training
from crashing: the next window updated, in place, a buffer the
backward had saved. That break splits every TONN layer into frames
compiled one per layer, and past dynamo's recompile limit (8) the
rest run eager.

Build the new scale out of place and rebind the buffer instead. The
tensor a window's backward saved, or recomputes from
(pytorch/pytorch#201089), is never written to, so streams run and
gradients match eager, with the layer in one graph.

nine_b's checkpoint wrapper restored scales with copy_ into a held
reference; it now swaps the attribute.

Tests: compiled gradients of one layer, compiled stream training of
a small model, and a fullgraph compile of a layer.

Fixes exea-labs#75

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@raunak2007

raunak2007 commented Nov 26, 2026 •

Copy link
Copy Markdown
Author

Rebased on b54cf92 and measured on the one-TONN-layer layout. The main result doesn't depend on this PR: compiled ExeaLM is faster than eager with fdef3ec as it is.

On one L4 at 0.1B (exealm_mid_100m.json, bf16, torch 2.15.0, 400 steps with streams from step 200, every configuration run back to back in one container), tokens per second:

--batch 4 one window streams
eager 18,300 17,700
compiled, nairr (fdef3ec) 32,200 (1.75x) 19,600 (1.11x)
compiled, this PR 35,800 (1.95x) 28,200 (1.59x)

The one-window phase is 80% of the plan's steps. Weighting the two phases 80/20, a whole run comes to about 1.57x eager compiled with fdef3ec and 1.87x with this PR, before compile time. That is arithmetic from these batch-4 ratios, not a measured full run. --batch 8 gives the same stream-phase ratios (1.13x and 1.59x); the description has its table. Both fixes give correct gradients, and compiled runs end at the same held-out loss as eager.

Why fdef3ec stalls in the stream phase: its compiler.disable call is a graph break inside ExeaLM.forward's loop over layers, so dynamo skips the whole forward ("graph breaks in loops are not supported") and compiles each layer as its own frame. The 13 attention layers share one frame (mix, baseline.py:100) with guards on len(ctx['stream']), which differs at every attention layer once streams start. The frame recompiles once per layer, reaches recompile_limit (8), and the remaining attention layers run eager.

Two things you can take separately:

  1. The recompile limit, without this PR: torch._dynamo.config.recompile_limit = 64 before compiling took nairr's stream phase from about 20,000 to 24,800 tokens/s at --batch 4. That run compiled each block separately and ran in a different container from its baseline (separate containers differed by up to about 15%), so it shows where the time goes rather than a measured speed-up of --compile 1. The guard is in the streaming code, which I haven't touched.
  2. This PR: no graph breaks and 3 recompiles against nairr's 16 (the same three causes, plus 13 more, 8 of them the attention frame up to the limit), for 1.59x eager in the stream phase. It compiles for longer, because the whole model is one graph: about 180 s at the start and 240 s when streams begin, against 95 s and 130 s, from runs that started with an empty cache.

Also, on both trees the first compiled call runs before the TONN plan cache (_plan_key) exists, so the whole model compiles twice at the start; building the plan before torch.compile would skip one.

All of this is one GPU, 0.1B, synthetic text. I haven't run several GPUs or an H100, so I can't say how much of the 0.72x in results/expanse/README.md this explains. Could you run compile_check.sb on this branch with --config experiments/expanse/exealm_mid_500m.json in place of exealm_core.json? It's the only way to confirm this on the H100 at the plan's batch.

Corrections to what I posted earlier: #75's "no recompiles" held only for a 5-step run without streams, and its gradient figures and my L4 comment above were first measured on main's TONN layer (fd70f9f). Rerun on nairr at 7abbcfb they hold; #75 and that comment now say so.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant