Skip to content

vllm.v1.kv_offload.cpu.worker

Platform-independent CPU offloading worker.

Copy planning, transfer queues and local host-memory allocation live here. Copies, streams/events and host registration go through the CopyEngine and DeviceRuntime contracts; device selection, synchronization and pinned host allocation use torch directly.

Classes:

  • CPUOffloadingWorker –

    OffloadingWorker for CPU offloading.

  • CopyPlan –

    Precomputed fragment-copy template for one data ref under the canonical

  • CopyPlanner –

    Computes the copy descriptors of transfers in one direction, either

  • SingleDirectionOffloadingHandler –

    Handles transfers for a single direction, either CPU->device or

  • TensorRows –

    Where the rows of a 2D int8 KV tensor are: row i starts at

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__(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, runtime=None)

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

submit_store(job_id, device_ptrs, dst_spec)

Async device -> 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.

CopyPlanner

Computes the copy descriptors of transfers in one direction, either CPU->device or device->CPU, from the resolved device pointers and where the rows of each CPU KV tensor are.

Methods:

  • __init__ –

    Initialize a CopyPlanner.

__init__(cpu_rows, blocks_per_chunk, layer_refs_per_group, device_to_cpu, canonical_layout=False)

Initialize a CopyPlanner.

Parameters:

  • cpu_rows

    (list[TensorRows]) –

    rows of each CPU KV cache tensor, one CPU chunk each.

  • blocks_per_chunk

    (int) –

    number of device blocks per CPU chunk.

  • layer_refs_per_group

    (list[list[CanonicalKVCacheRef]]) –

    list of CanonicalKVCacheRef per group.

  • device_to_cpu

    (bool) –

    if True, transfer from device to CPU; otherwise CPU to device.

  • canonical_layout

    (bool, default: False ) –

    if True, CPU pages use the canonical layout described by the refs' mappings.

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

_fill_ops(device_ptrs, cpu_spec, all_src, all_dst, all_sizes)

Fill the copy descriptors of one transfer.

Returns (number of descriptors filled, bytes they copy).

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

SingleDirectionOffloadingHandler

Bases: CopyPlanner

Handles transfers for a single direction, either CPU->device or device->CPU. Transfers are guaranteed to be executed in order of their submission. Each transfer uses a unique 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, device_to_cpu, canonical_layout=False, *, engine)

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 device blocks per CPU chunk.

  • layer_refs_per_group

    (list[list[CanonicalKVCacheRef]]) –

    list of CanonicalKVCacheRef per group.

  • device_to_cpu

    (bool) –

    if True, transfer from device to CPU; otherwise CPU to device.

  • canonical_layout

    (bool, default: False ) –

    if True, CPU pages use the canonical layout described by the refs' mappings.

  • engine

    (CopyEngine) –

    copy engine for this direction.

shutdown()

Drain this direction and release its transfer-side resources.

TensorRows

Bases: NamedTuple

Where the rows of a 2D int8 KV tensor are: row i starts at base + i * stride and holds size bytes. Copy plans need nothing more, so they can address memory that this process has not allocated.

_build_copy_plan(ref, device_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 device 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.

compute_sub_block_ptrs(block_ids, blocks_per_chunk, output, rows, 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 = rows.size // blocks_per_chunk (device 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.

  • rows

    (TensorRows) –

    the rows of 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.