Skip to content

[nvfp4 training][rl] Add the four-over-six grouped GEMM and dispatcher wiring - #14

Closed
wolfcomos wants to merge 1 commit into
4over6/ao2-cutedslfrom
4over6/ao3-grouped-dispatcher
Closed

[nvfp4 training][rl] Add the four-over-six grouped GEMM and dispatcher wiring#14
wolfcomos wants to merge 1 commit into
4over6/ao2-cutedslfrom
4over6/ao3-grouped-dispatcher

Conversation

@wolfcomos

@wolfcomos wolfcomos commented Aug 30, 2026

Copy link
Copy Markdown
Owner

Stacked on #12/#13. Integrates the four-over-six recipe with MoE training for routed-expert layers, reusing existing pieces rather than adding kernels: the #13 quantizer (its CuTe DSL fast path dispatches automatically) and the existing F.scaled_grouped_mm for the GEMMs.

  • Per-tensor activations: each token group's amax expands to a per-row vector so the whole packed tensor quantizes in one call — bitwise identical to quantizing each group separately — and one scaled_grouped_mm with per-group second-level scales runs the forward.
  • Expert weights quantize in one flattened call for both block shapes (the quantizer accepts a per-row amax with 16x16 blocks under a tile-uniform contract): one kernel launch regardless of expert count.
  • Row-scaled activations: one scaled_grouped_mm carrying the constant per-tensor factor in every group's slot, its bf16 output upcast and scaled by the raw per-row amaxes. The grouped GEMM emits bf16 only, which costs one output rounding vs per-group dense GEMMs; tests emulate that rounding in a loop oracle and compare bitwise (with a reduction-order SQNR fallback), and separately bound the rounding cost.
  • Backward: high_precision or dequantized grouped GEMMs (four-over-six has no quantized backward); dequantized differentiates the quantized forward itself from the saved 4-bit codes and scales.

Wiring mirrors the Float8/MXFP8 recipes exactly: NVFP4FourOverSixTrainingOpConfig(TrainingOpBaseConfig) — a pytree-constant dataclass with explicit __eq__/__hash__ — plus an isinstance branch in _quantize_then_scaled_grouped_mm. The dispatcher branch owns the offs[-1] tail slice / zero-extend for padded token dispatchers that over-allocate A past the logical rows. quantize_() model conversion for the grouped config is future work; framework integrations drive the grouped GEMM dispatcher (see the torchtitan converters). Adds an NVFP4 four-over-six section to the moe_training README.

Tests: 40 grouped tests (grouped-vs-dense forward parity, row-scaled fused-vs-oracle, backward bitwise references including empty groups and ragged padding, dispatcher round-trip with over-allocated tails, fullgraph compile in both scale granularities) plus the dense suite with the relaxed quantizer combinations, all green on GB200.

Stack: #12#13 ← this PR.

@wolfcomos
wolfcomos force-pushed the 4over6/ao3-grouped-dispatcher branch from b6d440f to e576049 Compare August 31, 2026 02:01
@wolfcomos
wolfcomos force-pushed the 4over6/ao3-grouped-dispatcher branch from e576049 to 3621d9e Compare August 31, 2026 03:09
@wolfcomos
wolfcomos force-pushed the 4over6/ao3-grouped-dispatcher branch from 3621d9e to ed08374 Compare August 31, 2026 04:14
@wolfcomos
wolfcomos force-pushed the 4over6/ao3-grouped-dispatcher branch from ed08374 to 3f5fa5d Compare August 31, 2026 04:37
@wolfcomos
wolfcomos force-pushed the 4over6/ao3-grouped-dispatcher branch from 3f5fa5d to 5c3fe59 Compare August 31, 2026 04:44
@wolfcomos
wolfcomos force-pushed the 4over6/ao3-grouped-dispatcher branch from 5c3fe59 to 9f57abe Compare August 31, 2026 06:35
@wolfcomos wolfcomos changed the title [NVFP4 4over6] Add the grouped GEMM and wire it into the training dispatcher [nvfp4 training][rl] Add the four-over-six grouped GEMM and dispatcher wiring Aug 31, 2026
…atcher

Integrate the four-over-six recipe with MoE training: routed-expert
layers get four_over_six_grouped_mm, built by reusing the existing
pieces rather than adding kernels — the quantizer from the dense PRs
(the CuTe DSL fast path dispatches automatically) and the existing
F.scaled_grouped_mm for the GEMMs.

- Per-tensor activations: each token group's amax expands to a per-row
  vector so the whole packed tensor quantizes in one call, bitwise
  identical to quantizing each group separately; one scaled_grouped_mm
  with per-group second-level scales runs the forward.
- Expert weights quantize in one flattened call for both block shapes
  (the quantizer accepts a per-row amax with 16x16 blocks under a
  tile-uniform contract), one kernel launch regardless of expert count.
- Row-scaled activations: one scaled_grouped_mm carrying the constant
  per-tensor factor, its bf16 output upcast and scaled by the raw
  per-row amaxes (the grouped GEMM emits bf16 only, one output rounding
  vs a dense per-group loop; tests pin it against a rounding-emulated
  loop oracle).
- Backward is high-precision or dequantized grouped GEMMs (no
  quantized backward); dequantized differentiates the quantized
  forward itself from the saved 4-bit codes and scales.

Wiring mirrors the Float8/MXFP8 recipes exactly:
NVFP4FourOverSixTrainingOpConfig(TrainingOpBaseConfig) — a
pytree-constant dataclass with explicit __eq__/__hash__ — plus an
isinstance branch in _quantize_then_scaled_grouped_mm. The dispatcher
branch owns the offs[-1] tail slice / zero-extend for padded token
dispatchers that over-allocate A past the logical rows. Also adds an
NVFP4 four-over-six section to the moe_training README.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@wolfcomos
wolfcomos force-pushed the 4over6/ao3-grouped-dispatcher branch from 9f57abe to 8a7dadc Compare August 31, 2026 07:02
@wolfcomos

Copy link
Copy Markdown
Owner Author

Opened upstream as pytorch#4853 (same head). This fork draft stays as the staging copy.

@wolfcomos

Copy link
Copy Markdown
Owner Author

Live upstream as pytorch#4853 — closing the staging draft. The branch stays: it is the upstream PR's head.

@wolfcomos wolfcomos closed this Aug 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant