Skip to content

vllm.model_executor.models.transformers.fusers.base

Base classes for the Transformers backend fusers.

Classes:

  • BaseFuser –

    A detected fusion and how to apply it.

  • RewriteFuser –

    A fuser that rewrites the module's forward and rebinds it.

  • StackedFuser –

    A fuser that merges sibling projections into one stacked linear and

Functions:

  • fused_head_size –

    The head size of module, which head counts are derived from.

  • local_output_sizes –

    Source for the per-rank widths of the merged linear self.<merged_name>.

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_stacked entries this fuser contributes

  • validate –

    Whether this fuser can be applied to this module instance.

Attributes:

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.

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 module in place (see Fusers.__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:

Attributes:

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).

fused_head_size(module, vllm_config)

The head size of module, which head counts are derived from.

Prefer the module's own head_dim, which Transformers sets per instance: the model-wide value is the largest head size across layers, so on a heterogeneous checkpoint it would divide out the wrong number of heads, and it is the text head size, so it does not describe a vision tower at all.

local_output_sizes(merged_name)

Source for the per-rank widths of the merged linear self.<merged_name>.