Repository navigation
[DistMuon] Split fused gated linear shape for correct NS computation - #4531
WhoisZihan wants to merge 4 commits into
Conversation
|
can you add/test FusedQKVLinear as well? |
|
@rakkit That's good advice. The new implementation introduces @dataclass(frozen=True, slots=True)
class MatrixBatchLayout:
"""Interpret a flat 2D tensor as a batch of Muon matrices.
``matrix_rows`` is required for ``Owned`` compute and optional with
``BlockShard``, where it can be derived from the block size. When
``num_interleaved_matrices`` is greater than one, each storage group has
shape ``[matrix_rows, num_interleaved_matrices, columns]``.
"""
matrix_rows: int | None = None
num_interleaved_matrices: int = 1flowchart LR
A["Parameter gradient<br/>same shape as parameter"] --> B{"ComputeLayout"}
B -->|"Owned()"| C["Gather complete tensor<br/>to one compute owner"]
B -->|"BlockShard(dim=0,<br/>block_size=storage_group_rows)"| D["Distribute complete storage groups<br/>without splitting matrix boundaries"]
C --> E{"MatrixBatchLayout?"}
D --> E
E -->|"None"| F["Owned: entire tensor is one matrix<br/>BlockShard: each block is one matrix"]
E -->|"Non-interleaved"| G["View as<br/>[num_matrices, R, C]"]
E -->|"Interleaved, F > 1"| H["View storage as<br/>[G, R, F, C]"]
H --> I["Permute view to<br/>[G, F, R, C]"]
F --> J["Logical matrix batch"]
G --> J
I --> J
J --> K["Newton-Schulz independently<br/>over the last two dimensions"]
K --> L["Write through logical view strides"]
L --> M["Original storage layout preserved"]
M --> N["Reshard and apply Muon update"]
O["Configuration examples"] --> P["Non-interleaved per-head QKV<br/>BlockShard block_size=head_dim<br/>matrix_batch omitted"]
O --> Q["Interleaved fused gate/up<br/>block_size=2*R when BlockShard<br/>num_interleaved_matrices=2"]
O --> S["Owned matrix batch<br/>matrix_rows must be explicit"]
The implementation supports concatenated matrices, interleaved fused projections, and per-head weights. Interleaved layouts use a zero-copy permuted view, and the computed direction is written back through the view strides to preserve the original storage layout.
Fused w13 storage (interleaved)
┌──────────┬──────────┬──────────┬──────────┐
│ gate r0 │ up r0 │ gate r1 │ up r1 │ ...
└──────────┴──────────┴──────────┴──────────┘
│ zero-copy view + permute
▼
┌─────────────────────┐
│ Matrix 0: gate [R,C]│ ── NS independently
├─────────────────────┤
│ Matrix 1: up [R,C]│ ── NS independently
└─────────────────────┘
│ copy_ through view strides
▼
Original interleaved storageModel integrationKimi’s DistMuon configuration now recognizes fused SwiGLU w13 parameters after overrides, registers them in the appropriate optimizer buckets, and treats gate/up as two independent matrices. Since only Kimi uses DistMuon at the moment, I did not add e2e tests for other models that uses QKV fusion. But these cases are covered in unit tests. The coverage includes fused gate/up layouts, per-head fused QKV across MQA/GQA/MHA, configuration idempotency, and a successful two-GPU Kimi FSDP+EP end-to-end run. |
|
@anijain2305 @tianyu-l is this PR the same or different to the swiglue weight fusion chat yesterday? |
It could be related. Some thoughts
|
Allow Muon to treat packed parameter storage as independent logical matrices, including concatenated blocks, interleaved fused projections, and per-head weights. Preserve the logical view through Newton-Schulz computation and write results back through the original storage strides.
7680f61 to
3a195a7
Compare
|
Rebased onto the latest main after #4535 landed and updated the configuration registry accordingly, other parts remains unchanged. Since dense |
|
@WhoisZihan thanks for the PR! I believe interleaved layout is less natural than stacked layout
We are making that change in #4676. Could you adapt? cc @anijain2305 |
@tianyu-l Makes sense. Since #4676 changes the linear layout from interleaved to stacked, the Though I wonder is it necessary to As for #4677 , seem the interleave still happens in NOTE. the latest commit depends on #4676, I've managed to rebase locally and run some tests, but it might better wait for it to get merged. |
kimi_k3_debugmodel trains with DistMuon instead of AdamW. Matrix parameters use per-head compute layouts for both attention variants; the fused MLA projections are split into one Newton-Schulz matrix per logical projection, following Kimi's implementation (pytorch#4692): - wq_b: per head [q_nope; q_rope] via num_rows_per_segment - wkv_a: one block [kv_latent; k_rope] - wkv_b: per head [k_nope; v] KDA q/k/v/forget_b/output_gate are per head with block_size=head_dim; forget_a and output_proj are whole-matrix. Routed experts reuse the K2.7 per-expert layout. Dense feed_forward and shared_experts w13 follow pytorch#4531 and stay whole-matrix until the stacked layout from pytorch#4676 lands. AdamW keeps the non-matrix parameters (norms, embeddings, LM head, A_log, dt_bias, depthwise convs), the per-head-scalar beta rows, the single-row residual projections, and the vision tower.
kimi_k3_debugmodel trains with DistMuon instead of AdamW. Matrix parameters use per-head compute layouts for both attention variants; the fused MLA projections are split into one Newton-Schulz matrix per logical projection, following Kimi's implementation (pytorch#4692): - wq_b: per head [q_nope; q_rope] via num_rows_per_segment - wkv_a: one block [kv_latent; k_rope] - wkv_b: per head [k_nope; v] KDA q/k/v/forget_b/output_gate are per head with block_size=head_dim; forget_a and output_proj are whole-matrix. Routed experts reuse the K2.7 per-expert layout. Dense feed_forward and shared_experts w13 follow pytorch#4531 and stay whole-matrix until the stacked layout from pytorch#4676 lands. AdamW keeps the non-matrix parameters (norms, embeddings, LM head, A_log, dt_bias, depthwise convs), the per-head-scalar beta rows, the single-row residual projections, and the vision tower.
|
@WhoisZihan Thanks for your patience as we make several changes around the fused matrices. You have gotten the right picture that for Linear, we will stack the weights, and for QKV, they will still be interleaved. So we need different Muon handling for the two. Lets wait for the PRs to be merged. I am hopeful that they could be merged this week. |
|
@WhoisZihan I added you as co-author for the same w13, w2 fix: #4811 the delta is mainly
this is more aligned with other parts of DistMuon, per-expert, per-head sharding at the right boundary
|
@weifengpy Awesome, thanks! I'll try rebasing onto main after it gets merged, and see if there is still anything missing. Looks most of changes of this pr have been absorbed into the new PR. The only thing left is for |
…#4811) w13 didn't get any fqn match after splitting w123 into w13 and w2. this PR fixes it: * #4535 * https://github.com/pytorch/torchtitan/pull/4676/changes#r4058499709 * same problem first raised in #4531 by @WhoisZihan the next follow up is adding validation prevent future cases like above in optimizer init: #4816 Co-authored-by: Zihan Yang <[email protected]> Co-authored-by: Zihan Yang <[email protected]>
Cool, let's close this pr and use a new pr for that. |

Fix DistMuon NS shape for fused gated linear parameters. Fused SwiGLU stores w13.weight in the interleaved layout
These rows represent two independent logical matrices and should not be orthogonalized as a single matrix, which leads to incorrect result for muon and post-NS normalization used in normuon.
To correctly identify fused gate and layout, an explicit
matrix_layoutmetadata is added toComputeLayout. Before muon computation, fused gate/up rows are converted into an independent matrix batch, so that we can apply NS orthogonalization independently. The interleaved layout is restored afterwards.The default standard matrix layout is unchanged