Repository navigation
Save trailing adds so replay skips them - #5070
Merged
Merged
Conversation
[ghstack-poisoned]
anijain2305
requested review from
fegin,
tianyu-l,
wconstab and
wwwjn
as code owners
October 5, 2026 21:45
This was referenced Oct 5, 2026
anijain2305
added a commit
that referenced
this pull request
Oct 5, 2026
`torch.utils.checkpoint` stops replay once every tensor saved under the checkpoint has been recomputed, so it never reruns the trailing ops of a block that save nothing. `remat.checkpoint` replays the whole block, so the bare end-of-block residual add and the shared-expert add replayed, and their `recompute_needs_tensor` markers kept their inputs (e.g. the MoE combine output and the shared-expert output) alive from the forward until replay. Make these adds fixed `recompute=False` regions (`ffn_residual`, `moe.shared_add`) and drop the markers. A saved add keeps nothing resident because it saves nothing for backward, its inputs need no persisting, and replay skips it, matching checkpoint early stop. Muse Glimmer's region also covers the trailing `post_ffn_norm`: it saves its input, which a saved w2 keeps anyway, and a per-token rstd, which is negligible. With every region saved (RegionAC `*`), activation memory drops from 8.18 to 7.93 GiB on qwen3 MoE (30B-A3B width, 4 layers, FSDP4 + EP4) and from 9.11 to 8.67 GiB on DeepSeek V3 16B (same setup), one tensor per layer plus one more per layer with shared experts, at the same step time; losses are bitwise identical. ghstack-source-id: 879da0b Pull-Request: #5070
anijain2305
added a commit
that referenced
this pull request
Oct 5, 2026
`torch.utils.checkpoint` stops replay once every tensor saved under the checkpoint has been recomputed, so it never reruns the trailing ops of a block that save nothing. `remat.checkpoint` replays the whole block, so the bare end-of-block residual add and the shared-expert add replayed, and their `recompute_needs_tensor` markers kept their inputs (e.g. the MoE combine output and the shared-expert output) alive from the forward until replay. Make these adds fixed `recompute=False` regions (`ffn_residual`, `moe.shared_add`) and drop the markers. A saved add keeps nothing resident because it saves nothing for backward, its inputs need no persisting, and replay skips it, matching checkpoint early stop. Only the adds are wrapped: Muse Glimmer's trailing `post_ffn_norm` stays bare, with its marker. With every region saved (RegionAC `*`), activation memory drops from 8.18 to 7.93 GiB on qwen3 MoE (30B-A3B width, 4 layers, FSDP4 + EP4) and from 9.11 to 8.67 GiB on DeepSeek V3 16B (same setup), one tensor per layer plus one more per layer with shared experts, at the same step time; losses are bitwise identical. ghstack-source-id: 6dfa17d Pull-Request: #5070
tianyu-l
approved these changes
Oct 6, 2026
[ghstack-poisoned]
anijain2305
added a commit
that referenced
this pull request
Oct 6, 2026
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.15.0) (oldest at bottom): * #4893 * #5070 * __->__ #4958 Human note ---------- * With EP > 1, the all-to-all dispatcher declares two remat regions, `dispatch` and `combine`. The previous regions covered only the all-to-all collectives; these also cover the processing around them. * With EP = 1 there is no communication, so dispatch and combine are not regions; they are always recomputed. * The combine now multiplies the bf16 expert outputs by the fp32 router scores instead of casting them to fp32 first. The product is still computed in fp32, but the multiply now saves the bf16 input for backward instead of an fp32 copy. This is unrelated to remat but lands in the same commit. Agent note ---------- Before, the split sync ran as a bare op, so it replayed in every backward (one host sync per MoE layer) even with `ep_communication` saved. The operator-level SelectiveAC avoided this by always saving CUDA -> CPU copies; a saved `dispatch` region gives a torch_remat policy the same behavior. The pins on the all-to-all outputs and on the w2 output are gone, since those tensors are now region inputs or outputs: when w2 and the combine are both saved, the w2 output is no longer kept. Regions return only tensors, so the splits come back as CPU tensors and the pre-permute shape is derived from them; TorchAO's padded `_unpermute` allocates and strips its sentinel row itself. Without EP there is no communication, so the local dispatch and combine stay bare. Bare readers of the routed-expert outputs (the dtype cast and postprocess after w2, the local scatter-add, the TP zero-fill, the shared-expert add, and the DeepEP/HybridEP kernels) call `recompute_needs_tensor` before they read. The combine also multiplies the bf16 expert outputs by the float32 router scores directly instead of casting them to float32 first. Type promotion still computes the product in float32, but the multiply now saves the bf16 output for backward rather than a float32 copy twice its size. The local, all-to-all, and TorchAO EP=1 combines share `_score_and_scatter_add`. Losses are bitwise identical; on qwen3 MoE (30B-A3B width, 4 layers, TP2 EP2) activation memory without AC drops from 6.5 GiB to 5.5 GiB (256 MiB per layer).
[ghstack-poisoned]
[ghstack-poisoned]
submartingales
pushed a commit
to botcanlearn/torchtitan-upstream
that referenced
this pull request
Oct 6, 2026
`torch.utils.checkpoint` stops replay once every tensor saved under the checkpoint has been recomputed, so it never reruns the trailing ops of a block that save nothing. `remat.checkpoint` replays the whole block, so the bare end-of-block residual add and the shared-expert add replayed, and their `recompute_needs_tensor` markers kept their inputs (e.g. the MoE combine output and the shared-expert output) alive from the forward until replay. Make these adds fixed `recompute=False` regions (`ffn_residual`, `moe.shared_add`) and drop the markers. A saved add keeps nothing resident because it saves nothing for backward, its inputs need no persisting, and replay skips it, matching checkpoint early stop. Only the adds are wrapped: Muse Glimmer's trailing `post_ffn_norm` stays bare, with its marker. With every region saved (RegionAC `*`), activation memory drops from 8.18 to 7.93 GiB on qwen3 MoE (30B-A3B width, 4 layers, FSDP4 + EP4) and from 9.11 to 8.67 GiB on DeepSeek V3 16B (same setup), one tensor per layer plus one more per layer with shared experts, at the same step time; losses are bitwise identical. ghstack-source-id: 2f532de Pull-Request: pytorch/torchtitan#5070
This was referenced Oct 7, 2026
This was referenced Oct 7, 2026
Closed
Closed
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.
Stack from ghstack (oldest at bottom):
torch.utils.checkpointstops replay once every tensor saved under thecheckpoint has been recomputed, so it never reruns the trailing ops of a block
that save nothing.
remat.checkpointreplays the whole block, so the bareend-of-block residual add and the shared-expert add replayed, and their
recompute_needs_tensormarkers kept their inputs (e.g. the MoE combine outputand the shared-expert output) alive from the forward until replay.
Make these adds fixed
recompute=Falseregions (ffn_residual,moe.shared_add) and drop the markers. A saved add keeps nothing residentbecause it saves nothing for backward, its inputs need no persisting, and
replay skips it, matching checkpoint early stop. Trailing ops that save what
they produce, such as Muse Glimmer's
post_ffn_norm, stay bare.With every region saved (RegionAC
*), activation memory drops from 8.18 to7.93 GiB on qwen3 MoE (30B-A3B width, 4 layers, FSDP4 + EP4) and from 9.11 to
8.67 GiB on DeepSeek V3 16B (same setup), one tensor per layer plus one more
per layer with shared experts, at the same step time; losses are bitwise
identical.