Skip to content

[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
jackoh/remat-1-regionsfrom
jackoh/remat-2-offload
Open

jack8558 wants to merge 24 commits into
jackoh/remat-1-regionsfrom
jackoh/remat-2-offload

Conversation

@jack8558

@jack8558 jack8558 commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #32. Stacked on #153. Second of 3 PRs:

PR What it does Depends on
#153 Region tags + a SAC policy decide what the scan body saves; no PyTorch patches main, PyTorch nightly 2.16.0.dev20261120+
#154 (this) Scan host offload picked by name (compile.scan_offload); attention save works with offload #153
#222 (draft) compile.save_last_n_layers under scan #154

Requires 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_offload finds the tensor to send to host by shape: a value the forward scan saves and the backward scan reads, largest wins. So it:

  • can offload only one tensor;
  • can pick the wrong one. On the debugmodel it picked the loss logits (Host offload into scan bodies should be tag-driven, like JAX's remat policy #32), and once the body also saves the attention output (compile.save_regions), it can pick that instead of the layer input;
  • has no way to name what goes to host, as JAX does (checkpoint_name + save_and_offload_only_these_names);
  • warns and runs without offload when unsure: a correct run at the wrong memory.

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.

Name What goes to host How the pass finds it in the forward body
layer_input (default) the saved layer input the output clone(carry): how scan returns a saved body input
a save region, e.g. attention.inner_attention that region's saved values (Splash out + LSE) outputs whose op carries the remat_region label (scan keeps node meta)
main this PR
Which value goes to host a forward-scan save read by the backward scan, largest wins the values named in compile.scan_offload
Saves not named n/a stay in HBM
Can't offload two cases warn and skip every case raises
tpu_selective_and_offload under scan refused save regions in the body; named saves on host

The 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 in save_regions (so full_and_scan_offload takes only layer_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_gb is ignored (every named save goes to host), and setting it logs a warning.

--compile.use-scan-layers --compile.memory_policy=tpu_selective_and_offload \
--compile.save_regions attention.inner_attention \
--compile.scan_offload layer_input attention.inner_attention
Lines Files
Code +255 / −273 3
Docs +12 / −5 1
Tests +337 / −131 3

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 head aabae6c539 (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, and tpu-test.yml runs only for PRs into main.

The default is unchanged.

Run Compared with Loss + grad norm Step HLO Step time Peak memory
Qwen3-235B, 64 v7x, 15 steps main 15/15 identical — 24.81 vs 24.82 s 77.77 GiB both
Llama 3 405B b3, 64 v7x, 10 steps main 10/10 identical same program 99.42 vs 100.87 s 94.72 GiB both
Llama 3 8B, 8 v6e, default before scan_offload 15/15 identical 0 of 5,731 lines differ 2.93 s both 16.35 GiB both
Llama 3 8B, 8 v6e, tpu_selective_and_offload + attention save before scan_offload 15/15 identical 0 of 5,568 lines differ 2.73 vs 2.74 s 18.81 GiB both

The 64-chip runs are at 69771b9b3c, which comes before compile.scan_offload (they ran the old shape-based selector) and before main 34a0d8b49e was merged in. The 8-chip rows show that the commit adding scan_offload (8f67ea0c14) compiles to the same step program as the commit before it (b47c11afd6). After 8f67ea0c14, only names, docstrings, one config-time warning and the #153/#154 review edits (merged in) changed; these were not re-run at the head aabae6c539.

Named offload (8B, 8 v6e): adding attention.inner_attention to scan_offload cuts 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.

…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
jack8558 force-pushed the jackoh/remat-2-offload branch from 0d99d39 to 65b5c7b Compare November 5, 2026 14:14
@jack8558
jack8558 force-pushed the jackoh/remat-1-regions branch from 04ad66d to 3fa4759 Compare November 5, 2026 14:14
@jack8558 jack8558 changed the title Pick the scan offload by tag, and allow attention save with offload under scan [TPU][Scan remat] 2/4: Pick the scan offload by tag, and allow attention save with offload Nov 5, 2026
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.
@jack8558 jack8558 changed the title [TPU][Scan remat] 2/4: Pick the scan offload by tag, and allow attention save with offload [TPU][Scan remat] 2/3: Offload the scan's saved layer input, and allow attention save with offload Nov 23, 2026
…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.
@jack8558
jack8558 marked this pull request as ready for review November 25, 2026 07:39
"'tpu_selective_and_offload'"
)
return
known = {"layer_input", *SCAN_SAVE_REGIONS}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should scan_offload require layer_input (or warn without it)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
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.

Host offload into scan bodies should be tag-driven, like JAX's remat policy

2 participants