Skip to content

vllm.model_executor.layers.quantization.utils.mxfp4_utils

Functions:

_swizzle_mxfp4(quant_tensor, scale, num_warps=8)

Weight swizzle for mxfp4 moe, used for OAI mxfp4 kernel.

dequantize_mxfp4_e8m0(weight, scale, out_dtype)

Dequantize MXFP4 weights [..., K // 2] (two E2M1 values per byte, the even element in the low nibble) with E8M0 scales [..., K // 32] to [..., K]. Values are scaled in fp32, then cast to out_dtype.

dequantize_mxfp4_moe_weights(w13_weight, w13_scale, w2_weight, w2_scale, out_dtype)

Dequantize MXFP4 MoE weights for online requantization.

Parameters:

  • w13_weight

    (Tensor) –

    [E, N13, K // 2] packed MXFP4 weights.

  • w13_scale

    (Tensor) –

    [E, N13, K // 32] E8M0 scales.

  • w2_weight

    (Tensor) –

    [E, K, I // 2] packed MXFP4 weights.

  • w2_scale

    (Tensor) –

    [E, K, I // 32] E8M0 scales.

  • out_dtype

    (dtype) –

    The dequantized dtype.

Returns:

downcast_to_mxfp(src_tensor, axis, out_quant_tensor=None, out_scale=None, BLOCK_OUT_DIM=128, BLOCK_QUANT_DIM=32)

Convert the src weights to MXFP4. The src weight is quantized along the axis dimension into packed e2m1 values (torch.uint8, two values per byte), so the size of that dimension in the output is half of the logical (unpacked) size.

get_aiter_mx_scale_swizzle()

AITER swizzle_mx_scale arg matching the load-time scale swizzle.

mx_scale_kwargs(scale)

PrecisionConfig weight-scale kwargs: 3.8 uses b_mx_scale/b_microblock_size, 3.5.1/3.6 use weight_scale.

mxfp4_quantize(x)

Quantize a bf16/fp16 tensor to MXFP4 along its last dimension.

Dispatches to the fastest backend available on the current platform:

  • the native XPU custom op on XPU,
  • aiter on ROCm when aiter is installed,
  • and the portable Triton kernel (downcast_to_mxfp) otherwise.

Returns packed FP4 values (uint8, two values per byte) and per-block (group-32) e8m0 scales (uint8).

should_use_cdna4_mx_scale_swizzle()

Whether to use the CDNA4 swizzled scale layout for mxfp4 on gfx950.

CDNA4 swizzle requires BLOCK_K%256==0; at TP>=4 the A8W4 dispatch picks BK<256 tiles for the smaller per-rank shapes, so swizzle must be off. Used by both the weight-load swizzle in _swizzle_mxfp4 and the kernel-argument gate in aiter_mxfp4_w4a8_moe; they must agree.

weight_mx_scale(precision_config)

mxfp4 weight scale from a triton_kernels PrecisionConfig: 3.8 exposes it as b_mx_scale, 3.5.1/3.6 as weight_scale.