Skip to content

vllm.model_executor.layers.quantization.utils.nvfp4_utils

Functions:

_csf32_blocks(rows, cols) cached

Origin, width and page of each 32x2 block in storage order, and the flat [rows, cols] position of each code.

csf_weights_transform(encodings)

Pair CSF scale streams back into canonical E4M3 scale tensors.

The returned transform is lazy over a (name, tensor) weight stream: tensors named <name>.<stream> for a declared encoding are buffered per scale and, once the encoding's stream set completes, replaced by the decoded <name> tensor; everything else passes through unchanged.

decode_csf_scale(encoding, streams)

Rebuild one [rows, cols] float8_e4m3fn block-scale matrix.

nvfp4_weight_padding_bytes(layer)

Compute K-dim padding (packed bytes) of an NVFP4 weight, Aligned with pad_nvfp4_weight_for_cutlass above.

pad_nvfp4_activation_for_cutlass(x_fp4, weights_padding_bytes)

Pad packed FP4 activations to match the K-dimension padding applied to weights. The padding is in bytes (tensor dimension), not FP4 elements.

pad_nvfp4_weight_for_cutlass(weight, alignment=32)

Pad packed NVFP4 weights so that both N (rows) and K (columns) satisfy the alignment constraints required by CUTLASS / FlashInfer FP4 kernels.

CUTLASS FP4 kernel requires both K and N matrix dimensions to be divisible by 32 for aligned memory access and efficient tensor core operations.

slice_nvfp4_output(out, output_size)

Slice the output tensor to remove padding in N dimension if weight was padded.

swizzle_blockscale(scale)

Pad and block-interleave the FP4 block-scales so that they match the data layout expected by the CUTLASS / FlashInfer kernels.

Parameters:

  • scale

    (Tensor) –

    The FP4 block-scale tensor to swizzle.

Returns:

  • Tensor –

    The swizzled tensor with the same logical shape as scale.