Repository navigation
Keep TONN gradients right under torch.compile - #76
raunak2007 wants to merge 1 commit into
Conversation
|
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>
f24bad5 to
5e9b33c
Compare
|
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 (
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. Why fdef3ec stalls in the stream phase: its Two things you can take separately:
Also, on both trees the first compiled call runs before the TONN plan cache ( 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 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. |
Fixes #75. Rebased on nairr at b54cf92, where it replaces the
torch.compiler.disablesplit from fdef3ec._unit_scalereadsself.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.pyheld a reference toinner.scaleacross 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 onlen(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.pyat 0.1B withexealm_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)--batch 4(16, then 4)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 withfullgraph=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