Skip to content

[MoE] Make routed expert projections first-class grouped linears - #4872

Merged
sanketpurandare merged 8 commits into
mainfrom
gh/sanketpurandare/46/head
Sep 24, 2026
Merged

sanketpurandare merged 8 commits into
mainfrom
gh/sanketpurandare/46/head

Conversation

@sanketpurandare

@sanketpurandare sanketpurandare commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Summary:
Replace the opaque RoutedExperts.inner_experts owner with explicit w13,
w2, activation_fn, and token_dispatcher children. Add a reusable,
bias-free GroupedLinear whose structured W13 storage is [E, 2, F, D];
grouped GEMM consumes a zero-copy [E, 2F, D] view. Its public configuration
matches Linear: out_features names one projection's output width and
num_linears=2 retains the gate/up axis.

This removes the temporary expert-owner, replacement, and sharding APIs raised
in review. Eager FSDP, GraphTrainer, quantization converters, model-specific
activations, and checkpoint adapters now share one explicit parameter
hierarchy. Logical initializer names retain their shape suffixes (w1_EFD,
w2_EDF, and w3_EFD), while runtime parameters use standard module keys
(w13.weight and w2.weight). HF checkpoint schemas remain compatible through
adapter-boundary conversion. DeepSeek V4 now performs the same native-to-logical
expert conversion as the other adapters, preventing native W13 keys from
leaking into exported HF checkpoints.

FSDP derives matrix-row shard placements through the shared linear helper for
both stacked Linear and GroupedLinear modules; no W13/W2 FQN special case
remains. MoE.num_experts is the canonical runtime expert count, and GPT-OSS
uses the shared MoE.Config without an empty model-specific subclass. The
existing fused-linear state adapter helpers also own the optional
routed W13 split/merge for HF schemas with separate expert projections, while
GPT-OSS and Qwen3.5 retain their fused HF layouts.

The standard MXFP8 and Float8 converters specialize each grouped linear at the
grouped-MM seam. Kimi retains SiTU and its optimizer metadata addresses the
native W13/W2 parameters. GPT-OSS keeps its grouped bias in a model-specific
GroupedLinear subclass and otherwise follows current main: routed expert
matrices are complete locally unless EP shards the expert axis.

MXFP8 grouped linears retain their existing 1x32 weight-scale format. Structured
W13 is flattened without a copy before quantization. Square 32x32 grouped
weights and preventing a columnwise block from spanning an unaligned gate/up
boundary remain a separate follow-up rather than expanding this ownership
refactor.

The parent W13 layout change already resets DeepSeek V3 and Qwen3 deterministic
goldens. GPT-OSS already used one fused W13 grouped GEMM and initialized W13
before W2. Its numerical reset instead comes from changing W13 storage from
HF-interleaved [E, 2F, D] to blocked [E, 2, F, D]: the same random values
now initialize different gate/up rows. A local fixed-weight comparison converts
the interleaved weight and bias into the blocked layout and produces bitwise
equal forward output; only backward reduction ordering may introduce secondary
rounding differences. GPT-OSS A10G fake-PG and real-PG goldens are regenerated
from full-precision TensorBoard metrics.

Test Plan:

  • python -m pytest tests/unit_tests/cpu -q --deselect tests/unit_tests/cpu/test_lora.py::test_lora_forward (1245 passed, 23 skipped, 1 deselected)
  • Focused GraphTrainer scheduling and chunk tests (108 passed, 120 deselected)
  • python -m pytest -q tests/unit_tests/gpu/test_fsdp_moe_sharding.py tests/unit_tests/gpu/flex_shard/test_dist_muon.py tests/unit_tests/gpu/test_fused_swiglu_override.py tests/unit_tests/gpu/test_compile_moe.py (25 passed)
  • Focused Kimi native-FQN/checkpoint tests (2 passed) and grouped-quantization CPU tests (10 passed)
  • Float8 structured-wrapper tests (2 passed); all 12 Float8 GPU tests collect. The local SM103 TorchAO grouped backward kernel is unsupported, so execution remains a B200 CI gate rather than a local workaround.
  • FSDP expert-placement tests cover expert-axis and matrix-row sharding through the generic linear placement helper.
  • State-dict adapter tests cover separate and fused HF expert schemas, including DTensor placement round-trips and DeepSeek V4 key validation.
  • A local old-interleaved versus new-blocked GPT-OSS fixed-weight forward comparison reports torch_equal=True and max_abs_diff=0.0.
  • A10G GPT-OSS fake-PG and real-PG numerics were exported from the exact CI configurations using full-precision TensorBoard metrics.
  • Scoped formatting, flake8, pydoclint, codespell, link, and targeted Pyrefly checks passed. Project-wide Pyrefly retains one unrelated nullable-video-duration baseline error.

CI notes:

Review history:
This replaces #4539, 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]
Summary:
Replace the opaque `RoutedExperts.inner_experts` owner with explicit `w13`,
`w2`, `activation_fn`, and `token_dispatcher` children. Add a reusable,
bias-free `GroupedLinear` whose structured W13 storage is `[E, 2, F, D]`;
grouped GEMM consumes a zero-copy `[E, 2F, D]` view. Its public configuration
matches `Linear`: `out_features` names one projection's output width and
`num_linears=2` retains the gate/up axis.

This removes the temporary expert-owner, replacement, and sharding APIs raised
in review. Eager FSDP, GraphTrainer, quantization converters, model-specific
activations, and checkpoint adapters now share one explicit parameter
hierarchy. Logical initializer names retain their shape suffixes (`w1_EFD`,
`w2_EDF`, and `w3_EFD`), while runtime parameters use standard module keys
(`w13.weight` and `w2.weight`). HF checkpoint schemas remain compatible through
adapter-boundary conversion. DeepSeek V4 now performs the same native-to-logical
expert conversion as the other adapters, preventing native W13 keys from
leaking into exported HF checkpoints.

FSDP derives matrix-row shard placements through the shared linear helper for
both stacked `Linear` and `GroupedLinear` modules; no W13/W2 FQN special case
remains. `MoE.num_experts` is the canonical runtime expert count, and GPT-OSS
uses the shared `MoE.Config` without an empty model-specific subclass. The
existing fused-linear state adapter helpers also own the optional
routed W13 split/merge for HF schemas with separate expert projections, while
GPT-OSS and Qwen3.5 retain their fused HF layouts.

The standard MXFP8 and Float8 converters specialize each grouped linear at the
grouped-MM seam. Kimi retains SiTU and its optimizer metadata addresses the
native W13/W2 parameters. GPT-OSS keeps its grouped bias in a model-specific
`GroupedLinear` subclass and otherwise follows current main: routed expert
matrices are complete locally unless EP shards the expert axis.

MXFP8 grouped linears retain their existing 1x32 weight-scale format. Structured
W13 is flattened without a copy before quantization. Square 32x32 grouped
weights and preventing a columnwise block from spanning an unaligned gate/up
boundary remain a separate follow-up rather than expanding this ownership
refactor.

The parent W13 layout change already resets DeepSeek V3 and Qwen3 deterministic
goldens. GPT-OSS already used one fused W13 grouped GEMM and initialized W13
before W2. Its numerical reset instead comes from changing W13 storage from
HF-interleaved `[E, 2F, D]` to blocked `[E, 2, F, D]`: the same random values
now initialize different gate/up rows. A local fixed-weight comparison converts
the interleaved weight and bias into the blocked layout and produces bitwise
equal forward output; only backward reduction ordering may introduce secondary
rounding differences. GPT-OSS A10G fake-PG and real-PG goldens are regenerated
from full-precision TensorBoard metrics.

Test Plan:
- `python -m pytest tests/unit_tests/cpu -q --deselect tests/unit_tests/cpu/test_lora.py::test_lora_forward` (1245 passed, 23 skipped, 1 deselected)
- Focused GraphTrainer scheduling and chunk tests (108 passed, 120 deselected)
- `python -m pytest -q tests/unit_tests/gpu/test_fsdp_moe_sharding.py tests/unit_tests/gpu/flex_shard/test_dist_muon.py tests/unit_tests/gpu/test_fused_swiglu_override.py tests/unit_tests/gpu/test_compile_moe.py` (25 passed)
- Focused Kimi native-FQN/checkpoint tests (2 passed) and grouped-quantization CPU tests (10 passed)
- Float8 structured-wrapper tests (2 passed); all 12 Float8 GPU tests collect. The local SM103 TorchAO grouped backward kernel is unsupported, so execution remains a B200 CI gate rather than a local workaround.
- FSDP expert-placement tests cover expert-axis and matrix-row sharding through the generic linear placement helper.
- State-dict adapter tests cover separate and fused HF expert schemas, including DTensor placement round-trips and DeepSeek V4 key validation.
- A local old-interleaved versus new-blocked GPT-OSS fixed-weight forward comparison reports `torch_equal=True` and `max_abs_diff=0.0`.
- A10G GPT-OSS fake-PG and real-PG numerics were exported from the exact CI configurations using full-precision TensorBoard metrics.
- Scoped formatting, flake8, pydoclint, codespell, link, and targeted Pyrefly checks passed. Project-wide Pyrefly retains one unrelated nullable-video-duration baseline error.

CI notes:
- The B200 run https://github.com/pytorch/torchtitan/actions/runs/35924519733 failed while extracting the repository checkout (`gzip: unexpected end of file`) before tests executed.
- The GraphTrainer failures in https://github.com/pytorch/torchtitan/actions/runs/35924514118 and https://github.com/pytorch/torchtitan/actions/runs/35924514111 reproduce in scheduled upstream-main runs https://github.com/pytorch/torchtitan/actions/runs/35860469802 and https://github.com/pytorch/torchtitan/actions/runs/35859018087, respectively.



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


ghstack-source-id: 716faaedf5a888743d2c67990283da8670f529f5

[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
…-class grouped linears"

Summary:
Replace the opaque `RoutedExperts.inner_experts` owner with explicit `w13`,
`w2`, `activation_fn`, and `token_dispatcher` children. Add a reusable,
bias-free `GroupedLinear` whose structured W13 storage is `[E, 2, F, D]`;
grouped GEMM consumes a zero-copy `[E, 2F, D]` view. Its public configuration
matches `Linear`: `out_features` names one projection's output width and
`num_linears=2` retains the gate/up axis.

This removes the temporary expert-owner, replacement, and sharding APIs raised
in review. Eager FSDP, GraphTrainer, quantization converters, model-specific
activations, and checkpoint adapters now share one explicit parameter
hierarchy. Logical initializer names retain their shape suffixes (`w1_EFD`,
`w2_EDF`, and `w3_EFD`), while runtime parameters use standard module keys
(`w13.weight` and `w2.weight`). HF checkpoint schemas remain compatible through
adapter-boundary conversion. DeepSeek V4 now performs the same native-to-logical
expert conversion as the other adapters, preventing native W13 keys from
leaking into exported HF checkpoints.

FSDP derives matrix-row shard placements through the shared linear helper for
both stacked `Linear` and `GroupedLinear` modules; no W13/W2 FQN special case
remains. `MoE.num_experts` is the canonical runtime expert count, and GPT-OSS
uses the shared `MoE.Config` without an empty model-specific subclass. The
existing fused-linear state adapter helpers also own the optional
routed W13 split/merge for HF schemas with separate expert projections, while
GPT-OSS and Qwen3.5 retain their fused HF layouts.

The standard MXFP8 and Float8 converters specialize each grouped linear at the
grouped-MM seam. Kimi retains SiTU and its optimizer metadata addresses the
native W13/W2 parameters. GPT-OSS keeps its grouped bias in a model-specific
`GroupedLinear` subclass and otherwise follows current main: routed expert
matrices are complete locally unless EP shards the expert axis.

MXFP8 grouped linears retain their existing 1x32 weight-scale format. Structured
W13 is flattened without a copy before quantization. Square 32x32 grouped
weights and preventing a columnwise block from spanning an unaligned gate/up
boundary remain a separate follow-up rather than expanding this ownership
refactor.

The parent W13 layout change already resets DeepSeek V3 and Qwen3 deterministic
goldens. GPT-OSS already used one fused W13 grouped GEMM and initialized W13
before W2. Its numerical reset instead comes from changing W13 storage from
HF-interleaved `[E, 2F, D]` to blocked `[E, 2, F, D]`: the same random values
now initialize different gate/up rows. A local fixed-weight comparison converts
the interleaved weight and bias into the blocked layout and produces bitwise
equal forward output; only backward reduction ordering may introduce secondary
rounding differences. GPT-OSS A10G fake-PG and real-PG goldens are regenerated
from full-precision TensorBoard metrics.

Test Plan:
- `python -m pytest tests/unit_tests/cpu -q --deselect tests/unit_tests/cpu/test_lora.py::test_lora_forward` (1245 passed, 23 skipped, 1 deselected)
- Focused GraphTrainer scheduling and chunk tests (108 passed, 120 deselected)
- `python -m pytest -q tests/unit_tests/gpu/test_fsdp_moe_sharding.py tests/unit_tests/gpu/flex_shard/test_dist_muon.py tests/unit_tests/gpu/test_fused_swiglu_override.py tests/unit_tests/gpu/test_compile_moe.py` (25 passed)
- Focused Kimi native-FQN/checkpoint tests (2 passed) and grouped-quantization CPU tests (10 passed)
- Float8 structured-wrapper tests (2 passed); all 12 Float8 GPU tests collect. The local SM103 TorchAO grouped backward kernel is unsupported, so execution remains a B200 CI gate rather than a local workaround.
- FSDP expert-placement tests cover expert-axis and matrix-row sharding through the generic linear placement helper.
- State-dict adapter tests cover separate and fused HF expert schemas, including DTensor placement round-trips and DeepSeek V4 key validation.
- A local old-interleaved versus new-blocked GPT-OSS fixed-weight forward comparison reports `torch_equal=True` and `max_abs_diff=0.0`.
- A10G GPT-OSS fake-PG and real-PG numerics were exported from the exact CI configurations using full-precision TensorBoard metrics.
- Scoped formatting, flake8, pydoclint, codespell, link, and targeted Pyrefly checks passed. Project-wide Pyrefly retains one unrelated nullable-video-duration baseline error.

CI notes:
- The B200 run https://github.com/pytorch/torchtitan/actions/runs/35924519733 failed while extracting the repository checkout (`gzip: unexpected end of file`) before tests executed.
- The GraphTrainer failures in https://github.com/pytorch/torchtitan/actions/runs/35924514118 and https://github.com/pytorch/torchtitan/actions/runs/35924514111 reproduce in scheduled upstream-main runs https://github.com/pytorch/torchtitan/actions/runs/35860469802 and https://github.com/pytorch/torchtitan/actions/runs/35859018087, respectively.



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




[ghstack-poisoned]
…inears"

Summary:
Replace the opaque `RoutedExperts.inner_experts` owner with explicit `w13`,
`w2`, `activation_fn`, and `token_dispatcher` children. Add a reusable,
bias-free `GroupedLinear` whose structured W13 storage is `[E, 2, F, D]`;
grouped GEMM consumes a zero-copy `[E, 2F, D]` view. Its public configuration
matches `Linear`: `out_features` names one projection's output width and
`num_linears=2` retains the gate/up axis.

This removes the temporary expert-owner, replacement, and sharding APIs raised
in review. Eager FSDP, GraphTrainer, quantization converters, model-specific
activations, and checkpoint adapters now share one explicit parameter
hierarchy. Logical initializer names retain their shape suffixes (`w1_EFD`,
`w2_EDF`, and `w3_EFD`), while runtime parameters use standard module keys
(`w13.weight` and `w2.weight`). HF checkpoint schemas remain compatible through
adapter-boundary conversion. DeepSeek V4 now performs the same native-to-logical
expert conversion as the other adapters, preventing native W13 keys from
leaking into exported HF checkpoints.

FSDP derives matrix-row shard placements through the shared linear helper for
both stacked `Linear` and `GroupedLinear` modules; no W13/W2 FQN special case
remains. `MoE.num_experts` is the canonical runtime expert count, and GPT-OSS
uses the shared `MoE.Config` without an empty model-specific subclass. The
existing fused-linear state adapter helpers also own the optional
routed W13 split/merge for HF schemas with separate expert projections, while
GPT-OSS and Qwen3.5 retain their fused HF layouts.

The standard MXFP8 and Float8 converters specialize each grouped linear at the
grouped-MM seam. Kimi retains SiTU and its optimizer metadata addresses the
native W13/W2 parameters. GPT-OSS keeps its grouped bias in a model-specific
`GroupedLinear` subclass and otherwise follows current main: routed expert
matrices are complete locally unless EP shards the expert axis.

MXFP8 grouped linears retain their existing 1x32 weight-scale format. Structured
W13 is flattened without a copy before quantization. Square 32x32 grouped
weights and preventing a columnwise block from spanning an unaligned gate/up
boundary remain a separate follow-up rather than expanding this ownership
refactor.

The parent W13 layout change already resets DeepSeek V3 and Qwen3 deterministic
goldens. GPT-OSS already used one fused W13 grouped GEMM and initialized W13
before W2. Its numerical reset instead comes from changing W13 storage from
HF-interleaved `[E, 2F, D]` to blocked `[E, 2, F, D]`: the same random values
now initialize different gate/up rows. A local fixed-weight comparison converts
the interleaved weight and bias into the blocked layout and produces bitwise
equal forward output; only backward reduction ordering may introduce secondary
rounding differences. GPT-OSS A10G fake-PG and real-PG goldens are regenerated
from full-precision TensorBoard metrics.

Test Plan:
- `python -m pytest tests/unit_tests/cpu -q --deselect tests/unit_tests/cpu/test_lora.py::test_lora_forward` (1245 passed, 23 skipped, 1 deselected)
- Focused GraphTrainer scheduling and chunk tests (108 passed, 120 deselected)
- `python -m pytest -q tests/unit_tests/gpu/test_fsdp_moe_sharding.py tests/unit_tests/gpu/flex_shard/test_dist_muon.py tests/unit_tests/gpu/test_fused_swiglu_override.py tests/unit_tests/gpu/test_compile_moe.py` (25 passed)
- Focused Kimi native-FQN/checkpoint tests (2 passed) and grouped-quantization CPU tests (10 passed)
- Float8 structured-wrapper tests (2 passed); all 12 Float8 GPU tests collect. The local SM103 TorchAO grouped backward kernel is unsupported, so execution remains a B200 CI gate rather than a local workaround.
- FSDP expert-placement tests cover expert-axis and matrix-row sharding through the generic linear placement helper.
- State-dict adapter tests cover separate and fused HF expert schemas, including DTensor placement round-trips and DeepSeek V4 key validation.
- A local old-interleaved versus new-blocked GPT-OSS fixed-weight forward comparison reports `torch_equal=True` and `max_abs_diff=0.0`.
- A10G GPT-OSS fake-PG and real-PG numerics were exported from the exact CI configurations using full-precision TensorBoard metrics.
- Scoped formatting, flake8, pydoclint, codespell, link, and targeted Pyrefly checks passed. Project-wide Pyrefly retains one unrelated nullable-video-duration baseline error.

CI notes:
- The B200 run https://github.com/pytorch/torchtitan/actions/runs/35924519733 failed while extracting the repository checkout (`gzip: unexpected end of file`) before tests executed.
- The GraphTrainer failures in https://github.com/pytorch/torchtitan/actions/runs/35924514118 and https://github.com/pytorch/torchtitan/actions/runs/35924514111 reproduce in scheduled upstream-main runs https://github.com/pytorch/torchtitan/actions/runs/35860469802 and https://github.com/pytorch/torchtitan/actions/runs/35859018087, respectively.



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




[ghstack-poisoned]
sanketpurandare added a commit that referenced this pull request Sep 24, 2026
Summary:
Replace the opaque `RoutedExperts.inner_experts` owner with explicit `w13`,
`w2`, `activation_fn`, and `token_dispatcher` children. Add a reusable,
bias-free `GroupedLinear` whose structured W13 storage is `[E, 2, F, D]`;
grouped GEMM consumes a zero-copy `[E, 2F, D]` view. Its public configuration
matches `Linear`: `out_features` names one projection's output width and
`num_linears=2` retains the gate/up axis.

This removes the temporary expert-owner, replacement, and sharding APIs raised
in review. Eager FSDP, GraphTrainer, quantization converters, model-specific
activations, and checkpoint adapters now share one explicit parameter
hierarchy. Logical initializer names retain their shape suffixes (`w1_EFD`,
`w2_EDF`, and `w3_EFD`), while runtime parameters use standard module keys
(`w13.weight` and `w2.weight`). HF checkpoint schemas remain compatible through
adapter-boundary conversion. DeepSeek V4 now performs the same native-to-logical
expert conversion as the other adapters, preventing native W13 keys from
leaking into exported HF checkpoints.

FSDP derives matrix-row shard placements through the shared linear helper for
both stacked `Linear` and `GroupedLinear` modules; no W13/W2 FQN special case
remains. `MoE.num_experts` is the canonical runtime expert count, and GPT-OSS
uses the shared `MoE.Config` without an empty model-specific subclass. The
existing fused-linear state adapter helpers also own the optional
routed W13 split/merge for HF schemas with separate expert projections, while
GPT-OSS and Qwen3.5 retain their fused HF layouts.

The standard MXFP8 and Float8 converters specialize each grouped linear at the
grouped-MM seam. Kimi retains SiTU and its optimizer metadata addresses the
native W13/W2 parameters. GPT-OSS keeps its grouped bias in a model-specific
`GroupedLinear` subclass and otherwise follows current main: routed expert
matrices are complete locally unless EP shards the expert axis.

MXFP8 grouped linears retain their existing 1x32 weight-scale format. Structured
W13 is flattened without a copy before quantization. Square 32x32 grouped
weights and preventing a columnwise block from spanning an unaligned gate/up
boundary remain a separate follow-up rather than expanding this ownership
refactor.

The parent W13 layout change already resets DeepSeek V3 and Qwen3 deterministic
goldens. GPT-OSS already used one fused W13 grouped GEMM and initialized W13
before W2. Its numerical reset instead comes from changing W13 storage from
HF-interleaved `[E, 2F, D]` to blocked `[E, 2, F, D]`: the same random values
now initialize different gate/up rows. A local fixed-weight comparison converts
the interleaved weight and bias into the blocked layout and produces bitwise
equal forward output; only backward reduction ordering may introduce secondary
rounding differences. GPT-OSS A10G fake-PG and real-PG goldens are regenerated
from full-precision TensorBoard metrics.

Test Plan:
- `python -m pytest tests/unit_tests/cpu -q --deselect tests/unit_tests/cpu/test_lora.py::test_lora_forward` (1245 passed, 23 skipped, 1 deselected)
- Focused GraphTrainer scheduling and chunk tests (108 passed, 120 deselected)
- `python -m pytest -q tests/unit_tests/gpu/test_fsdp_moe_sharding.py tests/unit_tests/gpu/flex_shard/test_dist_muon.py tests/unit_tests/gpu/test_fused_swiglu_override.py tests/unit_tests/gpu/test_compile_moe.py` (25 passed)
- Focused Kimi native-FQN/checkpoint tests (2 passed) and grouped-quantization CPU tests (10 passed)
- Float8 structured-wrapper tests (2 passed); all 12 Float8 GPU tests collect. The local SM103 TorchAO grouped backward kernel is unsupported, so execution remains a B200 CI gate rather than a local workaround.
- FSDP expert-placement tests cover expert-axis and matrix-row sharding through the generic linear placement helper.
- State-dict adapter tests cover separate and fused HF expert schemas, including DTensor placement round-trips and DeepSeek V4 key validation.
- A local old-interleaved versus new-blocked GPT-OSS fixed-weight forward comparison reports `torch_equal=True` and `max_abs_diff=0.0`.
- A10G GPT-OSS fake-PG and real-PG numerics were exported from the exact CI configurations using full-precision TensorBoard metrics.
- Scoped formatting, flake8, pydoclint, codespell, link, and targeted Pyrefly checks passed. Project-wide Pyrefly retains one unrelated nullable-video-duration baseline error.

CI notes:
- The B200 run https://github.com/pytorch/torchtitan/actions/runs/35924519733 failed while extracting the repository checkout (`gzip: unexpected end of file`) before tests executed.
- The GraphTrainer failures in https://github.com/pytorch/torchtitan/actions/runs/35924514118 and https://github.com/pytorch/torchtitan/actions/runs/35924514111 reproduce in scheduled upstream-main runs https://github.com/pytorch/torchtitan/actions/runs/35860469802 and https://github.com/pytorch/torchtitan/actions/runs/35859018087, respectively.



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



ghstack-source-id: 00a6085
Pull Request resolved: #4872
…-class grouped linears"

Summary:
Replace the opaque `RoutedExperts.inner_experts` owner with explicit `w13`,
`w2`, `activation_fn`, and `token_dispatcher` children. Add a reusable,
bias-free `GroupedLinear` whose structured W13 storage is `[E, 2, F, D]`;
grouped GEMM consumes a zero-copy `[E, 2F, D]` view. Its public configuration
matches `Linear`: `out_features` names one projection's output width and
`num_linears=2` retains the gate/up axis.

This removes the temporary expert-owner, replacement, and sharding APIs raised
in review. Eager FSDP, GraphTrainer, quantization converters, model-specific
activations, and checkpoint adapters now share one explicit parameter
hierarchy. Logical initializer names retain their shape suffixes (`w1_EFD`,
`w2_EDF`, and `w3_EFD`), while runtime parameters use standard module keys
(`w13.weight` and `w2.weight`). HF checkpoint schemas remain compatible through
adapter-boundary conversion. DeepSeek V4 now performs the same native-to-logical
expert conversion as the other adapters, preventing native W13 keys from
leaking into exported HF checkpoints.

FSDP derives matrix-row shard placements through the shared linear helper for
both stacked `Linear` and `GroupedLinear` modules; no W13/W2 FQN special case
remains. `MoE.num_experts` is the canonical runtime expert count, and GPT-OSS
uses the shared `MoE.Config` without an empty model-specific subclass. The
existing fused-linear state adapter helpers also own the optional
routed W13 split/merge for HF schemas with separate expert projections, while
GPT-OSS and Qwen3.5 retain their fused HF layouts.

The standard MXFP8 and Float8 converters specialize each grouped linear at the
grouped-MM seam. Kimi retains SiTU and its optimizer metadata addresses the
native W13/W2 parameters. GPT-OSS keeps its grouped bias in a model-specific
`GroupedLinear` subclass and otherwise follows current main: routed expert
matrices are complete locally unless EP shards the expert axis.

MXFP8 grouped linears retain their existing 1x32 weight-scale format. Structured
W13 is flattened without a copy before quantization. Square 32x32 grouped
weights and preventing a columnwise block from spanning an unaligned gate/up
boundary remain a separate follow-up rather than expanding this ownership
refactor.

The parent W13 layout change already resets DeepSeek V3 and Qwen3 deterministic
goldens. GPT-OSS already used one fused W13 grouped GEMM and initialized W13
before W2. Its numerical reset instead comes from changing W13 storage from
HF-interleaved `[E, 2F, D]` to blocked `[E, 2, F, D]`: the same random values
now initialize different gate/up rows. A local fixed-weight comparison converts
the interleaved weight and bias into the blocked layout and produces bitwise
equal forward output; only backward reduction ordering may introduce secondary
rounding differences. GPT-OSS A10G fake-PG and real-PG goldens are regenerated
from full-precision TensorBoard metrics.

Test Plan:
- `python -m pytest tests/unit_tests/cpu -q --deselect tests/unit_tests/cpu/test_lora.py::test_lora_forward` (1245 passed, 23 skipped, 1 deselected)
- Focused GraphTrainer scheduling and chunk tests (108 passed, 120 deselected)
- `python -m pytest -q tests/unit_tests/gpu/test_fsdp_moe_sharding.py tests/unit_tests/gpu/flex_shard/test_dist_muon.py tests/unit_tests/gpu/test_fused_swiglu_override.py tests/unit_tests/gpu/test_compile_moe.py` (25 passed)
- Focused Kimi native-FQN/checkpoint tests (2 passed) and grouped-quantization CPU tests (10 passed)
- Float8 structured-wrapper tests (2 passed); all 12 Float8 GPU tests collect. The local SM103 TorchAO grouped backward kernel is unsupported, so execution remains a B200 CI gate rather than a local workaround.
- FSDP expert-placement tests cover expert-axis and matrix-row sharding through the generic linear placement helper.
- State-dict adapter tests cover separate and fused HF expert schemas, including DTensor placement round-trips and DeepSeek V4 key validation.
- A local old-interleaved versus new-blocked GPT-OSS fixed-weight forward comparison reports `torch_equal=True` and `max_abs_diff=0.0`.
- A10G GPT-OSS fake-PG and real-PG numerics were exported from the exact CI configurations using full-precision TensorBoard metrics.
- Scoped formatting, flake8, pydoclint, codespell, link, and targeted Pyrefly checks passed. Project-wide Pyrefly retains one unrelated nullable-video-duration baseline error.

CI notes:
- The B200 run https://github.com/pytorch/torchtitan/actions/runs/35924519733 failed while extracting the repository checkout (`gzip: unexpected end of file`) before tests executed.
- The GraphTrainer failures in https://github.com/pytorch/torchtitan/actions/runs/35924514118 and https://github.com/pytorch/torchtitan/actions/runs/35924514111 reproduce in scheduled upstream-main runs https://github.com/pytorch/torchtitan/actions/runs/35860469802 and https://github.com/pytorch/torchtitan/actions/runs/35859018087, respectively.



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




[ghstack-poisoned]
…inears"

Summary:
Replace the opaque `RoutedExperts.inner_experts` owner with explicit `w13`,
`w2`, `activation_fn`, and `token_dispatcher` children. Add a reusable,
bias-free `GroupedLinear` whose structured W13 storage is `[E, 2, F, D]`;
grouped GEMM consumes a zero-copy `[E, 2F, D]` view. Its public configuration
matches `Linear`: `out_features` names one projection's output width and
`num_linears=2` retains the gate/up axis.

This removes the temporary expert-owner, replacement, and sharding APIs raised
in review. Eager FSDP, GraphTrainer, quantization converters, model-specific
activations, and checkpoint adapters now share one explicit parameter
hierarchy. Logical initializer names retain their shape suffixes (`w1_EFD`,
`w2_EDF`, and `w3_EFD`), while runtime parameters use standard module keys
(`w13.weight` and `w2.weight`). HF checkpoint schemas remain compatible through
adapter-boundary conversion. DeepSeek V4 now performs the same native-to-logical
expert conversion as the other adapters, preventing native W13 keys from
leaking into exported HF checkpoints.

FSDP derives matrix-row shard placements through the shared linear helper for
both stacked `Linear` and `GroupedLinear` modules; no W13/W2 FQN special case
remains. `MoE.num_experts` is the canonical runtime expert count, and GPT-OSS
uses the shared `MoE.Config` without an empty model-specific subclass. The
existing fused-linear state adapter helpers also own the optional
routed W13 split/merge for HF schemas with separate expert projections, while
GPT-OSS and Qwen3.5 retain their fused HF layouts.

The standard MXFP8 and Float8 converters specialize each grouped linear at the
grouped-MM seam. Kimi retains SiTU and its optimizer metadata addresses the
native W13/W2 parameters. GPT-OSS keeps its grouped bias in a model-specific
`GroupedLinear` subclass and otherwise follows current main: routed expert
matrices are complete locally unless EP shards the expert axis.

MXFP8 grouped linears retain their existing 1x32 weight-scale format. Structured
W13 is flattened without a copy before quantization. Square 32x32 grouped
weights and preventing a columnwise block from spanning an unaligned gate/up
boundary remain a separate follow-up rather than expanding this ownership
refactor.

The parent W13 layout change already resets DeepSeek V3 and Qwen3 deterministic
goldens. GPT-OSS already used one fused W13 grouped GEMM and initialized W13
before W2. Its numerical reset instead comes from changing W13 storage from
HF-interleaved `[E, 2F, D]` to blocked `[E, 2, F, D]`: the same random values
now initialize different gate/up rows. A local fixed-weight comparison converts
the interleaved weight and bias into the blocked layout and produces bitwise
equal forward output; only backward reduction ordering may introduce secondary
rounding differences. GPT-OSS A10G fake-PG and real-PG goldens are regenerated
from full-precision TensorBoard metrics.

Test Plan:
- `python -m pytest tests/unit_tests/cpu -q --deselect tests/unit_tests/cpu/test_lora.py::test_lora_forward` (1245 passed, 23 skipped, 1 deselected)
- Focused GraphTrainer scheduling and chunk tests (108 passed, 120 deselected)
- `python -m pytest -q tests/unit_tests/gpu/test_fsdp_moe_sharding.py tests/unit_tests/gpu/flex_shard/test_dist_muon.py tests/unit_tests/gpu/test_fused_swiglu_override.py tests/unit_tests/gpu/test_compile_moe.py` (25 passed)
- Focused Kimi native-FQN/checkpoint tests (2 passed) and grouped-quantization CPU tests (10 passed)
- Float8 structured-wrapper tests (2 passed); all 12 Float8 GPU tests collect. The local SM103 TorchAO grouped backward kernel is unsupported, so execution remains a B200 CI gate rather than a local workaround.
- FSDP expert-placement tests cover expert-axis and matrix-row sharding through the generic linear placement helper.
- State-dict adapter tests cover separate and fused HF expert schemas, including DTensor placement round-trips and DeepSeek V4 key validation.
- A local old-interleaved versus new-blocked GPT-OSS fixed-weight forward comparison reports `torch_equal=True` and `max_abs_diff=0.0`.
- A10G GPT-OSS fake-PG and real-PG numerics were exported from the exact CI configurations using full-precision TensorBoard metrics.
- Scoped formatting, flake8, pydoclint, codespell, link, and targeted Pyrefly checks passed. Project-wide Pyrefly retains one unrelated nullable-video-duration baseline error.

CI notes:
- The B200 run https://github.com/pytorch/torchtitan/actions/runs/35924519733 failed while extracting the repository checkout (`gzip: unexpected end of file`) before tests executed.
- The GraphTrainer failures in https://github.com/pytorch/torchtitan/actions/runs/35924514118 and https://github.com/pytorch/torchtitan/actions/runs/35924514111 reproduce in scheduled upstream-main runs https://github.com/pytorch/torchtitan/actions/runs/35860469802 and https://github.com/pytorch/torchtitan/actions/runs/35859018087, respectively.



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




[ghstack-poisoned]
sanketpurandare added a commit that referenced this pull request Sep 24, 2026
Summary:
Replace the opaque `RoutedExperts.inner_experts` owner with explicit `w13`,
`w2`, `activation_fn`, and `token_dispatcher` children. Add a reusable,
bias-free `GroupedLinear` whose structured W13 storage is `[E, 2, F, D]`;
grouped GEMM consumes a zero-copy `[E, 2F, D]` view. Its public configuration
matches `Linear`: `out_features` names one projection's output width and
`num_linears=2` retains the gate/up axis.

This removes the temporary expert-owner, replacement, and sharding APIs raised
in review. Eager FSDP, GraphTrainer, quantization converters, model-specific
activations, and checkpoint adapters now share one explicit parameter
hierarchy. Logical initializer names retain their shape suffixes (`w1_EFD`,
`w2_EDF`, and `w3_EFD`), while runtime parameters use standard module keys
(`w13.weight` and `w2.weight`). HF checkpoint schemas remain compatible through
adapter-boundary conversion. DeepSeek V4 now performs the same native-to-logical
expert conversion as the other adapters, preventing native W13 keys from
leaking into exported HF checkpoints.

FSDP derives matrix-row shard placements through the shared linear helper for
both stacked `Linear` and `GroupedLinear` modules; no W13/W2 FQN special case
remains. `MoE.num_experts` is the canonical runtime expert count, and GPT-OSS
uses the shared `MoE.Config` without an empty model-specific subclass. The
existing fused-linear state adapter helpers also own the optional
routed W13 split/merge for HF schemas with separate expert projections, while
GPT-OSS and Qwen3.5 retain their fused HF layouts.

The standard MXFP8 and Float8 converters specialize each grouped linear at the
grouped-MM seam. Kimi retains SiTU and its optimizer metadata addresses the
native W13/W2 parameters. GPT-OSS keeps its grouped bias in a model-specific
`GroupedLinear` subclass and otherwise follows current main: routed expert
matrices are complete locally unless EP shards the expert axis.

MXFP8 grouped linears retain their existing 1x32 weight-scale format. Structured
W13 is flattened without a copy before quantization. Square 32x32 grouped
weights and preventing a columnwise block from spanning an unaligned gate/up
boundary remain a separate follow-up rather than expanding this ownership
refactor.

The parent W13 layout change already resets DeepSeek V3 and Qwen3 deterministic
goldens. GPT-OSS already used one fused W13 grouped GEMM and initialized W13
before W2. Its numerical reset instead comes from changing W13 storage from
HF-interleaved `[E, 2F, D]` to blocked `[E, 2, F, D]`: the same random values
now initialize different gate/up rows. A local fixed-weight comparison converts
the interleaved weight and bias into the blocked layout and produces bitwise
equal forward output; only backward reduction ordering may introduce secondary
rounding differences. GPT-OSS A10G fake-PG and real-PG goldens are regenerated
from full-precision TensorBoard metrics.

Test Plan:
- `python -m pytest tests/unit_tests/cpu -q --deselect tests/unit_tests/cpu/test_lora.py::test_lora_forward` (1245 passed, 23 skipped, 1 deselected)
- Focused GraphTrainer scheduling and chunk tests (108 passed, 120 deselected)
- `python -m pytest -q tests/unit_tests/gpu/test_fsdp_moe_sharding.py tests/unit_tests/gpu/flex_shard/test_dist_muon.py tests/unit_tests/gpu/test_fused_swiglu_override.py tests/unit_tests/gpu/test_compile_moe.py` (25 passed)
- Focused Kimi native-FQN/checkpoint tests (2 passed) and grouped-quantization CPU tests (10 passed)
- Float8 structured-wrapper tests (2 passed); all 12 Float8 GPU tests collect. The local SM103 TorchAO grouped backward kernel is unsupported, so execution remains a B200 CI gate rather than a local workaround.
- FSDP expert-placement tests cover expert-axis and matrix-row sharding through the generic linear placement helper.
- State-dict adapter tests cover separate and fused HF expert schemas, including DTensor placement round-trips and DeepSeek V4 key validation.
- A local old-interleaved versus new-blocked GPT-OSS fixed-weight forward comparison reports `torch_equal=True` and `max_abs_diff=0.0`.
- A10G GPT-OSS fake-PG and real-PG numerics were exported from the exact CI configurations using full-precision TensorBoard metrics.
- Scoped formatting, flake8, pydoclint, codespell, link, and targeted Pyrefly checks passed. Project-wide Pyrefly retains one unrelated nullable-video-duration baseline error.

CI notes:
- The B200 run https://github.com/pytorch/torchtitan/actions/runs/35924519733 failed while extracting the repository checkout (`gzip: unexpected end of file`) before tests executed.
- The GraphTrainer failures in https://github.com/pytorch/torchtitan/actions/runs/35924514118 and https://github.com/pytorch/torchtitan/actions/runs/35924514111 reproduce in scheduled upstream-main runs https://github.com/pytorch/torchtitan/actions/runs/35860469802 and https://github.com/pytorch/torchtitan/actions/runs/35859018087, respectively.



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



ghstack-source-id: 00a6085
Pull Request resolved: #4872
…-class grouped linears"

Summary:
Replace the opaque `RoutedExperts.inner_experts` owner with explicit `w13`,
`w2`, `activation_fn`, and `token_dispatcher` children. Add a reusable,
bias-free `GroupedLinear` whose structured W13 storage is `[E, 2, F, D]`;
grouped GEMM consumes a zero-copy `[E, 2F, D]` view. Its public configuration
matches `Linear`: `out_features` names one projection's output width and
`num_linears=2` retains the gate/up axis.

This removes the temporary expert-owner, replacement, and sharding APIs raised
in review. Eager FSDP, GraphTrainer, quantization converters, model-specific
activations, and checkpoint adapters now share one explicit parameter
hierarchy. Logical initializer names retain their shape suffixes (`w1_EFD`,
`w2_EDF`, and `w3_EFD`), while runtime parameters use standard module keys
(`w13.weight` and `w2.weight`). HF checkpoint schemas remain compatible through
adapter-boundary conversion. DeepSeek V4 now performs the same native-to-logical
expert conversion as the other adapters, preventing native W13 keys from
leaking into exported HF checkpoints.

FSDP derives matrix-row shard placements through the shared linear helper for
both stacked `Linear` and `GroupedLinear` modules; no W13/W2 FQN special case
remains. `MoE.num_experts` is the canonical runtime expert count, and GPT-OSS
uses the shared `MoE.Config` without an empty model-specific subclass. The
existing fused-linear state adapter helpers also own the optional
routed W13 split/merge for HF schemas with separate expert projections, while
GPT-OSS and Qwen3.5 retain their fused HF layouts.

The standard MXFP8 and Float8 converters specialize each grouped linear at the
grouped-MM seam. Kimi retains SiTU and its optimizer metadata addresses the
native W13/W2 parameters. GPT-OSS keeps its grouped bias in a model-specific
`GroupedLinear` subclass and otherwise follows current main: routed expert
matrices are complete locally unless EP shards the expert axis.

MXFP8 grouped linears retain their existing 1x32 weight-scale format. Structured
W13 is flattened without a copy before quantization. Square 32x32 grouped
weights and preventing a columnwise block from spanning an unaligned gate/up
boundary remain a separate follow-up rather than expanding this ownership
refactor.

The parent W13 layout change already resets DeepSeek V3 and Qwen3 deterministic
goldens. GPT-OSS already used one fused W13 grouped GEMM and initialized W13
before W2. Its numerical reset instead comes from changing W13 storage from
HF-interleaved `[E, 2F, D]` to blocked `[E, 2, F, D]`: the same random values
now initialize different gate/up rows. A local fixed-weight comparison converts
the interleaved weight and bias into the blocked layout and produces bitwise
equal forward output; only backward reduction ordering may introduce secondary
rounding differences. GPT-OSS A10G fake-PG and real-PG goldens are regenerated
from full-precision TensorBoard metrics.

Test Plan:
- `python -m pytest tests/unit_tests/cpu -q --deselect tests/unit_tests/cpu/test_lora.py::test_lora_forward` (1245 passed, 23 skipped, 1 deselected)
- Focused GraphTrainer scheduling and chunk tests (108 passed, 120 deselected)
- `python -m pytest -q tests/unit_tests/gpu/test_fsdp_moe_sharding.py tests/unit_tests/gpu/flex_shard/test_dist_muon.py tests/unit_tests/gpu/test_fused_swiglu_override.py tests/unit_tests/gpu/test_compile_moe.py` (25 passed)
- Focused Kimi native-FQN/checkpoint tests (2 passed) and grouped-quantization CPU tests (10 passed)
- Float8 structured-wrapper tests (2 passed); all 12 Float8 GPU tests collect. The local SM103 TorchAO grouped backward kernel is unsupported, so execution remains a B200 CI gate rather than a local workaround.
- FSDP expert-placement tests cover expert-axis and matrix-row sharding through the generic linear placement helper.
- State-dict adapter tests cover separate and fused HF expert schemas, including DTensor placement round-trips and DeepSeek V4 key validation.
- A local old-interleaved versus new-blocked GPT-OSS fixed-weight forward comparison reports `torch_equal=True` and `max_abs_diff=0.0`.
- A10G GPT-OSS fake-PG and real-PG numerics were exported from the exact CI configurations using full-precision TensorBoard metrics.
- Scoped formatting, flake8, pydoclint, codespell, link, and targeted Pyrefly checks passed. Project-wide Pyrefly retains one unrelated nullable-video-duration baseline error.

CI notes:
- The B200 run https://github.com/pytorch/torchtitan/actions/runs/35924519733 failed while extracting the repository checkout (`gzip: unexpected end of file`) before tests executed.
- The GraphTrainer failures in https://github.com/pytorch/torchtitan/actions/runs/35924514118 and https://github.com/pytorch/torchtitan/actions/runs/35924514111 reproduce in scheduled upstream-main runs https://github.com/pytorch/torchtitan/actions/runs/35860469802 and https://github.com/pytorch/torchtitan/actions/runs/35859018087, respectively.



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




[ghstack-poisoned]
…inears"

Summary:
Replace the opaque `RoutedExperts.inner_experts` owner with explicit `w13`,
`w2`, `activation_fn`, and `token_dispatcher` children. Add a reusable,
bias-free `GroupedLinear` whose structured W13 storage is `[E, 2, F, D]`;
grouped GEMM consumes a zero-copy `[E, 2F, D]` view. Its public configuration
matches `Linear`: `out_features` names one projection's output width and
`num_linears=2` retains the gate/up axis.

This removes the temporary expert-owner, replacement, and sharding APIs raised
in review. Eager FSDP, GraphTrainer, quantization converters, model-specific
activations, and checkpoint adapters now share one explicit parameter
hierarchy. Logical initializer names retain their shape suffixes (`w1_EFD`,
`w2_EDF`, and `w3_EFD`), while runtime parameters use standard module keys
(`w13.weight` and `w2.weight`). HF checkpoint schemas remain compatible through
adapter-boundary conversion. DeepSeek V4 now performs the same native-to-logical
expert conversion as the other adapters, preventing native W13 keys from
leaking into exported HF checkpoints.

FSDP derives matrix-row shard placements through the shared linear helper for
both stacked `Linear` and `GroupedLinear` modules; no W13/W2 FQN special case
remains. `MoE.num_experts` is the canonical runtime expert count, and GPT-OSS
uses the shared `MoE.Config` without an empty model-specific subclass. The
existing fused-linear state adapter helpers also own the optional
routed W13 split/merge for HF schemas with separate expert projections, while
GPT-OSS and Qwen3.5 retain their fused HF layouts.

The standard MXFP8 and Float8 converters specialize each grouped linear at the
grouped-MM seam. Kimi retains SiTU and its optimizer metadata addresses the
native W13/W2 parameters. GPT-OSS keeps its grouped bias in a model-specific
`GroupedLinear` subclass and otherwise follows current main: routed expert
matrices are complete locally unless EP shards the expert axis.

MXFP8 grouped linears retain their existing 1x32 weight-scale format. Structured
W13 is flattened without a copy before quantization. Square 32x32 grouped
weights and preventing a columnwise block from spanning an unaligned gate/up
boundary remain a separate follow-up rather than expanding this ownership
refactor.

The parent W13 layout change already resets DeepSeek V3 and Qwen3 deterministic
goldens. GPT-OSS already used one fused W13 grouped GEMM and initialized W13
before W2. Its numerical reset instead comes from changing W13 storage from
HF-interleaved `[E, 2F, D]` to blocked `[E, 2, F, D]`: the same random values
now initialize different gate/up rows. A local fixed-weight comparison converts
the interleaved weight and bias into the blocked layout and produces bitwise
equal forward output; only backward reduction ordering may introduce secondary
rounding differences. GPT-OSS A10G fake-PG and real-PG goldens are regenerated
from full-precision TensorBoard metrics.

Test Plan:
- `python -m pytest tests/unit_tests/cpu -q --deselect tests/unit_tests/cpu/test_lora.py::test_lora_forward` (1245 passed, 23 skipped, 1 deselected)
- Focused GraphTrainer scheduling and chunk tests (108 passed, 120 deselected)
- `python -m pytest -q tests/unit_tests/gpu/test_fsdp_moe_sharding.py tests/unit_tests/gpu/flex_shard/test_dist_muon.py tests/unit_tests/gpu/test_fused_swiglu_override.py tests/unit_tests/gpu/test_compile_moe.py` (25 passed)
- Focused Kimi native-FQN/checkpoint tests (2 passed) and grouped-quantization CPU tests (10 passed)
- Float8 structured-wrapper tests (2 passed); all 12 Float8 GPU tests collect. The local SM103 TorchAO grouped backward kernel is unsupported, so execution remains a B200 CI gate rather than a local workaround.
- FSDP expert-placement tests cover expert-axis and matrix-row sharding through the generic linear placement helper.
- State-dict adapter tests cover separate and fused HF expert schemas, including DTensor placement round-trips and DeepSeek V4 key validation.
- A local old-interleaved versus new-blocked GPT-OSS fixed-weight forward comparison reports `torch_equal=True` and `max_abs_diff=0.0`.
- A10G GPT-OSS fake-PG and real-PG numerics were exported from the exact CI configurations using full-precision TensorBoard metrics.
- Scoped formatting, flake8, pydoclint, codespell, link, and targeted Pyrefly checks passed. Project-wide Pyrefly retains one unrelated nullable-video-duration baseline error.

CI notes:
- The B200 run https://github.com/pytorch/torchtitan/actions/runs/35924519733 failed while extracting the repository checkout (`gzip: unexpected end of file`) before tests executed.
- The GraphTrainer failures in https://github.com/pytorch/torchtitan/actions/runs/35924514118 and https://github.com/pytorch/torchtitan/actions/runs/35924514111 reproduce in scheduled upstream-main runs https://github.com/pytorch/torchtitan/actions/runs/35860469802 and https://github.com/pytorch/torchtitan/actions/runs/35859018087, respectively.



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




[ghstack-poisoned]
sanketpurandare added a commit that referenced this pull request Sep 24, 2026
Summary:
Replace the opaque `RoutedExperts.inner_experts` owner with explicit `w13`,
`w2`, `activation_fn`, and `token_dispatcher` children. Add a reusable,
bias-free `GroupedLinear` whose structured W13 storage is `[E, 2, F, D]`;
grouped GEMM consumes a zero-copy `[E, 2F, D]` view. Its public configuration
matches `Linear`: `out_features` names one projection's output width and
`num_linears=2` retains the gate/up axis.

This removes the temporary expert-owner, replacement, and sharding APIs raised
in review. Eager FSDP, GraphTrainer, quantization converters, model-specific
activations, and checkpoint adapters now share one explicit parameter
hierarchy. Logical initializer names retain their shape suffixes (`w1_EFD`,
`w2_EDF`, and `w3_EFD`), while runtime parameters use standard module keys
(`w13.weight` and `w2.weight`). HF checkpoint schemas remain compatible through
adapter-boundary conversion. DeepSeek V4 now performs the same native-to-logical
expert conversion as the other adapters, preventing native W13 keys from
leaking into exported HF checkpoints.

FSDP derives matrix-row shard placements through the shared linear helper for
both stacked `Linear` and `GroupedLinear` modules; no W13/W2 FQN special case
remains. `MoE.num_experts` is the canonical runtime expert count, and GPT-OSS
uses the shared `MoE.Config` without an empty model-specific subclass. The
existing fused-linear state adapter helpers also own the optional
routed W13 split/merge for HF schemas with separate expert projections, while
GPT-OSS and Qwen3.5 retain their fused HF layouts.

The standard MXFP8 and Float8 converters specialize each grouped linear at the
grouped-MM seam. Kimi retains SiTU and its optimizer metadata addresses the
native W13/W2 parameters. GPT-OSS keeps its grouped bias in a model-specific
`GroupedLinear` subclass and otherwise follows current main: routed expert
matrices are complete locally unless EP shards the expert axis.

MXFP8 grouped linears retain their existing 1x32 weight-scale format. Structured
W13 is flattened without a copy before quantization. Square 32x32 grouped
weights and preventing a columnwise block from spanning an unaligned gate/up
boundary remain a separate follow-up rather than expanding this ownership
refactor.

The parent W13 layout change already resets DeepSeek V3 and Qwen3 deterministic
goldens. GPT-OSS already used one fused W13 grouped GEMM and initialized W13
before W2. Its numerical reset instead comes from changing W13 storage from
HF-interleaved `[E, 2F, D]` to blocked `[E, 2, F, D]`: the same random values
now initialize different gate/up rows. A local fixed-weight comparison converts
the interleaved weight and bias into the blocked layout and produces bitwise
equal forward output; only backward reduction ordering may introduce secondary
rounding differences. GPT-OSS A10G fake-PG and real-PG goldens are regenerated
from full-precision TensorBoard metrics.

Test Plan:
- `python -m pytest tests/unit_tests/cpu -q --deselect tests/unit_tests/cpu/test_lora.py::test_lora_forward` (1245 passed, 23 skipped, 1 deselected)
- Focused GraphTrainer scheduling and chunk tests (108 passed, 120 deselected)
- `python -m pytest -q tests/unit_tests/gpu/test_fsdp_moe_sharding.py tests/unit_tests/gpu/flex_shard/test_dist_muon.py tests/unit_tests/gpu/test_fused_swiglu_override.py tests/unit_tests/gpu/test_compile_moe.py` (25 passed)
- Focused Kimi native-FQN/checkpoint tests (2 passed) and grouped-quantization CPU tests (10 passed)
- Float8 structured-wrapper tests (2 passed); all 12 Float8 GPU tests collect. The local SM103 TorchAO grouped backward kernel is unsupported, so execution remains a B200 CI gate rather than a local workaround.
- FSDP expert-placement tests cover expert-axis and matrix-row sharding through the generic linear placement helper.
- State-dict adapter tests cover separate and fused HF expert schemas, including DTensor placement round-trips and DeepSeek V4 key validation.
- A local old-interleaved versus new-blocked GPT-OSS fixed-weight forward comparison reports `torch_equal=True` and `max_abs_diff=0.0`.
- A10G GPT-OSS fake-PG and real-PG numerics were exported from the exact CI configurations using full-precision TensorBoard metrics.
- Scoped formatting, flake8, pydoclint, codespell, link, and targeted Pyrefly checks passed. Project-wide Pyrefly retains one unrelated nullable-video-duration baseline error.

CI notes:
- The B200 run https://github.com/pytorch/torchtitan/actions/runs/35924519733 failed while extracting the repository checkout (`gzip: unexpected end of file`) before tests executed.
- The GraphTrainer failures in https://github.com/pytorch/torchtitan/actions/runs/35924514118 and https://github.com/pytorch/torchtitan/actions/runs/35924514111 reproduce in scheduled upstream-main runs https://github.com/pytorch/torchtitan/actions/runs/35860469802 and https://github.com/pytorch/torchtitan/actions/runs/35859018087, respectively.

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

ghstack-source-id: 85ad344
Pull Request resolved: #4872
@sanketpurandare
sanketpurandare changed the base branch from gh/sanketpurandare/46/base to main September 24, 2026 20:04
@sanketpurandare
sanketpurandare merged commit eb83b90 into main Sep 24, 2026
20 of 21 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