Repository navigation
Conversation
The scan body now runs under torch.utils.checkpoint with a selective checkpoint policy that reads each op's remat_region: save the MoE expert counts and compile.save_regions, recompute everything else, the FSDP gather included. This replaces the hook that tagged the body's joint graph. It needs a torch whose checkpoint tags nodes while scan traces its body, even under the graph trainer's non-strict make_fx (PyTorch (a) + b'). scan_with_remat_policy raises on a torch without it. The scan offload no longer finds the saved layer input by a layer_input tag, which a SAC policy cannot set on a placeholder. It matches the body output clone(carry placeholder) instead.
compile.save_last_n_layers now works with --compile.use-scan-layers. The layers run as consecutive scans over slices of the same stacks, each with its own save regions. fence_scan_segments puts an optimization barrier between consecutive scans in the same direction, so one loop's weight gathers do not overlap the previous loop's reduce-scatters. With no last-N setting, or N >= n_layers, it is one scan, as before.
Conflicts with #153/#154's r2 fixes, resolved to this branch's SAC design: - scan_remat_policy.py: keep scan_with_remat_policy; the r2 joint-graph tagger is gone with the window it ran in. - tpu_host_offload.py: keep matching clone(carry); take r2's forward-scan count, its two error messages (reworded: no tag now) and the output-reader fix. - tests: RealScanTest and the offload distributed test find the saved carry as clone(carry) instead of by the layer_input tag; r2's tagger-only tests and helpers are dropped. r2's unmatched-save-region check comes next.
Carries #153's r2 check ("Raise on an unmatched scan save region") over to the SAC policy: the policy records each region it saves, and scan_with_remat_policy raises after scan if a requested one never came up, e.g. attention.inner_attention with SDPA instead of Splash. Also ports r2's two gather tests: a gather inside a save region, and the real spmd_types gather_slice, are both recomputed while the region around them is saved.
Both are torch's job now: - activation_memory_budget defaults to 1.0, where min-cut honours every tag. Upstream torchtitan never pins it; only MemoryBudgetAC sets it, from a user flag. - pytorch#203534's HopGraphMinCutPartitioner enters preserve_node_meta() around the joint-graph trace, so the policy sees each op's region. test_the_budget_is_restored becomes test_the_policy_holds_at_the_default_budget. Every test in the file, including the non-strict make_fx one, passes without either context.
Conflicts with #153's r2 fixes: - config.py: keep this branch's message (save_last_n_layers is allowed). - test_tpu_graph_trainer_config.py: keep r2's unknown-region case; drop r2's case that refuses save_last_n_layers under scan (this branch adds it). - layer_stack.py: r2's has_moe wording plus this branch's last-N field.
# Conflicts: # torchtitan/experiments/tpu/graph_trainer/layer_stack.py # torchtitan/experiments/tpu/graph_trainer/scan_remat_policy.py # torchtitan/experiments/tpu/graph_trainer/tests/test_scan_remat_policy.py # torchtitan/experiments/tpu/graph_trainer/tests/test_tpu_graph_trainer_config.py # torchtitan/experiments/tpu/graph_trainer/tests/test_tpu_host_offload.py
The last-N gradient test becomes a second case of the scan-vs-loop gradient test, the three _segments tests one table test, and the last-N config test a case of the parametrized save_regions config test.
…emat-3-segments-1119
…emat-3-segments-1119
…emat-3-segments-1119
…emat-3-segments-1119
…emat-3-segments-1119
… segment - config: `save_last_n_layers` under scan raises with `graph_split.enabled`, because the split program skips `fence_scan_segments`. A `scan_offload` name saved only in `save_last_n_layers` gets its own message. The scan model gets `save_last_n_layers` only under the policies that allow it, like `save_regions`. - fence_scan_segments: when last-N splits the layers and the graph has scans, finding no pair to fence raises instead of running unfenced. Graphs with no scan (optimizer, grad norm) stay silent. - scan offload: the saved-carry check runs per forward scan and names it; with last-N there is one forward scan per segment. - Nits: log the last-N clause only when set; `frozenset()` in the `_segments` docstring; one assert for the fence's first value; drop the N=0 segments case the config already rejects.
…ts-1119 # Conflicts: # torchtitan/experiments/tpu/graph_trainer/config.py # torchtitan/experiments/tpu/graph_trainer/layer_stack.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #154 (and #153). Review only this PR's own commits: aabae6c...d53dffe
Part of #29. Third of 3 PRs:
compile.scan_offload); attention save works with offloadcompile.save_last_n_layersunder scan, as fenced scan segmentsRequires PyTorch nightly ≥ 2.16.0.dev20261120 (through #153): pytorch/pytorch#203334 (
012eb2b6a70) and pytorch/pytorch#203534 (4378929563f).Goal
Let a recipe save attention in only the last N layers under scan, to trade memory for speed in finer steps than all-or-nothing.
This PR
Change:
compile.save_last_n_layersworks with--compile.use-scan-layers:One scan body has one partition, so the layers run as consecutive scans over slices of the same stacks, each with its own save regions:
fence_scan_segmentsputs an optimization barrier between consecutive scans in the same direction. Without it the next loop's weight gathers overlap the previous loop's reduce-scatters and both sets of buffers are live (4 real Qwen3-235B layers, 8 v6e chips, last 2 saved: 37.4 GiB unfenced (compile OOM), 17.7 GiB fenced, 19.8 GiB as one scan).save_regions(unit test).Validation
64 v7x chips (128 ranks), this PR alone against main: tree = this PR at
f3c0b6cdd(jackoh/remat-3-segments-1119), not the top of the stack. Main =a83bff731. Both arms on the same image, which must have PyTorch with pytorch/pytorch#203334 + #203534 (nightly 2.16.0.dev20261120 or later) and torch_tpu main. Step time = median of the steps after warmup, without the profiled step.64-chip runs on
f3c0b6cdd; since then: merges of #154 and main, and the review fixes (8e2d4efa9e: config checks, a per-segment offload check, the fence's no-pair check), and the #153/#154 review edits (comments, a docstring, tests, a constant, the PyTorch-fix check and the no-grad path). On the current headd53dffe891, 8 chips: default step HLO identical to main, and the last-8 step HLO identical to before the fixes.nw-mn235-rs64nw-a3235-rs64nw-mn405-rs64nw-a3405-rs64(window r-875a2)Tests (proxy torch = 2.16.0.dev20261112 + the merged pytorch#203334 and #203534 commits, torch_tpu main e10d1a346, 8 v6e; stock nightly 2.16.0.dev20261120 (image native-ab-e10d1a346-nw1120tn): 8/8 test modules pass at head
d53dffe891):test_scan_remat_policy,test_layer_stack,test_tpu_memory_policy,test_tpu_graph_trainer_config,test_tpu_host_offload,test_fence_scan_segments): all pass.test_layer_stack_distributed11/11,test_tpu_host_offload_distributed3/3.memory:at step 15: all layers 27.73 GiB, last 8 layers 25.41 GiB, last 8 layers with offload 21.11 GiB (loss + grad norm 15/15 equal to last 8 without offload).Results, 8 v6e chips, image
native-ab-22d846b, libtpu 0.0.51, 30 steps, vs main on the same torch (11/05-11/06, older base; proxy-torch re-run above; stock nightly, this PR's head: attention in the last 8 layers trains 15 steps, 2.880 s, XLA peak 12.03 GiB):llama3_8b_scanseq 8192 b2, defaultdebugmodel_moe, global aux loss), defaultf32[8,64]counts all-reduce (8 layers x 64 experts), which XLA moves out of the forward loop; the backward loop has none. Same as main.fence_scan_segments: fenced 2 scan boundaries(one per direction). The host stackbf16[32,16384,4096]becomes twobf16[16,16384,4096].64 v7x chips (128 ranks),
qwen3_moe_235b_scanb16, 15 steps, vs main. tok/s/rank = median of steps 3–15 without the profiled step 13. Run on an older head; the expert-count label has since moved intomoe.py(metadata only).Segment gradients: each segment's weight gradient is f32 and covers only its rows, so backward pads both to
[n_layers, ...]and adds them. Measured not at the memory peak: 8B last 8, 0 of 6 pad/add fusions in the XLA peak live set; 235B last 4 (64 chips, jobjackoh-nw-sg235-0920), 0 of 10, live for about 5 instructions at the end of backward (at most 8.95 GiB live) against a 43.11 GiB peak, which sits in the last-4 backward where the saved attention stack is live.Verdict: keep it opt-in. +3.6% at 8B for half the layers; at 235B, last 4 is flat on speed for +8 GiB, and all layers does not compile.