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.
-
storage_view–A
dtypetensor onstorageat elementoffset.set_does -
weights_already_processed–Mark that weights are already in post-processed (runtime) format, so
_extent(t)
¶
_in_own_storage(old, new, whole_storage=False)
¶
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.
With whole_storage (direct reload), new may use the rest of
old's storage, so a weight that post-load processing grew keeps its
address on a reload too, as do a padded view with gaps (ROCm FP8 padding)
and an emptied weight (the checkpoint bytes stay for the next reload).
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.
Under direct reload, a new_tensor larger than the parameter at cold start
may be written into the parameter's existing storage on a reload, so do not
read a local alias of the old data (w = layer.weight.data) after this call.
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
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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | |
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
storage_view(storage, offset, shape, stride, dtype)
¶
A dtype tensor on storage at element offset. set_ does
not bounds-check: callers check that the view fits.
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).