vllm.model_executor.models.transformers.fusers
¶
Concrete fusers for the Transformers modeling backend.
Modules:
-
attention–Attention fuser: the module that dispatches to the attention interface.
-
base–Base classes for the Transformers backend fusers.
-
glu–GLU projection fuser:
act(gate(x)) * up(x)-> a fused gate/up linear. -
merged_column–Fuser for parallel linear projections.
-
mla–MLA fuser: adapt a Transformers MLA attention module for vLLM's MLA layer.
-
moe–MoE fuser: run an HF MoE block's gate, experts and shared experts in
FusedMoE. -
packed_qkv–Packed-QKV fuser:
c_attn(x).split((q, kv, kv))-> aQKVParallelLinear. -
qkv–QKV projection fuser:
q(x), k(x), v(x)-> a fused qkv linear + split. -
rms_norm–RMSNorm fuser: detect the norm structurally and swap in vLLM's fused RMSNorm.
Classes:
-
AttentionFuser–A module that dispatches through the Transformers attention interface.
-
BaseFuser–A detected fusion and how to apply it.
-
GLUFuser–Fuser for the GLU pattern
act(gate(x)) * up(x). -
MLAFuser–Fuser for the MLA attention pattern.
-
MergedColumnParallelFuser–Fuser for merging column-parallel linear projections.
-
MoEBlockFuser–Fuser for MoE block
experts,gateandshared_experts(optional). -
PackedQKVFuser–Fuser for attention with q, k and v packed into one projection.
-
QKVFuser–Fuser for the attention QKV pattern
q(x), k(x), v(x). -
RMSNormFuser–Fuser for RMSNorm patterns, including Gemma-style zero-centered weights.
-
RewriteFuser–A fuser that rewrites the module's forward and rebinds it.
-
StackedFuser–A fuser that merges sibling projections into one stacked linear and
AttentionFuser
dataclass
¶
Bases: BaseFuser
A module that dispatches through the Transformers attention interface.
Methods:
-
layer_index–The layer
modulecomputes attention for, if it declares one. -
scale–The softmax scale
modulepasses to the interface, orNone. -
sinks–The per-head sink tensor
modulepasses to the interface, orNone. -
validate–Whether
modulewill actually dispatch to vLLM.
Attributes:
-
s_aux_expr(expr | None) –Source of the
s_aux=the module hands the interface, if it hands one. -
scale_expr(expr | None) –Source of the
scaling=the module hands the interface, if it hands one. -
source_cls(str) –Class of the HF module that dispatches (for logging).
s_aux_expr = None
class-attribute
instance-attribute
¶
Source of the s_aux= the module hands the interface, if it hands one.
scale_expr = None
class-attribute
instance-attribute
¶
Source of the scaling= the module hands the interface, if it hands one.
source_cls
instance-attribute
¶
Class of the HF module that dispatches (for logging).
layer_index(module)
¶
The layer module computes attention for, if it declares one.
scale(module)
¶
The softmax scale module passes to the interface, or None.
sinks(module)
¶
The per-head sink tensor module passes to the interface, or None.
validate(module, vllm_config)
¶
Whether module will actually dispatch to vLLM.
BaseFuser
dataclass
¶
Bases: ABC
A detected fusion and how to apply it.
match analyses the module class once (cached, see get_fusers); fuse
then applies the fusion to an instance in recursive_replace, returning the
module to install in its place.
Methods:
-
fuse–Apply the fusion to an already-validated
module, returning the -
info–A human-readable description of the fusion at
name, for logging. -
match–Match the pattern in
graph, returning a fuser if found. -
orig_to_new_stacked–WeightsMapper.orig_to_new_stackedentries this fuser contributes -
validate–Whether this fuser can be applied to this
moduleinstance.
Attributes:
-
packed_modules_mapping(dict[str, list[str]]) –packed_modules_mappingentries this fuser contributes (none unless -
redefines_forward(bool) –Whether
fusegives the module a different forward,
packed_modules_mapping
property
¶
packed_modules_mapping entries this fuser contributes (none unless
it stacks weights).
redefines_forward = True
class-attribute
¶
Whether fuse gives the module a different forward,
by rewriting its source or by returning a different module in its place.
fuse(module, prefix, vllm_config)
abstractmethod
¶
Apply the fusion to an already-validated module, returning the
module to install in its place (mutated in place, or freshly built).
info(name)
abstractmethod
¶
A human-readable description of the fusion at name, for logging.
match(graph, module)
abstractmethod
classmethod
¶
Match the pattern in graph, returning a fuser if found.
orig_to_new_stacked(prefix)
¶
WeightsMapper.orig_to_new_stacked entries this fuser contributes
(none unless it stacks weights).
validate(module, vllm_config)
abstractmethod
¶
Whether this fuser can be applied to this module instance.
GLUFuser
dataclass
¶
Bases: MergedColumnParallelFuser
Fuser for the GLU pattern act(gate(x)) * up(x).
Methods:
-
update_forward–Replace
act(gate(x)) * up(x)withact(gate_up(x))in source.
_get_act_and_mul(act)
classmethod
¶
Get the ...AndMul equivalent of a Transformers activation module.
_get_act_and_mul_name(act)
staticmethod
¶
Get the name of act if it has an ...AndMul equivalent.
_get_glu_nodes(graph, module)
classmethod
¶
Search graph for the GLU pattern act(gate(x)) * up(x).
_is_act_of_gate(node, module)
classmethod
¶
Is node act(gate(x)) where gate is linear and act is not linear.
update_forward(module)
¶
Replace act(gate(x)) * up(x) with act(gate_up(x)) in source.
MLAFuser
dataclass
¶
Bases: StackedFuser
Fuser for the MLA attention pattern.
Methods:
-
update_forward–Merge
q_a_projandkv_a_projinto one fused down proj then split.
Attributes:
MergedColumnParallelFuser
dataclass
¶
Bases: StackedFuser
Fuser for merging column-parallel linear projections.
Methods:
-
match–Fuse the module's sibling linears when there is only one such group.
-
update_attrs–Replace the module's parallel linears with one merged projection.
-
update_forward–Replace the parallel calls with one merged call and split.
-
validate–Check that the parallel linears are compatible for merging.
Attributes:
-
merged_name(str) –Programmatic name for the merged projection, based on the original names.
merged_name
property
¶
Programmatic name for the merged projection, based on the original names.
match(graph, module)
classmethod
¶
Fuse the module's sibling linears when there is only one such group.
update_attrs(module, prefix, vllm_config)
¶
Replace the module's parallel linears with one merged projection.
update_forward(module)
¶
Replace the parallel calls with one merged call and split.
validate(module, vllm_config)
¶
Check that the parallel linears are compatible for merging.
MoEBlockFuser
dataclass
¶
Fuser for MoE block experts, gate and shared_experts (optional).
Methods:
-
gate–Rebuild the HF gate as a
GateLinearfor vLLM's fused MoE. -
match–Matches a block computing
experts(x, ids, weights) [* c] [+ shared(x)] -
rewrite_forward–Rewrite
moe_block.forwardto 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) –Noneif 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
cmust 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.
PackedQKVFuser
dataclass
¶
Bases: RewriteFuser
Fuser for attention with q, k and v packed into one projection.
Methods:
-
update_forward–Rewrite the split sizes or slice bounds to the per-rank widths.
-
validate–Shapes must be compatible with a head-sharded packed GEMM.
_rewrite_slices(funcdef)
¶
Point the three qkv[..., a:b] slices at the per-rank sections.
_rewrite_split(funcdef)
¶
Point the unique self.<qkv_name>(...)....split((a, b, c), ...) at the
per-rank sections.
_sliced_sizes(node)
staticmethod
¶
(q, kv) if the output of node is only sliced into q, k and v.
The three users must be x[..., :q], x[..., q:q + kv] and
x[..., q + kv:] on node itself, so slices of anything derived from
it (e.g. rotary) never match.
_split_sizes(node)
staticmethod
¶
(q, kv) from a split((q, kv, kv), ...) call, if it is one.
update_forward(module)
¶
Rewrite the split sizes or slice bounds to the per-rank widths.
validate(module, vllm_config)
¶
Shapes must be compatible with a head-sharded packed GEMM.
QKVFuser
dataclass
¶
Bases: StackedFuser
Fuser for the attention QKV pattern q(x), k(x), v(x).
Methods:
-
update_forward–Replace
q(x), k(x), v(x)withqkv(x).split(sizes, -1)in source. -
validate–Shapes must be compatible for a single merged, head-sharded GEMM.
_get_qkv_nodes(graph, module)
classmethod
¶
Search graph for the QKV pattern q(x), k(x), v(x).
update_forward(module)
¶
Replace q(x), k(x), v(x) with qkv(x).split(sizes, -1) in source.
A projection may be guarded by an existence check -- a
self.<proj> is (not) None comparison (e.g. Gemma 4's v_proj) or a bare
truthiness test -- which is folded to its constant value (see
bypass_existence_guard). The calls may sit in different branches, so the
fused GEMM is inserted before the earliest of them, in the innermost block
that dominates all three.
validate(module, vllm_config)
¶
Shapes must be compatible for a single merged, head-sharded GEMM.
RMSNormFuser
dataclass
¶
Bases: BaseFuser
Fuser for RMSNorm patterns, including Gemma-style zero-centered weights.
Methods:
-
fuse–Fuse the matched RMSNorm pattern into a vLLM fused RMSNorm CustomOp.
-
match–Match a graph to the RMSNorm pattern, returning a fuser if found.
Attributes:
-
eps(float | None) –epsitself, when it is not held in an attribute (see_eps_source). -
eps_attr(str | None) –Attribute holding
eps, read per instance infuse. -
source_cls(str) –Class name of the norm this was matched from (for logging).
-
zero_centered(bool) –Gemma-style
(1 + weight)scaling (weight initialised at zero).
eps = None
class-attribute
instance-attribute
¶
eps itself, when it is not held in an attribute (see _eps_source).
eps_attr = None
class-attribute
instance-attribute
¶
Attribute holding eps, read per instance in fuse.
source_cls
instance-attribute
¶
Class name of the norm this was matched from (for logging).
zero_centered
instance-attribute
¶
Gemma-style (1 + weight) scaling (weight initialised at zero).
_eps_from_graph(graph)
staticmethod
¶
Extract the eps constant from the graph, if present.
_eps_source(graph, module)
classmethod
¶
Where fuse should read eps from, resolved once per class.
fuse(module, prefix, vllm_config)
¶
Fuse the matched RMSNorm pattern into a vLLM fused RMSNorm CustomOp.
match(graph, module)
classmethod
¶
Match a graph to the RMSNorm pattern, returning a fuser if found.
RewriteFuser
dataclass
¶
Bases: BaseFuser
A fuser that rewrites the module's forward and rebinds it.
match and update_forward analyse the class once; fuse swaps the
submodules and binds the compiled forward on an instance in place, so it
keeps its class and any attribute the fusion does not consume.
Methods:
-
fuse–Fuse an already-validated
modulein place (seeFusers.__getitem__). -
update_attrs–Replace
module's submodules with their vLLM equivalents. -
update_forward–Rewrite and compile
type(module)'s forward source.
Attributes:
-
fused_forward(Callable) –The compiled rewritten forward, set by
update_forward. -
source_cls(str) –Class of the HF module the fused projections belonged to (for logging).
fused_forward = field(init=False, repr=False)
class-attribute
instance-attribute
¶
The compiled rewritten forward, set by update_forward.
source_cls
instance-attribute
¶
Class of the HF module the fused projections belonged to (for logging).
fuse(module, prefix, vllm_config)
¶
Fuse an already-validated module in place (see Fusers.__getitem__).
Builds the merged submodule and binds the compiled forward.
update_attrs(module, prefix, vllm_config)
abstractmethod
¶
Replace module's submodules with their vLLM equivalents.
update_forward(module)
abstractmethod
¶
Rewrite and compile type(module)'s forward source.
Raises if the source does not admit the rewrite (fusion is then skipped).
StackedFuser
dataclass
¶
Bases: RewriteFuser
A fuser that merges sibling projections into one stacked linear and rewrites the forward to call it.
Methods:
-
orig_to_new_stacked–WeightsMapper.orig_to_new_stackedentries for one fused instance.
Attributes:
-
merged_cls_name(str) –Name of the vLLM class the merged projection becomes (for logging).
-
merged_name(str) –Attribute name of the merged module created by
update_attrs. -
packed_modules_mapping(dict[str, list[str]]) –{merged_name: [projection names]}so quantization can unpack the -
shards(list[tuple[str, ShardId]]) –Each projection's original name and its shard id in the merged module.
merged_cls_name
class-attribute
¶
Name of the vLLM class the merged projection becomes (for logging).
merged_name
abstractmethod
property
¶
Attribute name of the merged module created by update_attrs.
packed_modules_mapping
property
¶
{merged_name: [projection names]} so quantization can unpack the
fused layer into its per-shard configs.
shards
abstractmethod
property
¶
Each projection's original name and its shard id in the merged module.
Source for both orig_to_new_stacked and packed_modules_mapping.
_check_input_stable(funcdef, module, calls, block, indices)
¶
Raise unless hoisting the merged GEMM preserves what it reads.
Fusing moves every projection to one call at min(indices), so the
merged GEMM reads the input once, up front, where the last of calls
would have read it later. That holds only if nothing in between changes
the input, and a change need not name it: writing any view that shares
its storage changes it too. Both halves of the check over-approximate,
since a false hit costs a fusion while a miss returns wrong numbers.
_splice_merged_split(funcdef, calls, block, index)
¶
Insert temps = self.<merged_name>(arg).split(sizes, -1) at
block[index] and replace each of calls with its temp name.
calls must share one input argument; block[index] must be where
they are (or would be) evaluated. Raises if a generated temporary
would shadow an existing name in funcdef.
_unguarded_calls(funcdef, names)
¶
One self.<name>(arg) call per projection, existence guards folded.
update_attrs deletes the projections it merges, so any reference to one beyond
its call site must be a guard on its existence; folding those to their constant
value keeps the rewritten forward off a name that no longer exists. A reference
that is not such a guard raises, so fusion is skipped.
orig_to_new_stacked(prefix)
¶
WeightsMapper.orig_to_new_stacked entries for one fused instance.
Maps each checkpoint name to (merged_name, shard_id), keyed by qualname
so only this exact layer is remapped, never a same-named projection
elsewhere (e.g. an unfused MoE expert's gate_proj).