Skip to content

vllm.model_executor.layers.quantization.modelopt

Classes:

Functions:

CkptCtx dataclass

Per-checkpoint facts a QuantKey cannot carry.

FormatScheme

Optional per-format hooks that compose around the QuantKey schemes.

Extension seam for residue that belongs to a format as a whole rather than a single QuantKey — an extra parameter the weight/activation schemes don't cover, or a tweak before/after their process. All hooks default to no-ops; most formats need none (they are a pure (weight, activation) key pair). To add one: subclass, override the hooks you need, and return an instance from the format's resolve() branch — no change to ModelOptLinearMethod itself.

Methods:

  • apply –

    Wrap the kernel's forward. kernel_apply(layer, x, bias) -> Tensor.

  • extra_weights –

    Register format-level params (after the key schemes' weights).

  • kernel_weight_shape –

    Return the weight shape the selected kernel will receive.

  • post_process –

    Run after the key schemes' process, before the kernel's.

  • pre_process –

    Run before the key schemes' process.

apply(layer, x, bias, kernel_apply)

Wrap the kernel's forward. kernel_apply(layer, x, bias) -> Tensor.

Default: delegate unchanged. A format whose residue lives at compute time (e.g. run the GEMM on a padded weight then slice the output back to its logical width, #53132-style) overrides this and calls kernel_apply in the middle.

extra_weights(layer, shapes, ctx, wl)

Register format-level params (after the key schemes' weights).

kernel_weight_shape(layer)

Return the weight shape the selected kernel will receive.

post_process(layer)

Run after the key schemes' process, before the kernel's.

pre_process(layer)

Run before the key schemes' process.

KDynamicNoParam

Bases: QuantKeyScheme

Dynamic activation with no stored scale (W8A8): quantized at runtime in the kernel. NOT the same as activation=None (weight-only) — init_fp8 needs a non-None activation key. Activation-role only. Serves the fp8 per-token, fp8 per-block, and mxfp8 dynamic activation keys.

KFp8Block128

Bases: QuantKeyScheme

128x128 block-static FP8 weight ('PbWo'). Weight-role only. ModelOpt exports the scale 4-D [out_blk,1,in_blk,1]; process squeezes to 2-D. No transpose (block kernel keeps [out,in]).

KFp8StaticChannel

Bases: QuantKeyScheme

Per-channel static FP8 weight (the 'PcPt' weight). Weight-role only — there is no static per-channel activation today.

KFp8StaticTensor

Bases: QuantKeyScheme

Plain per-tensor static FP8 — bivalent: serves BOTH the weight slot and the activation slot (W8A8). One key in both QuantSpec slots.

KInt8Static

Bases: QuantKeyScheme

Symmetric int8 weight with a scale per ctx.group_size input elements (INT8_PB_WO). Weight-role only.

The checkpoint stores int8 [out, in]; the loader repacks it into the WNA16 kernels' layout: uint8b128 (q + 128), four per int32 along the input dim.

KMxfp8Static

Bases: QuantKeyScheme

MXFP8 weight: fp8-e4m3 values + per-32-block e8m0 (uint8) scale. Weight-role only. process is validate-only plus an idempotency guard.

KNvfp4Dynamic

Bases: QuantKeyScheme

NVFP4 activation scheme (W4A4). Has a static global input scale on disk; the per-group scale is computed at runtime inside the kernel.

KNvfp4Static

Bases: QuantKeyScheme

NVFP4 weight scheme (W4A4 and W4A16 share it). Weight-role only today.

ModelOptFp8Config

Bases: ModelOptQuantConfigBase

Config class for ModelOpt FP8.

ModelOptFp8MoEMethod

Bases: FusedMoEMethodBase

MoE method for ModelOpt FP8. Supports loading FP8 checkpoints with static weight scale and activation scale.

Parameters:

ModelOptInt8Config

Bases: ModelOptQuantConfigBase

Config class for ModelOpt INT8_PB_WO (per-block INT8 weight-only).

ModelOptInt8PbWoMoEMethod

Bases: FusedMoEMethodBase

INT8_PB_WO routed experts on the shared WNA16 MoE oracle.

Each expert loads ModelOpt's int8 weight and its weight_scale; the weights pack on load into the oracle's layout (uint8b128, four per int32 along K). The steps mirror a per-QuantKey MoE scheme (setup in __init__, create_weights, process_weights_after_loading, get_fused_moe_quant_config), so this maps onto one for kInt8Static.

ModelOptKVCacheMethod

Bases: BaseKVCacheMethod

Supports loading kv-cache scaling factors from FP8 or NVFP4 checkpoints.

ModelOptLinearMethod

Bases: LinearMethodBase

Generic, format-agnostic ModelOpt linear method. Holds a weight scheme + an activation scheme (from the QuantSpec pair) and an optional FormatScheme, runs a fixed lifecycle, selects the kernel from the pair, and applies.

Registered for weight_loader_v2 (like every ModelOpt linear method) so the BasevLLMParameter params route through the v2 fused loader, not the legacy shape-assert path.

Methods:

dequantize_weight(layer)

Reconstruct serialized weights for online requantization.

ModelOptMixedPrecisionConfig

Bases: ModelOptQuantConfigBase

Config class for ModelOpt MIXED_PRECISION.

Supports checkpoints where different layers use different quantization algorithms (e.g., FP8 for dense layers and NVFP4 for MoE experts). The per-layer algorithm is specified in the quantized_layers dict inside config.json's quantization_config (preferred) or the legacy hf_quant_config.json.

Methods:

_int8_pb_wo_experts_group_size(prefix, quant_algo)

The group size of INT8_PB_WO routed experts, or None if they are not.

One kernel serves every expert and projection of a MoE layer, so if any of them is INT8_PB_WO, all must be, with one group size.

_resolve_layer_field(prefix, field)

Look up a quantized_layers field for a vLLM-side layer prefix.

Tries three strategies in order: 1. Direct lookup in quantized_layers. 2. Packed/fused-layer lookup (unfuse via packed_modules_mapping). 3. Prefix-based lookup for RoutedExperts (any child key starts with prefix + ".").

Returns the field's value (upper-cased if a string), or None if the prefix is not found or its entry lacks the field.

_resolve_quant_algo(prefix)

Look up the upper-cased quant_algo for a vLLM-side layer prefix, or None if the prefix is not found.

get_checkpoint_weight_mapper()

Pair CSF scale streams, on top of the usual name mappings.

get_quant_method(layer, prefix)

Return quantize-method based on layer.

ModelOptMxFp8Config

Bases: ModelOptQuantConfigBase

Config class for ModelOpt MXFP8.

Methods:

routed_expert_weight_key()

The weight format the routed experts are stored in.

MXFP8, or NVFP4 (moe_quant_algo: NVFP4).

ModelOptMxFp8FusedMoE

Bases: FusedMoEMethodBase

FlashInfer TRTLLM MXFP8 block-scale MoE for ModelOpt checkpoints.

_check_weight_dtypes(layer) staticmethod

Validate weight and scale dtypes before processing.

_dequant_mxfp8_weights_to_bf16(layer)

One-time MXFP8->BF16 weight dequant for the emulation path.

On devices without a native MXFP8 MoE kernel (e.g. gfx942 / MI300), Mxfp8EmulationTritonExperts otherwise dequantizes every expert weight to BF16 on every forward step -- the dominant cost (conc1 ~1.3 tok/s). Doing the dequant once here and replacing the MXFP8 parameters with BF16 makes the MoE run exactly like a plain BF16 checkpoint (full precision, no per-step dequant); SwiGLU-OAI is still applied by the experts' activation() override. The MXFP8 weights are freed by replace_parameter (BF16 is 2x their size; the small E8M0 scale tensors are left in place, unused).

ModelOptNvFp4Config

Bases: ModelOptQuantConfigBase

Config class for ModelOpt FP4.

Methods:

get_checkpoint_weight_mapper()

Pair CSF scale streams, on top of the usual name mappings.

ModelOptNvFp4EmbeddingMethod

Bases: QuantizeMethodBase

W4A16 NVFP4 token embedding.

Weights use the ModelOpt NVFP4 layout: two e2m1 values per byte (low nibble first), one e4m3 scale per 16 values and a per-tensor fp32 scale. A lookup gathers the packed rows and dequantizes only those.

ModelOptNvFp4FusedMoE

Bases: FusedMoEMethodBase

MoE Method for FP4 Quantization.

Parameters:

Methods:

_build_moe_kernel(layer)

Build the modular MoE kernel from the (already in-format) weights.

_restore_padded_moe_dims(layer)

Recover the padded moe_config dims from the exported weights.

dequantize_weight(layer)

Yield (experts, w13, w2) dequantized ModelOpt NVFP4 experts.

process_weights_after_loading(layer)

Convert NVFP4 MoE weights into kernel format and setup the kernel.

uses_weight_scale_2_pattern()

FP4 variants use 'weight_scale_2' pattern for per-tensor weight scales.

ModelOptQuantConfigBase

Bases: QuantizationConfig

Methods:

_extract_modelopt_quant_algo(hf_quant_cfg) staticmethod

Extract upper-cased quant_algo from a modelopt config.

Returns the quant_algo string (upper-cased), or None if the config is not a modelopt config.

is_layer_excluded(prefix)

Check if a layer should be excluded from quantization.

Handles both exact matching (for fused layers) and ModelOpt wildcard matching.

The ModelOpt exclude_modules list is a list of wildcards.

QuantKeyScheme

One scheme per QuantKey. Selected by key content; the base supplies the role from the slot it fills the key into (wkey->WEIGHT, akey->ACT).

Every scheme branches on role explicitly and rejects any role it has not validated — never falling through to the wrong role's registration, which would allocate the wrong parameters and produce garbage silently.

RuntimeDtypes dataclass

Runtime/model dtypes kernels need — format-agnostic.

Shapes dataclass

Layer geometry from create_weights args.

_DropInputScale

Bases: FormatScheme

Interim: register then drop a W4A16 checkpoint's on-disk input_scale.

Kept for backward compat with previously-exported ModelOpt checkpoints.

_Fp8PbWoPartialBlock

Bases: FormatScheme

FP8_PB_WO output width that is not a multiple of 128 (a partial trailing block; e.g. GLM's replicated fused_qkv_a_proj = 2048 + 576 = 2624).

The block kernel needs full 128-blocks, so pad the weight up to the block boundary with zeros before the kernel's post-load, then slice the GEMM output back to the logical width. This is #53132's approach, expressed as the format's compute-time residue on the generic method. The cdiv-sized scale from KFp8Block128 already matches the padded block count, so only the weight rows and the output need adjusting.

_nvfp4_embedding_kernel(ids_ptr, packed_ptr, scale_ptr, global_scale_ptr, out_ptr, hidden, packed_stride, scale_stride, BLOCK)

Gather NVFP4 rows by token id and dequantize them in one pass.

_pack_int8_pb_wo(weight)

An INT8_PB_WO int8 [out, in] weight in the WNA16 kernels' layout: uint8b128 (q + 128), four per int32 along the input dim.

algos_owned_by(config_name)

The linear algos a given config accepts, for its quant_algo validation.

build_linear_method(config, algo, prefix)

Construct the linear method for algo.

Returns a bespoke method if one is registered in LINEAR_METHOD_BUILDERS, else the generic ModelOptLinearMethod built from resolve(algo, config, prefix). Single indirection point shared by the homogeneous and mixed-precision dispatch, so a new format plugs in without editing either get_quant_method.

maybe_fuse_global_scales(layer)

Alpha = input_global_scale * weight_global_scale, presence-gated.

W4A4 has both -> computed; W4A16 has no input_global_scale -> skipped.

resolve(algo, subcfg, prefix)

Turn a sub-config into (QuantSpec, CkptCtx, format_scheme).

Strictly read-only over subcfg: in mixed precision the same sub-config is shared with the MoE method, so writing back would leak a linear-only change into MoE.

select_linear_kernel(spec, layer, rt, weight_shape=None, ctx=None)

Thin family dispatcher on the weight key: nvfp4 / mxfp8 / int8 / fp8.