vllm.v1.kv_offload.cpu.policies.base
¶
Classes:
-
CachePolicy–Encapsulates both chunk organization (data structures) and replacement
-
ChunkStatus–Offloading status for a single chunk of KV data.
Functions:
-
order_request_keys–Return one head-to-tail order across all KV cache groups.
CachePolicy
¶
Bases: ABC
Encapsulates both chunk organization (data structures) and replacement decisions (which chunk to evict). LRU and ARC differ in both dimensions — ARC's ghost lists and target_t1_size live at the intersection of storage and eviction, so they cannot be separated cleanly.
Methods:
-
clear–Remove ALL chunks regardless of ref_cnt.
-
evict–Evict exactly n chunks, skipping any in protected.
-
get–Find chunk in data structures. Returns None if not present.
-
insert–Add a newly allocated chunk with the storing request's context.
-
mark_evictable–Called when a chunk's ref_cnt transitions to 0.
-
mark_non_evictable–Called when a chunk's ref_cnt transitions from 0.
-
on_request_access–Observe first accesses without allocating new chunks.
-
on_request_finished–Apply one request-scoped cache access in prefix order.
-
on_retention_changed–Called whenever a resident chunk gains its first lease or loses its last.
-
on_store_miss–Observe store misses before their cache entries are inserted.
-
remove–Remove a chunk (used to clean up after a failed store).
clear()
abstractmethod
¶
Remove ALL chunks regardless of ref_cnt.
Ghost lists and adaptive state are also reset.
evict(n, protected)
abstractmethod
¶
Evict exactly n chunks, skipping any in protected.
Built-in policies prefer eligible non-retained chunks, falling back to eligible retained chunks if needed. Retention is optional for external policies.
Returns a list of (key, chunk) for the evicted chunks, or None if n evictions cannot be satisfied. The operation is atomic: if None is returned, no state changes are made.
For ARC: ghost list cleanup (trimming to cache_capacity) is performed at the end of a successful eviction.
get(key)
abstractmethod
¶
Find chunk in data structures. Returns None if not present.
insert(key, chunk, req_context)
abstractmethod
¶
Add a newly allocated chunk with the storing request's context.
For ARC: also removes from ghost lists.
mark_evictable(key)
¶
Called when a chunk's ref_cnt transitions to 0.
mark_non_evictable(key)
¶
Called when a chunk's ref_cnt transitions from 0.
on_request_access(keys, req_context)
¶
Observe first accesses without allocating new chunks.
Keys first observed here may not yet be resident. This hook can
maintain prefix order during a request without committing the
frequency access handled by on_request_finished.
on_request_finished(key_groups, insertion_only_keys, reused_keys, req_context)
abstractmethod
¶
Apply one request-scoped cache access in prefix order.
key_groups contains keys observed for each KV cache group in
head-to-tail order. insertion_only_keys and reused_keys
distinguish chunks only created by this request from ready chunks it
actually reused, which matters for policies such as ARC where reuse
changes frequency but insertion and pending observations do not.
on_retention_changed(key, retained)
¶
Called whenever a resident chunk gains its first lease or loses its last.
on_store_miss(keys, req_context)
¶
Observe store misses before their cache entries are inserted.
Policies may override this hook when a miss has distinct semantics, such as ARC adapting to a ghost-list hit.
remove(key)
abstractmethod
¶
Remove a chunk (used to clean up after a failed store).
ChunkStatus
¶
Bases: Structure
Offloading status for a single chunk of KV data. Holds the following information:
ref_cnt - the current number of transfers using this chunk as a source. A value of -1 indicates the chunk is not yet ready to be read. lease_cnt - the number of leases preferring to keep the chunk resident. chunk_id - index of the physical CPU buffer slot. ready_time - monotonic time at which the chunk became readable. last_demand_time - monotonic time of the last demand, or ready_time if there was none. ever_used - whether a GPU load or a peer demanded the chunk.
Attributes:
order_request_keys(key_groups, req_context)
¶
Return one head-to-tail order across all KV cache groups.