Skip to content

vllm.model_executor.models.transformers.utils

Transformers modeling backend utilities.

Functions:

_PreambleDone

Bases: Exception

Stops a decoder forward before its first layer.

_UninitializedEmbedding

Bases: Embedding

Make __init__ inert, so that VocabParallelEmbedding.__init__' call to super().__init__ does not invoke nn.Embedding.__init__.

_VocabParallelEmbeddingBase

Bases: VocabParallelEmbedding, _UninitializedEmbedding

Orders VocabParallelEmbedding ahead of nn.Embedding in the MRO, so that super().forward(...) in an nn.Embedding subclass reaches vLLM's embedding.

_is_tensor_type(annotation)

Match torch.Tensor and legacy types like torch.BoolTensor.

_rebase_on_vocab_parallel(cls)

Subclass cls so that VocabParallelEmbedding supersedes its nn.Embedding.

Parameters:

  • cls

    (type[Embedding]) –

    The nn.Embedding subclass to rebase. Cached, so a given cls always maps to the same class.

Returns:

attrsetter(attr)

Set a possibly nested attribute, like the inverse of attrgetter.

can_enable_torch_compile(vllm_config)

Callable to be passed to @support_torch_compile's enable_if argument.

Defaults to True but is disabled in the following situations:

  • The model uses dynamic rope scaling that can still rescale within max_model_len (otherwise maybe_disable_dynamic_rope_update removes it).

decoder_mutates_inputs_embeds(model, first_layer, hidden_size, uses_mrope)

Whether model's pre-layer code modifies inputs_embeds in place.

Runs model on one token and stops it at first_layer. If the probe fails, assumes it does, so callers fall back to copying the input.

dynamic_rope_update_is_noop(vllm_config)

Whether max_model_len keeps Transformers' dynamic rope from ever rescaling.

@dynamic_rope_update only recomputes inv_freq once positions grow past max_position_embeddings, which vLLM never feeds beyond max_model_len.

get_default_seq_pooling_type(model_config)

CLS for encoders and LAST for decoders, like vLLM's own pooling models.

has_bidirectional_attention(model)

Whether any of the attention layers in model has is_causal=False.

infer_dynamic_arg_dims(cls, dim=0)

Infer dynamic_arg_dims from the signature of cls.forward.

dim is dynamic for every argument which is either unannotated or annotated with a tensor type, optionally in a union with other types, and which has no default other than None. String annotations are not resolved.

init_on_device_without_buffers(device)

A context manager under which models are initialized with all parameters on the specified device. However buffers are not initialized on specified device.

Parameters:

  • device

    (`torch.device`) –

    Device to initialize all parameters on.

maybe_disable_dynamic_rope_update(model, vllm_config)

Skip @dynamic_rope_update in rotary modules where inv_freq can be fixed.

The update syncs on torch.max(position_ids) every forward, which breaks torch.compile and CUDA graph capture. Relabelling the module's rope_type as "default" skips it:

  • dynamic: where it can never rescale, keeping the inv_freq from init
  • longrope: always, picking the long factors iff max_model_len exceeds original_max_position_embeddings like native vLLM, since switching per forward would read KV cached under one set with the other

maybe_per_layer(value, layer_idx)

Pick layer_idx's entry from a config field that may be sized per layer.

named_state(module)

module's own state (i.e. named parameters and buffers).

recursive_replace_linear(model, quant_config, prefix='')

Recursively replace linear modules in the model as needed.

replace_conv_class(conv)

Replace a Transformers Conv2d/Conv3d with vLLM's Conv2d/Conv3d.

Parameters:

  • conv

    (TorchConv) –

    nn.Conv2d or nn.Conv3d to be replaced.

Returns:

  • VllmConv | TorchConv –

    The new Conv2dLayer or Conv3dLayer. If the conv module is not supported,

  • VllmConv | TorchConv –

    returns the original conv module.

replace_embedding_class(embedding, quant_config=None, *, prefix='')

Replace nn.Embedding with VocabParallelEmbedding.

Parameters:

  • embedding

    (Embedding) –

    The nn.Embedding holding a vocab table.

  • quant_config

    (QuantizationConfig | None, default: None ) –

    Quantization config for the new embedding.

  • prefix

    (str, default: '' ) –

    Qualname of embedding, used to look up its quantization method.

Returns:

  • Module –

    The module to install in embedding's place.

replace_layernorm_class(layernorm)

Replace a standard (mean-centered) nn.LayerNorm with vLLM's LayerNorm.

Parameters:

  • layernorm

    (LayerNorm) –

    nn.LayerNorm to be replaced.

Returns:

  • Module –

    The new LayerNorm. If the layernorm is not supported (a subclass with

  • Module –

    its own behavior, multi-dim normalized_shape, or not both

  • Module –

    elementwise-affine and biased), returns the original module unchanged.

replace_linear_class(linear, style='replicate', quant_config=None, *, prefix='')

Replace nn.Linear with one of vLLM's tensor parallel linear classes.

Parameters:

  • linear

    (Linear) –

    nn.Linear to be replaced.

  • style

    (Style, default: 'replicate' ) –

    Tensor parallel style of the new linear, e.g. "colwise".

  • quant_config

    (QuantizationConfig | None, default: None ) –

    Quantization config for the new linear.

  • prefix

    (str, default: '' ) –

    Module prefix of the new linear, used for quantization lookup.

Returns: