Skip to content

vllm.model_executor.models.transformers.fusers.moe

MoE fuser: run an HF MoE block's gate, experts and shared experts in FusedMoE.

Classes:

  • MoEBlockFuser –

    Fuser for MoE block experts, gate and shared_experts (optional).

  • SharedExpertMLP –

    Wraps an HF shared expert, applying the output gating it is paired with.

MoEBlockFuser dataclass

Fuser for MoE block experts, gate and shared_experts (optional).

Methods:

  • gate –

    Rebuild the HF gate as a GateLinear for vLLM's fused MoE.

  • match –

    Matches a block computing experts(x, ids, weights) [* c] [+ shared(x)]

  • rewrite_forward –

    Rewrite moe_block.forward to route through vLLM's fused MoE.

  • shared_expert_gate –

    Rebuild the HF shared expert's sigmoid gate, if any, as a

  • shared_experts –

    Build the HF shared expert (and its optional gate)

Attributes:

  • graph (Graph | None) –

    The block's forward with the routed and shared experts replaced by one call

  • scoring_func (str | None) –

    None if routing stays in the Transformers gate.

graph = field(default=None, repr=False, compare=False) class-attribute instance-attribute

The block's forward with the routed and shared experts replaced by one call to the fused MoE.

scoring_func instance-attribute

None if routing stays in the Transformers gate.

_match_experts(graph, experts_name, moe_block) staticmethod

Matches experts(x, ids, weights) with _, weights, ids = gate(x).

Returns the experts call, its hidden states argument, x (that argument without reshapes and casts) and the gate call.

_match_router(gate) staticmethod

Matches topk(score(linear(x))) or score(topk(linear(x))), score being softmax/sigmoid.

Returns the scoring function, the dtype the router computes in, and whether the top-k weights are renormalized (None if the trace was cut short before it could say).

_match_shared_experts(node, x, moe_block) staticmethod

Matches shared(x) or sigmoid(shared_gate(x)) * shared(x).

Returns the shared expert's name and its optional gate's name.

gate(moe_block, prefix, out_dtype=None)

Rebuild the HF gate as a GateLinear for vLLM's fused MoE.

match(moe_block, experts_name, routed_scaling_factor=1.0) classmethod

Matches a block computing experts(x, ids, weights) [* c] [+ shared(x)] with _, weights, ids = gate(x), up to reshapes, casts and nn.Identity.

Anything else in the block's forward, e.g. a norm before or after the experts, is kept around the fused MoE.

Parameters:

  • moe_block

    (Module) –

    The MoE block.

  • experts_name

    (str) –

    The name of the experts in moe_block.

  • routed_scaling_factor

    (float, default: 1.0 ) –

    The scale vLLM routing applies to the experts' output, which c must equal.

Returns:

  • MoEBlockFuser | None –

    A fuser if more than the experts can run in vLLM, else None.

rewrite_forward(moe_block)

Rewrite moe_block.forward to route through vLLM's fused MoE.

shared_expert_gate(moe_block, prefix)

Rebuild the HF shared expert's sigmoid gate, if any, as a ReplicatedLinear.

shared_experts(moe_block, prefix)

Build the HF shared expert (and its optional gate) as a SharedExpertMLP for vLLM's fused MoE.

SharedExpertMLP

Bases: Module

Wraps an HF shared expert, applying the output gating it is paired with.

_erase_unused(graph, modules)

Erase unused nodes, keeping calls to child modules not in modules and in-place writes, which may have side effects.

_forced_dtype(nodes)

The floating dtype nodes cast to, if any.

Computations that must run in higher precision say so in their forward (e.g. hidden_states.type(torch.float32)), so the dtype is readable from the graph even when the config does not name it.

_is_scalar_gate(module)

A linear projecting to a single logit (the shared-expert sigmoid gate).

_only_feeds(source, node)

Is source used only to compute node, along the arguments _unwrap and _scale follow, with every value on the way (and node) used once?

_own_returns(node)

return statements in node's own scope, not in nested functions.

_reaches(node, key)

Returns the set of nodes reachable from node by following key edges.

_reads_input(node, x, moe_block)

Is node a child module of moe_block called on just x?

_renormalizes(topk)

Are the top-k weights divided by their own sum?

Every HF router spells renormalization as a division by a sum over the selection, optionally offset by an epsilon.

_returns_tuple(cls)

Does cls.forward() return a tuple?

_run_moe(experts, hidden_states)

Run experts: MoERunner, which also handles routing and any shared experts.

_scale(node)

(c, x) if node is x * c, c * x or x / c for a constant c.

_unwrap(node, moe_block)

Strip reshapes, dtype casts and nn.Identity calls.