Repository navigation
[DistMuon] fix coverage for FeedForward w13 (S1-to-S0) and w2 (Owned) - #4811
Merged
Merged
Conversation
weifengpy
added a commit
that referenced
this pull request
Sep 21, 2026
A parameter can have a DistMuon compute layout while an optimizer-selection regex routes it to AdamW. Kimi's old w[123] pattern misses fused w13 weights, so its catch-all silently selects AdamW despite the unused Muon metadata. Validate local trainable parameters against the actual optimizer assignments after grouping, before constructing optimizers for that model part. Conflicting assignments now raise an actionable ValueError naming the parameter and assigned optimizer. Mapped parameters omitted from all groups also raise; checkpoint-wrapped names are canonicalized, and frozen parameters and entries for other pipeline stages are ignored. Make this validation safe to land independently: derive Kimi's FFN selection and compute metadata from a shared w2-only mapping. This removes the stale w13 layout/bucket entries and preserves the existing AdamW assignment. Optimizer behavior and checkpoint state format remain unchanged. The explicit w13 switch to Muon with Shard(0) compute is in the dependent #4811. Validation: - 37 CPU tests pass, including nine assignment-validation cases, all four Kimi recipes, EP overrides, and existing optimizer parameter-group coverage. Kimi tests verify w13 remains on AdamW and has no Muon layout/bucket entry. - Before/after 10-step C4 checks compare main with this validation base using the actual Kimi recipe assignments, without an optimizer override: eight H100s, FSDP8/EP8, sequence length 256, seed 42, and strict deterministic algorithms. All 10 full-precision training losses, 10 gradient norms, seven held-out losses, and per-rank assignment/update summaries are bitwise identical. - Changed-file type checks pass. Full pre-commit passes all other hooks and reports the same 30 existing Pyrefly diagnostics and source locations; unrelated automatic suppression removals are excluded. ghstack-source-id: 451de93 ghstack-comment-id: 5754025658 Pull-Request: #4813
weifengpy
added a commit
that referenced
this pull request
Sep 21, 2026
Kimi's dense and shared-expert fused w13 weights currently use AdamW. Select them for DistMuon and use Shard(0) compute to distribute the two complete [F, D] matrices in each native [2, F, D] parameter across ranks. Keep w2 on Owned compute. Enable the required Shard(1) storage -> Shard(0) compute transition on one mesh axis, with other non-unit storage axes replicated. The existing redistribution planner gathers complete matrices and returns update rows to the original storage ranks, including uneven row shards and empty compute participants. Parameters retain their native matrix-batch view; transport and Muon kernels are unchanged. A [2, F, D] batch uses at most two compute ranks; no performance improvement is claimed. This intentionally switches w13 from AdamW to Muon across the Kimi debug, Moonlight, Kimi-VL, and Kimi-K2.5 recipes. Model checkpoint names and shapes are unchanged, but old AdamW state for w13 does not supply Muon momentum state. Load old model weights with fresh optimizer state when adopting this change. Depends on the independent validation base #4813. This PR combines the redistribution support and the Kimi optimizer/layout change previously split across #4811 and #4812. Validation: - 41 CPU tests and five subtests pass, covering exact redistribution routes in both directions, uneven row shards, empty compute ranks, replicated mesh axes, unsupported-layout rejection, all four Kimi recipes, EP overrides, and optimizer assignment validation. - The combined production tree is identical to the previously tested stack at 3360775; only a CPU test function was renamed during consolidation. The six-test GPU DistMuon suite passed, covering Owned, BlockShard, checkpoint state, expert routing, and the new matrix-sharded compute path. - On H100, [2, 7, 5] matches Owned bitwise over 10 synthetic training steps with seed 42 and strict deterministic algorithms on two and four ranks, including loss, gradient norm, gradients, parameters, and momentum. An extra [3, 5, 7] case uses a 1e-6 absolute tolerance: local Newton-Schulz baddbmm batching can produce a one-FP32-ULP rounding difference. Every shape is not guaranteed bitwise equivalent to Owned. - Two 200-step C4 runs on eight H100s use the six-layer Kimi debug decoder, FSDP8/EP8, sequence length 256, seed 42, and strict deterministic algorithms. Both use the same 4,096 training documents and 512 held-out documents and train on 409,600 tokens. Runtime checks confirm optimizer assignments, Shard(1) storage, Shard(0) compute with six empty participants, and updated w13 weights on every rank. | w13 optimizer | Initial held-out loss | Held-out loss at step 200 | Mean training loss, last 20 steps | | --- | --- | --- | --- | | AdamW baseline | 8.0564976 | 2.7773604 | 2.7658222 | | DistMuon, Shard(0) | 8.0564976 | 2.7758863 | 2.7614102 | The C4 runs are a short learning check on the debug decoder, not full-model convergence or a performance comparison. Changed-file type checks pass. Full pre-commit reports the same 30 existing Pyrefly diagnostics as the validation base; all other hooks pass and unrelated automatic suppression removals are excluded.
weifengpy
force-pushed
the
pr/dist-muon-stacked-shard0
branch
from
September 21, 2026 01:12
8e6742e to
a3a6850
Compare
weifengpy
force-pushed
the
pr/dist-muon-stacked-shard0
branch
3 times, most recently
from
September 21, 2026 02:43
c3cd802 to
00e658b
Compare
weifengpy
commented
Sep 21, 2026
weifengpy
force-pushed
the
pr/dist-muon-stacked-shard0
branch
5 times, most recently
from
September 21, 2026 05:18
16934a9 to
35e26a8
Compare
weifengpy
added a commit
that referenced
this pull request
Sep 21, 2026
A DistMuon selection regex can match some parameters while silently leaving others with compute layouts assigned to AdamW. The existing parameter-coverage check accepts that configuration because every parameter still has an optimizer. Require every locally trainable parameter with a DistMuon compute layout to be assigned to DistMuon. Otherwise, raise ValueError naming the parameter and its assignment before constructing optimizers for that model part. Also catch mapped parameters omitted from every group, including when no DistMuon group exists. Canonicalize checkpoint-wrapped names and ignore frozen parameters and entries for other pipeline stages. The optimizer container collects assignments and dispatches registered validators; the DistMuon-specific rule lives beside DistMuon. Keep existing string identifiers, taking DistMuon's identifier from `DistMuon.__name__`. Stacked on #4811 to review the optimizer-container validation separately from Kimi's parameter selection and Shard(0) compute changes. Validation: - All 23 existing optimizer parameter-group tests pass on the parent and this change. No test files are modified. - Standalone checks reject the old w[123] regex, metadata with only AdamW groups, and mapped unassigned parameters before optimizer construction. Wrapped valid assignments and frozen/off-stage entries are accepted. - Ten seed-42 deterministic CPU steps with mixed Adam/AdamW match the parent bitwise for loss, gradient norm, parameters, and optimizer state. - A previous eight-GPU deterministic C4 comparison of the valid Muon configuration with and without this startup validation matched all 10 full-precision training losses, gradient norms, and seven held-out losses. - Full pre-commit reports the same 30 existing Pyrefly errors; all other hooks pass. Focused type checks pass.
weifengpy
force-pushed
the
pr/dist-muon-stacked-shard0
branch
from
September 21, 2026 05:27
35e26a8 to
90ed00f
Compare
weifengpy
added a commit
that referenced
this pull request
Sep 21, 2026
A DistMuon selection regex can match some parameters while silently leaving others with compute layouts assigned to AdamW. The existing parameter-coverage check accepts that configuration because every parameter still has an optimizer. Require every locally trainable parameter with a DistMuon compute layout to be assigned to DistMuon. Otherwise, raise ValueError naming the parameter and its assignment before constructing optimizers for that model part. Also catch mapped parameters omitted from every group, including when no DistMuon group exists. Canonicalize checkpoint-wrapped names and ignore frozen parameters and entries for other pipeline stages. The optimizer container collects assignments and dispatches registered validators; the DistMuon-specific rule lives beside DistMuon. Keep existing string identifiers, taking DistMuon's identifier from `DistMuon.__name__`. Stacked on #4811 to review the optimizer-container validation separately from Kimi's parameter selection and Shard(0) compute changes. Validation: - All 23 existing optimizer parameter-group tests pass on the parent and this change. No test files are modified. - Standalone checks reject the old w[123] regex, metadata with only AdamW groups, and mapped unassigned parameters before optimizer construction. Wrapped valid assignments and frozen/off-stage entries are accepted. - Ten seed-42 deterministic CPU steps with mixed Adam/AdamW match the parent bitwise for loss, gradient norm, parameters, and optimizer state. - A previous eight-GPU deterministic C4 comparison of the valid Muon configuration with and without this startup validation matched all 10 full-precision training losses, gradient norms, and seven held-out losses. - Full pre-commit reports the same 30 existing Pyrefly errors; all other hooks pass. Focused type checks pass.
weifengpy
force-pushed
the
pr/dist-muon-stacked-shard0
branch
from
September 21, 2026 05:33
90ed00f to
a13c8f7
Compare
weifengpy
commented
Sep 21, 2026
| else: | ||
| raise ValueError(f"unsupported storage-to-compute layout for {fqn!r}") | ||
|
|
||
| if redistribution_storage_mesh_axis is not None and type(compute_sharding) is Shard: |
Contributor
Author
There was a problem hiding this comment.
allow Shard(1)-to-Shard(0) redistribution
weifengpy
commented
Sep 21, 2026
| rf"attention\.(?:{'|'.join(attention_shardings)})\.weight|" | ||
| rf"routed_experts\.inner_experts\.(?:{'|'.join(expert_projections)})|" | ||
| r"feed_forward\.w[123]\.weight|" | ||
| r"feed_forward\.(?:w13|w2)\.weight|" |
Contributor
Author
There was a problem hiding this comment.
w13 didn't get any fqn match after splitting w123 into w13 and w2. this PR fixes it:
- Make fused gate-up projection the default #4535
- https://github.com/pytorch/torchtitan/pull/4676/changes#r4058499709
ideally we have have validation in optimizer init stage to catch this and it has to live in torchtitan: #4816
weifengpy
marked this pull request as ready for review
September 21, 2026 05:46
weifengpy
added a commit
that referenced
this pull request
Sep 21, 2026
A DistMuon selection regex can match some parameters while silently leaving others with compute layouts assigned to AdamW. The existing parameter-coverage check accepts that configuration because every parameter still has an optimizer. Require every locally trainable parameter with a DistMuon compute layout to be assigned to DistMuon. Otherwise, raise ValueError naming the parameter and its assignment before constructing optimizers for that model part. Also catch mapped parameters omitted from every group, including when no DistMuon group exists. Canonicalize checkpoint-wrapped names and ignore frozen parameters and entries for other pipeline stages. The optimizer container collects assignments and dispatches registered validators; the DistMuon-specific rule lives beside DistMuon. Keep existing string identifiers, taking DistMuon's identifier from `DistMuon.__name__`. Stacked on #4811 to review the optimizer-container validation separately from Kimi's parameter selection and Shard(0) compute changes. Validation: - All 23 existing optimizer parameter-group tests pass on the parent and this change. No test files are modified. - Standalone checks reject the old w[123] regex, metadata with only AdamW groups, and mapped unassigned parameters before optimizer construction. Wrapped valid assignments and frozen/off-stage entries are accepted. - Ten seed-42 deterministic CPU steps with mixed Adam/AdamW match the parent bitwise for loss, gradient norm, parameters, and optimizer state. - A previous eight-GPU deterministic C4 comparison of the valid Muon configuration with and without this startup validation matched all 10 full-precision training losses, gradient norms, and seven held-out losses. - Full pre-commit reports the same 30 existing Pyrefly errors; all other hooks pass. Focused type checks pass.
weifengpy
force-pushed
the
pr/dist-muon-stacked-shard0
branch
from
September 21, 2026 07:23
a13c8f7 to
0a2a8a8
Compare
tianyu-l
approved these changes
Sep 21, 2026
anijain2305
approved these changes
Sep 21, 2026
weifengpy
added a commit
that referenced
this pull request
Sep 21, 2026
A DistMuon selection regex can match some parameters while silently leaving others with compute layouts assigned to AdamW. The existing parameter-coverage check accepts that configuration because every parameter still has an optimizer. Require every locally trainable parameter with a DistMuon compute layout to be assigned to DistMuon. Otherwise, raise ValueError naming the parameter and its assignment before constructing optimizers for that model part. Also catch mapped parameters omitted from every group, including when no DistMuon group exists. Canonicalize checkpoint-wrapped names and ignore frozen parameters and entries for other pipeline stages. The optimizer container collects assignments and dispatches registered validators; the DistMuon-specific rule lives beside DistMuon. Keep existing string identifiers, taking DistMuon's identifier from `DistMuon.__name__`. Stacked on #4811 to review the optimizer-container validation separately from Kimi's parameter selection and Shard(0) compute changes. Validation: - All 23 existing optimizer parameter-group tests pass on the parent and this change. No test files are modified. - Standalone checks reject the old w[123] regex, metadata with only AdamW groups, and mapped unassigned parameters before optimizer construction. Wrapped valid assignments and frozen/off-stage entries are accepted. - Ten seed-42 deterministic CPU steps with mixed Adam/AdamW match the parent bitwise for loss, gradient norm, parameters, and optimizer state. - A previous eight-GPU deterministic C4 comparison of the valid Muon configuration with and without this startup validation matched all 10 full-precision training losses, gradient norms, and seven held-out losses. - Full pre-commit reports the same 30 existing Pyrefly errors; all other hooks pass. Focused type checks pass.
weifengpy
force-pushed
the
pr/dist-muon-stacked-shard0
branch
from
September 21, 2026 19:43
0a2a8a8 to
11dd361
Compare
Select dense and shared-expert w13 and w2 weights for DistMuon. Use Shard(0) compute for each native [2, F, D] w13 parameter so gate and up remain separate complete matrices; keep w2 on Owned compute. Restore the original region-based redistribution from replicated, row-sharded Shard(1), or column-sharded Shard(2) storage to Shard(0) compute on one mesh axis. Other non-unit storage axes stay replicated for this case. Existing complete-matrix and transport validation remains in place; the transport planner and Muon kernels are unchanged. Ignore local stride differences only for empty storage shards, since autograd can give empty gradients different strides from their parameters without changing ownership or addressing any elements. This restores the production implementation from a13c8f7. A two-matrix batch uses at most two compute ranks; no performance gain is claimed. Model checkpoint names and shapes are unchanged. Switching w13 from AdamW to Muon requires fresh optimizer state for those parameters. Validation: - All 23 existing optimizer parameter-group tests and 8 FlexShard CPU tests pass. The optimizer parameter-group test file is unchanged from the base. - All 6 distributed GPU tests pass. The native matrix-batch oracle covers row, column, and replicated storage -> Shard(0), plus row storage -> Owned, over three updates with momentum, Nesterov, weight decay, and Kimi's learning-rate scaling. Four ranks cover two empty compute participants; real autograd also reproduces the padded empty-column stride mismatch. - The restored production files are identical to a13c8f7, whose 200-step deterministic eight-H100 C4 debug run reached held-out loss 2.7758863 (AdamW-w13 baseline: 2.7773604; initial loss: 8.0564976). The subsequent Owned comparison matched all 200 losses and gradient norms and all six validation losses bitwise. This is a short learning check on the debug decoder, not full-model convergence or a performance comparison. - Full pre-commit reports the same 30 existing Pyrefly diagnostics as the unchanged base; all other hooks pass. Unrelated suppression edits are excluded. Co-authored-by: Zihan Yang <[email protected]>
weifengpy
added a commit
that referenced
this pull request
Sep 21, 2026
A DistMuon selection regex can match some parameters while silently leaving others with compute layouts assigned to AdamW. The existing parameter-coverage check accepts that configuration because every parameter still has an optimizer. Require every locally trainable parameter with a DistMuon compute layout to be assigned to DistMuon. Otherwise, raise ValueError naming the parameter and its assignment before constructing optimizers for that model part. Also catch mapped parameters omitted from every group, including when no DistMuon group exists. Canonicalize checkpoint-wrapped names and ignore frozen parameters and entries for other pipeline stages. The optimizer container collects assignments and dispatches registered validators; the DistMuon-specific rule lives beside DistMuon. Keep existing string identifiers, taking DistMuon's identifier from `DistMuon.__name__`. Stacked on #4811 to review the optimizer-container validation separately from Kimi's parameter selection and Shard(0) compute changes. Validation: - All 23 existing optimizer parameter-group tests pass on the parent and this change. No test files are modified. - Standalone checks reject the old w[123] regex, metadata with only AdamW groups, and mapped unassigned parameters before optimizer construction. Wrapped valid assignments and frozen/off-stage entries are accepted. - Ten seed-42 deterministic CPU steps with mixed Adam/AdamW match the parent bitwise for loss, gradient norm, parameters, and optimizer state. - A previous eight-GPU deterministic C4 comparison of the valid Muon configuration with and without this startup validation matched all 10 full-precision training losses, gradient norms, and seven held-out losses. - Full pre-commit reports the same 30 existing Pyrefly errors; all other hooks pass. Focused type checks pass.
weifengpy
force-pushed
the
pr/dist-muon-stacked-shard0
branch
from
September 21, 2026 19:46
11dd361 to
074332c
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.
w13 didn't get any fqn match after splitting w123 into w13 and w2. this PR fixes it:
the next follow up is adding validation prevent future cases like above in optimizer init: #4816
Co-authored-by: Zihan Yang [email protected]