[kv_offload+HMA][4/N]: Support sliding window lookup (#36645)

Signed-off-by: Or Ozeri <oro@il.ibm.com>
Co-authored-by: Nicolò Lucchesi <nlucches@redhat.com>
This commit is contained in:
Or Ozeri
2026-04-20 12:53:51 +03:00
committed by GitHub
co-authored by Nicolò Lucchesi
parent 2aab9acf48
commit f774ba028a
7 changed files with 269 additions and 78 deletions
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
from collections import defaultdict
from collections.abc import Iterable
from collections.abc import Iterable, Sequence
from dataclasses import dataclass, field
from itertools import islice
from typing import Any, NamedTuple
@@ -132,6 +132,49 @@ class OffloadingConnectorScheduler:
self._reqs_being_stored = defaultdict[ReqId, set[OffloadKey]](set)
self._reqs_being_loaded = defaultdict[ReqId, set[OffloadKey]](set)
def _maximal_prefix_lookup(
self, keys: Iterable[OffloadKey], req_context: ReqContext
) -> int | None:
"""Find the length of the maximal prefix of offloaded blocks."""
hit_count = 0
defer_lookup = False
for key in keys:
result = self.manager.lookup(key, req_context)
if result is None:
defer_lookup = True
# continue lookup to allow manager to kick-off async lookups
# for all blocks (until a miss is detected)
result = True
if not result:
break
hit_count += 1
return hit_count if not defer_lookup else None
def _sliding_window_lookup(
self,
keys: Sequence[OffloadKey],
sliding_window_size: int,
req_context: ReqContext,
) -> int | None:
"""Find the maximal ending position of consecutive offloaded blocks
within a sliding window."""
defer_lookup = False
consecutive_hits = 0
for idx in range(len(keys) - 1, -1, -1):
result = self.manager.lookup(keys[idx], req_context)
if result is None:
defer_lookup = True
# continue lookup to allow manager to kick-off async lookups
# for all blocks (until a hit is detected)
result = False
if not result:
consecutive_hits = 0
else:
consecutive_hits += 1
if consecutive_hits == sliding_window_size:
return idx + sliding_window_size if not defer_lookup else None
return consecutive_hits if not defer_lookup else None
def get_num_new_matched_tokens(
self, request: Request, num_computed_tokens: int
) -> tuple[int | None, bool]:
@@ -184,9 +227,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:],
req_status.req_context,
# Full attention relays on all previous KV cache blocks.
# Thus, we search for a maximal prefix of KV cache which are all cached.
hits = self._maximal_prefix_lookup(
offload_keys[start_block_idx:], req_status.req_context
)
if hits is None:
# indicates a lookup that should be tried later
+7 -13
View File
@@ -7,8 +7,7 @@ This class runs in the scheduler, tracks which blocks are offloaded
and their address.
The class provides the following primitives:
lookup() - find the length of the maximal series of blocks,
starting from the first one, that are all offloaded.
lookup() - check whether a single block is offloaded and ready.
prepare_load() - prepare given blocks to be read.
The given blocks will be protected from eviction.
This function returns a LoadSpec which encapsulates
@@ -91,23 +90,18 @@ class OffloadingEvent:
class OffloadingManager(ABC):
@abstractmethod
def lookup(
self,
keys: Iterable[OffloadKey],
req_context: ReqContext,
) -> int | None:
def lookup(self, key: OffloadKey, req_context: ReqContext) -> bool | None:
"""
Finds the length of the maximal series of blocks, starting from the
first one, that are all offloaded.
Checks whether a single block is offloaded and ready to be read.
Args:
keys: the keys identifying the blocks to lookup.
key: the key identifying the block to lookup.
req_context: per-request context (e.g. kv_transfer_params).
Returns:
An integer representing the maximal number of blocks that
are currently offloaded, or None if the lookup should be retried
later. Returning None will delay the request handling by the vLLM
True if the block is offloaded and ready, False if not,
or None if the lookup should be retried later.
Returning None will delay the request handling by the vLLM
scheduler.
"""
pass
+3 -12
View File
@@ -84,18 +84,9 @@ class CPUOffloadingManager(OffloadingManager):
# --- OffloadingManager interface ---
def lookup(
self,
keys: Iterable[OffloadKey],
req_context: ReqContext,
) -> int | None:
hit_count = 0
for key in keys:
block = self._policy.get(key)
if block is None or not block.is_ready:
break
hit_count += 1
return hit_count
def lookup(self, key: OffloadKey, req_context: ReqContext) -> bool | None:
block = self._policy.get(key)
return block is not None and block.is_ready
def prepare_load(
self,
+12 -13
View File
@@ -27,8 +27,9 @@ class FilterReusedOffloadingManager(OffloadingManager):
All methods are delegated to the *backing* manager. Two methods are
intercepted:
* ``lookup`` — records each visited key in an internal LRU counter.
* ``prepare_store`` — filters out keys that have not yet
* ``lookup`` — records the visited key in an internal LRU
counter, then delegates to the backing manager.
crossed the threshold *before* calling the backing
``prepare_store``.
@@ -66,18 +67,16 @@ class FilterReusedOffloadingManager(OffloadingManager):
# Intercepted methods
# ------------------------------------------------------------------
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:
if key in self.counts:
self.counts.move_to_end(key)
self.counts[key] += 1
else:
if len(self.counts) >= self.max_tracker_size:
self.counts.popitem(last=False) # evict LRU
self.counts[key] = 1
return self._backing.lookup(keys, req_context)
def lookup(self, key: OffloadKey, req_context: ReqContext) -> bool | None:
"""Record the key, then delegate lookup to backing manager."""
if key in self.counts:
self.counts.move_to_end(key)
self.counts[key] += 1
else:
if len(self.counts) >= self.max_tracker_size:
self.counts.popitem(last=False) # evict LRU
self.counts[key] = 1
return self._backing.lookup(key, req_context)
def prepare_store(
self, keys: Iterable[OffloadKey], req_context: ReqContext