vllm.model_executor.models.transformers.fusers ¶
Concrete fusers for the Transformers modeling backend.
Modules:
-
base–Base classes for the Transformers backend fusers.
-
glu–GLU projection fuser:
act(gate(x)) * up(x)-> a fused gate/up linear. -
moe–MoE fuser: route an HF MoE block through
FusedMoEwith vLLM's own routing. -
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:
-
BaseFuser–A detected fusion and how to apply it.
-
GLUFuser–Fuser for the GLU pattern
act(gate(x)) * up(x). -
MoEBlockFuser–Fuser for MoE block
experts,gateandshared_experts(optional). -
QKVFuser–Fuser for the attention QKV pattern
q(x), k(x), v(x). -
RMSNormFuser–Fuser for RMSNorm patterns, including Gemma-style zero-centered weights.
-
StackedFuser–A fuser that merges sibling projections into one stacked linear and
BaseFuser dataclass ¶
Bases: ABC
A detected fusion and how to apply it.
match analyses the module class once (cached, see get_fuser); 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
Source code in vllm/model_executor/models/transformers/fusers/base.py
packed_modules_mapping property ¶
packed_modules_mapping entries this fuser contributes (none unless it stacks weights).
fuse(module, prefix, model_config, quant_config) abstractmethod ¶
Apply the fusion to an already-validated module, returning the module to install in its place (mutated in place, or freshly built).
Source code in vllm/model_executor/models/transformers/fusers/base.py
info(name) abstractmethod ¶
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, model_config) abstractmethod ¶
GLUFuser dataclass ¶
Bases: StackedFuser
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.
Source code in vllm/model_executor/models/transformers/fusers/glu.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | |
_get_act_and_mul(act) classmethod ¶
Get the ...AndMul equivalent of a Transformers activation module.
Source code in vllm/model_executor/models/transformers/fusers/glu.py
_get_act_and_mul_name(act) staticmethod ¶
Get the name of act if it has an ...AndMul equivalent.
Source code in vllm/model_executor/models/transformers/fusers/glu.py
_get_glu_nodes(graph, module) classmethod ¶
Search graph for the GLU pattern act(gate(x)) * up(x).
Source code in vllm/model_executor/models/transformers/fusers/glu.py
_is_act_of_gate(node, module) classmethod ¶
Is node act(gate(x)) where gate is linear and act is not linear.
Source code in vllm/model_executor/models/transformers/fusers/glu.py
update_forward(module) ¶
Replace act(gate(x)) * up(x) with act(gate_up(x)) in source.
Source code in vllm/model_executor/models/transformers/fusers/glu.py
MoEBlockFuser dataclass ¶
Fuser for MoE block experts, gate and shared_experts (optional).
Methods:
-
gate–Rebuild the HF gate as a
ReplicatedLinearfor vLLM's fused MoE. -
rewrite_forward–Rewrite
moe_block.forwardto route through vLLM's fused MoE. -
shared_experts–Build the HF shared expert (and its optional gate)
Source code in vllm/model_executor/models/transformers/fusers/moe.py
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | |
_match_router(gate) staticmethod ¶
Matches topk(score(linear(x))), score being softmax/sigmoid.
Source code in vllm/model_executor/models/transformers/fusers/moe.py
_match_shared_experts(graph, experts) staticmethod ¶
Detects the shared expert and its optional gate by dataflow.
Source code in vllm/model_executor/models/transformers/fusers/moe.py
gate(moe_block, prefix) ¶
Rebuild the HF gate as a ReplicatedLinear for vLLM's fused MoE.
Source code in vllm/model_executor/models/transformers/fusers/moe.py
rewrite_forward(moe_block) ¶
Rewrite moe_block.forward to route through vLLM's fused MoE.
shared_experts(moe_block, prefix) ¶
Build the HF shared expert (and its optional gate) as a SharedExpertMLP for vLLM's fused MoE.
Source code in vllm/model_executor/models/transformers/fusers/moe.py
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.
Source code in vllm/model_executor/models/transformers/fusers/qkv.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | |
_get_qkv_nodes(graph, module) classmethod ¶
Search graph for the QKV pattern q(x), k(x), v(x).
Source code in vllm/model_executor/models/transformers/fusers/qkv.py
update_forward(module) ¶
Replace q(x), k(x), v(x) with qkv(x).split(sizes, -1) in source.
Source code in vllm/model_executor/models/transformers/fusers/qkv.py
validate(module, model_config) ¶
Shapes must be compatible for a single merged, head-sharded GEMM.
Source code in vllm/model_executor/models/transformers/fusers/qkv.py
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:
-
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).
Source code in vllm/model_executor/models/transformers/fusers/rms_norm.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | |
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.
Source code in vllm/model_executor/models/transformers/fusers/rms_norm.py
fuse(module, prefix, model_config, quant_config) ¶
Fuse the matched RMSNorm pattern into a vLLM fused RMSNorm CustomOp.
Source code in vllm/model_executor/models/transformers/fusers/rms_norm.py
match(graph, module) classmethod ¶
Match a graph to the RMSNorm pattern, returning a fuser if found.
Source code in vllm/model_executor/models/transformers/fusers/rms_norm.py
StackedFuser dataclass ¶
Bases: BaseFuser
A fuser that merges sibling projections into one stacked linear and rewrites the forward to call it.
match and update_forward analyse the class once; fuse builds the merged submodule 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__). -
orig_to_new_stacked–WeightsMapper.orig_to_new_stackedentries for one fused instance. -
update_attrs–Replace
module's submodules with the merged module. -
update_forward–Rewrite and compile
type(module)'s forward source.
Attributes:
-
fused_forward(Callable) –The compiled rewritten forward, set by
update_forward. -
merged_cls(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.
-
source_cls(str) –Class of the HF module the fused projections belonged to (for logging).
Source code in vllm/model_executor/models/transformers/fusers/base.py
fused_forward = field(init=False, repr=False) class-attribute instance-attribute ¶
The compiled rewritten forward, set by update_forward.
merged_cls class-attribute ¶
Name of the vLLM class the merged projection becomes (for logging).
merged_name class-attribute ¶
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.
source_cls instance-attribute ¶
Class of the HF module the fused projections belonged to (for logging).
fuse(module, prefix, model_config, quant_config) ¶
Fuse an already-validated module in place (see Fusers.__getitem__).
Builds the merged submodule and binds the compiled forward.
Source code in vllm/model_executor/models/transformers/fusers/base.py
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).
Source code in vllm/model_executor/models/transformers/fusers/base.py
update_attrs(module, prefix, model_config, quant_config) abstractmethod ¶
Replace module's submodules with the merged module.
Source code in vllm/model_executor/models/transformers/fusers/base.py
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).