vllm.v1.kv_offload.cpu.shared_offload_region
¶
Classes:
-
SharedOffloadRegion–Single mmap-backed memory region shared across all workers for a
SharedOffloadRegion
¶
Single mmap-backed memory region shared across all workers for a vLLM instance. Workers coordinate via the filesystem: the first worker to open the file with O_EXCL becomes the creator and calls ftruncate; the rest open the existing file and wait until it reaches the expected size. Each worker then mmap()s the full file.
File path: mmap_path if given, and engine_id is then unused;
otherwise /dev/shm/vllm_offload_{engine_id}.mmap. Further segment files
add a .{i} suffix. When a barrier is given, the path is unlinked once
every worker has mapped the file, so the kernel reclaims the memory when
the last worker exits, no matter how it exits; mappings taken before the
unlink stay valid.
Regions opened at the same mmap_path share its files. The caller owns
files at a custom path: pass no barrier to keep them for later openers.
populate_shard=(index, count) pre-faults only this worker's contiguous
share, for layouts where every worker maps the same bytes. The barrier
also waits for all shards to finish pre-faulting before returning.
num_segments splits backing files beneath the contiguous mapping.
populate=False skips pre-faulting, for a mapper whose peers pre-fault
every page or that calls prefault() later.
private=True declares that no peer maps this file: it takes no barrier
and the path is unlinked as soon as this worker has mapped it.
Methods:
-
create_kv_memoryview–Return a zero-copy memoryview over the entire KV buffer.
-
create_next_canonical_view–Allocate a strided int8 view shared by all workers for one
-
create_next_worker_view–Allocate a strided int8 view for this worker, one canonical tensor.
-
iter_worker_slot_ranges–Yield this worker's slots as page-aligned (offset, length) ranges.
-
prefault–Pre-fault a range without holding the GIL.
-
prefault_ranges–Ranges a deferred pre-fault covers: this rank's slots, or the whole
_map_segments()
¶
Replace the anonymous mapping with the segment files, back to back.
create_kv_memoryview()
¶
Return a zero-copy memoryview over the entire KV buffer.
Shape: (num_chunks, row_stride_bytes). Secondary tiers address
chunk b as view[b].
create_next_canonical_view(tensor_page_size)
¶
Allocate a strided int8 view shared by all workers for one canonical tensor (canonical layout).
Must be called once per canonical tensor, instead of create_next_worker_view. The full mmap layout is:
|<-------- canonical area ------->|<-------- unused ------->|
| all workers share this area | |
| | |
| [ canonical_t0 | canonical_t1 ] | |
| [ canonical_t0 | canonical_t1 ] | |
| [ canonical_t0 | canonical_t1 ] | |
^ ^
_canonical_offset=0, then advances by each tensor's size
Each canonical_t{i} cell is that tensor's canonical page for the chunk. Canonical areas are carved consecutively from the start of each chunk row; consecutive rows are separated by row_stride. Every worker gets the identical byte ranges and writes only its disjoint bytes within them, as described by its canonical mappings — unlike create_next_worker_view, which gives each worker a private cpu_page_size slot per row.
The trailing unused bytes exist only when the canonical pages sum to less than row_stride: page-alignment padding of the row, or deduplication of KV replicated across workers (e.g. the MLA latent), where one canonical copy replaces world_size worker copies.
Parameters:
create_next_worker_view(tensor_page_size)
¶
Allocate a strided int8 view for this worker, one canonical tensor.
Must be called once per canonical tensor. The full mmap layout is:
worker0_chunk0 | worker1_chunk0 | ... | worker{M-1}_chunk0
worker0_chunk1 | worker1_chunk1 | ... | worker{M-1}_chunk1
...
Each worker_chunk cell is cpu_page_size bytes and holds all canonical tensors for that worker and chunk concatenated: [ tensor0_data | tensor1_data | ... | tensor{L-1}_data ]
Consecutive rows are separated by row_stride = cpu_page_size * M.
Returns an int8 tensor of shape (num_chunks, tensor_page_size) with stride (row_stride, 1). Using int8 keeps stride == bytes, so swap_blocks address arithmetic works without any dtype conversion.
Parameters:
iter_worker_slot_ranges()
¶
Yield this worker's slots as page-aligned (offset, length) ranges.
prefault(offset, length)
¶
Pre-fault a range without holding the GIL.
prefault_ranges(shard=None)
¶
Ranges a deferred pre-fault covers: this rank's slots, or the whole
region, which shard=(index, count) walks from that share onwards.
_get_populate_write_fn(mmap_obj)
¶
Select the pre-faulting method once for this mmap.
_wait_for_file_size(fd, expected_size, timeout=30.0)
¶
Spin-wait until the file reaches expected_size (creator truncated it).