vllm.model_executor.utils
¶
Utils for model executor.
Functions:
-
get_live_param–getattr(layer, name, None), treating retired or empty params as absent. -
get_moe_expert_mapping–Get the expert mapping from a model.
-
kernel_parameters–The layer's own parameters as kernels see them: live parameters only, each
-
move_parameter–Register the
src_nameparameter object underdst_nameas well. -
replace_parameter–Replace a parameter of a layer while maintaining the ability to reload the
-
retire_parameter–Mark a loaded parameter that no kernel reads after post-processing.
-
set_kernel_parameter–replace_parameter, for a tensor kernels read undername: the parameter -
set_weight_attrs–Set attributes on a weight tensor.
-
weights_already_processed–Mark that weights are already in post-processed (runtime) format, so
_extent(t)
¶
_in_own_storage(old, new)
¶
new copied into the bytes old spans if it fits, so the loaded
storage is kept (with its data_ptr); else new itself. An equal-size
result fits too: a repack to the same byte count (e.g. FP8 → Marlin int32)
then keeps the address captured CUDA graphs read.
Source code in vllm/model_executor/utils.py
get_live_param(layer, name)
¶
getattr(layer, name, None), treating retired or empty params as absent.
Source code in vllm/model_executor/utils.py
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
kernel_parameters(layer)
¶
The layer's own parameters as kernels see them: live parameters only, each
object once, under its runtime name (see move_parameter).
Source code in vllm/model_executor/utils.py
move_parameter(layer, src_name, dst_name)
¶
Register the src_name parameter object under dst_name as well.
Both names then refer to one object and so to one storage: the loader writes
through the checkpoint name, kernels read the runtime name, and a later
replace_parameter on either name updates both. dst_name becomes the
name kernel_parameters reports for the object.
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 a smaller new_tensor is copied into the parameter's own storage
(so the loaded storage never shrinks), else .data is rebound to it. 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
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | |
retire_parameter(layer, name)
¶
Mark a loaded parameter that no kernel reads after post-processing.
It stays registered with its loaded data (so it can be loaded again);
kernel_parameters and get_live_param skip it.
Source code in vllm/model_executor/utils.py
set_kernel_parameter(layer, name, tensor)
¶
replace_parameter, for a tensor kernels read under name: the parameter
is live even if it was retired before, and name becomes its runtime name
if it is registered under several names (see move_parameter).
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).