Skip to content

Save trailing adds so replay skips them - #5070

Merged
anijain2305 merged 10 commits into
mainfrom
gh/anijain2305/58/head
Oct 6, 2026
Merged

anijain2305 merged 10 commits into
mainfrom
gh/anijain2305/58/head

Conversation

@anijain2305

@anijain2305 anijain2305 commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

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. 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 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-poisoned]
[ghstack-poisoned]
@pytorch-bot pytorch-bot Bot added ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/rl labels Oct 5, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 5, 2026
[ghstack-poisoned]
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
[ghstack-poisoned]
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
[ghstack-poisoned]
[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).
@anijain2305
anijain2305 changed the base branch from gh/anijain2305/58/base to main October 6, 2026 17:51
@anijain2305
anijain2305 changed the base branch from main to gh/anijain2305/58/base October 6, 2026 17:52
[ghstack-poisoned]
[ghstack-poisoned]
@anijain2305
anijain2305 changed the base branch from gh/anijain2305/58/base to main October 6, 2026 17:52
[ghstack-poisoned]
[ghstack-poisoned]
@anijain2305
anijain2305 merged commit 304b57c into main Oct 6, 2026
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/rl CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants