Skip to content

vllm.model_executor.layers.quantization.utils.fp8_utils

Functions:

_maybe_pad_fp8_weight(weight)

Pad the weight tensor. This is an optimization on ROCm platform, which can benefit from tensors located far enough from one another in memory

_per_token_group_quant_fp8(y_ptr, y_q_ptr, y_s_ptr, group_size, y_num_columns, y_row_stride, eps, fp8_min, fp8_max, use_ue8m0, BLOCK)

A Triton-accelerated function to perform per-token-group quantization on a tensor. This function converts the tensor values into float8 values.

_per_token_group_quant_fp8_colmajor(y_ptr, y_q_ptr, y_s_ptr, group_size, y_num_columns, y_row_stride, y_s_col_stride, eps, fp8_min, fp8_max, use_ue8m0, BLOCK)

A Triton-accelerated function to perform per-token-group quantization on a tensor. This function converts the tensor values into float8 values.

_silu_mul_per_token_group_quant_fp8_colmajor(y_ptr, y_q_ptr, y_s_ptr, expert_ends_ptr, M, N, y_s_col_stride, eps, clamp_limit, alpha, beta, fp8_min, fp8_max, use_ue8m0, HAS_CLAMP, GROUP_SIZE, BLOCK_M, BLOCK_N, EXPERT_ALIGNMENT)

Each thread block (BLOCK_N) computes [BLOCK_M, GROUP_SIZE] act-mul outputs. Then the thread block quantizes the [BLOCK_M, GROUP_SIZE] block of values and fills the outputs tensors at the right positions.

_upcast_e8m0_to_fp32(scale)

Upcast E8M0 (exponent-only) scale to float32.

E8M0 stores only the 8-bit biased exponent (bias=127). To convert to float32 we place those 8 bits into the exponent field of an IEEE-754 float32 (bits 23-30) with sign=0 and mantissa=0.

_w8a8_triton_block_scaled_mm(A, B, C, As, Bs, M, N, K, group_n, group_k, stride_am, stride_ak, stride_bk, stride_bn, stride_cm, stride_cn, stride_As_m, stride_As_k, stride_Bs_k, stride_Bs_n, BLOCK_SIZE_M, BLOCK_SIZE_N, BLOCK_SIZE_K, GROUP_SIZE_M, A_LARGE, B_LARGE, C_LARGE)

Triton-accelerated function used to perform linear operations (dot product) on input tensors A and B with block-wise quantization, and store the result in output tensor C.

create_fp8_input_scale(output_partition_sizes, weight_loader)

Create input scale parameter for static activation quantization.

create_fp8_scale_parameter(parameter_type, output_partition_sizes, input_size_per_partition, block_size, weight_loader, scale_dtype=None)

Create scale parameter based on quantization strategy.

create_fp8_weight_parameter(output_size_per_partition, input_size_per_partition, weight_loader)

Create FP8 weight parameter.

dequant_block_fp8(weight, scale_inv, block_size, out_dtype=torch.float32)

[..., N, K] fp8 x [..., ceil(N/bn), ceil(K/bk)] block scales -> out_dtype.

Leading dimensions (e.g. the expert dimension) are batched. The product is computed in fp32, out_dtype only controls the final cast.

dequantize_fp8_moe_weights(w13_weight, w13_scale, w2_weight, w2_scale, *, weight_key, num_w13_shards, weight_block_size, static_input_scales, out_dtype)

Dequantize FP8 MoE weights for online requantization.

Parameters:

  • w13_weight

    (Tensor) –

    [E, num_w13_shards * I, K] fp8 weights.

  • w13_scale

    (Tensor) –

    Per weight_key, [E, num_w13_shards * ceil(I / bn), ceil(K / bk)] (block), [E, num_w13_shards] (tensor, per expert and shard) or [E, num_w13_shards * I] / [E, num_w13_shards * I, 1] (channel).

  • w2_weight

    (Tensor) –

    [E, K, I] fp8 weights.

  • w2_scale

    (Tensor) –

    Per weight_key, [E, ceil(K / bn), ceil(I / bk)] (block), [E] (tensor) or [E, K] / [E, K, 1] (channel).

  • weight_key

    (QuantKey) –

    The weight quantization key, per-tensor, per-channel or block.

  • num_w13_shards

    (int) –

    2 for gated (w1, w3), else 1. Shards of a fused w13 with a single tensor scale are handled through the scale shape.

  • weight_block_size

    (Sequence[int] | None) –

    The [bn, bk] block shape, required for block scales.

  • static_input_scales

    (bool) –

    Whether the checkpoint has static activation scales, which are not supported: w1 and w3 share one input-scale slot, which the layerwise loader counts twice.

  • out_dtype

    (dtype) –

    The dequantized dtype.

Returns:

get_fp8_block_weight_scale(layer)

Return the block-FP8 weight scale for supported quant methods.

get_w8a8_block_fp8_configs(N, K, block_n, block_k) cached

Return optimized configurations for the w8a8 block fp8 kernel. The return value will be a dictionary that maps an irregular grid of batch sizes to configurations of the w8a8 block fp8 kernel. To evaluate the kernel on a given batch size bs, the closest batch size in the grid should be picked and the associated configuration chosen to invoke the kernel.

input_to_float8(x, dtype=None)

This function quantizes input values to float8 values " "with tensor-wise quantization.

per_token_group_quant_fp8(x, group_size, eps=1e-10, dtype=None, column_major_scales=False, tma_aligned_scales=False, out_q=None, use_ue8m0=None)

Function to perform per-token-group quantization on an input tensor x. It converts the tensor values into signed float8 values and returns the quantized tensor along with the scaling factor used for quantization.

Parameters:

  • x

    (Tensor) –

    The input tensor with ndim >= 2.

  • group_size

    (int) –

    The group size used for quantization.

  • eps

    (float, default: 1e-10 ) –

    The minimum to avoid dividing zero.

  • dtype

    (dtype | None, default: None ) –

    The dtype of output tensor. Note that only torch.float8_e4m3fn is supported for now.

  • column_major_scales

    (bool, default: False ) –

    Outputs scales in column major.

  • tma_aligned_scales

    (bool, default: False ) –

    Outputs scales in TMA-aligned layout.

  • out_q

    (Tensor | None, default: None ) –

    Optional output tensor. If not provided, function will create.

  • use_ue8m0

    (bool | None, default: None ) –

    If True, round each scale down to a power of two (UE8M0 format). None defers to the platform default.

Returns:

  • Tensor –

    tuple[torch.Tensor, torch.Tensor]: The quantized tensor and the

  • Tensor –

    scaling factor.

per_token_group_quant_fp8_packed_for_deepgemm(x, group_size, eps=1e-10, use_ue8m0=None, out_q=None)

FP8 per-token-group quantization for DeepGEMM.

Returns:

  • tuple[Tensor, Tensor] –

    (x_q, x_s_packed) x_q: FP8 activations, same shape as x. x_s_packed: Int32 tensor with logical shape [mn, ceil(num_groups_per_row / 4)], laid out with TMA-aligned stride along the packed-K dimension

process_fp8_input_tensor_strategy_moe(w13_input_scale, w2_input_scale, enable_eplb)

Process moe input scales for tensor-wise quantization strategy.

process_fp8_weight_block_strategy(weight, weight_scale)

Normalize FNUZ if needed and return (N, K) weight (no transpose).

process_fp8_weight_channel_strategy(weight, weight_scale, input_scale=None)

Normalize FNUZ if needed and return (K, N) weight.

process_fp8_weight_tensor_strategy(weight, weight_scale, logical_widths, input_scale=None)

Requantize fused shards to one scale and return (K, N) weight.

process_fp8_weight_tensor_strategy_moe(weight, weight_scales, shard_size, num_experts, is_act_and_mul=True)

Process moe weights for tensor-wise quantization strategy.

requant_weight_ue8m0_inplace(weight, weight_scale, block_size=(128, 128))

Re-quantise weight so that its per-block scaling factors are in the UE8M0 (power-of-two) format expected by the new DeepGEMM kernels inplace.

Parameters:

  • weight

    (Tensor) –

    Block-quantised weight tensor stored in torch.float8_e4m3fn. Expected shape (..., M, K).

  • weight_scale

    (Tensor) –

    Corresponding per-block scale tensor (torch.float32) with shape (..., M // block_size[0], K // block_size[1]).

  • block_size

    (Sequence[int], default: (128, 128) ) –

    2-element iterable [block_m, block_k] describing the block quantisation granularity.

silu_mul_per_token_group_quant_fp8_colmajor(input, output=None, use_ue8m0=None, eps=1e-10, clamp_limit=None, group_size=128, alpha=1.0, beta=0.0, *, expert_ends=None, expert_alignment=0)

Gated activation + block-fp8 quant. alpha/beta select the gate (silu: alpha=1, beta=0; swigluoai: alpha, beta from config).

Optional expert endpoints follow DeepGEMM's prefix-sum layout. Padding values and scales are left unwritten and must not be consumed as live rows.

silu_mul_quant_fp8_packed_triton(input, group_size=128, output_q=None, clamp_limit=None, alpha=1.0, beta=0.0, *, expert_ends=None, expert_alignment=0)

Fuse activation and FP8 quantization, optionally skipping expert padding.

expert_ends uses DeepGEMM's prefix-sum layout: each entry is the exclusive end of an expert's live rows, including earlier alignment gaps. An expert starts at the preceding end rounded up to expert_alignment. Padding outputs are left unwritten and must not be consumed as live rows. Without expert_ends, the original dense launch configuration is used and the metadata loads are compiled out.

validate_fp8_block_shape(layer, input_size, output_size, input_size_per_partition, output_partition_sizes, block_size)

Validate block quantization shapes for tensor parallelism.

validate_fp8_block_shape_moe(intermediate_size_per_partition, block_size)

Validate fused MoE block quantization shapes for tensor parallelism.

w8a8_triton_block_scaled_mm(A, B, As, Bs, block_size, output_dtype=torch.float16)

This function performs matrix multiplication with block-wise quantization. It takes two input tensors A and B with scales As and Bs. The output is returned in the specified output_dtype.

Parameters:

  • A

    (Tensor) –

    The input tensor, e.g., activation.

  • B

    (Tensor) –

    The input tensor, e.g., weight.

  • As

    (Tensor) –

    The per-token-group quantization scale for A.

  • Bs

    (Tensor) –

    The per-block quantization scale for B.

  • block_size

    (list[int]) –

    The block size for per-block quantization. It should be 2-dim, e.g., [128, 128].

  • output_dtype

    (dtype, default: float16 ) –

    The dtype of the returned tensor.

Returns:

  • Tensor –

    torch.Tensor: The result of matmul.