[Refactor][kv_offload]: rename block→chunk (#52615)

Signed-off-by: Ronen Schaffer <ronen.schaffer@ibm.com>
This commit is contained in:
Ronen Schaffer
2026-09-09 17:45:49 +03:00
committed by GitHub
parent 9ffb8cea96
commit 138d137b5b
34 changed files with 933 additions and 916 deletions
+87 -87
View File
@@ -23,7 +23,7 @@ from vllm.v1.kv_offload.cpu.common import (
CPULoadStoreSpec,
CPUOffloadingMetrics,
)
from vllm.v1.kv_offload.cpu.policies.base import BlockStatus, CachePolicy
from vllm.v1.kv_offload.cpu.policies.base import CachePolicy, ChunkStatus
from vllm.v1.kv_offload.cpu.policies.factory import CachePolicyFactory
@@ -34,14 +34,14 @@ class CPUOffloadingManager(OffloadingManager):
register their own or be loaded out-of-tree via cache_policy_module_path).
The manager owns all shared logic: ref-counting, event emission,
block pool management, and the prepare_store/complete_store skeletons.
Policy-specific block organization and eviction decisions are delegated
chunk pool management, and the prepare_store/complete_store skeletons.
Policy-specific chunk organization and eviction decisions are delegated
to the CachePolicy implementation.
"""
def __init__(
self,
num_blocks: int,
num_chunks: int,
cache_policy: str = "lru",
cache_policy_module_path: str | None = None,
enable_events: bool = False,
@@ -49,59 +49,59 @@ class CPUOffloadingManager(OffloadingManager):
max_tracker_size: int = 64_000,
):
self.medium: Medium = Medium.CPU
self._num_blocks: int = num_blocks
self._num_allocated_blocks: int = 0
self._num_chunks: int = num_chunks
self._num_allocated_chunks: int = 0
self._free_list: list[int] = []
self.events: list[OffloadingEvent] | None = [] if enable_events else None
policy_cls = CachePolicyFactory.get_cache_policy_cls(
cache_policy, cache_policy_module_path
)
self._policy: CachePolicy = policy_cls(cache_capacity=num_blocks)
# Track the number of blocks in the cache that are evictable. i.e. ref_cnt 0.
self._num_evictable_cache_blocks: int = 0
# Track blocks with an in-flight store (ref_cnt -1, not yet completed).
self._num_write_pending_blocks: int = 0
self._policy: CachePolicy = policy_cls(cache_capacity=num_chunks)
# Track the number of chunks in the cache that are evictable. i.e. ref_cnt 0.
self._num_evictable_cache_chunks: int = 0
# Track chunks with an in-flight store (ref_cnt -1, not yet completed).
self._num_write_pending_chunks: int = 0
self.store_threshold: int = store_threshold
self.max_tracker_size: int = max_tracker_size
self.stores_skipped_in_current_batch: int = 0
self.allocation_sizes_in_current_batch: list[int] = []
# Number of block references. It is ordered so can evict the LRU entry in O(1).
# Number of chunk references. It is ordered so can evict the LRU entry in O(1).
self.counts: OrderedDict[OffloadKey, int] | None = (
OrderedDict() if store_threshold >= 2 else None
)
# --- block pool ---
# --- chunk pool ---
def _get_num_free_blocks(self) -> int:
return len(self._free_list) + self._num_blocks - self._num_allocated_blocks
def _get_num_free_chunks(self) -> int:
return len(self._free_list) + self._num_chunks - self._num_allocated_chunks
def _allocate_blocks(self, keys: list[OffloadKey]) -> list[BlockStatus]:
num_fresh = min(len(keys), self._num_blocks - self._num_allocated_blocks)
def _allocate_chunks(self, keys: list[OffloadKey]) -> list[ChunkStatus]:
num_fresh = min(len(keys), self._num_chunks - self._num_allocated_chunks)
num_reused = len(keys) - num_fresh
assert len(self._free_list) >= num_reused
# allocate fresh blocks
blocks: list[BlockStatus] = []
# allocate fresh chunks
chunks: list[ChunkStatus] = []
for _ in range(num_fresh):
blocks.append(BlockStatus(self._num_allocated_blocks))
self._num_allocated_blocks += 1
chunks.append(ChunkStatus(self._num_allocated_chunks))
self._num_allocated_chunks += 1
# allocate reused blocks
# allocate reused chunks
for _ in range(num_reused):
blocks.append(BlockStatus(self._free_list.pop()))
return blocks
chunks.append(ChunkStatus(self._free_list.pop()))
return chunks
def _free_block(self, block: BlockStatus) -> None:
self._free_list.append(block.block_id)
def _free_chunk(self, chunk: ChunkStatus) -> None:
self._free_list.append(chunk.chunk_id)
def _get_load_store_spec(
self,
keys: Iterable[OffloadKey],
blocks: Iterable[BlockStatus],
chunks: Iterable[ChunkStatus],
) -> CPULoadStoreSpec:
return CPULoadStoreSpec([block.block_id for block in blocks])
return CPULoadStoreSpec([chunk.chunk_id for chunk in chunks])
def _record_accesses(self, keys: Collection[OffloadKey]) -> None:
"""Record an offer without evicting its tracked candidates."""
@@ -132,10 +132,10 @@ class CPUOffloadingManager(OffloadingManager):
@override
def lookup(self, key: OffloadKey, req_context: ReqContext) -> LookupResult:
block = self._policy.get(key)
if block is None:
chunk = self._policy.get(key)
if chunk is None:
return LookupResult.MISS
if not block.is_ready:
if not chunk.is_ready:
return LookupResult.HIT_PENDING
return LookupResult.HIT
@@ -145,18 +145,18 @@ class CPUOffloadingManager(OffloadingManager):
keys: Collection[OffloadKey],
req_context: ReqContext,
) -> LoadStoreSpec:
blocks = []
chunks = []
for key in keys:
block = self._policy.get(key)
assert block is not None, f"Block {key!r} not found in cache"
assert block.is_ready, f"Block {key!r} is not ready for reading"
if block.ref_cnt == 0:
chunk = self._policy.get(key)
assert chunk is not None, f"Chunk {key!r} not found in cache"
assert chunk.is_ready, f"Chunk {key!r} is not ready for reading"
if chunk.ref_cnt == 0:
self._policy.mark_non_evictable(key)
self._num_evictable_cache_blocks -= 1 # ref_cnt 0 -> 1
assert self._num_evictable_cache_blocks >= 0
block.ref_cnt += 1
blocks.append(block)
return self._get_load_store_spec(keys, blocks)
self._num_evictable_cache_chunks -= 1 # ref_cnt 0 -> 1
assert self._num_evictable_cache_chunks >= 0
chunk.ref_cnt += 1
chunks.append(chunk)
return self._get_load_store_spec(keys, chunks)
@override
def touch(self, keys: Collection[OffloadKey], req_context: ReqContext) -> None:
@@ -167,12 +167,12 @@ class CPUOffloadingManager(OffloadingManager):
self, keys: Collection[OffloadKey], req_context: ReqContext
) -> None:
for key in keys:
block = self._policy.get(key)
assert block is not None, f"Block {key!r} not found"
assert block.ref_cnt > 0, f"Block {key!r} ref_cnt is already 0"
block.ref_cnt -= 1
if block.ref_cnt == 0:
self._num_evictable_cache_blocks += 1 # ref_cnt 1 -> 0
chunk = self._policy.get(key)
assert chunk is not None, f"Chunk {key!r} not found"
assert chunk.ref_cnt > 0, f"Chunk {key!r} ref_cnt is already 0"
chunk.ref_cnt -= 1
if chunk.ref_cnt == 0:
self._num_evictable_cache_chunks += 1 # ref_cnt 1 -> 0
self._policy.mark_evictable(key)
@override
@@ -186,7 +186,7 @@ class CPUOffloadingManager(OffloadingManager):
self._record_accesses(keys)
keys = [k for k in keys if self.counts.get(k, 0) >= self.store_threshold]
self.stores_skipped_in_current_batch += num_keys - len(keys)
# filter out blocks that are already stored
# filter out chunks that are already stored
keys_to_store = [k for k in keys if self._policy.get(k) is None]
if not keys_to_store:
@@ -197,29 +197,29 @@ class CPUOffloadingManager(OffloadingManager):
)
self.allocation_sizes_in_current_batch.append(len(keys_to_store))
num_blocks_to_evict = len(keys_to_store) - self._get_num_free_blocks()
num_chunks_to_evict = len(keys_to_store) - self._get_num_free_chunks()
to_evict: list[OffloadKey] = []
if num_blocks_to_evict > 0:
if num_blocks_to_evict > self._num_evictable_cache_blocks:
if num_chunks_to_evict > 0:
if num_chunks_to_evict > self._num_evictable_cache_chunks:
# Eviction will fail.
return None
# There is a still a chance for eviction failure as some of the
# idle blocks might be in the protected list.
# idle chunks might be in the protected list.
# Blocks from the original input are excluded from eviction candidates:
# a block that was already stored must remain in the cache after this call.
# Chunks from the original input are excluded from eviction candidates:
# a chunk that was already stored must remain in the cache after this call.
protected = set(keys)
evicted = self._policy.evict(num_blocks_to_evict, protected)
evicted = self._policy.evict(num_chunks_to_evict, protected)
if evicted is None:
return None
# cache-policy removes only idle blocks.
self._num_evictable_cache_blocks -= len(evicted)
assert self._num_evictable_cache_blocks >= 0
# cache-policy removes only idle chunks.
self._num_evictable_cache_chunks -= len(evicted)
assert self._num_evictable_cache_chunks >= 0
for key, block in evicted:
self._free_block(block)
for key, chunk in evicted:
self._free_chunk(chunk)
to_evict.append(key)
if to_evict and self.events is not None:
@@ -231,17 +231,17 @@ class CPUOffloadingManager(OffloadingManager):
)
)
blocks = self._allocate_blocks(keys_to_store)
assert len(blocks) == len(keys_to_store), (
"Block pool did not allocate the expected number of blocks"
chunks = self._allocate_chunks(keys_to_store)
assert len(chunks) == len(keys_to_store), (
"Chunk pool did not allocate the expected number of chunks"
)
for key, block in zip(keys_to_store, blocks):
self._policy.insert(key, block)
self._num_write_pending_blocks += len(keys_to_store)
for key, chunk in zip(keys_to_store, chunks):
self._policy.insert(key, chunk)
self._num_write_pending_chunks += len(keys_to_store)
# build store specs for allocated blocks
store_spec = self._get_load_store_spec(keys_to_store, blocks)
# build store specs for allocated chunks
store_spec = self._get_load_store_spec(keys_to_store, chunks)
return PrepareStoreOutput(
keys_to_store=keys_to_store,
@@ -260,20 +260,20 @@ class CPUOffloadingManager(OffloadingManager):
if success:
for key in keys:
block = self._policy.get(key)
if block is not None and not block.is_ready:
block.ref_cnt = 0
self._num_write_pending_blocks -= 1
self._num_evictable_cache_blocks += 1
chunk = self._policy.get(key)
if chunk is not None and not chunk.is_ready:
chunk.ref_cnt = 0
self._num_write_pending_chunks -= 1
self._num_evictable_cache_chunks += 1
self._policy.mark_evictable(key)
stored_keys.append(key)
else:
for key in keys:
block = self._policy.get(key)
if block is not None and not block.is_ready:
self._num_write_pending_blocks -= 1
chunk = self._policy.get(key)
if chunk is not None and not chunk.is_ready:
self._num_write_pending_chunks -= 1
self._policy.remove(key)
self._free_block(block)
self._free_chunk(chunk)
if stored_keys and self.events is not None:
self.events.append(
@@ -286,17 +286,17 @@ class CPUOffloadingManager(OffloadingManager):
@override
def reset_cache(self) -> None:
# Clear ALL blocks unconditionally. The scheduler's _stale_job_threshold
# Clear ALL chunks unconditionally. The scheduler's _stale_job_threshold
# guarantees that complete_load / complete_store are never called for
# pre-reset jobs, so no lazy cleanup is needed. The scheduler also
# flushes in-flight load job IDs to the workers before any new stores
# can begin, preventing a cross-direction data race on reused offload block IDs.
# can begin, preventing a cross-direction data race on reused offload chunk IDs.
self._policy.clear()
self._num_evictable_cache_blocks = 0
self._num_write_pending_blocks = 0
self._num_evictable_cache_chunks = 0
self._num_write_pending_chunks = 0
self._free_list.clear()
self._num_allocated_blocks = 0
self._num_allocated_chunks = 0
@override
def take_events(self) -> Iterable[OffloadingEvent]:
@@ -309,11 +309,11 @@ class CPUOffloadingManager(OffloadingManager):
# Compute cache usage.
num_used = (
self._num_allocated_blocks
self._num_allocated_chunks
- len(self._free_list)
- self._num_evictable_cache_blocks
- self._num_evictable_cache_chunks
)
usage = num_used / self._num_blocks if self._num_blocks > 0 else 0.0
usage = num_used / self._num_chunks if self._num_chunks > 0 else 0.0
stats.set_gauge(CPUOffloadingMetrics.CPU_CACHE_USAGE_PERC, usage)
for allocation_size in self.allocation_sizes_in_current_batch:
@@ -323,8 +323,8 @@ class CPUOffloadingManager(OffloadingManager):
self.allocation_sizes_in_current_batch.clear()
write_usage = (
self._num_write_pending_blocks / self._num_blocks
if self._num_blocks > 0
self._num_write_pending_chunks / self._num_chunks
if self._num_chunks > 0
else 0.0
)
read_usage = max(usage - write_usage, 0.0)