Repository navigation
[TPU][Scan remat] 2/3: Offload the scan's saved layer input, and allow attention save with offload - #154
Open
jack8558 wants to merge 24 commits into
Open
[TPU][Scan remat] 2/3: Offload the scan's saved layer input, and allow attention save with offload#154jack8558 wants to merge 24 commits into
jack8558 wants to merge 24 commits into
Conversation
…nder scan The scan region pass tags the carry placeholder layer_input. Scan's clone of a saved carry copies that meta, so the post-trace offload pass finds the layer input by tag instead of by size, and leaves other saved values (attention output, LSE, MoE counts) in HBM. Every case it cannot offload now raises. tpu_selective_and_offload now runs under scan: save regions in the body, layer input on host. The default program is unchanged.
jack8558
force-pushed
the
jackoh/remat-2-offload
branch
from
November 5, 2026 14:14
0d99d39 to
65b5c7b
Compare
jack8558
force-pushed
the
jackoh/remat-1-regions
branch
from
November 5, 2026 14:14
04ad66d to
3fa4759
Compare
jack8558
added a commit
that referenced
this pull request
Nov 6, 2026
…d rework Build the toy config from TpuGraphTrainerCompileConfig, so a field a pass starts reading (use_scan_layers, under #154) gets its default. Tag the forward scan's saved carry layer_input, as scan_remat_policy does. Main's offload picks the largest stack; #154's picks by that tag and raises when it finds none.
Brings in origin/main and #153's review fixes. Conflicts (same resolution as the #153 fix needed before): - graph_trainer/config.py: the scan error message names both tpu_selective policies and save_last_n_layers. - graph_trainer/scan_remat_policy.py: docstring keeps the layer_input paragraph and the fsdp.gather paragraph. - graph_trainer/tpu_memory_policy.py, tests/test_scan_remat_policy.py: keep both imports, at main's components/remat_regions path. Also point tpu_host_offload and its tests at components/remat_regions.
- CPU test: trace a real scan under scan_body_remat_window and check the forward body's saved clone carries layer_input (and the attention region next to it), then that the pass offloads exactly that clone. - TPU test: the attention-save arm also checks splash runs in the forward body and a forward-scan output carries the region tag. - With forward scans but no tagged layer_input, the error now says so and counts the scans. - _only_read_by treats the graph output as a live reader. - The generic-offload-under-scan error names both scan offload policies.
# Conflicts: # torchtitan/experiments/tpu/graph_trainer/scan_remat_policy.py # torchtitan/experiments/tpu/graph_trainer/tests/test_scan_remat_policy.py
…arry With the SAC policy, scan returns the layer input it saves for backward as clone(carry). The offload pass now finds that output directly instead of a layer_input tag, so the LAYER_INPUT region and the meta-loss message go away. Tests: the four raise-and-leave-untouched offload tests become one parametrized test, the two real-scan cases one, and the two scan save_regions config tests one test over both memory policies.
…guide PR 2 makes tpu_selective_and_offload work with scan: the save regions stay in the scan body and the layer input goes to host from inside the loops. The guide still said scan rejects it.
"layer_input" (default) keeps #154's saved-carry rule; a save region name also offloads that region's saved values from inside the scan bodies. Unknown, unsaved, or unmatched names raise.
Table-driven cases and shared helpers; same behavior covered. - test_tpu_host_offload.py: the three which-saves-go-to-host tests become one parameterized test; the region-not-saved raise joins the raise table (and now also checks the bodies are untouched); the two real-scan offload tests become one parameterized test. Drop a stack-device assertion that always passed (torch.empty is already on CPU). - test_tpu_host_offload_distributed.py: drop two presence asserts the exactly-one counts already cover; one helper runs both trainings.
Same string value and config default; the old name matched a guard against the removed r2 layer_input tag.
… scan detail in the branch comment
jack8558
commented
Nov 25, 2026
… the forward-scan counter
jack8558
marked this pull request as ready for review
November 25, 2026 07:39
jack8558
requested review from
alexanderb14,
jerryx99,
tanvibajpai,
vlasenkoalexey and
wconstab
November 25, 2026 07:44
This was referenced Nov 25, 2026
tanvibajpai
reviewed
Nov 25, 2026
| "'tpu_selective_and_offload'" | ||
| ) | ||
| return | ||
| known = {"layer_input", *SCAN_SAVE_REGIONS} |
Contributor
There was a problem hiding this comment.
Should scan_offload require layer_input (or warn without it)?
Contributor
Author
There was a problem hiding this comment.
It's in the default, so it's only missing if the user drops it on purpose, e.g. to keep layer inputs in HBM and offload attention instead.
- tpu_memory_policy.py: without scan, save-region values always stay on device; only scan's compile.scan_offload can send them to host. - config.py: use SAVED_CARRY from tpu_host_offload for the scan_offload default and its three checks, instead of the "layer_input" string.
jack8558
added a commit
that referenced
this pull request
Nov 26, 2026
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.
Fixes #32. Stacked on #153. Second of 3 PRs:
compile.scan_offload); attention save works with offloadcompile.save_last_n_layersunder scanRequires PyTorch nightly ≥ 2.16.0.dev20261120 (through #153): pytorch/pytorch#203334 (
012eb2b6a70) and pytorch/pytorch#203534 (4378929563f).Goal
Pick what the scan keeps on host by name, like JAX's offload policy: name a saved value, and it goes to host; everything not named stays in HBM.
Problem
From #32:
full_and_scan_offloadfinds the tensor to send to host by shape: a value the forward scan saves and the backward scan reads, largest wins. So it:compile.save_regions), it can pick that instead of the layer input;checkpoint_name+save_and_offload_only_these_names);Naming was not possible before because tags inside a scan body were lost in tracing (#32, "Why we can't do this today"). pytorch/pytorch#203334 (merged) keeps them, and #153 uses them.
Proposal
One config field,
compile.scan_offload: tuple[str, ...] = ("layer_input",). The default is the old behavior: only the layer input goes to host.layer_input(default)clone(carry): howscanreturns a saved body inputattention.inner_attentionremat_regionlabel (scankeeps node meta)compile.scan_offloadtpu_selective_and_offloadunder scanThe offload stays inside the forward body and the reload on the backward body's
index_select, so the default program is unchanged.Errors at config time (
config.py): an unknown name; an empty list or a repeat; a region not insave_regions(sofull_and_scan_offloadtakes onlylayer_input); a non-default value without scan + a scan offload policy. At pass time: a name that matches no save raises.With scan offload,
compile.cpu_offload_budget_gbis ignored (every named save goes to host), and setting it logs a warning.Validation
All runs use one image: PyTorch nightly 2.16.0.dev20261120 (has pytorch/pytorch#203334 and #203534), torch_tpu
e10d1a346, libtpu 0.0.51. CPU tests at headaabae6c539(scan remat policy, config, host offload, 8-chip host offload distributed, memory policy): all pass, 0 failures. CI does not run on this PR: its base is #153's branch, andtpu-test.ymlruns only for PRs into main.The default is unchanged.
scan_offloadtpu_selective_and_offload+ attention savescan_offloadThe 64-chip runs are at
69771b9b3c, which comes beforecompile.scan_offload(they ran the old shape-based selector) and before main34a0d8b49ewas merged in. The 8-chip rows show that the commit addingscan_offload(8f67ea0c14) compiles to the same step program as the commit before it (b47c11afd6). After8f67ea0c14, only names, docstrings, one config-time warning and the #153/#154 review edits (merged in) changed; these were not re-run at the headaabae6c539.Named offload (8B, 8 v6e): adding
attention.inner_attentiontoscan_offloadcuts runtime memory 24.80 → 20.92 GiB, with the same loss and step time (2.73 → 2.72 s). Chip memory (HBM) drops 28.36 → 24.48 GiB in XLA's memory report; the 'compiled peak' our tool printed also counted host-offloaded buffers.