Skip to content

[MoE] Make structured fused grouped-expert projection the default - #4871

Merged
sanketpurandare merged 2 commits into
mainfrom
gh/sanketpurandare/45/head
Sep 24, 2026
Merged

sanketpurandare merged 2 commits into
mainfrom
gh/sanketpurandare/45/head

Conversation

@sanketpurandare

@sanketpurandare sanketpurandare commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Summary:
Carry forward the reviewed implementation from #4552 while
storing routed-expert W13 as [E, 2, F, D]. The explicit gate/up axis matches
DistMoE's native parameter layout and keeps the feature axis independently
shardable. Standard grouped GEMM consumes a zero-copy [E, 2F, D] view and
returns [R, 2, F] before the existing activation.

Logical initialization and external HF keys remain w1_EFD, w2_EDF, and
w3_EFD. Native model, DCP, optimizer, and EMA state use the physical
w13_E2FD key, keeping named_parameters() and state-dict FQNs identical.
Adapters split or merge W13 only at the HF boundary; the model installs no
state-dict hooks, so save and load share the same native contract. Routing and
activation semantics are unchanged.

The physical fusion intentionally resets deterministic from-seed numerics. The
old parameter order consumed random values as W1, W2, W3; the fused parameter
initializes its slices as W1, W3 before W2. It also replaces two BF16 grouped
GEMMs with one wider W13 grouped GEMM, whose rounding need not match two
independent launches. Fixed-weight forward/backward tests validate the mapping,
and the affected DeepSeek V3 and Qwen3 A10G goldens are regenerated from
full-precision TensorBoard metrics for fake-PG and real-PG configurations.

Fused W13 also adds a second native matrix-batch dimension. DistMuon now accepts
native [..., rows, columns] parameters, matching the existing Newton-Schulz
implementation, and its expert redistribution test exercises the production
[E, 2, F, D] layout without adding another test case.

Kimi K3's DistMuon configuration now assigns the physical w13_E2FD and
w2_EDF expert parameters directly, so the fused W13 weight cannot fall
through to the fallback AdamW group.

This is the Sanket-owned replacement for #4552; Animesh's PR and stack remain
untouched.

Test Plan:

  • python -m pytest tests/unit_tests/cpu -q --deselect tests/unit_tests/cpu/test_lora.py::test_lora_forward (1244 passed, 23 skipped, 1 deselected)
  • python -m pytest -q tests/unit_tests/cpu/test_convert_ema_to_hf.py tests/unit_tests/cpu/test_state_dict_adapter.py tests/unit_tests/cpu/test_moe.py (41 passed)
  • Transformer-backend native-W13 round-trip and focused MoE/quantization tests (60 passed, 23 skipped)
  • python -m pytest -q tests/unit_tests/gpu/flex_shard/test_dist_muon.py (7 passed)
  • python -m pytest -q tests/unit_tests/gpu/test_fsdp_moe_sharding.py tests/unit_tests/gpu/test_fused_swiglu_override.py (20 passed)
  • Native state-key, EMA-export, and HF adapter round-trip tests pass without model state-dict hooks.
  • The Kimi K3 optimizer-pattern test verifies that native W13/W2 expert parameters are owned by DistMuon and stale logical W1/W3 names are absent.
  • A10G fake-PG and real-PG numerics were exported from the exact CI configurations; repeated DeepSeek V3 and Qwen3 runs produced identical full-precision trajectories.
  • Scoped formatting, flake8, pydoclint, codespell, link, and targeted Pyrefly checks passed. Project-wide Pyrefly retains one unrelated nullable-video-duration baseline error.

Review history:
This replaces #4643, which retains the original review discussion and approvals.

Summary:
Carry forward the reviewed implementation from #4552 while
storing routed-expert W13 as `[E, 2, F, D]`. The explicit gate/up axis matches
DistMoE's native parameter layout and keeps the feature axis independently
shardable. Standard grouped GEMM consumes a zero-copy `[E, 2F, D]` view and
returns `[R, 2, F]` before the existing activation.

Logical initialization and external HF keys remain `w1_EFD`, `w2_EDF`, and
`w3_EFD`. Native model, DCP, optimizer, and EMA state use the physical
`w13_E2FD` key, keeping `named_parameters()` and state-dict FQNs identical.
Adapters split or merge W13 only at the HF boundary; the model installs no
state-dict hooks, so save and load share the same native contract. Routing and
activation semantics are unchanged.

The physical fusion intentionally resets deterministic from-seed numerics. The
old parameter order consumed random values as W1, W2, W3; the fused parameter
initializes its slices as W1, W3 before W2. It also replaces two BF16 grouped
GEMMs with one wider W13 grouped GEMM, whose rounding need not match two
independent launches. Fixed-weight forward/backward tests validate the mapping,
and the affected DeepSeek V3 and Qwen3 A10G goldens are regenerated from
full-precision TensorBoard metrics for fake-PG and real-PG configurations.

Fused W13 also adds a second native matrix-batch dimension. DistMuon now accepts
native `[..., rows, columns]` parameters, matching the existing Newton-Schulz
implementation, and its expert redistribution test exercises the production
`[E, 2, F, D]` layout without adding another test case.

Kimi K3's DistMuon configuration now assigns the physical `w13_E2FD` and
`w2_EDF` expert parameters directly, so the fused W13 weight cannot fall
through to the fallback AdamW group.

This is the Sanket-owned replacement for #4552; Animesh's PR and stack remain
untouched.

Test Plan:
- `python -m pytest tests/unit_tests/cpu -q --deselect tests/unit_tests/cpu/test_lora.py::test_lora_forward` (1244 passed, 23 skipped, 1 deselected)
- `python -m pytest -q tests/unit_tests/cpu/test_convert_ema_to_hf.py tests/unit_tests/cpu/test_state_dict_adapter.py tests/unit_tests/cpu/test_moe.py` (41 passed)
- Transformer-backend native-W13 round-trip and focused MoE/quantization tests (60 passed, 23 skipped)
- `python -m pytest -q tests/unit_tests/gpu/flex_shard/test_dist_muon.py` (7 passed)
- `python -m pytest -q tests/unit_tests/gpu/test_fsdp_moe_sharding.py tests/unit_tests/gpu/test_fused_swiglu_override.py` (20 passed)
- Native state-key, EMA-export, and HF adapter round-trip tests pass without model state-dict hooks.
- The Kimi K3 optimizer-pattern test verifies that native W13/W2 expert parameters are owned by DistMuon and stale logical W1/W3 names are absent.
- A10G fake-PG and real-PG numerics were exported from the exact CI configurations; repeated DeepSeek V3 and Qwen3 runs produced identical full-precision trajectories.
- Scoped formatting, flake8, pydoclint, codespell, link, and targeted Pyrefly checks passed. Project-wide Pyrefly retains one unrelated nullable-video-duration baseline error.



Review history:
This replaces #4643, which retains the original review discussion and approvals.


ghstack-source-id: 9c5d570c958d3b8c336765cfb58f213c6d2eb86e

[ghstack-poisoned]
@pytorch-bot pytorch-bot Bot added ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/rl labels Sep 24, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Sep 24, 2026
…default"

Summary:
Carry forward the reviewed implementation from #4552 while
storing routed-expert W13 as `[E, 2, F, D]`. The explicit gate/up axis matches
DistMoE's native parameter layout and keeps the feature axis independently
shardable. Standard grouped GEMM consumes a zero-copy `[E, 2F, D]` view and
returns `[R, 2, F]` before the existing activation.

Logical initialization and external HF keys remain `w1_EFD`, `w2_EDF`, and
`w3_EFD`. Native model, DCP, optimizer, and EMA state use the physical
`w13_E2FD` key, keeping `named_parameters()` and state-dict FQNs identical.
Adapters split or merge W13 only at the HF boundary; the model installs no
state-dict hooks, so save and load share the same native contract. Routing and
activation semantics are unchanged.

The physical fusion intentionally resets deterministic from-seed numerics. The
old parameter order consumed random values as W1, W2, W3; the fused parameter
initializes its slices as W1, W3 before W2. It also replaces two BF16 grouped
GEMMs with one wider W13 grouped GEMM, whose rounding need not match two
independent launches. Fixed-weight forward/backward tests validate the mapping,
and the affected DeepSeek V3 and Qwen3 A10G goldens are regenerated from
full-precision TensorBoard metrics for fake-PG and real-PG configurations.

Fused W13 also adds a second native matrix-batch dimension. DistMuon now accepts
native `[..., rows, columns]` parameters, matching the existing Newton-Schulz
implementation, and its expert redistribution test exercises the production
`[E, 2, F, D]` layout without adding another test case.

Kimi K3's DistMuon configuration now assigns the physical `w13_E2FD` and
`w2_EDF` expert parameters directly, so the fused W13 weight cannot fall
through to the fallback AdamW group.

This is the Sanket-owned replacement for #4552; Animesh's PR and stack remain
untouched.

Test Plan:
- `python -m pytest tests/unit_tests/cpu -q --deselect tests/unit_tests/cpu/test_lora.py::test_lora_forward` (1244 passed, 23 skipped, 1 deselected)
- `python -m pytest -q tests/unit_tests/cpu/test_convert_ema_to_hf.py tests/unit_tests/cpu/test_state_dict_adapter.py tests/unit_tests/cpu/test_moe.py` (41 passed)
- Transformer-backend native-W13 round-trip and focused MoE/quantization tests (60 passed, 23 skipped)
- `python -m pytest -q tests/unit_tests/gpu/flex_shard/test_dist_muon.py` (7 passed)
- `python -m pytest -q tests/unit_tests/gpu/test_fsdp_moe_sharding.py tests/unit_tests/gpu/test_fused_swiglu_override.py` (20 passed)
- Native state-key, EMA-export, and HF adapter round-trip tests pass without model state-dict hooks.
- The Kimi K3 optimizer-pattern test verifies that native W13/W2 expert parameters are owned by DistMuon and stale logical W1/W3 names are absent.
- A10G fake-PG and real-PG numerics were exported from the exact CI configurations; repeated DeepSeek V3 and Qwen3 runs produced identical full-precision trajectories.
- Scoped formatting, flake8, pydoclint, codespell, link, and targeted Pyrefly checks passed. Project-wide Pyrefly retains one unrelated nullable-video-duration baseline error.



Review history:
This replaces #4643, which retains the original review discussion and approvals.




[ghstack-poisoned]
@sanketpurandare
sanketpurandare changed the base branch from gh/sanketpurandare/45/base to main September 24, 2026 19:53
@sanketpurandare
sanketpurandare merged commit 7031b7f into main Sep 24, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/rl 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