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
-
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 the
nn.EmbeddinginembeddingwithVocabParallelEmbedding. -
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) cached ¶
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: The new class, to assign to __class__ of an instance of cls.
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
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: The new Conv2dLayer or Conv3dLayer. If the conv module is not supported, returns the original conv module.
Source code in vllm/model_executor/models/transformers/utils.py
replace_embedding_class(embedding, quant_config=None, *, prefix='') ¶
Replace the nn.Embedding in embedding with VocabParallelEmbedding.
Parameters:
-
(embedding¶Module) –The module returned by
model.get_input_embeddings(). -
(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: The module to install with model.set_input_embeddings(). Composing and inheriting modules are mutated in place and returned as-is. Raises: ValueError: If embedding composes anything other than one nn.Embedding, which would leave the input embedding weights ambiguous.
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.
Returns: The new linear.