vllm.model_executor.models.transformers.fx_utils ¶
fx tracing and forward-source rewriting for the Transformers backend fusers.
A small engine, independent of any particular pattern: trace a module's forward with torch.fx (tolerating a partial graph), inspect the resulting nodes, and rewrite the forward's source (AST) so only matched calls change while the rest stays live Python. fusion.py builds the concrete fusion patterns on top.
Functions:
-
compile_forward–Compile
funcdefinfn's module so tracebacks point at the source. -
find_node–The first node in
graphmatchingpredicate, orNone. -
forward_input_count–The number of tensor inputs
cls.forwarddeclares, excludingselfand -
forward_parameters–cls.forward's signature parameters, or empty if uninspectable. -
innermost_block–The innermost statement list containing
node, and the index within. -
is_fn–Is node
<target>(). -
is_leaf_call–Is node a call recorded by
_as_leaf_call(e.g. an attention interface). -
is_linear–Is node
nn.Linear.__call__(). -
is_method–Is node
.<name>(). -
is_op–Is node
torch.<name>(),F.<name>(),operator.<name>(), orTensor.<name>(). -
output_value–The value the graph's
outputnode returns, if the trace reached one. -
peel–Strip dtype-cast wrappers (
.to(...),.float(),.type_as(...)). -
recover_forward–Parse the source of
cls.forward, ready for rewriting. -
replace_expr–Replace the expression
old(by identity) withnewwithinmodule. -
returned_linear–Name of the Linear producing the graph's (first) output value.
-
single_self_call–The unique
self.<name>(arg)call infuncdef. -
trace–Trace
module.forward, returning the partial graph on failure. -
upstream_linear–Nearest linear producing
node, walking back through splits/reshapes.
_MODULE_CALL = nn.Module.__call__ module-attribute ¶
The unpatched nn.Module.__call__. During tracing fx patches it to record call_module nodes; meta execution must call modules for real.
_UNKNOWN = object() module-attribute ¶
Sentinel meta value for proxies whose concrete value could not be inferred. Distinct from None, which is a valid concrete value (e.g. attn_weights).
_AllLeafTracer ¶
Bases: Tracer
Tracer that treats every submodule as a leaf.
Each child stays one call_module node, so matching sees the module's own forward structure (activations aren't decomposed into e.g. sigmoid * x). Every traced op is also executed on meta tensors (see _MetaProxy) so shape unpacks and *-splats trace through; anything else untraceable ends the trace early and the partial graph is matched.
Attributes:
Source code in vllm/model_executor/models/transformers/fx_utils.py
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 | |
varkw = None class-attribute instance-attribute ¶
Name of the traced forward's **kwargs parameter, if any.
_infer_meta(kind, target, args, kwargs) ¶
Execute the op on meta tensors; PyTorch infers the output value.
Source code in vllm/model_executor/models/transformers/fx_utils.py
_MetaAttribute ¶
Bases: _MetaProxy, Attribute
Attribute proxy (e.g. x.shape) carrying its meta value.
Proxy.__getattr__ constructs Attribute directly, bypassing Tracer.proxy, so the meta value must be grafted on here too.
Source code in vllm/model_executor/models/transformers/fx_utils.py
_MetaProxy ¶
Bases: Proxy
Proxy carrying the meta-tensor value of the traced expression.
Shape questions (len, iteration, .shape unpacks) are answered by executing each op on the meta values, so PyTorch's meta kernels are the single source of shape inference — no per-op rules.
Source code in vllm/model_executor/models/transformers/fx_utils.py
_as_leaf_call(fn, length=None) ¶
Wrap any callable so tracing records it as one opaque call_function node.
Lets the trace continue past untraceable bodies. Only the proxy arguments carry into the node's dataflow; the rest are dropped rather than lifted into the graph. length declares how many values the callable returns, so unpacking its result also traces. Called without proxies (i.e. outside tracing), the wrapper is a passthrough.
Source code in vllm/model_executor/models/transformers/fx_utils.py
_leaf_attention_interfaces() ¶
Patch AttentionInterface.get_interface so traced forwards see a leaf node.
vllm_attention_function needs runtime context so it is untraceable. Every interface returns (attn_output, attn_weights).
Source code in vllm/model_executor/models/transformers/fx_utils.py
_reference_weight(module) ¶
A weight whose trailing dim is the module's hidden size.
Linears and 2-D gate weights are [out, hidden]; norm weights are [hidden]. Used to fabricate a placeholder input of matching size/dtype.
Source code in vllm/model_executor/models/transformers/fx_utils.py
compile_forward(funcdef, fn) ¶
Compile funcdef in fn's module so tracebacks point at the source.
Source code in vllm/model_executor/models/transformers/fx_utils.py
find_node(graph, predicate) ¶
The first node in graph matching predicate, or None.
forward_input_count(cls) ¶
The number of tensor inputs cls.forward declares, excluding self and any *args/**kwargs. Read from the signature, so it is independent of whether the trace completes (unlike counting placeholders).
Source code in vllm/model_executor/models/transformers/fx_utils.py
forward_parameters(cls) ¶
cls.forward's signature parameters, or empty if uninspectable.
Source code in vllm/model_executor/models/transformers/fx_utils.py
innermost_block(block, node) ¶
The innermost statement list containing node, and the index within.
Source code in vllm/model_executor/models/transformers/fx_utils.py
is_fn(node, target) ¶
is_leaf_call(node) ¶
Is node a call recorded by _as_leaf_call (e.g. an attention interface).
is_linear(node, module) ¶
Is node nn.Linear.__call__().
is_method(node, name) ¶
is_op(node, name) ¶
Is node torch.<name>(), F.<name>(), operator.<name>(), or Tensor.<name>().
Source code in vllm/model_executor/models/transformers/fx_utils.py
output_value(graph) ¶
The value the graph's output node returns, if the trace reached one.
Source code in vllm/model_executor/models/transformers/fx_utils.py
peel(node) ¶
Strip dtype-cast wrappers (.to(...), .float(), .type_as(...)).
Source code in vllm/model_executor/models/transformers/fx_utils.py
recover_forward(cls) ¶
Parse the source of cls.forward, ready for rewriting.
Source code in vllm/model_executor/models/transformers/fx_utils.py
replace_expr(module, old, new) ¶
Replace the expression old (by identity) with new within module.
Source code in vllm/model_executor/models/transformers/fx_utils.py
returned_linear(graph, module) ¶
Name of the Linear producing the graph's (first) output value.
Source code in vllm/model_executor/models/transformers/fx_utils.py
single_self_call(funcdef, name) ¶
The unique self.<name>(arg) call in funcdef.
Raises unless name appears exactly once, as such a call, so the source rewrite agrees with the fx match.
Source code in vllm/model_executor/models/transformers/fx_utils.py
trace(module) ¶
Trace module.forward, returning the partial graph on failure.
Source code in vllm/model_executor/models/transformers/fx_utils.py
upstream_linear(node, module) ¶
Nearest linear producing node, walking back through splits/reshapes.
Non-linear submodules are transparent too (e.g. the dropout GPT-style attentions apply after their output projection). Never walks through a leaf call (e.g. an attention interface): its inputs are what attention consumes, not what produced the value.