vllm.model_executor.utils
¶
Utils for model executor.
Functions:
-
get_moe_expert_mapping–Get the expert mapping from a model.
-
replace_parameter–Replace a parameter of a layer while maintaining the ability to reload the
-
set_weight_attrs–Set attributes on a weight tensor.
-
weights_already_processed–Mark that weights are already in post-processed (runtime) format, so
get_moe_expert_mapping(model)
¶
Get the expert mapping from a model.
It will be retrieved from the first module that has a get_expert_mapping method.
If the model manually implements get_expert_mapping, it will be used.
Otherwise, it will use the first RoutedExperts layer.
Source code in vllm/model_executor/utils.py
replace_parameter(layer, param_name, new_tensor)
¶
Replace a parameter of a layer while maintaining the ability to reload the
weight. Called within implementations of the process_weights_after_loading method.
The existing parameter object is kept (and so its class and loader attributes
for weight reloading): new_tensor is copied into it when shape, stride,
dtype and device match, which preserves data_ptr for captured CUDA graphs;
otherwise the parameter's .data is rebound to new_tensor. A new
parameter is registered only if none exists under param_name, or if the
existing one is an online requantization source (released after conversion).
Custom attributes set on new_tensor (e.g. kernel dispatch flags such as
is_shuffled) are carried over to the parameter, except weight_loader,
which is always taken from the existing parameter.
This function should not be called on weights which are tied/shared
Parameters:
-
(layer¶Module) –Layer containing parameter to replace
-
(param_name¶str) –Name of parameter to replace
-
(new_tensor¶Tensor | None) –New data of the new parameter, or None to set the parameter to None
Source code in vllm/model_executor/utils.py
set_weight_attrs(weight, weight_attrs)
¶
Set attributes on a weight tensor.
This method is used to set attributes on a weight tensor. This method will not overwrite existing attributes.
Parameters:
-
(weight¶Tensor) –The weight tensor.
-
(weight_attrs¶dict[str, Any] | None) –A dictionary of attributes to set on the weight tensor.
Source code in vllm/model_executor/utils.py
weights_already_processed()
¶
Mark that weights are already in post-processed (runtime) format, so
process_weights_after_loading skips tensor transforms (used by the
weight cache IPC loader).