Repository navigation
Make fused grouped-expert projection the default - #4552
anijain2305 wants to merge 27 commits into
Conversation
ghstack-source-id: 68bc79b Pull-Request: pytorch/torchtitan#4552
| self.linear_beta = config.linear_beta | ||
|
|
||
| def forward( | ||
| def _activation( |
There was a problem hiding this comment.
commented in the linear PR
| class GroupedExperts(Module): | ||
| """SwiGLU experts with one physical interleaved gate-up parameter. | ||
|
|
||
| ``w13`` has shape ``(E, F, 2, D)`` and is flattened to ``(E, 2F, D)`` for |
There was a problem hiding this comment.
why don't we define w13 as (E, 2F, D) as in e.g. FusedQKV https://github.com/pytorch/torchtitan/blob/main/torchtitan/models/common/config_utils.py#L244
| return init | ||
|
|
||
|
|
||
| def _fuse_grouped_experts_param_init(param_init: dict | None) -> dict | None: |
There was a problem hiding this comment.
can be put together with https://github.com/pytorch/torchtitan/blob/main/torchtitan/models/common/config_utils.py#L109
In some sense, this is not part of core torchtitan. Users can do whatever.
|
Talked to @tianyu-l - we will wait for @jinsooihm to make GroupedExpert to only contain one weight. Update - We will land this first, and then @jinsooihm will refactor after it. |
| self.register_load_state_dict_pre_hook(self._merge_w13_on_load) | ||
|
|
||
| def forward( | ||
| def _activation( |
There was a problem hiding this comment.
related to my comment on first PR:
If we remove this, it seems the override only needs to happen on ActivationFn, not GroupedExperts which is nice, because we no longer need to separately override MLP and GroupedExperts
| ) | ||
| ) | ||
|
|
||
| def build(self, **kwargs): |
There was a problem hiding this comment.
Can this happen only in init? like in https://github.com/pytorch/torchtitan/blob/main/torchtitan/overrides/fused_swiglu.py#L621
Also, since we are making this default, we don't need to keep unfused sharding anyway. This is somewhat related to "we don't need to keep state_dict unfused" but I prefer we should do it in this PR, to avoid introducing code like _fuse_grouped_experts_sharding, although my opinion is not that strong.
| # Convert parameters from DTensors to plain Tensors, to work with | ||
| # dynamic-shape inputs in EP which cannot be easily expressed as DTensors. | ||
| w1_EFD = self.w1_EFD.to_local() | ||
| w13_E2FD = self.w13.to_local() |
There was a problem hiding this comment.
technically this notation is misleading, because each letter represents a single dim
Here's previous "workaround"
| gate: torch.Tensor, | ||
| up: torch.Tensor, | ||
| *, | ||
| offsets: torch.Tensor | None = None, |
There was a problem hiding this comment.
to alllow offset aware activations - silu_and_mul_op - for MoE.
| gate: torch.Tensor, | ||
| up: torch.Tensor, | ||
| *, | ||
| offsets: torch.Tensor | None = None, |
There was a problem hiding this comment.
since this is generic, we don't need to have boilerplate here -- maybe make it **kwargs
| state_dict[f"{prefix}w3_EFD"] = gate_up[:, :, 1, :].contiguous() | ||
|
|
||
|
|
||
| def split_fused_gate_up_state_dict( |
There was a problem hiding this comment.
if they are only used by HF state dict adapters, they probably should be put in https://github.com/pytorch/torchtitan/blob/main/torchtitan/models/utils.py
| def _make_fused_linear_init(gate_init: Callable, up_init: Callable) -> Callable: | ||
| """Build an initializer for an interleaved 2D gate/up linear weight.""" | ||
| init_logical_weight = _make_fused_gate_up_init(gate_init, up_init, gate_up_axis=1) | ||
| def _make_fused_linear_init( |
There was a problem hiding this comment.
I feel this doesn't belong to core definition of FeedForward, so let's put together with https://github.com/pytorch/torchtitan/blob/main/torchtitan/models/common/config_utils.py#L110
| # Convert parameters from DTensors to plain Tensors, to work with | ||
| # dynamic-shape inputs in EP which cannot be easily expressed as DTensors. | ||
| w1_EFD = self.w1_EFD.to_local() | ||
| w13_E_F_2_D = self.w13.to_local() |
There was a problem hiding this comment.
EF2D is proper? as there are indeed 4 dims
| up_RF = self._grouped_mm(A=x_RD.bfloat16(), weight_EOI=w3_EFD, offs=offsets_E) | ||
| h_RF = self.activation_fn(gate_RF, up_RF) | ||
| E, F, _, D = w13_E_F_2_D.shape | ||
| w13_E_2F_D = w13_E_F_2_D.reshape(E, F * 2, D) |
There was a problem hiding this comment.
in comparison, E_2F_D is a reasonable workaround
| # Routed-expert layout for the shared ``GroupedExperts`` (w13/w2). | ||
| _GROUPED_EXPERTS_PARAM_LAYOUT: dict[str, spmd.PerMeshAxisSpmdType] = { | ||
| "w1_EFD": spmd.S(1), | ||
| "w13": spmd.S(1), |
|
|
||
| _GROUPED_EXPERTS_PARAM_LAYOUT: dict[str, spmd.PerMeshAxisSpmdType] = { | ||
| "w1_EFD": spmd.S(1), | ||
| "w13": spmd.S(1), |
| enable_sp=enable_sp, | ||
| expert_param_layout={ | ||
| "w1_EFD": spmd.S(1), | ||
| "w13": spmd.S(1), |
|
Will need to revisit this after #4535 lands specifically about the (E, F, 2, D) vs (E, 2F, D) layout. If we can make the two consistent, it will be better for the readability. Update - The PR now has makes the grouped experts have the same layout of (E, 2F, D) for |
tianyu-l
left a comment
There was a problem hiding this comment.
This version looks very nice.
If @sanketpurandare and we agree with the layout, I'll stamp. O/w I need to take another look on the (E, 2, F, D) version. Thanks!
| offs=offsets_E, | ||
| ) | ||
| h_RF = self.activation_fn(gate_RF, up_RF) | ||
| gate_RF, up_RF = gate_up_R_2F.reshape(-1, F, 2).unbind(-1) |
There was a problem hiding this comment.
are you saying this unbind(-1) is pretty cheap?
There was a problem hiding this comment.
Is this PR bitwise numerics preserving?
|
@sanketpurandare will take over the Pr |
Summary: Carry forward the reviewed implementation from pytorch/torchtitan#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 checkpoint keys remain `w1_EFD`, `w2_EDF`, and `w3_EFD`. Routing, activation, grouped-GEMM, and dispatcher behavior are unchanged. This is the Sanket-owned replacement for #4552; Animesh's PR and stack remain untouched. Test Plan: - `CUDA_VISIBLE_DEVICES='' python -m pytest -q tests/unit_tests/cpu/test_moe.py tests/unit_tests/cpu/test_inference_moe.py tests/unit_tests/cpu/test_quantization.py torchtitan/experiments/transformers_modeling_backend/tests/test_moe_parallelism.py tests/unit_tests/gpu/test_fused_swiglu_override.py -k 'not TestFusedSwiGLUGroupedExpertsNumerics and not TestFusedSwiGLUOverrideKernels'` (69 passed, 3 skipped, 6 deselected) - TorchTitan pre-commit hooks pass except for the existing unrelated Pyrefly error in `torchtitan/hf_datasets/multimodal/utils/video.py`. - GPU coverage compares the structured and interleaved layouts bitwise for output, input gradients, and weight gradients; execution requires a grouped-MM-capable CUDA build. ghstack-source-id: 85b0f87 Pull-Request: pytorch/torchtitan#4643
Summary: Carry forward the reviewed implementation from pytorch/torchtitan#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 checkpoint keys remain `w1_EFD`, `w2_EDF`, and `w3_EFD`. Routing, activation, grouped-GEMM, and dispatcher behavior are unchanged. The physical fusion intentionally changes seeded random initialization order, so the affected real-PG integration goldens are refreshed; checkpoint-loaded weights retain their logical values. 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. This is the Sanket-owned replacement for #4552; Animesh's PR and stack remain untouched. Test Plan: - `python -m pytest -q tests/unit_tests/gpu/flex_shard/test_dist_muon.py` (2 passed) - `python -m pytest -q tests/unit_tests/gpu/test_fsdp_moe_sharding.py` (4 passed) - `CUDA_VISIBLE_DEVICES='' python -m pytest -q tests/unit_tests/cpu/test_moe.py tests/unit_tests/cpu/test_inference_moe.py tests/unit_tests/cpu/test_quantization.py torchtitan/experiments/transformers_modeling_backend/tests/test_moe_parallelism.py tests/unit_tests/gpu/test_fused_swiglu_override.py -k 'not TestFusedSwiGLUGroupedExpertsNumerics and not TestFusedSwiGLUOverrideKernels'` (69 passed, 3 skipped, 6 deselected) - TorchTitan formatting, flake8, pydoclint, codespell, and link checks passed; Pyrefly reports one existing unrelated optional-`av` diagnostic in `torchtitan/hf_datasets/multimodal/utils/video.py`. - The real-PG A10G model suite produced the refreshed 10-step loss and gradient-norm goldens. ghstack-source-id: 85b0f87 Pull-Request: pytorch/torchtitan#4643
Summary: Carry forward the reviewed implementation from pytorch/torchtitan#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 checkpoint keys remain `w1_EFD`, `w2_EDF`, and `w3_EFD`. Routing, activation, grouped-GEMM, and dispatcher behavior are unchanged. The physical fusion intentionally changes seeded random initialization order, so the affected real-PG integration goldens are refreshed; checkpoint-loaded weights retain their logical values. 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. This is the Sanket-owned replacement for #4552; Animesh's PR and stack remain untouched. Test Plan: - `python -m pytest -q tests/unit_tests/gpu/flex_shard/test_dist_muon.py` (2 passed) - `python -m pytest -q tests/unit_tests/gpu/test_fsdp_moe_sharding.py` (4 passed) - `CUDA_VISIBLE_DEVICES='' python -m pytest -q tests/unit_tests/cpu/test_moe.py tests/unit_tests/cpu/test_inference_moe.py tests/unit_tests/cpu/test_quantization.py torchtitan/experiments/transformers_modeling_backend/tests/test_moe_parallelism.py tests/unit_tests/gpu/test_fused_swiglu_override.py -k 'not TestFusedSwiGLUGroupedExpertsNumerics and not TestFusedSwiGLUOverrideKernels'` (69 passed, 3 skipped, 6 deselected) - TorchTitan formatting, flake8, pydoclint, codespell, and link checks passed; Pyrefly reports one existing unrelated optional-`av` diagnostic in `torchtitan/hf_datasets/multimodal/utils/video.py`. - The real-PG A10G model suite produced the refreshed 10-step loss and gradient-norm goldens. ghstack-source-id: ae440ac Pull Request resolved: pytorch/torchtitan#4643
Summary: Carry forward the reviewed implementation from pytorch/torchtitan#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 checkpoint keys remain `w1_EFD`, `w2_EDF`, and `w3_EFD`. Routing, activation, grouped-GEMM, and dispatcher behavior are unchanged. The physical fusion intentionally changes seeded random initialization order, so the affected real-PG integration goldens are refreshed; checkpoint-loaded weights retain their logical values. 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. This is the Sanket-owned replacement for #4552; Animesh's PR and stack remain untouched. Test Plan: - `python -m pytest -q tests/unit_tests/gpu/flex_shard/test_dist_muon.py` (2 passed) - `python -m pytest -q tests/unit_tests/gpu/test_fsdp_moe_sharding.py` (4 passed) - `CUDA_VISIBLE_DEVICES='' python -m pytest -q tests/unit_tests/cpu/test_moe.py tests/unit_tests/cpu/test_inference_moe.py tests/unit_tests/cpu/test_quantization.py torchtitan/experiments/transformers_modeling_backend/tests/test_moe_parallelism.py tests/unit_tests/gpu/test_fused_swiglu_override.py -k 'not TestFusedSwiGLUGroupedExpertsNumerics and not TestFusedSwiGLUOverrideKernels'` (69 passed, 3 skipped, 6 deselected) - TorchTitan formatting, flake8, pydoclint, codespell, and link checks passed; Pyrefly reports one existing unrelated optional-`av` diagnostic in `torchtitan/hf_datasets/multimodal/utils/video.py`. - The real-PG A10G model suite produced the refreshed 10-step loss and gradient-norm goldens. ghstack-source-id: ae440ac Pull Request resolved: pytorch/torchtitan#4643
… projection the 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. [ghstack-poisoned]
…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. [ghstack-poisoned]
…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]
Summary: Carry forward the reviewed implementation from pytorch/torchtitan#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. ghstack-source-id: 916a644 Pull Request resolved: pytorch/torchtitan#4643
Stack from ghstack (oldest at bottom):