[KV Offload] Pass request context (#39185)

Signed-off-by: omerpaz95 <omerpaz95@gmail.com>
This commit is contained in:
omerpaz95
2026-04-19 08:54:59 +03:00
committed by GitHub
parent 4b7f5ea1a0
commit 4353c9cb4a
7 changed files with 170 additions and 81 deletions
@@ -19,6 +19,7 @@ from vllm.v1.core.sched.output import SchedulerOutput
from vllm.v1.kv_offload.abstract import (
OffloadingManager,
OffloadKey,
ReqContext,
get_offload_block_hash,
make_offload_key,
)
@@ -74,6 +75,7 @@ class RequestOffloadState:
config: SchedulerOffloadConfig
req: Request
group_states: tuple[RequestGroupState, ...] = field(init=False)
req_context: ReqContext = field(init=False)
# number of hits in the GPU cache
num_locally_computed_tokens: int = 0
@@ -81,6 +83,7 @@ class RequestOffloadState:
self.group_states = tuple(
RequestGroupState() for _ in self.config.kv_group_configs
)
self.req_context = ReqContext(kv_transfer_params=self.req.kv_transfer_params)
def update_offload_keys(self) -> None:
for group_config, group_state in zip(
@@ -181,7 +184,10 @@ class OffloadingConnectorScheduler:
return 0, False
start_block_idx = num_computed_tokens // group_config.offloaded_block_size
hits = self.manager.lookup(offload_keys[start_block_idx:])
hits = self.manager.lookup(
offload_keys[start_block_idx:],
req_status.req_context,
)
if hits is None:
# indicates a lookup that should be tried later
return None, False
@@ -249,7 +255,7 @@ class OffloadingConnectorScheduler:
assert len(request.block_hashes) // self.config.block_size_factor >= num_blocks
offload_keys = group_state.offload_keys[start_block_idx:num_blocks]
src_spec = self.manager.prepare_load(offload_keys)
src_spec = self.manager.prepare_load(offload_keys, req_status.req_context)
dst_spec = GPULoadStoreSpec(
block_ids[num_computed_gpu_blocks:],
group_sizes=(num_pending_gpu_blocks,),
@@ -304,7 +310,9 @@ class OffloadingConnectorScheduler:
assert len(req.block_hashes) >= num_gpu_blocks
new_offload_keys = group_state.offload_keys[start_block_idx:num_blocks]
store_output = self.manager.prepare_store(new_offload_keys)
store_output = self.manager.prepare_store(
new_offload_keys, req_status.req_context
)
if store_output is None:
logger.warning(
"Request %s: cannot store %s blocks", req_id, num_new_blocks
+24 -4
View File
@@ -30,7 +30,7 @@ The class provides the following primitives:
from abc import ABC, abstractmethod
from collections.abc import Iterable
from dataclasses import dataclass
from typing import NewType
from typing import Any, NewType
# `OffloadKey` identifies an offloaded block. It combines a block hash with
# its KV cache group index, encoded as raw bytes to avoid tuple GC overhead.
@@ -53,6 +53,11 @@ def get_offload_group_idx(key: OffloadKey) -> int:
return int.from_bytes(key[-4:], "big", signed=False)
@dataclass
class ReqContext:
kv_transfer_params: dict[str, Any] | None = None
class LoadStoreSpec(ABC):
"""
Abstract metadata that encapsulates information allowing a worker
@@ -86,13 +91,18 @@ class OffloadingEvent:
class OffloadingManager(ABC):
@abstractmethod
def lookup(self, keys: Iterable[OffloadKey]) -> int | None:
def lookup(
self,
keys: Iterable[OffloadKey],
req_context: ReqContext,
) -> int | None:
"""
Finds the length of the maximal series of blocks, starting from the
first one, that are all offloaded.
Args:
keys: the keys identifying the blocks to lookup.
req_context: per-request context (e.g. kv_transfer_params).
Returns:
An integer representing the maximal number of blocks that
@@ -103,7 +113,11 @@ class OffloadingManager(ABC):
pass
@abstractmethod
def prepare_load(self, keys: Iterable[OffloadKey]) -> LoadStoreSpec:
def prepare_load(
self,
keys: Iterable[OffloadKey],
req_context: ReqContext,
) -> LoadStoreSpec:
"""
Prepare the given blocks to be read.
The given blocks will be protected from eviction until
@@ -112,6 +126,7 @@ class OffloadingManager(ABC):
Args:
keys: the keys identifying the blocks.
req_context: per-request context (e.g. kv_transfer_params).
Returns:
A LoadStoreSpec that can be used by a worker to locate and load
@@ -139,7 +154,11 @@ class OffloadingManager(ABC):
return
@abstractmethod
def prepare_store(self, keys: Iterable[OffloadKey]) -> PrepareStoreOutput | None:
def prepare_store(
self,
keys: Iterable[OffloadKey],
req_context: ReqContext,
) -> PrepareStoreOutput | None:
"""
Prepare the given blocks to be offloaded.
The given blocks will be protected from eviction until
@@ -147,6 +166,7 @@ class OffloadingManager(ABC):
Args:
keys: the keys identifying the blocks.
req_context: per-request context (e.g. kv_transfer_params).
Returns:
A PrepareStoreOutput indicating which blocks need storing,
+16 -3
View File
@@ -9,6 +9,7 @@ from vllm.v1.kv_offload.abstract import (
OffloadingManager,
OffloadKey,
PrepareStoreOutput,
ReqContext,
)
from vllm.v1.kv_offload.cpu.policies.abstract import BlockStatus, CachePolicy
from vllm.v1.kv_offload.cpu.policies.arc import ARCCachePolicy
@@ -83,7 +84,11 @@ class CPUOffloadingManager(OffloadingManager):
# --- OffloadingManager interface ---
def lookup(self, keys: Iterable[OffloadKey]) -> int | None:
def lookup(
self,
keys: Iterable[OffloadKey],
req_context: ReqContext,
) -> int | None:
hit_count = 0
for key in keys:
block = self._policy.get(key)
@@ -92,7 +97,11 @@ class CPUOffloadingManager(OffloadingManager):
hit_count += 1
return hit_count
def prepare_load(self, keys: Iterable[OffloadKey]) -> LoadStoreSpec:
def prepare_load(
self,
keys: Iterable[OffloadKey],
req_context: ReqContext,
) -> LoadStoreSpec:
blocks = []
for key in keys:
block = self._policy.get(key)
@@ -112,7 +121,11 @@ class CPUOffloadingManager(OffloadingManager):
assert block.ref_cnt > 0, f"Block {key!r} ref_cnt is already 0"
block.ref_cnt -= 1
def prepare_store(self, keys: Iterable[OffloadKey]) -> PrepareStoreOutput | None:
def prepare_store(
self,
keys: Iterable[OffloadKey],
req_context: ReqContext,
) -> PrepareStoreOutput | None:
keys_list = list(keys)
# filter out blocks that are already stored
+11 -6
View File
@@ -16,6 +16,7 @@ from vllm.v1.kv_offload.abstract import (
OffloadingManager,
OffloadKey,
PrepareStoreOutput,
ReqContext,
)
@@ -65,7 +66,7 @@ class FilterReusedOffloadingManager(OffloadingManager):
# Intercepted methods
# ------------------------------------------------------------------
def lookup(self, keys: Iterable[OffloadKey]) -> int | None:
def lookup(self, keys: Iterable[OffloadKey], req_context: ReqContext) -> int | None:
"""Record each key, then delegate lookup to backing manager."""
keys = list(keys)
for key in keys:
@@ -76,9 +77,11 @@ class FilterReusedOffloadingManager(OffloadingManager):
if len(self.counts) >= self.max_tracker_size:
self.counts.popitem(last=False) # evict LRU
self.counts[key] = 1
return self._backing.lookup(keys)
return self._backing.lookup(keys, req_context)
def prepare_store(self, keys: Iterable[OffloadKey]) -> PrepareStoreOutput | None:
def prepare_store(
self, keys: Iterable[OffloadKey], req_context: ReqContext
) -> PrepareStoreOutput | None:
"""Filter out blocks below threshold, then delegate to backing.
Filtering is evaluated *before* calling the backing manager's
@@ -93,14 +96,16 @@ class FilterReusedOffloadingManager(OffloadingManager):
# Passing an empty list is intentional and safe — CPUOffloadingManager
# handles it correctly, returning a PrepareStoreOutput with empty lists.
# Delegate to the backing manager with only the eligible keys.
return self._backing.prepare_store(eligible)
return self._backing.prepare_store(eligible, req_context)
# ------------------------------------------------------------------
# Delegated methods
# ------------------------------------------------------------------
def prepare_load(self, keys: Iterable[OffloadKey]) -> LoadStoreSpec:
return self._backing.prepare_load(keys)
def prepare_load(
self, keys: Iterable[OffloadKey], req_context: ReqContext
) -> LoadStoreSpec:
return self._backing.prepare_load(keys, req_context)
def touch(self, keys: Iterable[OffloadKey]) -> None:
return self._backing.touch(keys)