[KV Offload] Pin only each worker's own offload slots (#59544)
Signed-off-by: Itay Etelis <92247226+Etelis@users.noreply.github.com> Co-authored-by: Robert Shaw <114415538+robertgshaw2-redhat@users.noreply.github.com> Co-authored-by: Codex <noreply@openai.com> Co-authored-by: Kevin H. Luu <khluu000@gmail.com> Co-authored-by: Or Ozeri <oro@il.ibm.com>
This commit is contained in:
co-authored by
Robert Shaw
Codex
Kevin H. Luu
Or Ozeri
parent
6605802904
commit
5341b5ba7d
@@ -3,7 +3,7 @@
|
||||
import functools
|
||||
import time
|
||||
from collections import deque
|
||||
from collections.abc import Sequence
|
||||
from collections.abc import Iterator, Sequence
|
||||
from dataclasses import dataclass
|
||||
from typing import NamedTuple
|
||||
|
||||
@@ -196,10 +196,14 @@ def _canonical_block_sizes(
|
||||
|
||||
# Bound registration size to avoid driver limits on large host allocations.
|
||||
MAX_HOST_REGISTER_CHUNK_BYTES = 64 * 1024**3
|
||||
# Amortize per-call overhead with the bytes not registered in each row.
|
||||
MIN_HOST_REGISTER_SAVED_BYTES = 3 * 1024**2
|
||||
|
||||
|
||||
def pin_mmap_region(region: SharedOffloadRegion) -> None:
|
||||
"""Register row-aligned chunks, rolling back on failure."""
|
||||
def pin_mmap_region(
|
||||
region: SharedOffloadRegion, worker_slot_only: bool = False
|
||||
) -> None:
|
||||
"""Register row-aligned chunks or this worker's slots, rolling back on failure."""
|
||||
rank = region.rank
|
||||
base_ptr = region._base.data_ptr()
|
||||
total_size = region.total_size_bytes
|
||||
@@ -208,15 +212,40 @@ def pin_mmap_region(region: SharedOffloadRegion) -> None:
|
||||
# registrations.
|
||||
rows_per_chunk = max(MAX_HOST_REGISTER_CHUNK_BYTES // region._row_stride, 1)
|
||||
chunk_size = rows_per_chunk * region._row_stride
|
||||
ranges: Iterator[tuple[int, int]] = (
|
||||
(offset, min(chunk_size, total_size - offset))
|
||||
for offset in range(0, total_size, chunk_size)
|
||||
)
|
||||
stride, slot, page = region._row_stride, region.cpu_page_size, region.page_size
|
||||
min_savings = max(page, MIN_HOST_REGISTER_SAVED_BYTES)
|
||||
if worker_slot_only and region.rank is not None and stride - slot >= min_savings:
|
||||
# Round each slot out to pages; the worker's next slot is a page away.
|
||||
ranges = region.iter_worker_slot_ranges()
|
||||
else:
|
||||
reason = (
|
||||
"whole-region layout requested"
|
||||
if not worker_slot_only
|
||||
else "no worker rank"
|
||||
if region.rank is None
|
||||
else f"less than {min_savings} bytes saved per row"
|
||||
)
|
||||
logger.info(
|
||||
"Registering full CPU offload region rank=%s (row=%d, slot=%d bytes): %s",
|
||||
rank,
|
||||
stride,
|
||||
slot,
|
||||
reason,
|
||||
)
|
||||
|
||||
# Register, drain and roll back through the same helper, so a failed
|
||||
# chunk leaves neither a pending error nor a partly pinned region.
|
||||
addresses: list[int] = []
|
||||
for offset in range(0, total_size, chunk_size):
|
||||
registered_bytes = 0
|
||||
for offset, size in ranges:
|
||||
address = base_ptr + offset
|
||||
size = min(chunk_size, total_size - offset)
|
||||
if host_register(address, size):
|
||||
addresses.append(address)
|
||||
registered_bytes += size
|
||||
continue
|
||||
logger.warning(
|
||||
"host_register failed for rank=%d at %.2f of %.2f GB; "
|
||||
@@ -234,7 +263,7 @@ def pin_mmap_region(region: SharedOffloadRegion) -> None:
|
||||
logger.debug(
|
||||
"Host-registered mmap region rank=%d %.2f GB in %d chunk(s)",
|
||||
rank,
|
||||
total_size / 1e9,
|
||||
registered_bytes / 1e9,
|
||||
len(addresses),
|
||||
)
|
||||
|
||||
@@ -785,7 +814,7 @@ class CPUOffloadingWorker(OffloadingWorker):
|
||||
pin_memory = PIN_MEMORY
|
||||
logger.info("Allocating %d CPU tensors...", len(kv_caches.tensors))
|
||||
if mmap_region is not None and pin_memory:
|
||||
pin_mmap_region(mmap_region)
|
||||
pin_mmap_region(mmap_region, worker_slot_only=not canonical_layout)
|
||||
host_memory_is_pinned = pin_memory and (
|
||||
mmap_region is None or mmap_region.is_pinned
|
||||
)
|
||||
|
||||
@@ -9,6 +9,8 @@ pure bandwidth optimization: when registration is unavailable the transfers
|
||||
still run correctly, just as pageable (slower) copies.
|
||||
"""
|
||||
|
||||
import functools
|
||||
|
||||
import torch
|
||||
|
||||
from vllm.distributed.device_communicators.cuda_wrapper import CudaRTLibrary
|
||||
@@ -18,9 +20,14 @@ from vllm.platforms import current_platform
|
||||
logger = init_logger(__name__)
|
||||
|
||||
|
||||
@functools.cache
|
||||
def _get_cudart() -> CudaRTLibrary:
|
||||
return CudaRTLibrary()
|
||||
|
||||
|
||||
def _load_cudart() -> CudaRTLibrary | None:
|
||||
try:
|
||||
return CudaRTLibrary()
|
||||
return _get_cudart()
|
||||
except (AssertionError, AttributeError, OSError):
|
||||
logger.warning(
|
||||
"Could not load the CUDA runtime for host registration; "
|
||||
|
||||
@@ -5,7 +5,7 @@ import errno
|
||||
import mmap
|
||||
import os
|
||||
import time
|
||||
from collections.abc import Callable
|
||||
from collections.abc import Callable, Iterator
|
||||
|
||||
import numpy as np
|
||||
import torch
|
||||
@@ -106,6 +106,7 @@ class SharedOffloadRegion:
|
||||
self.num_chunks = num_chunks
|
||||
self._row_stride = kv_bytes_per_chunk
|
||||
self.total_size_bytes = self.num_chunks * self._row_stride
|
||||
self.cpu_page_size = cpu_page_size
|
||||
|
||||
self.mmap_path = f"/dev/shm/vllm_offload_{engine_id}.mmap"
|
||||
self._creator = False # set True only if this worker creates the file
|
||||
@@ -208,15 +209,9 @@ class SharedOffloadRegion:
|
||||
|
||||
if rank is not None:
|
||||
# Populate only this worker's pages (one slot per chunk row).
|
||||
worker_offset = rank * cpu_page_size
|
||||
_t0 = time.perf_counter()
|
||||
page_size = self.page_size
|
||||
for chunk in range(num_chunks):
|
||||
raw_offset = chunk * self._row_stride + worker_offset
|
||||
aligned_offset = (raw_offset // page_size) * page_size
|
||||
end = raw_offset + cpu_page_size
|
||||
aligned_length = end - aligned_offset
|
||||
populate_write_fn(self.mmap_obj, aligned_offset, aligned_length)
|
||||
for offset, length in self.iter_worker_slot_ranges():
|
||||
populate_write_fn(self.mmap_obj, offset, length)
|
||||
logger.debug(
|
||||
"MADV_POPULATE_WRITE loop: %d chunks in %.3f s",
|
||||
num_chunks,
|
||||
@@ -230,6 +225,16 @@ class SharedOffloadRegion:
|
||||
"MADV_POPULATE_WRITE entire region: %.3f s", time.perf_counter() - _t0
|
||||
)
|
||||
|
||||
def iter_worker_slot_ranges(self) -> Iterator[tuple[int, int]]:
|
||||
"""Yield this worker's slots as page-aligned (offset, length) ranges."""
|
||||
assert self.rank is not None
|
||||
worker_offset = self.rank * self.cpu_page_size
|
||||
for row in range(self.num_chunks):
|
||||
start = row * self._row_stride + worker_offset
|
||||
aligned = start - start % self.page_size
|
||||
end = round_up(start + self.cpu_page_size, self.page_size)
|
||||
yield aligned, end - aligned
|
||||
|
||||
@property
|
||||
def base_tensor(self) -> torch.Tensor:
|
||||
if self._base is None:
|
||||
|
||||
Reference in New Issue
Block a user