vllm.v1.kv_offload.cpu.gpu_worker
¶
Classes:
-
CPUOffloadingWorker–OffloadingWorker for CPU offloading.
-
CopyPlan–Precomputed fragment-copy template for one data ref under the canonical
-
SingleDirectionOffloadingHandler–Handles transfers for a single direction, either CPU->GPU or GPU->CPU.
Functions:
-
compute_sub_block_ptrs–Compute byte pointers for sub-blocks of the given block IDs.
-
pin_mmap_region–Register row-aligned chunks or this worker's slots, rolling back on failure
CPUOffloadingWorker
¶
Bases: OffloadingWorker
OffloadingWorker for CPU offloading.
Composes two SingleDirectionOffloadingHandler instances (one for each direction) and exposes them through the explicit submit_store / submit_load API.
Methods:
-
__init__–defer_host_setup leaves pre-faulting and pinning mmap_region to
-
submit_load–Async CPU -> GPU.
-
submit_store–Async GPU -> CPU.
__init__(kv_caches, blocks_per_chunk, num_cpu_chunks, mmap_region=None, canonical_layout=False, defer_host_setup=False, prefault_shard=None, pin_pace=2.0)
¶
defer_host_setup leaves pre-faulting and pinning mmap_region to start_deferred_setup(); prefault_shard is then the share this worker pre-faults first, and pin_pace paces its pinning (see pin_mmap_region).
submit_load(job_id, src_spec, device_ptrs, expedited=False)
¶
Async CPU -> GPU.
submit_store(job_id, device_ptrs, dst_spec)
¶
Async GPU -> CPU.
CopyPlan
¶
Bases: NamedTuple
Precomputed fragment-copy template for one data ref under the canonical CPU layout, unrolled from the ref's mapped runs. Offsets are relative to the per-block base pointers on each side.
SingleDirectionOffloadingHandler
¶
Handles transfers for a single direction, either CPU->GPU or GPU->CPU. Transfers are guaranteed to be executed in order of their submission. Each transfer uses a unique CUDA stream, and its stream will start executing only after the streams of previous transfers have finished.
Methods:
-
__init__–Initialize a SingleDirectionOffloadingHandler.
-
shutdown–Drain this direction and release its transfer-side resources.
__init__(cpu_tensors, blocks_per_chunk, layer_refs_per_group, gpu_to_cpu, canonical_layout=False, host_memory_is_pinned=True)
¶
Initialize a SingleDirectionOffloadingHandler.
Parameters:
-
(cpu_tensors¶list[Tensor]) –list of CPU KV cache tensors. Each of shape (num_cpu_blocks, cpu_page_size_bytes) with dtype int8.
-
(blocks_per_chunk¶int) –number of GPU blocks per CPU chunk.
-
(layer_refs_per_group¶list[list[CanonicalKVCacheRef]]) –list of CanonicalKVCacheRef per group.
-
(gpu_to_cpu¶bool) –if True, transfer from GPU to CPU; otherwise CPU to GPU.
-
(canonical_layout¶bool, default:False) –if True, CPU pages use the canonical layout described by the refs' mappings.
-
(host_memory_is_pinned¶bool, default:True) –whether the CPU tensors are pinned, so GPU kernels may dereference them directly.
_estimate_max_copy_ops(group_sizes)
¶
Upper bound on the number of copy descriptors for a transfer.
Exact for the direct layout. The canonical path may fill fewer: writer rotation later drops the blocks this rank does not write.
_fill_canonical_ops(g_idx, device_ptrs, dev_ptr_offset, cpu_block_ids, group_size, cpu_skip_count, all_src, all_dst, all_sizes, op_idx)
¶
Fill one group's copy descriptors for the canonical layout: scatter each block through the ref's precomputed CopyPlan, keeping only the blocks this rank writes.
Returns (op_idx past the filled descriptors, bytes added).
_fill_direct_ops(g_idx, device_ptrs, dev_ptr_offset, cpu_block_ids, group_size, cpu_skip_count, all_src, all_dst, all_sizes, op_idx)
¶
Fill one group's copy descriptors for the direct (worker-private) layout: one whole-page copy per (block, ref).
Returns (op_idx past the filled descriptors, bytes added).
_filter_writer_blocks(block_bases_src, block_bases_dst, mapping, cpu_block_ids, group_size, cpu_skip_count)
¶
Keep only the blocks this rank writes: replicated ranks take turns writing shared canonical pages, keyed by the rank-consistent CPU-side canonical page id.
shutdown()
¶
Drain this direction and release its transfer-side resources.
_build_copy_plan(ref, gpu_to_cpu)
¶
Unroll one data ref's mapped runs into a per-fragment CopyPlan.
_canonical_block_sizes(layer_refs_per_group, num_tensors)
¶
Canonical CPU bytes per GPU block for each tensor, taken from the refs' mappings. Requires every ref to carry a mapping.
_canonical_page_ids(block_ids, blocks_per_chunk, count, skip_count)
¶
Global canonical page ids matching compute_sub_block_ptrs' enumeration. These identify canonical pages consistently across ranks, so they key CanonicalPageMapping.is_writer rotation.
_clear_pending_device_error()
¶
Clear the error a failed copy left pending for the next kernel launch.
_relax_stream_capture()
¶
Let this thread call cudaHostRegister while another captures a graph.
A global-mode capture otherwise fails on such a call from any thread.
_select_swap_blocks_fn(layer_refs_per_group, gpu_to_cpu, host_memory_is_pinned=True)
¶
Resolve the swap_blocks function for a handler at init time.
compute_sub_block_ptrs(block_ids, blocks_per_chunk, output, tensor, skip_count=0)
¶
Compute byte pointers for sub-blocks of the given block IDs.
Each block in block_ids contains blocks_per_chunk sub-blocks. The pointer for sub-block j of block b is: base_ptr + b * row_stride + j * block_page_size
where block_page_size = tensor.shape[1] // blocks_per_chunk (gpu page size).
This handles tensors where row_stride != blocks_per_chunk * block_page_size (e.g. non-contiguous CPU tensors).
Parameters:
-
(block_ids¶ndarray) –array of block IDs at the tensor's native granularity.
-
(blocks_per_chunk¶int) –number of sub-blocks per block.
-
(output¶ndarray) –pre-allocated pointer array to write pointers into.
-
(tensor¶Tensor) –the source or destination tensor.
-
(skip_count¶int, default:0) –sub-blocks to skip in the first block.
pin_mmap_region(region, worker_slot_only=False, should_stop=None, max_chunk_bytes=None, pace=0.0)
¶
Register row-aligned chunks or this worker's slots, rolling back on failure or when should_stop() turns true.
pace > 0 sleeps that multiple of each registration's duration after it: back-to-back registrations hold a CUDA driver lock that starves other threads' CUDA calls, which stalls serving while this runs in the background.