Batch-invariant MXFP8, MXFP4 and NVFP4 quantization
Experimental. cudnn.ops.mxfp8_quantize_forward, cudnn.ops.mxfp4_quantize_forward and
cudnn.ops.nvfp4_quantize_forward quantize the rows of a bf16, fp16 or fp32 tensor to MXFP8, MXFP4 and NVFP4 with one
declared recipe each. The codes and the UE8M0 scale bytes of a row
depend on that row alone: they are the same for any batch size, row position, tile configuration and SM count. Use them
where rollout and training must produce the same quantized operands, for example ahead of a
batch-invariant block-scaled matmul.
The output contract is FlashInfer’s mxfp8_quantize(..., batch_invariant=True), mxfp4_quantize(..., batch_invariant=True)
and nvfp4_quantize(..., batch_invariant=True), and the bytes are the same. The functions need CuTe DSL >= 4.8.0 and run on compute capability 10.0, 10.3 and 10.7;
compute capability 10.7 also needs a DSL that compiles with CUDA 13.4 or newer. A DSL outside these bounds raises
ImportError naming the version before any kernel is compiled.
MX recipe
The recipe is Transformer Engine’s MX recipe.
NVFP4 recipe
gs is the global scale. The per-tensor class takes it as an input. The per-token class takes the inverse base scale
g (Transformer Engine: 1 / (448 * 6)), derives each row’s global scale from the row’s amax, and returns it:
Use
Each function returns a TupleDict: it unpacks as a tuple in the order below and its fields are also available by name
(out["sf"]).
inputhas shape(..., K)withKa positive multiple of 32 (16 for NVFP4) and at most 131072 bytes per row. The leading dimensions are flattened to rows. An input that cannot be viewed as unit-stride rows, a row stride or a base address that is not a multiple of 16 bytes is copied first; the copy changes placement, not values.sf_layoutis"128x4"(rows padded to 128 and scale columns to 4, the layout block-scaled matmuls read),"8x4"(rows padded to 8, columns to 4) or"linear"(M x K / 32, orM x K / 16for NVFP4). Padding bytes are zero. FlashInfer’sSfLayoutmembers are accepted as well.global_scaleis a Python float or a one-element tensor. A tensor on another device is copied to the input’s device.enable_pdl=Nonefollows the device: programmatic dependent launch on compute capability 9.0 and newer.- The kernel runs on the current torch stream of
input.deviceand is compiled for that device’s architecture, whichever device is current. - Inputs outside the contract raise: a dtype other than bf16, fp16 or fp32, a
Kthat is not a multiple of 32, a CPU tensor, an unknown layout, a global scale that is not a scalar or an unsupported architecture. Nothing falls back to another implementation. - The functions take two keyword-only arguments for tests,
configandsm_count, that force a member of the enumerated configuration space or another part’s dispatcher pick. The bytes never depend on them.
Guarantees
On the same GPU architecture and software stack, a row with identical values gives identical bytes alone, packed with
other rows, reordered, duplicated or padded to a tile, and across repeated launches. The tests compare raw bytes, never
a tolerance. They also compare against an independent torch implementation of each recipe and against FlashInfer’s
batch-invariant quantizers. A Lean model of each recipe and of the kernels’ amax reductions, in
test/python/quantize/batch_invariant/lean/, checks the kernel placements of a set of generated cases against the
recipe’s reduction; a test keeps those cases equal to what the Python specs and schedules emit. See the test guide and the
determinism support matrix.
The kernels in python/cudnn/quantize/batch_invariant/ are adapted from FlashInfer’s flashinfer/batch_invariant/
and keep its Apache-2.0 notice; see LICENSING.md.