Skip to content

vllm.model_executor.layers.quantization.utils.quant_utils

This file is used for /tests and /benchmarks.

Classes:

  • GroupShape –

    This class describes the quantization group shape.

  • QuantKey –

    Class for identifying the type of quantization.

  • ScaleDesc –

    Class for describing a single quantization scaling factor.

Functions:

GroupShape

Bases: _GroupShape

This class describes the quantization group shape. It includes static members for common shapes (per-tensor, per-token).

QuantKey dataclass

Class for identifying the type of quantization. dtype: quantized data type scale: scale descriptor scale2: second-level scale descriptor symmetric: symmetric if True, asymmetric if False

ScaleDesc dataclass

Class for describing a single quantization scaling factor. dtype: data type of the scale static: static scale if True, dynamic if False group_shape: group shape of the scale

_dtype_abbr(dtype)

Return a stable short name for torch and ScalarType dtypes.

amax_for_moe_activation_quant(a_scale, enable_eplb)

Reduce a per-expert activation scale to one value shared by all experts.

Note: when EPLB is enabled and since this quantization scales get folded into the per-expert dequantization alphas, we can only ensure that the quant/dequant scales match by having a single quantization scale shared across all ranks.

amax_for_moe_weight_quant(amax, moe_tp_size)

Reduce a per-expert weight amax over the ranks that tensor-shard the MoE weights. That sharding is flattened over DP x PCP x TP, exactly the EP group's span. Under EP moe_tp_size is 1 and each rank owns whole experts, so no reduction is needed.

amax_for_tp_weight_quant(amax, is_sharded)

Reduce a weight amax over the TP group when the weight is sharded along a dim the amax reduces over, so each shard derives the same scale it would as part of the whole weight.

check_activation_quant_fallback(kernel, requested_key, executed_key)

Check a kernel that runs executed_key activations for a layer that requests requested_key.

Running a requested activation quantization as a different one (e.g. Marlin running W8A8 as W8A16) is a fallback. Adding activation quantization to an unquantized request is not.

Returns:

  • str | None –

    Why VLLM_STRICT_QUANT_SCHEME rejects the fallback, or None if the kernel

  • str | None –

    may be used. Warns once for an allowed fallback, so call this only once

  • str | None –

    the kernel is otherwise selectable.

convert_bf16_scales_to_fp8(quant_fp8, scales)

Convert a BF16 scale tensor into the pair of (fp8_scales, channel_scales) expected by W4A8 GEMM kernels.

convert_packed_uint4b8_to_signed_int4_inplace(t)

Convert int4b8 (packed to int32) to signed int4.

get_and_maybe_dequant_weights(layer, out_dtype=torch.float32)

Return layer's unquantized weights in [out, in] layout.

get_fp8_min_max()

Get the min and max values for FP8 quantization.

is_dynamic_per_token_outer_scale(key)

Whether key's second-level scale is computed per token at runtime.

prep_scale_for_group_broadcast(scale, x, group_shape)

Prepare the input quantization scale for group broadcasting.

Parameters:

  • scale

    (Tensor) –

    The scale tensor (scalar or 1D).

  • x

    (Tensor) –

    Target tensor whose shape determines broadcast dimensions.

  • group_shape

    (GroupShape | None) –

    GroupShape to broadcast over.

Returns:

  • Tensor –

    scale reshaped for correct broadcasting.

scaled_quantize(x, group_shape, quant_dtype, compute_dtype=None)

Args: x: Input tensor to quantize group_shape: Shape of quantization groups quant_dtype: Target quantized dtype (e.g., torch.float8_e4m3fn) compute_dtype: Optional dtype for intermediate computations. If None, uses input dtype. Use torch.float32 for higher precision.

weight_amax(weight, *, dim=None, keepdim=False)

max(|weight|), without materializing a full-size abs().