Skip to content

Add a DeepSeek V3 DistMuon recipe and test it with parallelism.storage=FlexShardConfig() - #6214

Draft
anshul-si wants to merge 5 commits into
gh/anshul-si/32/basefrom
gh/anshul-si/32/head
Draft

anshul-si wants to merge 5 commits into
gh/anshul-si/32/basefrom
gh/anshul-si/32/head

Conversation

@anshul-si

@anshul-si anshul-si commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

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 accepts
Moonlight 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 a
recipe'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 is
private to kimi_k2_7.py, which Kimi K3 already imports private helpers from. This
PR 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 that
DeepSeek V3 and both Kimi modules share it, as the FlexShard fork does
(weifengpy#23):

  • Renames. _per_expert_compute_layout becomes per_expert_compute_layout,
    _dist_muon_optimizer becomes mla_dist_muon_optimizer (which now takes a
    Decoder.Config), _align_dist_muon_expert_compute_layouts becomes
    align_dist_muon_expert_compute_layouts, and _KimiTrainerConfig becomes
    MLADistMuonTrainerConfig. That trainer config fits only mla_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.py and kimi_k3.py import the
    builder from dist_muon.py, so Kimi K3 no longer imports Kimi K2.7's private names.
  • Two logic changes, both from the fork. Dense layers are those whose
    feed-forward is a DenseFeedForward, where Kimi's code assumed layer 0. And each
    dense 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.py gains
deepseek_v3_debugmodel_dist_muon(seq_len, *, parallelism, attn_backend): the debug
model with mla_dist_muon_optimizer (DistMuon for its 51 matrices, per MLA head where a
projection is per head, and AdamW for the rest) at Kimi's debug learning rates, 8e-4
for both. It returns an MLADistMuonTrainerConfig, as the Kimi K2.7 recipes do, so the
routed 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_trainer rejects DistMuon under TP too.

tests/integration_tests/models.py gets two 4-GPU entries, each with DeepSeek V3
debug at dp_shard=4, ep=2, flex attention, sequence length 512, 10 steps and the
recipe's ChunkedLossWrapper:

  • deepseek_v3_dist_muon_fsdp+ep, with FSDP2. It checks loss and grad norm against
    the new golden deepseek_v3_dist_muon.txt, bitwise, under Fake PG on every ready PR
    and under Real PG on main, also on ROCm.
  • deepseek_v3_dist_muon_flex_shard+ep, the same config with
    storage=FlexShardConfig(), checked against the FSDP2 entry's golden, as
    the integration-tests PR's twins are.

At ep=2 on 4 GPUs, edp_shard=2, so the routed experts' DistMuon layouts are
strided: (_StridedShard(0, 2), Shard(0)) on (edp_shard, ep) for FSDP2, the
equivalent StorageLayout for FlexShard. The integration-tests PR lists FlexShard's
expert 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: under
Real 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's
rule: 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 its
real_pg_required subset on every ready PR and in the full suite on main, where it
adds one 4-GPU run. It skips ROCm, where FlexShard is unvalidated
(hardware_skips/skipped_tests.toml), and release validation's --exclude list
gains 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.txt
hold 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.py prints the run's values in golden-file
format 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 the
stub's # parallelism: FSDP=4, EP=2 line, which only export mode writes. #5796
regenerated 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 a ciflow/real-pg run or a
    dispatch of integration_test.yaml with export_results=true and
    test_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: a ciflow/rocm run. If one can't happen before landing, add the
    FSDP2 entry to rocm in hardware_skips/skipped_tests.toml, with the reason, rather
    than land the stub.

FLEX_SHARD_FSDP.md lists the twin among the tests that reproduce their FSDP2
counterparts' Real PG goldens, tests/README.md lists the topology, and
distributed/flex_shard/README.md points at dist_muon.py in place of kimi_k2_7.py
for the shared optimizer configuration. Alternatives the earlier revision weighed:
switching deepseek_v3_fsdp+ep to DistMuon (it would drop DeepSeek V3's AdamW golden
and need new goldens all the same), FSDP 8 x EP 8 as in that entry (edp_shard=1, so
no strided expert layouts), importing the builder from kimi_k2_7.py (DeepSeek V3's
recipes 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):

  • FlexShard matches FSDP2 on the new recipe. scripts/loss_compare.py . . --baseline-config <FSDP2> --test-config <FlexShard> --assert-equal --metrics loss,grad_norm on this commit (flex attention, deterministic, 20 steps), both runs
    from loss_compare's seed checkpoint of deepseek_v3_debugmodel, as the Real PG
    entries 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 logs
    show DistMuon over 51 parameters in every run, Applied FlexShard to the model and
    the graph's capture in the FlexShard runs, and each run loading the seed
    checkpoint. At 2 ranks, ep=2 leaves edp_shard=1, so the strided expert layouts
    the 4-GPU entries are for haven't run here.
  • Configs. On CPU, both entries' configs pass __post_init__ and
    validate_fake_pg_compatibility, and the twin's passes FlexShard's validation; their
    seed config is an FSDP2 config with AdamW only. The recipe with
    FlexShardConfig() passes FlexShard's validation at ep=1 and ep=2, and
    raises at tensor_parallel_degree=2.
  • The Kimi recipes are unchanged. repr() of every no-argument config in
    kimi_k2_7.py, kimi_k3.py and the models and b200 integration suites, after
    __post_init__, is identical before and after this PR (47 configs before, the same
    47 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_moe package, two need SM100 for MXFP8 or NVFP4) fail the same way before and
    after.
  • CPU tests. test_integration_test_definitions.py,
    test_disable_moe_load_balancing.py, test_transforms.py and
    test_context_parallel_validation.py (108 passed, 43 subtests), and
    test_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:

Co-authored-by: Wei Feng [email protected]

[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@anshul-si anshul-si changed the title Add a DeepSeek V3 DistMuon recipe and test it with parallelism.fsdp_backend=FlexShardConfig() Add a DeepSeek V3 DistMuon recipe and test it with parallelism.fsdp_backend=FlexShardBackend.Config() Nov 25, 2026
[ghstack-poisoned]
@anshul-si anshul-si changed the title Add a DeepSeek V3 DistMuon recipe and test it with parallelism.fsdp_backend=FlexShardBackend.Config() Add a DeepSeek V3 DistMuon recipe and test it with parallelism.storage=FlexShardConfig() Nov 30, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

1 participant