Add a remat checkpoint after the MoE expert-parallel combine - #5057
Open
NuojCheng wants to merge 1 commit into
Open
Add a remat checkpoint after the MoE expert-parallel combine#5057NuojCheng wants to merge 1 commit into
NuojCheng wants to merge 1 commit into
Conversation
NuojCheng
requested review from
A9isha,
RissyRan,
SurbhiJainUSC,
abhinavclemson,
aireenmei,
bvandermoon,
darisoy,
dipannita08,
gagika,
gobbleturk,
hengtaoguo,
huytransformer,
igorts-git,
jiangjy1982,
khatwanimohit,
michelle-yooh,
richjames0,
shralex,
shuningjin,
vipannalla,
xibinliu and
zxhe-sean
as code owners
August 28, 2026 23:22
There was a problem hiding this comment.
Code Review
This pull request introduces a new rematerialization policy key, moe_combine, representing the output of the expert-parallel combine collective (reduce-scatter or ragged all-to-all). This key is added to the configuration files base.yml, pyconfig_deprecated.py, and types.py. Additionally, checkpoints are registered in moe.py after the expert-parallel combine collective to allow saving this tensor on device, which avoids recomputing the collective during the backward pass. I have no feedback to provide as there are no review comments.
NuojCheng
force-pushed
the
chengnuojin-post-rs-remat
branch
from
August 28, 2026 23:24
b7ab024 to
dffc8fd
Compare
The backward pass of the sparse-matmul MoE path currently recomputes the expert-parallel combine collective (the ragged all-to-all, or the reduce-scatter on the ring-of-experts path). Name the combine output so a custom remat policy can keep it on device (or offload it) instead: remat_policy=custom moe_combine=device
NuojCheng
force-pushed
the
chengnuojin-post-rs-remat
branch
from
August 28, 2026 23:42
dffc8fd to
34bc678
Compare
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.
Description
In the sparse-matmul MoE path (
_moe_bodyinsrc/maxtext/layers/moe.py), the expert-parallel combine collective sits inside the remat region, so the backward pass recomputes it:unsort_output_and_ra2a(...)— the ragged all-to-all that returns tokens to their original shards, andpsum_scatterover the expert axis on theuse_ring_of_expertspath.Both are pure communication, so recomputing them buys nothing but latency. This PR names their outputs
moe_combineviaadc.checkpoint_name, so a custom remat policy can save the combine output on device (or offload it) and skip re-running the RS / A2A in the backward pass:The new knob is registered alongside the existing
moe_mlpwo-style tensors (base.yml,configs/types.py) and defaults to'remat', so behavior is unchanged unless it is set.Tests
Unit:
python -m pytest tests/unit/moe_test.py -q→ 11 passed, 8 skipped, 1 failed (test_gmm_grad_equivalence_tokamax_v2_fp8_static_ep1, which fails identically on the base commit without this change).Small-scale e2e on a v5p-8, EP=4 (DeepSeek-v3 shape,
base_emb_dim=2048,base_moe_mlp_dim=1408, 4 layers / 1 dense, 16 experts, top-8,per_device_batch_size=2,max_target_length=2048, synthetic data,remat_policy=custom):moe_combine=remat(today)moe_combine=devicetemp_size_in_bytes(train_compile.py compile_topology=v5p-8)Loss is bit-identical between the two runs at every step (e.g. step 11:
11.486both), as expected for a remat-policy-only change.The optimized HLO confirms the intended effect — counting
ragged-all-to-allinjit_train_step, the combine A2A recompute in the backward pass disappears:moe_combine=remat: 6 (fwd dispatch + fwd combine, plus recomputed dispatch and recomputed combine in bwd)moe_combine=device: 5 (the recomputed combine is gone)Checklist
Before submitting this PR, please make sure (put X in square brackets):
gemini-reviewlabel.