Skip to content

Support explicit replicated Muon compute - #4077

Closed
weifengpy wants to merge 61 commits into
gh/weifengpy/48/basefrom
gh/weifengpy/48/head
Closed

weifengpy wants to merge 61 commits into
gh/weifengpy/48/basefrom
gh/weifengpy/48/head

Conversation

[ghstack-poisoned]
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Aug 6, 2026
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Aug 6, 2026
ghstack-source-id: a6d6443
Pull-Request: #4077
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Aug 8, 2026
ghstack-source-id: 07fdfb7
Pull-Request: #4077
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Aug 8, 2026
ghstack-source-id: 09a5979
Pull-Request: #4077
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Aug 8, 2026
ghstack-source-id: eacc593
Pull-Request: #4077
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Aug 8, 2026
ghstack-source-id: 56b85a5
Pull-Request: #4077
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Aug 8, 2026
ghstack-source-id: cd5ec2b
Pull-Request: #4077
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Aug 9, 2026
ghstack-source-id: b6d6fe3
Pull-Request: #4077
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Aug 9, 2026
ghstack-source-id: 4118071
Pull-Request: #4077
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Aug 9, 2026
ghstack-source-id: cb11bba
Pull-Request: #4077
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Aug 9, 2026
ghstack-source-id: 87dc846
Pull-Request: #4077
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Aug 9, 2026
ghstack-source-id: 90a148f
Pull-Request: #4077
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Aug 9, 2026
ghstack-source-id: e1abc91
Pull-Request: #4077
[ghstack-poisoned]
@weifengpy weifengpy changed the title [Kimi K2.7] Support explicit replicated Muon compute Support explicit replicated Muon compute Aug 12, 2026
submartingales pushed a commit to botcanlearn/torchtitan-upstream that referenced this pull request Aug 12, 2026
* Add DistributedMuon to overlap packed all-to-all redistribution with optimizer compute while separating parameter storage and Muon compute layouts.
* Use per-head Muon by default in Kimi K2.7, Kimi K2.5, and Moonlight recipes.
* Support FSDP2 + EP. TP and PP are not supported yet but can be extended later.
* Leave oversharded Shard(0) QKV support to a later PR in the stack.
* The packed all-to-all redistribution and optimizer-compute overlap ideas are informed by [FSDP-Canzona](https://github.com/liangyuwang/FSDP-Canzona) and [Canzona: A Unified, Asynchronous, and Load-Balanced Framework for Distributed Matrix-based Optimizers](https://arxiv.org/abs/2602.06079) (Wang et al., 2026).
* Resolve pytorch/torchtitan#3353.

**Profiler trace** showing all-to-all overlapping during Muon: https://fburl.com/o7pyeuuy
<img width="608" height="322" alt="Screenshot 2026-08-09 at 7 12 39 PM" src="https://github.com/user-attachments/assets/22e344bd-1459-432d-a023-cc71efe72da1" />

**Memory snapshot** showing the expected sharded optimizer state dict.

ghstack (bottom to top):
* [#4060 - DistributedMuon with all-to-all/compute overlap (this PR)](pytorch/torchtitan#4060)
* [#4078 - Support oversharded QKV heads](pytorch/torchtitan#4078)
* [#4077 - Support explicit replicated Muon compute](pytorch/torchtitan#4077)

ghstack-source-id: 8fce56e
Pull-Request: pytorch/torchtitan#4060
Co-authored-by: Liangyu Wang <[email protected]>
[ghstack-poisoned]
submartingales pushed a commit to botcanlearn/torchtitan-upstream that referenced this pull request Aug 12, 2026
* Add DistributedMuon to overlap packed all-to-all redistribution with optimizer compute while separating parameter storage and Muon compute layouts.
* Use per-head Muon by default in Kimi K2.7, Kimi K2.5, and Moonlight recipes.
* Support FSDP2 + EP. TP and PP are not supported yet but can be extended later.
* Leave oversharded Shard(0) QKV support to a later PR in the stack.
* The packed all-to-all redistribution and optimizer-compute overlap ideas are informed by [FSDP-Canzona](https://github.com/liangyuwang/FSDP-Canzona) and [Canzona: A Unified, Asynchronous, and Load-Balanced Framework for Distributed Matrix-based Optimizers](https://arxiv.org/abs/2602.06079) (Wang et al., 2026).
* Resolve pytorch/torchtitan#3353.

**Profiler trace** showing all-to-all overlapping during Muon: https://fburl.com/o7pyeuuy
<img width="608" height="322" alt="Screenshot 2026-08-09 at 7 12 39 PM" src="https://github.com/user-attachments/assets/22e344bd-1459-432d-a023-cc71efe72da1" />

**Memory snapshot** showing the expected sharded optimizer state dict.

ghstack (bottom to top):
* [#4060 - DistributedMuon with all-to-all/compute overlap (this PR)](pytorch/torchtitan#4060)
* [#4078 - Support oversharded QKV heads](pytorch/torchtitan#4078)
* [#4077 - Support explicit replicated Muon compute](pytorch/torchtitan#4077)

ghstack-source-id: 54fb071
Pull-Request: pytorch/torchtitan#4060
Co-authored-by: Liangyu Wang <[email protected]>
[ghstack-poisoned]
submartingales pushed a commit to botcanlearn/torchtitan-upstream that referenced this pull request Aug 12, 2026
* Add DistributedMuon to overlap packed all-to-all redistribution with optimizer compute while separating parameter storage and Muon compute layouts.
* Use per-head Muon by default in Kimi K2.7, Kimi K2.5, and Moonlight recipes.
* Support FSDP2 + EP. TP and PP are not supported yet but can be extended later.
* Leave oversharded Shard(0) QKV support to a later PR in the stack.
* The packed all-to-all redistribution and optimizer-compute overlap ideas are informed by [FSDP-Canzona](https://github.com/liangyuwang/FSDP-Canzona) and [Canzona: A Unified, Asynchronous, and Load-Balanced Framework for Distributed Matrix-based Optimizers](https://arxiv.org/abs/2602.06079) (Wang et al., 2026).
* Resolve pytorch/torchtitan#3353.

**Profiler trace** showing all-to-all overlapping during Muon: https://fburl.com/o7pyeuuy
<img width="608" height="322" alt="Screenshot 2026-08-09 at 7 12 39 PM" src="https://github.com/user-attachments/assets/22e344bd-1459-432d-a023-cc71efe72da1" />

**Memory snapshot** showing the expected sharded optimizer state dict.

ghstack (bottom to top):
* [#4060 - DistributedMuon with all-to-all/compute overlap (this PR)](pytorch/torchtitan#4060)
* [#4078 - Support oversharded QKV heads](pytorch/torchtitan#4078)
* [#4077 - Support explicit replicated Muon compute](pytorch/torchtitan#4077)

ghstack-source-id: e09970f
Pull-Request: pytorch/torchtitan#4060
Co-authored-by: Liangyu Wang <[email protected]>
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
weifengpy added a commit that referenced this pull request Aug 15, 2026
ghstack-source-id: 2c5cda8
Pull-Request: #4077
weifengpy added a commit that referenced this pull request Aug 15, 2026
ghstack-source-id: 2c5cda8
Pull-Request: #4077
submartingales pushed a commit to botcanlearn/torchtitan-upstream that referenced this pull request Aug 15, 2026
[ghstack-poisoned]
[ghstack-poisoned]
submartingales pushed a commit to botcanlearn/torchtitan-upstream that referenced this pull request Aug 15, 2026
weifengpy added a commit that referenced this pull request Aug 23, 2026
DistMuon issues one Newton-Schulz call per parameter. A layer with many
same-shaped sibling matrices therefore produces a long tail of small NS calls,
each with its own GEMM sequence and its own compute scratch.

Batch such siblings into a single NS call, while keeping momentum preparation
and the parameter update per parameter. Which parameters batch together is
declared, never inferred.

Declaring the group

A bucket is sized for communication, so it is deliberately heterogeneous:
coarse buckets amortize collective launch overhead. Local compute wants the
opposite. Rather than re-deriving a compute partition from FQN strings and
tensor shapes, `BucketConfig.batch_groups` declares it directly, as fnmatch
patterns resolved against the FQNs the bucket already matched.

* The bucket remains the communication unit. `batch_groups` is a separate axis
  that only subdivides the bucket's local work.
* Members must share a matrix shape, dtype, and device. `DistMuon` validates
  this at construction and raises, so a bad declaration fails loudly instead
  of silently degrading.
* A group whose patterns select nothing is a configuration error, not a
  silently unbatched parameter. A stale pattern cannot quietly cost
  performance at every step.
* Parameters that no group names execute one at a time, as before.

Executing the group

* `_LocalBucketExecutor` is a new seam on the redistribution runtime
  (`_plan_local_bucket` / `_execute_local_bucket`) so scratch for a batch is
  preplanned and eagerly reserved during construction; a steady-state step
  does not allocate.
* A declared group is still split into scratch-bounded runs at
  `_MAX_LOCAL_MATRIX_BATCH_BYTES` (256 MiB), because the bound is physical and
  local shard shapes are not known until construction. That split warns, so a
  silently chunked group is visible.
* No Newton-Schulz settings are re-checked at execution time.
  `_validate_groups` already requires exactly one parameter group, so every
  member reads the same ns_coefficients, ns_steps, and eps even if that group
  dict is mutated after construction.

Kimi

`config_registry` declares two groups per routed-experts bucket: w1_EFD and
w3_EFD share the [F, D] matrix shape, and w2_EDF is its transpose. Both groups
span the bucket's layer pair, so `layers.1-2.routed-experts` costs two batched
NS calls rather than one per projection per layer.

A future group could also absorb w2_EDF through a transposed view, since
Newton-Schulz commutes with transpose and match_rms_adamw is
transpose-invariant. Not implemented here, but the declaration can express it
where a shape-derived key could not.

Test

`pytest tests/unit_tests/flex_shard/test_dist_muon.py` -- 6 passed on 2 GPUs.

`test_matches_plain_muon_across_flat_checkpoint` exercises both paths in one
optimizer: an oversharded stack whose storage shard boundary splits a matrix
(unbatched redistribution path), plus two aligned siblings declared as one
batch group. Both are compared against per-matrix `torch.optim.Muon`
references across a flat checkpoint save/load.

`TestDistMuonDeclaredBatchGroups` covers the contract: batching happens only
where declared -- an identically shaped undeclared sibling stays unbatched --
and each rejection path raises.

Submitted standalone against main rather than in the original ghstack stack:
the lower entries #4078 and #4102 have landed, and this change does not depend
on the still-open #4077.

ghstack-source-id: 74987c6
Pull-Request: #4280
weifengpy added a commit that referenced this pull request Aug 23, 2026
DistMuon issues one Newton-Schulz call per parameter. A layer with many
same-shaped sibling matrices therefore produces a long tail of small NS calls,
each with its own GEMM sequence and its own compute scratch.

Batch such siblings into a single NS call, while keeping momentum preparation
and the parameter update per parameter. Which parameters batch together is
declared, never inferred.

Declaring the group

A bucket is sized for communication, so it is deliberately heterogeneous:
coarse buckets amortize collective launch overhead. Local compute wants the
opposite. Rather than re-deriving a compute partition from FQN strings and
tensor shapes, `BucketConfig.batch_groups` declares it directly, as fnmatch
patterns resolved against the FQNs the bucket already matched.

* The bucket remains the communication unit. `batch_groups` is a separate axis
  that only subdivides the bucket's local work.
* Members must share a matrix shape, dtype, and device. `DistMuon` validates
  this at construction and raises, so a bad declaration fails loudly instead
  of silently degrading.
* A group whose patterns select nothing is a configuration error, not a
  silently unbatched parameter. A stale pattern cannot quietly cost
  performance at every step.
* Parameters that no group names execute one at a time, as before.

Executing the group

* `_LocalBucketExecutor` is a new seam on the redistribution runtime
  (`_plan_local_bucket` / `_execute_local_bucket`) so scratch for a batch is
  preplanned and eagerly reserved during construction; a steady-state step
  does not allocate.
* A declared group is still split into scratch-bounded runs at
  `_MAX_LOCAL_MATRIX_BATCH_BYTES` (256 MiB), because the bound is physical and
  local shard shapes are not known until construction. That split warns, so a
  silently chunked group is visible.
* No Newton-Schulz settings are re-checked at execution time.
  `_validate_groups` already requires exactly one parameter group, so every
  member reads the same ns_coefficients, ns_steps, and eps even if that group
  dict is mutated after construction.

Kimi

`config_registry` declares two groups per routed-experts bucket: w1_EFD and
w3_EFD share the [F, D] matrix shape, and w2_EDF is its transpose. Both groups
span the bucket's layer pair, so `layers.1-2.routed-experts` costs two batched
NS calls rather than one per projection per layer.

A future group could also absorb w2_EDF through a transposed view, since
Newton-Schulz commutes with transpose and match_rms_adamw is
transpose-invariant. Not implemented here, but the declaration can express it
where a shape-derived key could not.

Test

`pytest tests/unit_tests/flex_shard/test_dist_muon.py` -- 6 passed on 2 GPUs.

`test_matches_plain_muon_across_flat_checkpoint` exercises both paths in one
optimizer: an oversharded stack whose storage shard boundary splits a matrix
(unbatched redistribution path), plus two aligned siblings declared as one
batch group. Both are compared against per-matrix `torch.optim.Muon`
references across a flat checkpoint save/load.

`TestDistMuonDeclaredBatchGroups` covers the contract: batching happens only
where declared -- an identically shaped undeclared sibling stays unbatched --
and each rejection path raises.

Submitted standalone against main rather than in the original ghstack stack:
the lower entries #4078 and #4102 have landed, and this change does not depend
on the still-open #4077.

ghstack-source-id: 74987c6
Pull-Request: #4280
weifengpy added a commit that referenced this pull request Aug 23, 2026
DistMuon issues one Newton-Schulz call per parameter. A layer with many
same-shaped sibling matrices therefore produces a long tail of small NS calls,
each with its own GEMM sequence and its own compute scratch.

Batch compatible local matrices into a single NS call, while keeping momentum
preparation and the parameter update per parameter.

Grouping

Two local tensors can share one call exactly when they agree on matrix shape,
dtype, and device, so that is the whole grouping key. Nothing is read from the
FQN and nothing is declared in configuration:

* A bucket already bounds a batch, because local work is executed one bucket at
  a time and members of different buckets are never resident together. That is
  a property of the runtime, not a naming convention, so bucketing stays a pure
  communication concern and compute batching follows from it.
* Muon settings need no key either. `_validate_groups` requires exactly one
  parameter group, so every layout reads the same ns_coefficients, ns_steps,
  and eps even if that group dict is mutated after construction.
* A layout whose local compute is a lone matrix has no batch dimension to
  concatenate along and runs by itself.

Execution

* `_LocalBucketExecutor` is a new seam on the redistribution runtime
  (`_plan_local_bucket` / `_execute_local_bucket`) so scratch for a batch is
  preplanned and eagerly reserved during construction; a steady-state step does
  not allocate.
* A group is split into runs bounded by `_MAX_LOCAL_MATRIX_BATCH_BYTES`
  (256 MiB), because the bound is physical and local shard shapes are unknown
  until construction. The split warns, so a chunked group is visible rather
  than silent.

Measured on moonlight_16b_a3b at a simulated world size of 32: 26 batched
Newton-Schulz calls become 9, covering 78 parameters, with reserved buffers
unchanged at 292 MiB.

Test

`pytest tests/unit_tests/flex_shard/test_dist_muon.py` -- 5 passed on 2 GPUs.

`test_matches_plain_muon_across_flat_checkpoint` exercises both paths in one
optimizer: an oversharded stack whose storage shard boundary splits a matrix
(unbatched redistribution path), plus two aligned siblings that share one
batched call. Both are compared against per-matrix `torch.optim.Muon`
references across a flat checkpoint save/load.

`TestDistMuonLocalMatrixBatching` covers the grouping rule directly:
compatible matrices batch across layers inside one bucket, incompatible matrix
shapes stay in separate batches, and a batch never spans two buckets.

Submitted standalone against main rather than in the original ghstack stack:
the lower entries #4078 and #4102 have landed, and this change does not depend
on the still-open #4077.

ghstack-source-id: 1f125d0
Pull-Request: #4280
@yu-chen-code

Copy link
Copy Markdown
Contributor

Hi @weifengpy, I opened Draft PR #4411 to deduplicate DistMuon compute across
the HSDP dp_replicate axis.

With HSDP, replicas hold the same local parameter shards and currently execute
the same Muon/Newton–Schulz computation independently. For a replica degree of
R, #4411 lets one replica coordinate perform that computation and parameter
update, then broadcasts the updated local parameter shards to the other
replicas. The intended benefit is to reduce redundant optimizer compute by up
to a factor of R, subject to the additional broadcast overhead.

My understanding is that #4077 adds Replicate() as an explicit compute
placement, while #4411 keeps the existing replicated storage layout and
deduplicates computation across its replica axis.

Could you please take a look at whether this should remain a separate follow-up
or build on #4077? Feedback on the public API
(deduplicate_compute_mesh_axis) and the owner/broadcast design would be
particularly helpful.

The persistent optimizer-state and checkpoint layouts remain unchanged, so I
am not claiming persistent memory savings. GPU functionality, numerical
correctness, and end-to-end performance validation are ongoing.

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

Labels

ciflow/8gpu 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.

3 participants