Repository navigation
Conversation
[ghstack-poisoned]
This was referenced Nov 19, 2026
[ghstack-poisoned]
[ghstack-poisoned]
This branch has not been deployed
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):
Stacked on #5842, the expert-parallelism PR, the chunked-loss PR, the
checkpointing PR, the Hugging Face load PR, the tensor- and context-parallelism PR,
the DeepSeek V3 PR, the accelerator-graphs PR, the integration-tests-and-docs PR,
the EMA PR and the DistMuon PR.
Summary (human-written)
Description
The DistMuon PR lets FlexShard train with DistMuon, but no CI entry or debug recipe
combines DistMuon with a model FlexShard supports. The DistMuon recipes are Kimi
K2.7's, Kimi K3's and DeepSeek V4's. FlexShard rejects the multimodal Kimi K2.5,
Kimi-VL and Kimi K3 models and DeepSeek V4, which override
_apply_fsdp. It acceptsMoonlight 16B-A3B, Kimi K2.7's text-only recipe, but that is a full-size model that
no CI entry runs. So CI never trains DistMuon with FlexShard through
torchtitan.train, and nothing checks the two backends against each other at arecipe's DistMuon layouts: the DistMuon PR's unit tests use Llama 3 and Qwen3 MoE,
with compute layouts of their own. DeepSeek V3, which FlexShard supports without MTP
layers, is built from the same MLA attention and MoE blocks as Kimi K2.7
(
build_mla_moe_layers), so Kimi K2.7's DistMuon builder fits it. But the builder isprivate to
kimi_k2_7.py, which Kimi K3 already imports private helpers from. ThisPR shares the builder, adds a DeepSeek V3 DistMuon debug recipe, and adds an FSDP2
integration entry with it and a FlexShard twin that must reproduce the FSDP2 entry's
golden bitwise. It changes no training code.
The builder moves to the new
torchtitan_recipes/tests/models/dist_muon.py, so thatDeepSeek V3 and both Kimi modules share it, as the FlexShard fork does
(weifengpy#23):
_per_expert_compute_layoutbecomesper_expert_compute_layout,_dist_muon_optimizerbecomesmla_dist_muon_optimizer(which now takes aDecoder.Config),_align_dist_muon_expert_compute_layoutsbecomesalign_dist_muon_expert_compute_layouts, and_KimiTrainerConfigbecomesMLADistMuonTrainerConfig. That trainer config fits onlymla_dist_muon_optimizer:it realigns the routed experts' compute layouts with the final parallelism and
rejects TP. Kimi K3 and DeepSeek V4 keep their own trainer configs. Its TP
rejection's message drops "Kimi".
kimi_k2_7.pyandkimi_k3.pyimport thebuilder from
dist_muon.py, so Kimi K3 no longer imports Kimi K2.7's private names.feed-forward is a
DenseFeedForward, where Kimi's code assumed layer 0. And eachdense layer gets its own DistMuon bucket while consecutive MoE layers pair up, where
Kimi's code put layer 0 alone and paired the rest. With one dense layer, as in every
Kimi flavor and DeepSeek V3's debug model, both give the layouts and buckets of
before (the config comparison below). With more, the old code raises when DistMuon
is built, because the dense layers' MLP weights have no compute layout (DeepSeek V3
671B has three dense layers).
torchtitan_recipes/tests/models/deepseek_v3.pygainsdeepseek_v3_debugmodel_dist_muon(seq_len, *, parallelism, attn_backend): the debugmodel with
mla_dist_muon_optimizer(DistMuon for its 51 matrices, per MLA head where aprojection is per head, and AdamW for the rest) at Kimi's debug learning rates,
8e-4for both. It returns an
MLADistMuonTrainerConfig, as the Kimi K2.7 recipes do, so therouted experts' compute layouts follow a later change of expert parallelism, and tensor
parallelism raises when the config is built (#3353). With FlexShard, the DistMuon PR's
FlexShardConfig.validate_trainerrejects DistMuon under TP too.tests/integration_tests/models.pygets two 4-GPU entries, each with DeepSeek V3debug at
dp_shard=4, ep=2, flex attention, sequence length 512, 10 steps and therecipe's
ChunkedLossWrapper:deepseek_v3_dist_muon_fsdp+ep, with FSDP2. It checks loss and grad norm againstthe new golden
deepseek_v3_dist_muon.txt, bitwise, under Fake PG on every ready PRand under Real PG on main, also on ROCm.
deepseek_v3_dist_muon_flex_shard+ep, the same config withstorage=FlexShardConfig(), checked against the FSDP2 entry's golden, asthe integration-tests PR's twins are.
At
ep=2on 4 GPUs,edp_shard=2, so the routed experts' DistMuon layouts arestrided:
(_StridedShard(0, 2), Shard(0))on(edp_shard, ep)for FSDP2, theequivalent
StorageLayoutfor FlexShard. The integration-tests PR lists FlexShard'sexpert buckets on more than one rank as not covered end to end, and the DistMuon PR's
2-GPU runs don't reach these layouts either. Both entries set
loss_compare_seed_config=deepseek_v3_debugmodel, the same model with AdamW: underReal PG, the runner creates a seed checkpoint on one GPU, without sharding, and loads
it in both runs, and DistMuon can't step unsharded plain parameters (it raises
TypeError: DistMuon requires DTensor parameters or a StorageLayout for each plain one). The twin is Real PG only (use_real_pg=True), by the integration-tests PR'srule: FlexShard initializes each shard as its slice of one device's initialization,
while FSDP2's DTensor init draws each shard on its own, so the two start from the same
weights only when both load the seed checkpoint, which the runner skips under Fake PG
(
--no-seed-checkpoint). The twin therefore runs in the 8-GPU Real PG job, in itsreal_pg_requiredsubset on every ready PR and in the full suite on main, where itadds one 4-GPU run. It skips ROCm, where FlexShard is unvalidated
(
hardware_skips/skipped_tests.toml), and release validation's--excludelistgains it, as it lists the other FlexShard entries. The earlier revision of this PR ran
the twin under Fake PG too, which relied
on DTensor views of FlexShard's parameters that #5842 deleted, and the 1-GPU Fake PG
job no longer installs flex_shard.
The goldens are stubs:
tests/assets/losses/{fake_pg/a10g,real_pg/a10g,real_pg/mi350x}/deepseek_v3_dist_muon.txthold the golden header and steps 1 to 10 with zero loss and grad norm. They have to
come from CI's hardware, which I don't have, so both entries fail until they're
filled in. On a mismatch,
loss_compare.pyprints the run's values in golden-fileformat after "Baseline metrics in import file format"; paste the FSDP2 entry's block
(
# config: deepseek_v3_debugmodel_dist_muon_fsdp4_ep2) over the stub, keeping thestub's
# parallelism: FSDP=4, EP=2line, which only export mode writes. #5796regenerated goldens this way.
fake_pg/a10g: the FSDP2 entry's run in the Fake PG job of a ready PR(
ciflow/fake-pg).real_pg/a10g: a Real PG run of the FSDP2 entry, from aciflow/real-pgrun or adispatch of
integration_test.yamlwithexport_results=trueandtest_name=deepseek_v3_dist_muon_fsdp+ep, whose artifacts hold the exported file.Not from the twin's run: the twin is what the golden checks.
real_pg/mi350x: aciflow/rocmrun. If one can't happen before landing, add theFSDP2 entry to
rocminhardware_skips/skipped_tests.toml, with the reason, ratherthan land the stub.
FLEX_SHARD_FSDP.mdlists the twin among the tests that reproduce their FSDP2counterparts' Real PG goldens,
tests/README.mdlists the topology, anddistributed/flex_shard/README.mdpoints atdist_muon.pyin place ofkimi_k2_7.pyfor the shared optimizer configuration. Alternatives the earlier revision weighed:
switching
deepseek_v3_fsdp+epto DistMuon (it would drop DeepSeek V3's AdamW goldenand need new goldens all the same), FSDP 8 x EP 8 as in that entry (
edp_shard=1, sono strided expert layouts), importing the builder from
kimi_k2_7.py(DeepSeek V3'srecipes would depend on a Kimi module through private names), a copy of the builder
(about 200 duplicated lines), and
torchtitan_recipes/models/dist_muon.py, where open#5793 moves two of these helpers (every consumer here is a test recipe, and
torchtitan_recipes/models/holds verified recipes).What ran, on 2 GB200 GPUs with flex_shard
d6bb546(CI's pin):scripts/loss_compare.py . . --baseline-config <FSDP2> --test-config <FlexShard> --assert-equal --metrics loss,grad_normon this commit (flex attention, deterministic, 20 steps), both runsfrom loss_compare's seed checkpoint of
deepseek_v3_debugmodel, as the Real PGentries seed, reports equal loss and grad norm at every step at
dp_shard=2, ep=2,with accelerator graphs off as the recipe sets them under EP (step-20 loss 4.98743),
and at
dp_shard=2, with the recipe's forward-backward graph (4.98736). The logsshow DistMuon over 51 parameters in every run,
Applied FlexShard to the modelandthe graph's capture in the FlexShard runs, and each run loading the seed
checkpoint. At 2 ranks,
ep=2leavesedp_shard=1, so the strided expert layoutsthe 4-GPU entries are for haven't run here.
__post_init__andvalidate_fake_pg_compatibility, and the twin's passes FlexShard's validation; theirseed config is an FSDP2 config with AdamW only. The recipe with
FlexShardConfig()passes FlexShard's validation atep=1andep=2, andraises at
tensor_parallel_degree=2.repr()of every no-argument config inkimi_k2_7.py,kimi_k3.pyand themodelsandb200integration suites, after__post_init__, is identical before and after this PR (47 configs before, the same47 plus the two new entries after), apart from object addresses, one config's
absolute asset path, which holds the worktree's name, and the trainer config's
class name in the 7 Kimi K2.7 configs. The four that don't build here (two need the
dist_moepackage, two need SM100 for MXFP8 or NVFP4) fail the same way before andafter.
test_integration_test_definitions.py,test_disable_moe_load_balancing.py,test_transforms.pyandtest_context_parallel_validation.py(108 passed, 43 subtests), andtest_kimi_k3_config.py(2 passed). pre-commit passes on the changed files(pyrefly isn't installed here).
The earlier revision of this PR ran the integration runner as CI runs it, against a
temporary golden exported by the FSDP2 entry on the same hardware: under Fake PG on 1
GB200 (step-10 loss 6.79162) and under Real PG on 4 GB300 GPUs (6.78963), the twin
bitwise equal to the FSDP2 entry in both. It also checked that with the stubs, the
entries run all 10 steps, fail at step 1 and print their values in golden-file format.
Those runs predate #5842's rework of initialization, so they haven't been repeated for
the Real PG twin, which needs 4 GPUs. No new unit test: the integration entries cover
the recipe and the builder end to end, and the config comparison covers the move.
Open upstream PRs overlap the moved code, and whichever lands second rebases. None has
landed on main as of this PR's base:
mla_dist_muon_optimizer, and lifts the TP check, which now lives in the sharedMLADistMuonTrainerConfig, so it would lift it for DeepSeek V3 too. FlexShard's ownrejection of DistMuon under TP stays until FlexShard's storage layouts carry TP's
split. [distmuon] Support tensor-parallel storage layouts #4353, the earlier attempt, closed without landing.
per_expert_compute_layoutand the alignment helper intotorchtitan_recipes/models/dist_muon.py. Keepper_expert_compute_layoutin only onedist_muon.py.Co-authored-by: Wei Feng [email protected]