vllm.model_executor.utils
¶
Utils for model executor.
Functions:
-
copy_weight_–dst.copy_(src)for weight loaders. -
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. -
refill_derived–Set
obj.name(a plain attribute, never a parameter or buffer) to a -
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)
¶
Number of elements spanned by t in its storage.
_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).
copy_weight_(dst, src)
¶
dst.copy_(src) for weight loaders.
Model-specific weight loaders should call this instead of .copy_() when
copying a checkpoint tensor to the device. Off integrated GPUs it is a
plain copy_, except that a copy between two byte containers (uint8,
int8, float8_e8m0fnu, float4_e2m1fn_x2) keeps the bytes, where
copy_ would convert E8M0 scales by value. Float8 value formats still
convert by value.
On integrated GPUs, a host-to-device copy straight from a safetensors mmap
takes its page faults inside the driver copy, which is several times slower
than faulting them on the CPU, so touch one byte per page of src first.
PyTorch already stages non-contiguous and dtype-converting copies through
a CPU temporary. Sources with at most one element are skipped: they count
as contiguous with any stride, which view(torch.uint8) rejects.
get_live_param(layer, name)
¶
getattr(layer, name, None), treating retired or empty params as absent.
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.
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).
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.
refill_derived(obj, name, new)
¶
Set obj.name (a plain attribute, never a parameter or buffer) to a
tensor derived from the weights. A held tensor of the same shape, dtype and
device is refilled in place, so captured CUDA graphs keep its address; it
must own its storage. Returns the tensor now held under name.
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, and the direct-reload
attributes _ckpt and fill_on_reload, which belong to the parameter they
were set on.
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:
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.
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).
set_weight_attrs(weight, weight_attrs)
¶
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.
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).