Skip to content

vllm.model_executor.utils

Utils for model executor.

Functions:

_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)

refill_derived(obj: object, name: str, new: None) -> None
refill_derived(
    obj: object, name: str, new: torch.Tensor
) -> torch.Tensor

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:

  • 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

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)

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.

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).