Skip to content

vllm.model_executor.layers.fused_moe.routed_experts

Classes:

  • RoutedExperts –

    Container for routed expert weights and execution logic.

RoutedExperts

Bases: PluggableLayer

Container for routed expert weights and execution logic.

This module owns the expert weight parameters (w13_weight, w2_weight, scales, etc.) and handles: - Loading checkpoint weights into parameters - Executing routed experts via quant_method.apply()

Methods:

Attributes:

supports_precomputed_routing property

Whether a monolithic kernel can take vLLM's routing instead.

_flag_padding_for_reload()

Flag the params a direct reload zero-fills whole at begin; decided from the sizes at construction, before post-load processing.

_get_hidden_dim(shard_dim, ndim) staticmethod

Compute the hidden dimension index from the shard (intermediate) dimension and tensor rank.

For 2D weight tensors the two data dims are (0, 1). For 3D tensors with an expert dimension at dim 0, they are (1, 2). shard_dim occupies one of these; the hidden dimension is the other. For 1D tensors (e.g. per-channel scales) returns 0.

_get_quant_method(prefix, quant_config, moe_config)

Helper method to ensure quant_method is never None and of the proper type.

_load_combined_w13_weight_scale(shard_dim, loaded_weight, param, tp_rank)

Load w13 weight scales assuming that w1 weight scales and w3 weight scales are stored in the same loaded_weight tensor.

_load_model_weight_or_group_weight_scale(shard_dim, expert_data, shard_id, loaded_weight, tp_rank, is_scale=False)

Load grouped weight scales for group quantization or model weights

Parameters:

  • shard_dim

    (int) –

    dimension to shard

  • expert_data

    (Tensor) –

    parameter for a particular expert

  • shard_id

    (str) –

    either w1, w2, or w3

  • loaded_weight

    (Tensor) –

    checkpoint weight to load into the param

  • tp_rank

    (int) –

    tensor parallel rank

  • is_scale

    (bool, default: False ) –

    whether padding should use unit scales instead of zero weights.

_map_global_expert_id_to_local_expert_id(expert_id)

Map global expert ID to local expert ID.

_narrow_expert_data_for_padding(expert_data, loaded_weight, hidden_dim, shard_dim=None) staticmethod

Narrow expert_data to match loaded_weight for padded dimensions.

When backends (e.g., DeepEP) round up hidden_size, weight parameters are larger than checkpoint weights. Narrow the padded hidden dimension before copying. Similarly, when padding occurs on the shard (intermediate) dimension (e.g. for MXFP4 GEMM), narrow that dimension as well.

Parameters:

  • expert_data

    (Tensor) –

    The (possibly padded) parameter tensor to narrow.

  • loaded_weight

    (Tensor) –

    The checkpoint weight tensor with original size.

  • hidden_dim

    (int) –

    The dimension index corresponding to hidden_size. Must be non-negative.

  • shard_dim

    (int | None, default: None ) –

    The dimension index corresponding to the shard (intermediate) dimension. Defaults to None.

_orient_fused_weight(fused_weight, is_fused_checkpoint_transposed) staticmethod

Normalise a fused expert tensor to the vLLM weight layout.

build_expert_params_mapping(ckpt_gate_proj_name, ckpt_down_proj_name, ckpt_up_proj_name, num_experts, num_redundant_experts=0, routed_experts_prefix='routed_experts', lora_base_layer_prefix='', lora_base_layer_prefix_on_param_name='', include_fused=False, is_gated=True) staticmethod

Create expert parameter mapping for weight loading with redundant experts.

This mapping handles the physical-to-logical expert ID conversion needed when loading weights with EPLB redundant experts.

Parameters:

  • ckpt_gate_proj_name

    (str) –

    Name of gate projection in checkpoint

  • ckpt_down_proj_name

    (str) –

    Name of down projection in checkpoint

  • ckpt_up_proj_name

    (str | None) –

    Name of up projection in checkpoint

  • num_experts

    (int) –

    Number of logical (non-redundant) experts

  • num_redundant_experts

    (int, default: 0 ) –

    Number of redundant experts

  • lora_base_layer_prefix

    (str, default: '' ) –

    LoRA base_layer. prefix for the weight_name (checkpoint) side

  • lora_base_layer_prefix_on_param_name

    (str, default: '' ) –

    same, for the param_name side. Independent because get_expert_mapping resolves param_name via getattr against this layer's bare w13_weight/w2_weight (no prefix), while make_expert_params_mapping indexes the model-wide params_dict (prefix included).

  • include_fused

    (bool, default: False ) –

    Prepend the fused pre-fused-checkpoint entries

  • is_gated

    (bool, default: True ) –

    Whether w13 holds gate and up (gated activation) or a single up projection. Selects the fused checkpoint layout.

  • routed_experts_prefix

    (str, default: 'routed_experts' ) –

    Prefix of the routed experts submodule

Returns:

forward_modular(x, topk_weights, topk_ids, shared_experts=None, shared_experts_input=None)

Execute routed experts using the quantization method's apply function.

This is called by the runner after router selection (for modular kernels) quant_method.apply() which accesses the weights on this RoutedExperts instance.

Parameters:

  • x

    (Tensor) –

    Input tensor after any transforms

  • topk_weights

    (Tensor) –

    Routing weights from router (for modular kernels)

  • topk_ids

    (Tensor) –

    Selected expert IDs from router (for modular kernels)

  • shared_experts

    (SharedExperts | None, default: None ) –

    The shared experts (if any)

  • shared_experts_input

    (Tensor | None, default: None ) –

    Input for shared experts (if any)

Returns:

forward_monolithic(x, router_logits=None, input_ids=None, x_scale=None)

Execute routed experts using the quantization method's apply function.

This is called by the runner after router selection (for modular kernels) or with router logits (for monolithic kernels). It delegates to quant_method.apply() which accesses the weights on this RoutedExperts instance.

Parameters:

  • x

    (Tensor) –

    Input tensor after any transforms

  • router_logits

    (Tensor | None, default: None ) –

    Router logits (for monolithic kernels)

  • input_ids

    (Tensor | None, default: None ) –

    input ids for DeepSeek V4

  • x_scale

    (Tensor | None, default: None ) –

    Scales of x when the caller already quantized it (needs quant_method.accepts_quantized_input)

Returns:

forward_monolithic_routed(x, topk_weights, topk_ids)

Run a monolithic kernel on vLLM's routing, where padding rows are -1 and activate no expert, which the kernel's own routing cannot do.

make_expert_params_mapping(model, ckpt_gate_proj_name, ckpt_down_proj_name, ckpt_up_proj_name, num_experts, num_redundant_experts=0, routed_experts_prefix='routed_experts') staticmethod

Build the expert mapping, detecting the LoRA base_layer. prefix by scanning model's parameters.

Legacy entry point for models that still hand-roll load_weights; the RoutedExperts weight loader uses get_expert_mapping / build_expert_params_mapping instead (which take the prefix directly). See build_expert_params_mapping for the returned tuple format.

_index_expert_mapping(mapping)

Index by logical checkpoint ID, preserving entry order and EPLB replicas.

An empty index keeps the full scan for unsupported mapping names.