vllm.model_executor.offloader.prefetch
¶
Prefetch-based CPU offloading with async prefetching.
Uses static buffers and event-based stream forking for torch.compile + CUDA graph compatibility. Events allow the copy stream to join CUDA graph captures, ensuring H2D copies are properly captured.
Classes:
-
ParamInfo–Metadata about an offloaded parameter.
-
PrefetchOffloader–Prefetching-based offloader with group-based layer selection.
-
StaticBufferPool–Pre-allocated GPU buffer pool for offloaded parameters.
ParamInfo
dataclass
¶
Metadata about an offloaded parameter.
Attributes:
-
key(tuple[str, tuple[int, ...], tuple[int, ...], dtype]) –Unique key for buffer pool grouping.
-
num_bytes(int) –Size in bytes.
key
property
¶
Unique key for buffer pool grouping.
Includes parameter name to prevent different parameters with the same shape from sharing buffers within the same layer. Parameters with the same name across different layers will share buffers (via slots).
Includes stride because parameters with same shape but different strides need separate buffers to preserve memory layout.
num_bytes
property
¶
Size in bytes.
PrefetchOffloader
¶
Bases: BaseOffloader
Prefetching-based offloader with group-based layer selection.
Groups layers and uses async H2D prefetch to hide transfer latency. Uses static buffers and stream synchronization for torch.compile and CUDA graph compatibility.
Parameters:
-
(group_size¶int) –Group every N layers together.
-
(num_in_group¶int) –Offload this many layers per group (last N of each group).
-
(prefetch_step¶int) –Number of layers to prefetch ahead.
-
(mode¶str, default:'cpu') –Offload mode ("cpu" is currently supported).
Methods:
-
join_after_forward–Join copy_stream after model forward completes.
-
post_init–Allocate static buffer pool and start initial prefetches.
-
sync_prev_onload–Sync previous onload operations.
-
wrap_modules–Wrap modules with prefetch offloading logic.
_hook_module_forward(index, module)
¶
Hook module's forward with torch.compile-compatible sync.
_start_prefetch(layer_idx)
¶
Called by custom op - start async copy to static buffer.
_wait_for_layer(layer_idx)
¶
Called by custom op - wait for copy to complete.
Synchronization strategy: - During CUDA graph capture: use event-based wait (graph-compatible) - Outside capture (warmup/eager): use wait_stream (more robust)
During capture, we skip wait for pre-capture prefetches because: 1. sync_before_graph_capture() ensures pre-capture work is complete 2. We can't wait on pre-capture events during capture (isolation error)
join_after_forward()
¶
Join copy_stream after model forward completes.
Call this after the model forward pass but before CUDA graph capture ends. This ensures copy_stream is rejoined for any prefetches started during the forward pass.
We join ALL layers that have _prefetch_in_capture=True, meaning their prefetch was started during capture but not yet waited on (joined). This handles both full and piecewise cudagraph modes correctly: - Full mode: joins layers 0..prefetch_step-1 (prefetched by last layers) - Piecewise mode: joins only layers prefetched by THIS subgraph's layers
post_init()
¶
Allocate static buffer pool and start initial prefetches.
Note: Parameters have already been offloaded to CPU during wrap_modules() (in _CpuParamOffloader.init), so GPU memory is available for the static buffer pool.
sync_prev_onload()
¶
Sync previous onload operations.
Ensures any H2D copies in flight on copy_stream complete before the compute stream continues. Call this before CUDA graph capture/replay or when synchronization is needed.
wrap_modules(modules_generator, prefix='')
¶
Wrap modules with prefetch offloading logic.
StaticBufferPool
¶
Pre-allocated GPU buffer pool for offloaded parameters.
Allocates slot_capacity copies of each unique parameter (name, shape, stride, dtype), allowing for double/triple buffering during prefetch.
Buffer slots are reused circularly: layer N uses slot (N % slot_capacity).
The key includes parameter name to prevent different parameters within the same layer from sharing buffers. Parameters with the same name across different layers share buffers via the slot mechanism.
Methods:
-
get_buffer–Get a static buffer for the given name/shape/stride/dtype/slot.
get_buffer(name, shape, stride, dtype, slot_idx)
¶
Get a static buffer for the given name/shape/stride/dtype/slot.
_BaseParamOffloader
¶
Bases: ABC
Base class for parameter offloading strategies.
Methods:
-
assign_static_buffer–Point parameter data to GPU static buffer.
-
create–Factory method to create appropriate offloader for mode.
-
post_init–Initialize offloading (move parameter to storage).
-
sync_cpu_storage–Sync CPU storage with current param.data.
_param
property
¶
Get the parameter being offloaded.
Supports dotted names (e.g. 'self_attn.qkv_proj.weight') by traversing the module hierarchy.
assign_static_buffer(gpu_buffer)
abstractmethod
¶
Point parameter data to GPU static buffer.
create(mode, **kwargs)
staticmethod
¶
Factory method to create appropriate offloader for mode.
post_init()
¶
Initialize offloading (move parameter to storage).
sync_cpu_storage()
abstractmethod
¶
Sync CPU storage with current param.data.
Called after process_weights_after_loading to update _cpu_storage with the final processed weights.
_CpuParamOffloader
¶
Bases: _BaseParamOffloader
Offload parameter to pinned CPU memory.
Uses GPU static buffers as the actual parameter, with CPU storage kept separately. This ensures torch.compile sees GPU tensors at trace time.
The offloading happens in two phases: 1. init() - copies GPU data to CPU, frees GPU memory immediately 2. assign_static_buffer() - points param.data to GPU static buffer
Methods:
-
assign_static_buffer–Point parameter data to GPU static buffer.
-
post_init–No-op: offloading done in offload_to_cpu/assign_static_buffer.
-
sync_cpu_storage–Sync CPU storage with current param.data.
_offload_to_cpu_internal()
¶
Copy parameter data to pinned CPU storage and free GPU memory.
This replaces param.data with CPU storage, allowing weight loading to continue writing to CPU memory. GPU memory is freed when the original GPU tensor is garbage collected.
_update_cpu_storage_from_param()
¶
Update _cpu_storage from current param.data, ensuring pinned memory.
After process_weights_after_loading, device_loading_context creates
non-pinned CPU tensors via p.data = p.data.to("cpu"). Using
non-pinned memory with copy_(src, non_blocking=True) causes CUDA to
perform a stream synchronization before the copy, breaking the
event-based fork synchronization and potentially allowing the copy
to overwrite the GPU buffer while the compute stream still reads it.
This method ensures _cpu_storage always uses pinned memory when available, re-pinning if necessary.
assign_static_buffer(gpu_buffer)
¶
Point parameter data to GPU static buffer.
This is called after weight loading AND process_weights_after_loading complete. At this point: - param.data may have been replaced by device_loading_context (which creates new CPU tensors after quantization processing) - We need to update _cpu_storage to point to current param.data so that prefetch copies the processed weights, not stale data - Then point param.data to the GPU buffer for torch.compile
post_init()
¶
No-op: offloading done in offload_to_cpu/assign_static_buffer.
sync_cpu_storage()
¶
Sync CPU storage with current param.data.
Called after process_weights_after_loading to update _cpu_storage with the final processed weights. This is critical because: 1. process_weights_after_loading may transform weights (quantization) 2. device_loading_context creates NEW CPU tensors when moving back 3. Our old _cpu_storage would have pre-processed or stale data
If the parameter no longer exists on the module (e.g. online requantization source parameters released by process_weights_after_loading()), the offloader marks itself as deleted and skips the sync. The caller (_ModuleOffloader.sync_cpu_storage) is responsible for removing these stale entries.
_ModuleOffloader
¶
Manages offloading for a single module.
Uses static buffers from a shared pool instead of dynamic allocation.
Methods:
-
assign_buffer_slot–Assign this module to a buffer slot in the pool.
-
get_param_infos–Get parameter metadata for buffer pool allocation.
-
post_init–Collect total offloaded bytes (offloading already done in init).
-
start_onload_to_static–Start async copy from CPU storage to GPU buffer.
-
sync_cpu_storage–Sync CPU storage with current param.data.
assign_buffer_slot(pool, slot_idx)
¶
Assign this module to a buffer slot in the pool.
Also assigns static GPU buffers to each parameter offloader, which moves the parameter data to point to the GPU buffer.
get_param_infos()
¶
Get parameter metadata for buffer pool allocation.
Note: sync_cpu_storage() must be called before this method to ensure _cpu_storage reflects the final processed weights (after quantization).
post_init()
¶
Collect total offloaded bytes (offloading already done in init).
start_onload_to_static()
¶
Start async copy from CPU storage to GPU buffer.
Uses event-based forking to join copy_stream to CUDA graph capture. This ensures H2D copies are properly captured when recording a graph.
IMPORTANT: We must wait for the compute stream before copying, because the previous layer's forward may still be using the buffer (GPU ops are async). Without this sync, we could overwrite the buffer while it's being read.
sync_cpu_storage()
¶
Sync CPU storage with current param.data.
Called after process_weights_after_loading to ensure _cpu_storage contains the final processed weights, not stale pre-loading data.
Parameters whose underlying nn.Parameter was deleted by process_weights_after_loading (e.g. transient KV-cache scale params) are pruned from self._param_offloaders so they do not participate in buffer-pool allocation or prefetching.
_get_next_prefetch_index(index, prefetch_step, module_count)
¶
Return a refill target that preserves static-buffer slot ownership.