Skip to content

vllm.model_executor.layers.quantization.utils.mxfp8_utils

Functions:

_flashinfer_mxfp8_quantize() cached

FlashInfer's MXFP8 quantize when this device uses it, else None.

_mxfp8_e4m3_quantize_torch(x, is_sf_swizzled_layout=False)

Naive MXFP8 quantization. For each block of 32 elements along the last dimension, compute a shared e8m0 scale that fits the block-wise amax into the finite float8_e4m3fn range, and quantize each element to float8_e4m3fn.

Returns (quantized_values [same shape, fp8], scales uint8). Scale shape depends on is_sf_swizzled_layout: False -> [..., K//32] (row-major 2D) True -> [flat swizzled 1D]

_mxfp8_e4m3_quantize_triton(x)

Fused 2D MXFP8 quant (non-swizzled, row-major [M, K//32] scales).

_mxfp8_quant_triton_kernel()

Lazily-built Triton kernel: per-32-block E8M0 scale + FP8-E4M3 quant.

Fuses what _mxfp8_e4m3_quantize_torch does in several elementwise passes into one launch. Each program handles [BLOCK_M, BLOCK_K], i.e. BLOCK_K / 32 MX blocks of BLOCK_M rows, so rows are read in long runs.

dequant_mxfp8_to_bf16(x, scales)

Dequantize MXFP8 tensor to BF16.

mxfp8_e4m3_quantize_fake(x, is_sf_swizzled_layout=False, alignment=0)

Fake implementation for torch.compile tracing.

mxfp8_e4m3fn_to_e4m3fnuz(w, scale)

Reinterpret MXFP8 e4m3fn weights as e4m3fnuz without changing values.

The same bits read as fnuz are worth half, so each E8M0 exponent goes up by one. 0x80 is -0 in fn but NaN in fnuz, so it becomes 0. The weight is updated in place. Exponents 254 and 255 cannot be bumped and become 0xFF (NaN); real checkpoints never use them.

requant_block_fp8_to_ue8m0(weight, scale)

Requantize block-FP8 weights to E4M3 with one UE8M0 scale per 1x32.

The SM100 MegaMoE feeds its scales to the block-scaled MMA, so they have to be powers of two, one per row and 32 columns. A checkpoint with float32 scales per block (e.g. 128x128) is rounded once more to get there.

Parameters:

  • weight

    (Tensor) –

    (n, mn, k) E4M3 values.

  • scale

    (Tensor) –

    (n, mn / block_m, k / block_k) float32 dequantization scales (real = weight * scale).

Returns:

  • Tensor –

    (weight, sf): the requantized E4M3 weights and their UE8M0 scale

  • Tensor –

    bytes, (n, mn, k / 32) uint8.

swizzle_mxfp8_scale(sf, M, K)

Swizzle MXFP8 scales from row-major 2D to F8_128x4 layout.