Repository navigation
[kimi k3] Switch K3 recipe to per-head DistMuon - #4596
Conversation
731cdbf to
6b11d80
Compare
shuhuayu
left a comment
There was a problem hiding this comment.
thx for the pr. had some questions and comments.
|
|
||
|
|
||
| @dataclass(frozen=True, slots=True) | ||
| class MatrixBatchLayout: |
There was a problem hiding this comment.
i think using owned + MatrixBatchLayout is actually solving the problem of strided blocks for BlockShard, maybe we should add this @weifengpy @tianyu-l?
There was a problem hiding this comment.
commented in #4596 (comment) that i prefer BlockShard + shard_order_by_tensor_dim over adding new tricks to MatrixBatchLayout
thx for the review. I've create a issue #4692 with the overall plan. This PR is now main + #4693 (segments) + the K3 recipe commit, no longer stacked on #4531. Merge order: it should land after #4676 and #4643, since the recipe then switches w13 to Shard(0) over the [2, F, D] batch and follows the new routed-expert layout. Also renamed to "Switch K3 recipe". |
Thanks for quoting, I think they are changing the default fused w13 linear layout from interleaved to stacked in #4676 , so MatrixBatchLayout is removed in my pr for now. Is there specific situation when we need to shard an owned parameter? If so, maybe we should add it back. |
6b11d80 to
f3371be
Compare
Thanks for the explanation. I think removing MatrixBatchLayout is ok for me, since w13 becomes a native [2, F, D] batch after #4676 anyway. |
3064574 to
1d6209b
Compare
| class _MatrixBatchView: | ||
| matrix_rows: int | ||
| matrix_columns: int | ||
| num_rows_per_segment: tuple[int, ...] | None = None |
There was a problem hiding this comment.
we don't have this in Kimi 2.7 . how come Kimi 3 need this? If it's strided BlockShard, can we use shard_order_by_tensor_dim to address this?
There was a problem hiding this comment.
i am thinking of using strided blockshard to handle the fused w13 layout like physical [6, 2]:
row 0: gate_0
row 1: up_0
row 2: gate_1
row 3: up_1
row 4: gate_2
row 5: up_2
not sure we can use shard_order_by_tensor_dim to address this since iiuc 1) it only accepts shard 2) it is two layer reordering, but here w13 is an interleaved fusion.
There was a problem hiding this comment.
we are moving away from interleaved #4596 (comment)
There was a problem hiding this comment.
i see, let's wait the upstream changes get landed in this case.
There was a problem hiding this comment.
thanks for explaining. [2, F, D] with 2 being leading dim make things easy
There was a problem hiding this comment.
i am thinking of using strided blockshard to handle the fused w13 layout like physical [6, 2]:
row 0: gate_0 row 1: up_0 row 2: gate_1 row 3: up_1 row 4: gate_2 row 5: up_2not sure we can
btw, this is exactly the definition of w1 and w3 being _StridedShard on 1d mesh (just mention it for fun)
There was a problem hiding this comment.
how about copy kernel efficiency? #4676 (comment)
weifengpy
left a comment
There was a problem hiding this comment.
done with review: I care about whether changes in distributed/flex_shard are true gaps, thus i have the comment above
|
|
…pe (#4755) ## Summary Prerequisite split out of #4596, as requested there: `_dist_muon_optimizer` in the Kimi K2.7 recipe took a single `lr` for both the DistMuon and the AdamW parameter groups. This changes it to `muon_lr` and `adamw_lr` so a recipe can tune them independently. All four recipes (`kimi_k2_5_debugmodel`, `moonlight_16b_a3b`, `kimi_k2_5`, `kimi_vl_a3b`) keep passing the same value to both, so no configuration changes. The K3 recipe in #4596 uses the same signature. ## Test plan - Built all four K2.7 recipes and checked both param groups still get the recipe's lr. - `pre-commit run` and pyrefly clean on the file.
## Summary Split out of #4596 as requested there. The K2.7 DistMuon recipe built `compute_sharding_by_fqn`, the per-layer FQN lists and the bucket FQNs through three intermediate tuples and comprehensions; this collapses them into one loop over layers and one loop over bucket groups, and drops `bucket_fqns`. Pure refactor. K2.7 is the only recipe on main that builds DistMuon buckets; the K3 recipe in #4596 is written the same way. ## Test plan - Dumped `bucket_configs`, `compute_sharding_by_fqn` and the param groups for `kimi_k2_5_debugmodel`, `moonlight_16b_a3b`, `kimi_k2_5` and `kimi_vl_a3b` on `main` and on this branch: identical. - `pre-commit run` and pyrefly clean on the file.
a2d94d8 to
9405936
Compare
0df5669 to
fbd096a
Compare
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 stay whole-matrix until the stacked layout from pytorch#4676 lands, then switch to Shard(0) compute over the [2, F, D] batch. 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 now defaults to DistMuon, which rejects TP-produced _StridedShard storage, so the fsdp2 x tp2 x ep2 test replaces the optimizer with AdamW the same way kimi_k2_5_debugmodel_seed_checkpoint does. Add kimi_k3_mm_muon (fsdp2 x ep2) to cover the default optimizer.
fbd096a to
7198b25
Compare
|
Hi @JavaZeroo, I rebased it on #4799 and used most of you changes, thx. |
| from torchtitan_recipes.tests import _set_spmd_typechecking | ||
|
|
||
|
|
||
| def kimi_k3_debugmodel_mm() -> Trainer.Config: |
There was a problem hiding this comment.
why still keep kimi_k3_debugmodel_mm ? in other words, why not update it in place to use muon?
There was a problem hiding this comment.
muon for tp is not supported yet, and we want to keep this test for tp composibility. will merge these two test once the distmuon tp pr lands.
Summary
Implements the "Add Per-Head Muon" item from the Kimi K3 tracking issue #4272:
kimi_k3_debugmodelnow trains with DistMuon instead of AdamW, with per-head compute layouts for both attention variants.cc @tianyu-l @shuhuayu @weifengpy
Design
MLA layers (
layers.N.attention): Support K3's extra outputgateprojection (num_heads * v_head_dimrows) gets a per-value-head layout.Note K3 has no
q_lora_rankbranch:wq_a/wq_bare unconditional, unlike DeepSeek-V3.KDA layers (
layers.N.delta_attention, 3/4 of all layers):q_proj/k_proj/v_proj/forget_b/output_gateare per-head withblock_size=head_dim.forget_aandoutput_projstay whole-matrix.LatentMoE: routed experts reuse
_per_expert_compute_layoutfrom K2.7; router gate, shared experts, and the latentrouted_down/routed_upstay whole-matrix.Stay on AdamW:
beta(its rows are per-head scalars — per-head compute would degenerate toblock_size=1and Newton-Schulz on rank-1 rows is meaningless),A_log/dt_bias/depthwise convs (non-matrix), all norms, the single-row[1, D]residualprojections, embeddings, LM head, and the vision tower.
Numerics
Validated on 4x RTX 5090. All runs share one model-only seed checkpoint,
--debug.seed=42 --debug.deterministic, 200 steps, 8192 tokens/step:Loss curves: