Skip to content

KDA layer: gemm_precision="mxfp8" (Megatron-LM / TE MXFP8 recipe on every projection GEMM) + TE scale mode for convert_to_mxfp8 - #8757

Draft
huizzhan wants to merge 22 commits into
cursor/kda-fused-layer-bwd-8350from
cursor/kda-mxfp8-gemm-precision-9e7f
Draft

huizzhan wants to merge 22 commits into
cursor/kda-fused-layer-bwd-8350from
cursor/kda-mxfp8-gemm-precision-9e7f

Conversation

@huizzhan

@huizzhan huizzhan commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Stacked on #8739. Adds a GEMM precision mode to the Kimi-K3 KDA layer, kda_layer_fwd/bwd(..., gemm_precision="bf16"|"mxfp8") (or AITER_KDA_GEMM_PRECISION). "mxfp8" runs every projection GEMM in forward and backward the way NVIDIA/Megatron-LM#6872's Kimi-K3 recipes do: Transformer Engine MXFP8BlockScaling(fp8_format=E4M3). It also adds a TE round-up scale mode to aiter's Triton convert_to_mxfp8. The default torchao mode is unchanged, and the KDA mxfp8 path selects the TE mode.

Motivation

Megatron's Kimi-K3 recipe (mxfp8_THD64K_256GPU_TP1PP4EP64CP8_muon.yaml: fp8_recipe: mxfp8, fp8_format: e4m3, kda_disable_fp8 unset) runs every KDA projection as an MXFP8 TE linear, including the low-rank f_a/f_b and beta. In #8739 those projections are bf16 GEMMs. Separately, aiter's _convert_to_mxfp8_kernel uses torchao's E8M0 rule, floor(log2(rne_3bit(amax))) - 8. That rule can pick a scale one binade lower than TE's ceil(log2(amax/448)), so the block max clips at 448 (e.g. amax 460 or 230).

Recipe implemented (TE MXFP8BlockScaling, E4M3)

  • Every operand is E4M3 with one E8M0 scale per 32 values, blocked along the GEMM's reduction axis:
    • fprop: X and W rowwise.
    • dgrad: dY rowwise × W columnwise.
    • wgrad: dY columnwise × X columnwise.
  • Every copy is quantized from the 16-bit tensor (activations, gradients, and the bf16 staging copy of the fp32-master weight).
  • Scale byte = float_to_e8m0(amax * fp32(1/448)): the exponent rounded up, clamped to [0, 254], 0 for amax 0. Data = saturating RNE cast of x * 2^(127-byte).
  • Accumulation is fp32. Fprop and dgrad outputs are rounded to bf16; wgrad stays fp32 (main_grad).
  • f_b and g_proj outputs (raw_g, gate) are rounded to bf16 before the fp32 gate / sigmoid, so these epilogues run unfused on the MXFP8 path by default. The mxfp8_epilogue=1 fusion switch runs the FlyDSL epilogues on the MXFP8 operands instead, which skips the bf16 rounding of their GEMM output.

Changes

  • aiter/ops/mxfp8_linear.py (new):
    • Bit-exact torch port of TE's cast (mxfp8_quantize, float_to_e8m0) and dequantize/qdq.
    • mxfp8_mm(a, b, out16), with implementations auto / qdq / emulate.
    • mxfp8_matmul, a differentiable TE-linear emulation used by the torch backend.
    • On GPU, quantization goes through Triton convert_to_mxfp8(scale_mode="te").
  • aiter/ops/kda_fused_layer.py:
    • MXFP8ProjGemm is installed as the register_proj_gemm hook for the duration of an "mxfp8" call, and re-installed in backward.
    • _proj_gemm now passes ops to the hook and exposes _proj_gemm_default.
    • The torch backend emulates the recipe via _ref_linear and runs QKV as one projection, as Megatron's in_proj does.
    • New mxfp8_epilogue fusion switch.
    • LAST_PIECES["gemm"] records which implementation ran each GEMM.
  • aiter/ops/triton/.../quant_mxfp8.py: convert_to_mxfp8(..., scale_mode="torchao"|"te") and a TE_SCALE constexpr. TE mode takes the portable path; ASM and SR are rejected.

Which GEMMs run natively vs emulated (AITER_MXFP8_GEMM=auto, gfx950):

GEMM Path
fprop (qkv/concat, f_a, f_b, w_beta, g_proj, w_o) and every dgrad native aiter FP8 MFMA GEMM gemm_a8w8_mxfp8_bpreshuffle (1x32 E8M0, bf16 out = TE's output dtype)
every wgrad (fp32 out) bf16 GEMM on dequantized MXFP8 operands, which are exact in bf16: the MXFP8 product up to summation order
mxfp8_epilogue=1 gate / sigmoid GEMMs FlyDSL bf16 epilogue GEMMs on dequantized MXFP8 operands
non-gfx950, fp16/fp32 layers, CPU fp32 emulation

Performance (if applicable)

Not measured. GPU validation is done separately on one card. The new test tables time the mxfp8 block and each projection GEMM; GEMM timings include the per-call operand quantization.

Testing

  • Unit tests added or updated
  • Performance benchmarks run
  • Tested on MI250X
  • Tested on MI300X

Documentation

  • Docstrings updated
  • User guide updated
  • Performance guide updated
  • No documentation change is needed, with the reason stated below

Dependencies

  • No new third-party dependencies added
  • New dependencies are listed and justified below

Breaking Changes

None. gemm_precision defaults to "bf16", and convert_to_mxfp8 defaults to scale_mode="torchao".

PR Self-Review

Reviewed revision

6433844 (GPU results below are for 0a68c40)

Behavior and intent

With gemm_precision="mxfp8", every KDA projection GEMM (fwd, dgrad, wgrad) uses TE MXFP8 numerics. The default precision is unchanged. convert_to_mxfp8(scale_mode="te") is bit-exact with TE.

Validation and unverified scope

CPU, with no GPU in this environment:

  • The torch TE cast matches an independent scalar TE reimplementation, and matches Lumen's mxfp8_te_reference.py bit-exactly (scale bytes and E4M3 data, rowwise and columnwise, fp32 and bf16, amax 460/230/448, zero and subnormal blocks).
  • Triton TE-mode scale bytes match Lumen's reference (1-D and 2-D blocks) under TRITON_INTERPRET=1. The interpreter's fp32→fp8 cast is itself wrong (1.9667→1.0), so Triton E4M3 data bit-exactness is asserted only by the GPU tests.
  • Fused mxfp8 vs the torch-backend mxfp8 emulation agrees exactly on y and every weight gradient; dx is within one bf16 ulp. The CPU fused run uses emulated GEMMs.
  • Each backward GEMM has 3.7% relative-L2 error vs fp32, as expected for E4M3.
  • mxfp8 vs bf16 layer deviation scales linearly with mantissa width (4x per 2 bits), so the large deviation on the small random test layer is genuine conditioning, not a wiring bug.

GPU (one card, 0a68c40):

  • The projection GEMM table passes: 36 rows, err 0, Triton TE and HIP quant bit-exact, native fwd/dgrad rel-L2 1.7-2.8e-4, wgrad qdq about 5e-9.
  • The test_convert_mxfp8 TE/default-scale tests pass.
  • The block row at t=100 failed the old strict check on 482 of 88M w_q elements (max 0.0177). That is the effect of a bf16 rounding landing differently and moving a later fp8 rounding or block scale. It motivated the per-tensor grading in 6433844: a CPU simulation of 1% one-ulp output flips passes it, and an unquantized operand fails it (rel-L2 0.06-0.15).

Still unverified: the block rows on the GPU (t=100 compare under the new grading, the K3-width rows, the mxfp8-vs-bf16 deviation).

PR-specific question

N/A until the automated self-review comment posts.

Changed op tests

  • op_tests/test_kda_fused_layer_bwd.py:
    • test_kda_mxfp8_gemm: each projection × fwd/dgrad/wgrad on the aiter path vs fp32 emulation (tight, 1e-2), plus Triton TE cast vs torch TE port, bit-exact.
    • test_kda_mxfp8_block: the fused layer on aiter MXFP8 GEMMs vs the same layer on emulated GEMMs, and vs the torch-backend mxfp8 reference. Each tensor is graded by rel-L2, the fraction of elements outside an elementwise tolerance, and max delta (MXFP8_BLOCK_GRADES). A bf16 rounding that lands differently moves a few later fp8 roundings, so a strict all-elements check is the wrong test. Failures are collected and asserted after the table prints. mxfp8-vs-bf16 deviation is reported only.
    • Both run for bf16 layers.
  • op_tests/triton_tests/quant/test_convert_mxfp8.py: TE mode is bit-exact vs a torch TE reference (e4m3/e5m2, fp32/bf16, 1-D/2-D); the torchao default is unchanged; TE mode rejects ASM and SR.
Open in Web Open in Cursor 

cursoragent and others added 3 commits November 26, 2026 10:59
…ry projection GEMM via the register_proj_gemm hook); aiter.ops.mxfp8_linear

Co-authored-by: Sealeen Zhang <[email protected]>
…default torchao unchanged); KDA mxfp8 path quantizes through it; op tests for the mxfp8 GEMMs and block

Co-authored-by: Sealeen Zhang <[email protected]>
cursoragent and others added 17 commits November 26, 2026 11:12
…tion and max delta; assert after the table prints

Co-authored-by: Sealeen Zhang <[email protected]>
… into cursor/kda-mxfp8-gemm-precision-9e7f

Co-authored-by: Sealeen Zhang <[email protected]>
…rided input) for the wgrad operands, bit-identical to the plain path; opt-in hipBLASLt fp32-out wgrad (AITER_MXFP8_WGRAD=hipblaslt) and its probe; fastpath-vs-plain bitwise checks

Co-authored-by: Sealeen Zhang <[email protected]>
…opies) and a per-call quantized-operand cache (layer input X rowwise/columnwise; weights across calls with AITER_MXFP8_WEIGHT_CACHE=1), bit-identical

Co-authored-by: Sealeen Zhang <[email protected]>
…fp8 round_input_bf16), bf16 results for _proj_gemm(out16_ok=True) call sites, MXFP8 weight cache keyed on cached_weight_cat so qkv hits; bit-identical

Co-authored-by: Sealeen Zhang <[email protected]>
…lated layer relative to the recipe's own effect (noise ratio <= 0.25, catches unquantized wgrad operands the rel-L2 bound missed); outlier fraction reported only; determinism columns

Co-authored-by: Sealeen Zhang <[email protected]>
…16) shuffle_weight layout from the TE cast; native GEMM weights skip the separate shuffle pass (bit-identical)

Co-authored-by: Sealeen Zhang <[email protected]>
…ton specialization), skipping per-call argument binding; same compiled kernel

Co-authored-by: Sealeen Zhang <[email protected]>
…anyway (y = on @ w_o^T at its 3 sites, the unfused gate's f_b output), dropping fp32 round trips; bit-identical

Co-authored-by: Sealeen Zhang <[email protected]>
…d / out_cols) replaces the zero-padding copies of tokens and ragged dims; bit-identical to the padded cast

Co-authored-by: Sealeen Zhang <[email protected]>
… (opened by the layer for is_first_microbatch calls); drop_weight_cache / mxfp8_weight_cache_bytes; remove the version-validated AITER_MXFP8_WEIGHT_CACHE opt-in, which cannot see param.data / buffer updates

Co-authored-by: Sealeen Zhang <[email protected]>
…_weight_cache_invalidator, invalidate_weight_caches); MXFP8 weight operands cached across calls only under kda_weight_reuse(), dropped by a registered (weights, anchors) invalidator; test_kda_mxfp8_weight_cache

Co-authored-by: Sealeen Zhang <[email protected]>
…after a constexpr early return was still compiled and broke TE 2-D block scales)

Co-authored-by: Sealeen Zhang <[email protected]>
…ve_fwd intermediates)

Co-authored-by: Sealeen Zhang <[email protected]>
…r gemm_precision=mxfp8 (its NotImplemented falls through to the MXFP8 recipe) instead of being replaced; fixes test_kda_weight_cache_update under AITER_KDA_GEMM_PRECISION=mxfp8

Co-authored-by: Sealeen Zhang <[email protected]>
cursor Bot pushed a commit that referenced this pull request Nov 26, 2026
…raded self-check, non-aborting self-checks; #8744 13275a2) into kda-integration

Conflicts in op_tests/test_kda_fused_layer_bwd.py:
- test_kda_save_fwd_matches_recompute takes #8739's new signature
  (chunk_bwd=); #8754's config_name_of and #8757's MXFP8 tests are kept
- main(): #8739's non-aborting self-checks; #8757's
  test_kda_mxfp8_weight_cache joins them the same way (logged and recorded
  in SELF_CHECK_FAILURES instead of raising)

Co-authored-by: Sealeen Zhang <[email protected]>
cursoragent and others added 2 commits November 26, 2026 13:20
…yer, block and piece tables grade against the oracle with emulated MXFP8 GEMMs (layer-dtype input) by the noise ratio (vs the fused bf16 layer) instead of bf16 tolerances

Co-authored-by: Sealeen Zhang <[email protected]>
…ue self-checks); the mxfp8 weight-cache self-check reports the same way

Co-authored-by: Sealeen Zhang <[email protected]>
cursor Bot pushed a commit that referenced this pull request Nov 26, 2026
… into kda-integration

Conflicts in op_tests/test_kda_fused_layer_bwd.py: #8757's side of the
MXFP8 block was empty (already merged), HEAD kept; its main() now has the
same non-aborting mxfp8 weight-cache self-check, its wording taken.

Co-authored-by: Sealeen Zhang <[email protected]>

This branch has not been deployed

No deployments
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.

2 participants