Conversation
MXFP4QATMXFP8BlockScaling / MXFP4QATFloat8BlockScaling project weights onto the MXFP4 (E2M1, 1x32 power-of-two scale) grid before the host recipe quantizes them; activations and gradients are untouched. The projection is a fused fake-quantization with three bit-identical implementations (CUDA kernel, CuTe DSL, PyTorch reference) dispatched via NVTE_MXFP4_QAT_IMPL, with an identity-STE gradient. - scale contract matches the TileKernels deployment path (floor 2^-126, cap 2^125); integer-bit amax/scale-exponent derivation and non-FTZ PTX keep results bit-exact under --use_fast_math builds - the rowwise MXFP8 and 128x128 blockwise weight encodings of the projected weight are bitwise lossless (raw payload/scale verified); the columnwise 32x1 encoding is bounded - fix ptx.cuh exp2f expansion of UE8M0 code 0 (2^-127) and code 255 (NaN) - reject MXFP4 QAT loudly on surfaces that bypass the weight-quantization hook (te.ops, Userbuffers, fused grouped MLP, quantized primary weights); project the weight in FSDP2/GTP backward rematerialization; invalidate cached weight workspaces on base<->QAT recipe switches - tests: per-step bitwise losslessness, bf16-exhaustive and fp32 bit-fuzz oracles, RTNE midpoint/threshold vectors, fast-math immunity build, STE, misaligned/non-contiguous inputs, and a TileKernels/CuTe-DSL/CUDA/torch four-way bitwise matrix plus a 24-config e2e backward-override matrix
The MXFP4 grid reaches 6*2^125, far beyond fp16 range, so an fp16 pipeline cannot represent the projected weight or its dequantized form. quantize_weight now raises when a QAT recipe is active and the workspace/activation dtype is fp16 (fp16 weights were already rejected by the projection itself); documented in both recipe docstrings.
- exp2f JIT test now checks all 256 codes bitwise including the code-255 NaN payload (0x7FFFFFFF) - add an end-to-end nvte MXFP8 software-dequantize test with planted extreme scale codes through the real kernel: demands code 0 -> 2^-127 and code 255 -> NaN on a fixed build, and pins the pre-fix wheel behavior (flush to zero / Inf) until then - the bf16-dequant losslessness test auto-detects a fixed build instead of hard-asserting the pre-fix flush
Owner
Author
|
Re-targeting to NVIDIA/TransformerEngine main. |
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.
Overview
Weight-only MXFP4 quantization-aware training for DeepSeek-v4 MoE deployment: weights are projected onto the MXFP4 grid (E2M1 payloads, 1x32 power-of-two UE8M0 scales) before the host recipe quantizes them, so training sees exactly the deployment weight while activations and gradients keep the base recipe untouched.
Two recipes subclass their hosts and reuse the full TE pipeline (quantizers, QTensor layouts, caching,
backward_override):MXFP4QATMXFP8BlockScaling(MXFP8BlockScaling)MXFP4QATFloat8BlockScaling(Float8BlockScaling)(128x128 weight tiles)Design
One hook in
quantize_weightappliesmxfp4_fake_quantize(the fusedbf16 -> mxfp4(row) -> dequantize -> bf16projection) right before the host weight quantizer; everything downstream is stock TE. The projection output is exactly representable in bf16 (E2M1 needs 2 mantissa bits, scales are powers of two), so the handoff is lossless by construction:backward_override=None): bounded re-quantizationbackward_overrideweight semantics (verified by dgrad discriminators through real GEMMs):None-> row+col built from the same projected weight, dgrad consumes col fp8;dequantized-> dgrad dequantizes the stored rowwise encoding (bit-exact projected weight in bf16);high_precision-> dgrad uses the original unprojected parameter. Weight gradients flow through an identity STE to.grad/fusedmain_grad.Scale contract matches the TileKernels deployment path: floor 2^-126, cap 2^125, integer-bit amax/exponent derivation and non-FTZ PTX so results are bit-identical under
--use_fast_mathbuilds.Implementations
Three bit-identical implementations dispatched via
NVTE_MXFP4_QAT_IMPL(auto/cuda/cute_dsl/torch): the CUDA kernel (common/cast/mxfp4/fake_quantize_mxfp4.cuh,tex.mxfp4_fake_quantize), a CuTe DSL kernel, and a PyTorch reference. B200, 8192x8192 bf16: CuTe DSL 59us @ 4.5 TB/s, CUDA 67us @ 4.0 TB/s, torch 3052us.Correctness
28-test suite, all passing on B200, including a four-way bitwise matrix (TileKernels torch reference vs CuTe DSL vs CUDA vs torch reference) over every edge domain (+-0 with sign, 2^-127/2^-128/2^-133 subnormals, satfinite cap, RTNE midpoints, scale-threshold ulp triples, inf/NaN block poisoning, misaligned views) and every quant/dequant hop (fake-quant bf16+fp32, mxfp8 row raw encode/dequant, mxfp8 col, blockwise 128x128 row+col, bf16+fp32 dequant targets), plus all 65536 bf16 bit patterns exhaustively, fp32 raw-bit fuzz, a fast-math dual-build bit-parity gate, an identity-STE gradient check, and a 24-config e2e matrix (Linear+GroupedLinear x 2 recipes x 3 overrides x fused/unfused wgrad).
Cross-backend findings pinned in tests: TileKernels' e4m3 casts use a hardcoded 1e-4 amax floor (its e2m1 path uses the true 6*2^-126 deployment clamp); TE's mxfp8 columnwise representation canonicalizes -0 to +0.
Guards
Surfaces that bypass the weight hook reject QAT recipes loudly (te.ops, Userbuffers fused ops, fused grouped MLP, quantized primary weights); FSDP2/GTP backward rematerialization applies the projection; cached weight workspaces are invalidated on base<->QAT recipe switches; fp16 weights and fp16 activation/dequantize dtypes are rejected (the grid exceeds fp16 range).
Includes the
ptx.cuhexp2fUE8M0 code-0/255 fix (also submitted upstream as NVIDIA#3262).Known limitations