Repository navigation
Conversation
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
weifengpy
added a commit
that referenced
this pull request
Aug 24, 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: d4dfc8b Pull-Request: #4280
weifengpy
added a commit
that referenced
this pull request
Aug 24, 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 * The runtime hands local work over a bucket at a time rather than item by item, through two callbacks alongside the existing prepare/compute/finalize: `plan_local_work` reports a bucket's peak scratch to `reserve_buffers`, and `run_local_work` executes it in a reserved slot. Scratch is therefore preplanned and eagerly reserved during construction, so a steady-state step does not allocate. This replaces the old per-item local loop, leaving the runtime 31 lines shorter than before. * 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: 8cc24ac Pull-Request: #4280
weifengpy
added a commit
that referenced
this pull request
Aug 24, 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 * The runtime hands local work over a bucket at a time rather than item by item, through two callbacks alongside the existing prepare/compute/finalize: `plan_local_work` reports a bucket's peak scratch to `reserve_buffers`, and `run_local_work` executes it in a reserved slot. Scratch is therefore preplanned and eagerly reserved during construction, so a steady-state step does not allocate. This replaces the old per-item local loop, leaving the runtime 31 lines shorter than before. * 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: 67156c9 Pull-Request: #4280
weifengpy
added a commit
that referenced
this pull request
Aug 24, 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 * The runtime hands local work over a bucket at a time rather than item by item, through two callbacks alongside the existing prepare/compute/finalize: `plan_local_work` reports a bucket's peak scratch to `reserve_buffers`, and `run_local_work` executes it in a reserved slot. Scratch is therefore preplanned and eagerly reserved during construction, so a steady-state step does not allocate. This replaces the old per-item local loop, leaving the runtime 31 lines shorter than before. * 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: b410d9e Pull-Request: #4280
weifengpy
added a commit
that referenced
this pull request
Aug 24, 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 * The runtime hands local work over a bucket at a time rather than item by item, through two callbacks alongside the existing prepare/compute/finalize: `plan_local_work` reports a bucket's peak scratch to `reserve_buffers`, and `run_local_work` executes it in a reserved slot. Scratch is therefore preplanned and eagerly reserved during construction, so a steady-state step does not allocate. This replaces the old per-item local loop, leaving the runtime 31 lines shorter than before. * 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: b410d9e Pull-Request: #4280
weifengpy
added a commit
that referenced
this pull request
Aug 24, 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 * The runtime hands local work over a bucket at a time rather than item by item, through two callbacks alongside the existing prepare/compute/finalize: `plan_local_work` reports a bucket's peak scratch to `reserve_buffers`, and `run_local_work` executes it in a reserved slot. Scratch is therefore preplanned and eagerly reserved during construction, so a steady-state step does not allocate. This replaces the old per-item local loop, leaving the runtime 31 lines shorter than before. * No separate cap on batch scratch. Bucket size is already the memory knob -- a bucket's exchange buffer is sized to the whole bucket -- so letting it govern compute scratch too keeps one control surface instead of two. On moonlight_16b_a3b at a simulated world size of 32, a 256 MiB cap changed neither the call count nor the reserved bytes for the shipped bucketing. 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: 3c80135 Pull-Request: #4280
weifengpy
added a commit
that referenced
this pull request
Aug 25, 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
* The runtime hands local work over a bucket at a time rather than item by
item, through two callbacks alongside the existing prepare/compute/finalize:
`plan_local_work` reports a bucket's peak scratch to `reserve_buffers`, and
`run_local_work` executes it in a reserved slot. Scratch is therefore
preplanned and eagerly reserved during construction, so a steady-state step
does not allocate. This replaces the old per-item local loop, leaving the
runtime 31 lines shorter than before.
* No separate cap on batch scratch. Bucket size is already the memory knob --
a bucket's exchange buffer is sized to the whole bucket -- so letting it
govern compute scratch too keeps one control surface instead of two. On
moonlight_16b_a3b at a simulated world size of 32, a 256 MiB cap changed
neither the call count nor the reserved bytes for the shipped bucketing.
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.
Kimi
A bucket is the communication unit and now also bounds local compute batching,
so its layer span is one number trading reserved scratch against both collective
count and Newton-Schulz launches. `config_registry` widens that span from 2 MoE
layers to 5. Measured on kimi_k2_5_debugmodel, 8 GPUs, FSDP 8 / EP 8, mean over
10 timed steps after 2 warmup steps:
span collectives NS calls optimizer step reserved
2 4 11 13.60 ms 10.5 MiB
3 3 8 11.76 ms 13.8 MiB
5 2 5 9.84 ms 19.1 MiB
Widening improves both axes at once rather than trading between them, because
same-shaped parameters from every layer in a bucket now share one call. The
optimizer step drops about 28% at span 5, reproduced over three trials. It is
roughly 2% of the training step on this model, so end-to-end this is under 1%.
Reserved scratch grows with the span: on moonlight_16b_a3b at world size 32,
span 5 reserves 451 MiB against 292 MiB at span 2, and span 13 reserves 925 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: e1efe29
Pull-Request: #4280
weifengpy
added a commit
that referenced
this pull request
Aug 25, 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
* The runtime hands local work over a bucket at a time rather than item by
item, through two callbacks alongside the existing prepare/compute/finalize:
`plan_local_work` reports a bucket's peak scratch to `reserve_buffers`, and
`run_local_work` executes it in a reserved slot. Scratch is therefore
preplanned and eagerly reserved during construction, so a steady-state step
does not allocate. This replaces the old per-item local loop, leaving the
runtime 31 lines shorter than before.
* No separate cap on batch scratch. Bucket size is already the memory knob --
a bucket's exchange buffer is sized to the whole bucket -- so letting it
govern compute scratch too keeps one control surface instead of two. On
moonlight_16b_a3b at a simulated world size of 32, a 256 MiB cap changed
neither the call count nor the reserved bytes for the shipped bucketing.
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.
Kimi
A bucket is the communication unit and now also bounds local compute batching,
so its layer span is one number trading reserved scratch against both collective
count and Newton-Schulz launches. `config_registry` widens that span from 2 MoE
layers to 5. Measured on kimi_k2_5_debugmodel, 8 GPUs, FSDP 8 / EP 8, mean over
10 timed steps after 2 warmup steps:
span collectives NS calls optimizer step reserved
2 4 11 13.60 ms 10.5 MiB
3 3 8 11.76 ms 13.8 MiB
5 2 5 9.84 ms 19.1 MiB
Widening improves both axes at once rather than trading between them, because
same-shaped parameters from every layer in a bucket now share one call. The
optimizer step drops about 28% at span 5, reproduced over three trials. It is
roughly 2% of the training step on this model, so end-to-end this is under 1%.
Reserved scratch grows with the span: on moonlight_16b_a3b at world size 32,
span 5 reserves 451 MiB against 292 MiB at span 2, and span 13 reserves 925 MiB.
The span is a declared field, `muon_layers_per_bucket`, not a constant, because
the right point on that curve depends on available memory that the recipe
cannot know. It carries `tyro.conf.Suppress` for the same reason `model_spec`
does: the command-line surface is frozen and on its way out, so a new option
would only be surface to delete later. Overriding it is a config-as-code
operation, and `__post_init__` rederives the buckets from it either way.
Setting it back to 2 reproduces the previous bucket layout exactly.
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: bf1ba8a
Pull-Request: #4280
weifengpy
added a commit
that referenced
this pull request
Aug 25, 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
* The runtime hands local work over a bucket at a time rather than item by
item, through two callbacks alongside the existing prepare/compute/finalize:
`plan_local_work` reports a bucket's peak scratch to `reserve_buffers`, and
`run_local_work` executes it in a reserved slot. Scratch is therefore
preplanned and eagerly reserved during construction, so a steady-state step
does not allocate. This replaces the old per-item local loop, leaving the
runtime 31 lines shorter than before.
* No separate cap on batch scratch. Bucket size is already the memory knob --
a bucket's exchange buffer is sized to the whole bucket -- so letting it
govern compute scratch too keeps one control surface instead of two. On
moonlight_16b_a3b at a simulated world size of 32, a 256 MiB cap changed
neither the call count nor the reserved bytes for the shipped bucketing.
Measured on moonlight_16b_a3b, 8 GPUs, FSDP 8 / EP 8, local batch 8, sequence
length 4096, full activation checkpointing; mean over 10 timed steps after 2
warmup steps, three trials each:
without with
Newton-Schulz calls 132 54
collectives 14 14
optimizer step 135.7 ms 131.4 ms
peak allocated 33.68 GiB 34.13 GiB
optimizer scratch 379 MiB 643 MiB
Collectives are unchanged because batching only touches work that needs no
redistribution. The optimizer step is about 5% of the training step here, so
the 3.2% it saves is roughly 0.16% end to end.
Scratch grows because a batch buffer is sized to the whole batch rather than to
the largest single parameter. That is a 70% increase in scratch but only 1.3%
of peak allocated: the high-water mark is set during forward and backward, and
the optimizer phase runs at 68% of it, so the extra buffer sits well under the
mark rather than raising it.
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: 3c80135
Pull-Request: #4280
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):
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 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.
_validate_groupsrequires exactly oneparameter group, so every layout reads the same ns_coefficients, ns_steps,
and eps even if that group dict is mutated after construction.
concatenate along and runs by itself.
Execution
item, through two callbacks alongside the existing prepare/compute/finalize:
plan_local_workreports a bucket's peak scratch toreserve_buffers, andrun_local_workexecutes it in a reserved slot. Scratch is thereforepreplanned and eagerly reserved during construction, so a steady-state step
does not allocate. This replaces the old per-item local loop, leaving the
runtime 31 lines shorter than before.
a bucket's exchange buffer is sized to the whole bucket -- so letting it
govern compute scratch too keeps one control surface instead of two. On
moonlight_16b_a3b at a simulated world size of 32, a 256 MiB cap changed
neither the call count nor the reserved bytes for the shipped bucketing.
Measured on moonlight_16b_a3b, 8 GPUs, FSDP 8 / EP 8, local batch 8, sequence
length 4096, full activation checkpointing; mean over 10 timed steps after 2
warmup steps, three trials each:
Newton-Schulz calls 132 54
collectives 14 14
optimizer step 135.7 ms 131.4 ms
peak allocated 33.68 GiB 34.13 GiB
optimizer scratch 379 MiB 643 MiB
Collectives are unchanged because batching only touches work that needs no
redistribution. The optimizer step is about 5% of the training step here, so
the 3.2% it saves is roughly 0.16% end to end.
Scratch grows because a batch buffer is sized to the whole batch rather than to
the largest single parameter. That is a 70% increase in scratch but only 1.3%
of peak allocated: the high-water mark is set during forward and backward, and
the optimizer phase runs at 68% of it, so the extra buffer sits well under the
mark rather than raising it.
Test
pytest tests/unit_tests/flex_shard/test_dist_muon.py-- 5 passed on 2 GPUs.test_matches_plain_muon_across_flat_checkpointexercises both paths in oneoptimizer: 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.Muonreferences across a flat checkpoint save/load.
TestDistMuonLocalMatrixBatchingcovers 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.