Repository navigation
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
Conversation
…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]>
Co-authored-by: Sealeen Zhang <[email protected]>
…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]>
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]>
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]>
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Stacked on #8739. Adds a GEMM precision mode to the Kimi-K3 KDA layer,
kda_layer_fwd/bwd(..., gemm_precision="bf16"|"mxfp8")(orAITER_KDA_GEMM_PRECISION)."mxfp8"runs every projection GEMM in forward and backward the way NVIDIA/Megatron-LM#6872's Kimi-K3 recipes do: Transformer EngineMXFP8BlockScaling(fp8_format=E4M3). It also adds a TE round-up scale mode to aiter's Tritonconvert_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_fp8unset) 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_kerneluses torchao's E8M0 rule,floor(log2(rne_3bit(amax))) - 8. That rule can pick a scale one binade lower than TE'sceil(log2(amax/448)), so the block max clips at 448 (e.g. amax 460 or 230).Recipe implemented (TE
MXFP8BlockScaling, E4M3)float_to_e8m0(amax * fp32(1/448)): the exponent rounded up, clamped to [0, 254], 0 for amax 0. Data = saturating RNE cast ofx * 2^(127-byte).main_grad).mxfp8_epilogue=1fusion 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):mxfp8_quantize,float_to_e8m0) and dequantize/qdq.mxfp8_mm(a, b, out16), with implementationsauto/qdq/emulate.mxfp8_matmul, a differentiable TE-linear emulation used by the torch backend.convert_to_mxfp8(scale_mode="te").aiter/ops/kda_fused_layer.py:MXFP8ProjGemmis installed as theregister_proj_gemmhook for the duration of an"mxfp8"call, and re-installed in backward._proj_gemmnow passesopsto the hook and exposes_proj_gemm_default._ref_linearand runs QKV as one projection, as Megatron'sin_projdoes.mxfp8_epiloguefusion switch.LAST_PIECES["gemm"]records which implementation ran each GEMM.aiter/ops/triton/.../quant_mxfp8.py:convert_to_mxfp8(..., scale_mode="torchao"|"te")and aTE_SCALEconstexpr. TE mode takes the portable path; ASM and SR are rejected.Which GEMMs run natively vs emulated (
AITER_MXFP8_GEMM=auto, gfx950):gemm_a8w8_mxfp8_bpreshuffle(1x32 E8M0, bf16 out = TE's output dtype)mxfp8_epilogue=1gate / sigmoid GEMMsPerformance (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
Documentation
Dependencies
Breaking Changes
None.
gemm_precisiondefaults to"bf16", andconvert_to_mxfp8defaults toscale_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:
mxfp8_te_reference.pybit-exactly (scale bytes and E4M3 data, rowwise and columnwise, fp32 and bf16, amax 460/230/448, zero and subnormal blocks).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.GPU (one card, 0a68c40):
test_convert_mxfp8TE/default-scale tests pass.w_qelements (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.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.