vllm.model_executor.models.transformers.utils
¶
Transformers modeling backend utilities.
Functions:
-
attrsetter–Set a possibly nested attribute, like the inverse of attrgetter.
-
can_enable_torch_compile–Callable to be passed to
@support_torch_compile'senable_ifargument. -
init_on_device_without_buffers–A context manager under which models are initialized with all
-
maybe_per_layer–Pick
layer_idx's entry from a config field that may be sized per layer. -
named_state–module's own state (i.e. named parameters and buffers). -
recursive_replace_linear–Recursively replace linear modules in the model as needed.
-
replace_conv_class–Replace a Transformers Conv2d/Conv3d with vLLM's Conv2d/Conv3d.
-
replace_embedding_class–Replace
nn.EmbeddingwithVocabParallelEmbedding. -
replace_layernorm_class–Replace a standard (mean-centered)
nn.LayerNormwith vLLM's -
replace_linear_class–Replace nn.Linear with one of vLLM's tensor parallel linear classes.
_UninitializedEmbedding
¶
Bases: Embedding
Make __init__ inert, so that VocabParallelEmbedding.__init__'
call to super().__init__ does not invoke nn.Embedding.__init__.
Source code in vllm/model_executor/models/transformers/utils.py
_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.
Source code in vllm/model_executor/models/transformers/utils.py
_rebase_on_vocab_parallel(cls)
¶
Subclass cls so that VocabParallelEmbedding supersedes its nn.Embedding.
Parameters:
-
(cls¶type[Embedding]) –The
nn.Embeddingsubclass to rebase. Cached, so a givenclsalways maps to the same class.
Returns:
-
type[VocabParallelEmbedding]–The new class, to assign to
__class__of an instance ofcls.
Source code in vllm/model_executor/models/transformers/utils.py
attrsetter(attr)
¶
Set a possibly nested attribute, like the inverse of attrgetter.
Source code in vllm/model_executor/models/transformers/utils.py
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.
Source code in vllm/model_executor/models/transformers/utils.py
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.
Source code in vllm/model_executor/models/transformers/utils.py
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.
Source code in vllm/model_executor/models/transformers/utils.py
replace_conv_class(conv)
¶
Replace a Transformers Conv2d/Conv3d with vLLM's Conv2d/Conv3d.
Parameters:
-
(conv¶TorchConv) –nn.Conv2dornn.Conv3dto be replaced.
Returns:
-
VllmConv | TorchConv–The new
Conv2dLayerorConv3dLayer. If the conv module is not supported, -
VllmConv | TorchConv–returns the original conv module.
Source code in vllm/model_executor/models/transformers/utils.py
replace_embedding_class(embedding, quant_config=None, *, prefix='')
¶
Replace nn.Embedding with VocabParallelEmbedding.
Parameters:
-
(embedding¶Embedding) –The
nn.Embeddingholding 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.
Source code in vllm/model_executor/models/transformers/utils.py
replace_layernorm_class(layernorm)
¶
Replace a standard (mean-centered) nn.LayerNorm with vLLM's
LayerNorm.
Parameters:
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.
Source code in vllm/model_executor/models/transformers/utils.py
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.Linearto 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:
-
ColumnParallelLinear | RowParallelLinear | ReplicatedLinear–The new linear.