Skip to content

feat(hopper_nvfp4): Blackwell-exact NVFP4 MoE quantizers, routing and GEMMs for sm90 - #30

Draft
nxphi47 wants to merge 14 commits into
mainfrom
feat/hopper-nvfp4
Draft

nxphi47 wants to merge 14 commits into
mainfrom
feat/hopper-nvfp4

Conversation

@nxphi47

@nxphi47 nxphi47 commented Dec 4, 2026 •

Copy link
Copy Markdown

Adds hopper_nvfp4, the sm90 kernels for running a Blackwell NVFP4 MoE inference recipe on Hopper with the same numerics. The recipe is vLLM online nvfp4_per_token MoE + mxfp8 linears on the FlashInfer trtllm-gen backend.

  • Quantizers, bit-identical to the Blackwell ones
    • quantize_weight: vLLM scaled_fp4_quant as used by the online MoE weight quantization.
    • quantize_act: FlashInfer per-token NVFP4 with 4/6 block-scale selection and IEEE division (FLASHINFER_NVFP4_4OVER6=1, FLASHINFER_DISABLE_FP4_QUANT_FAST_MATH=1). It reproduces the module's fast-math (FTZ) FP32 ops with explicit .ftz PTX. Each 16-element block runs on one lane (large inputs) or eight (small inputs, for latency), keeping the reference's sequential error-sum order.
    • quantize_mxfp8 / mxfp8_fake_quant: FlashInfer cute-dsl MXFP8.
    • Only the sm100 cvt.rn.satfinite.e2m1x2 is replaced: a software RNE/satfinite conversion (NaN -> 0x7, -0 -> 0x8, as measured on B200).
  • trtllm-gen MoE routing and finalize, bit-identical
    • route: RenormalizeNaive = top-k of the router logits (ties go to the lower expert), then softmax over the k, with fast-math ex2.approx / div.approx and the warp butterfly sum. Takes BF16 or FP32 logits and, like trtllm-gen's TopKRedType, ranks them in their own type (32-bit keys for BF16, 64-bit for FP32).
    • moe_finalize: FMA accumulation in slot order, using the small-batch even/odd split when FlashInfer would.
  • moe_gemm (CUDA, repack_weight layout): grouped NVFP4 x NVFP4 expert GEMM with weights as the A operand (16 output features per tile) and tokens as B. block_m 8/16/32 (decode) stream the NVFP4 activations through mma.sync; block_m 64/128 (prefill) dequantize the activations to FP16 once (dequant_act) and run WGMMA with 2 warpgroups and a cp.async ring. Both operands are dequantized exactly to FP16 (E2M1 by bit placement: repack_weight positions each weight code's sign and magnitude so every A-fragment register takes one or two shift/mask steps; weight scales with cvt.e4m3x2; E2M1 x E4M3 has at most 6 significant bits), accumulated in FP32, and finished with the prime-rl trainer's NVFP4 epilogue (fused SwiGLU for GEMM1). nvfp4_moe_gemm (Triton, canonical layout) is the reference it is checked against.
  • mxfp8_linear (repack_mxfp8_weight layout): MXFP8 x MXFP8 linear for decode-sized inputs (up to 32 rows), with E4M3 weights at half the bytes of BF16. x goes through quantize_mxfp8, and the GEMM is launched with programmatic dependent launch, so its weight prefetch overlaps the quantizer.
    • One CTA per SM: a producer warp streams 16 KB weight stages into a shared-memory ring with TMA bulk copies, and 16 consumer warps split K, with partials added in fixed warp order.
    • E4M3 products are exact in FP16. Each 32-wide block is summed in FP32 and folded in with its two E8M0 scales, which is exact.

Verification

  • tests/hopper_nvfp4/test_b200_equivalence.py: quantizer outputs match sha256 hashes of outputs captured on a B200 (FlashInfer 0.7.3 / vLLM 0.34). Passes on H200.
  • Run during development, not committed:
    • all 2^32 FP32 inputs of the E2M1 conversion vs an independent reference: 0 mismatches.
    • 10.4M quantized values vs the full B200 vector set (activations, Qwen3.6 layer-0 expert weights, MXFP8 activations and weights): 0 differ.
    • mxfp8_fake_quant vs dequantizing quantize_mxfp8's outputs in PyTorch (89M values, M 1-4096, partial CTAs, block magnitudes from 1e-30 to 1e30, all-zero blocks): 0 differ.
    • GEMM1+SwiGLU, GEMM2 and finalize vs float64 references on the same quantized operands, for T in {1, 8, 64, 147, 148, 512, 2048}: 0 BF16 ulps.
    • CUDA moe_gemm vs float64 reference and vs the Triton GEMM (Qwen3.6 shapes, T 1-4096, every block_m): 0 BF16 ulps.
    • mxfp8_linear vs float64 reference on the Qwen3.6 dense shapes: M 1-32 (the sizes it serves) at most 2 BF16 ulps, M 64 at most 32; the cuBLAS GEMM on the dequantized operands reaches 14 and 64. In both, at most about 1e-4 of the outputs differ at all.
    • route on FP32 router logits vs trtllm-gen on a B200 (vLLM 0.34 with FP32 Qwen3.6 router logits, layers 0/20/39, T 1-4096, plus 8th/9th-place ties and duplicated columns): ids in kernel column order and weights bitwise equal on all 30 captures. The BF16 path is unchanged bitwise.
    • Layer outputs vs a Blackwell engine (same config, real-text prefills), with these kernels on the same inputs: Qwen3.6 routed experts (layers 0 and 20, T 1-2048) bit-identical at T 1 and 8, otherwise 99.3-100% bit-identical with relative error at most 4.6e-4 (the NVFP4 error itself is 0.08-0.19); MXFP8 linears at least 99.95% bit-identical.

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