Skip to content

vllm.model_executor.models.transformers.moe

Transformers modeling backend mixin for Mixture of Experts (MoE) models.

Classes:

MoEMixin

Bases: MixtureOfExperts, Base

Methods:

Attributes:

  • mlp_layers (list[Module]) –

    MoE blocks whose experts were replaced, for the MixtureOfExperts methods.

mlp_layers instance-attribute

MoE blocks whose experts were replaced, for the MixtureOfExperts methods.

_fused_shared_expert_kwargs(fuser, moe_block, prefix, intermediate_size, fused_layers)

FusedMoE kwargs running the block's shared expert as extra routed experts (ROCm AITER), if it can.

Records the layer index in fused_layers under the block's (shared expert, experts) names, relative to the layer.

recursive_replace()

Initialize the MoE layers.

TransformersMoERunner

Bases: MoERunner

Custom MoERunner for the Transformers modeling backend.

Methods:

  • forward –

    In Transformers experts.forward will have this signature, which is why

forward(hidden_states, topk_ids, topk_weights, **kwargs)

In Transformers experts.forward will have this signature, which is why it deliberately does not match MoERunner.forward.

We discard any extra kwargs because we cannot use them here.

TransformersRoutedExperts

Bases: RoutedExperts

_is_mixtral_format = None class-attribute instance-attribute

Does the checkpoint use Mixtral style naming. None until weights are loaded.

_fusable_shared_expert(shared, intermediate_size, num_shared_experts)

Can shared run as num_shared_experts extra routed experts?

It must be a bias-free SiLU GLU as wide as that many routed experts, with the projection names the routed experts' checkpoint mapping loads.

_hf_topk_router(hidden_states, gating_output, topk, renormalize, gate)

Route with a Transformers gate returning (_, topk_weights, topk_ids).

_hf_topk_router_precomputed(hidden_states, gating_output, topk, renormalize, moe_state)

Return (topk_weights, topk_ids) the HF block forward already computed: weights arrive as gating_output, ids via moe_state.

_transformers_moe_forward(hidden_states, topk_ids, topk_weights, layer_name)

Store the topk_ids in the layer and call the actual forward.