[Core] Refactor omni prefix cache into Manager/Controller (#6654)

Signed-off-by: ZhengWG <zwg0606@gmail.com>
Co-authored-by: amy-why-3459 <wuhaiyan17@huawei.com>
Co-authored-by: Hongsheng Liu <liuhongsheng4@huawei.com>
This commit is contained in:
authored and GitHub committed 2026-09-19 21:23:59 +08:00
1 parent ea84055507
commit 470ec60848
25 files changed
+5710 -2149

No files matched your search

+58
View File
@@ -0,0 +1,58 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
"""Omni prefix cache.
Structure mirrors vllm/v1/core.
Import invariant: this package must not import ``vllm`` at module
scope (``TYPE_CHECKING`` and function-body imports are fine).
``tests/core/test_prefix_cache.py`` loads these modules without a
vllm install; a top-level ``from vllm...`` turns that suite red.
That is also why these files use ``logging.getLogger`` instead of
the repo-wide ``init_logger`` — ``init_logger`` comes from
``vllm.logger``. The deviation is required, not an oversight.
``_merge_uncached_mm`` is the exception at *call* time: it lazily
imports ``vllm_omni.utils.mm_outputs``, which pulls in vllm. Import
of this package still succeeds without vllm; materialize of leftover
mm does not.
"""
from vllm_omni.core.prefix_cache.block_pool import PrefixBlockPool
from vllm_omni.core.prefix_cache.controller import OmniPrefixCacheController
from vllm_omni.core.prefix_cache.group_view import (
FullAttentionGroupView,
check_prefix_cache_kv_groups,
check_prefix_cache_kv_transfer,
get_prefix_cache_group_view,
stage_prefix_cache_config,
)
from vllm_omni.core.prefix_cache.interface import (
HIDDEN_KEY,
ModelCachePolicy,
OmniPrefixCacheStagingTimeoutError,
OmniPrefixCacheUnmatchError,
PrefixCacheConfig,
StageCacheOutputs,
WriteSchedule,
)
from vllm_omni.core.prefix_cache.manager import OmniPrefixCacheManager
__all__ = [
"HIDDEN_KEY",
"FullAttentionGroupView",
"ModelCachePolicy",
"OmniPrefixCacheController",
"OmniPrefixCacheManager",
"OmniPrefixCacheStagingTimeoutError",
"OmniPrefixCacheUnmatchError",
"StageCacheOutputs",
"PrefixBlockPool",
"PrefixCacheConfig",
"WriteSchedule",
"check_prefix_cache_kv_groups",
"check_prefix_cache_kv_transfer",
"get_prefix_cache_group_view",
"stage_prefix_cache_config",
]
+84
View File
@@ -0,0 +1,84 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
"""Pinned CPU block mirror (mirrors vLLM BlockPool)."""
import logging
import torch
from vllm_omni.core.prefix_cache.interface import PrefixCacheConfig, TensorName
logger = logging.getLogger(__name__)
class PrefixBlockPool:
"""Durable CPU mirror of vLLM KV slots, one tensor per name.
Storage is ``(num_blocks, block_size, feat)`` per key, viewed flat as
``(num_slots, feat)`` so vLLM slot ids index rows directly. Write
access is single-writer (the controller committer thread); readers
take row views.
``_caches`` is not an unbounded store and has no LRU. Dict keys are
tensor names (``__hidden_states__`` plus mm whose first dim is tokens),
opened once by ``ensure_key`` and never dropped — a model emits a
handful of those names, not a per-request set. Each value is a
fixed tensor sized to the same ``num_blocks`` as the upstream KV
pool. Rows *are* those kv slots: when vLLM recycles a block, the
next pool write overwrites that row. Slot reuse is the eviction.
"""
def __init__(self, config: PrefixCacheConfig):
self._config = config
self._caches: dict[TensorName, torch.Tensor] = {}
def _alloc(self, dtype: torch.dtype, feat: int) -> torch.Tensor:
return torch.zeros(
(self._config.num_blocks, self._config.block_size, feat),
dtype=dtype,
device="cpu",
# Pinning lets device→host overlap compute; unsupported on
# CPU-only builds where that overlap is off anyway.
pin_memory=torch.cuda.is_available(),
)
def alloc_key(self, key: str, dtype: torch.dtype, feat: int) -> torch.Tensor | None:
"""Allocate storage for a new ``key`` without publishing it (None if
already open). Pinned allocation is slow; run this unlocked and
hand the tensor to ``install_key`` under the manager's state lock."""
if key in self._caches:
return None
return self._alloc(dtype, feat)
def install_key(self, key: str, storage: torch.Tensor) -> None:
if key in self._caches:
return
self._caches[key] = storage
logger.info("prefix_cache: initialized mirror %s for key %s", list(storage.shape), key)
def ensure_key(self, key: str, dtype: torch.dtype, feat: int) -> None:
storage = self.alloc_key(key, dtype, feat)
if storage is not None:
self.install_key(key, storage)
def has_key(self, key: str) -> bool:
return key in self._caches
def keys(self) -> set[TensorName]:
return set(self._caches.keys())
def _flat(self, key: str) -> torch.Tensor:
cache = self._caches[key]
return cache.view(-1, cache.shape[-1])
def rows(self, key: str, slots: torch.Tensor) -> torch.Tensor:
"""Gather rows for non-contiguous slots (returns a copy)."""
return self._flat(key).index_select(0, slots)
def write(self, key: str, slots: torch.Tensor, src_cpu: torch.Tensor) -> None:
"""Write rows into the pool; caller (committer thread) is the single writer."""
if slots.dtype != torch.int64:
slots = slots.to(torch.int64)
# index_copy_ dispatches to a faster single-dim CPU path than
# advanced-indexing assignment (aten::index_put_).
self._flat(key).index_copy_(0, slots, src_cpu)
+976
View File
@@ -0,0 +1,976 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
"""Runs WriteTasks and the step device→host staging pool.
The manager owns request/slot identity and when to submit. This class
owns the staging pool, the GPU-byte budget, the copy queues, and the
single committer that writes into the CPU block pool.
Two device→host paths — the step path does not write `chunk.host` in
the committer:
JOIN_NEXT_STEP save already launched a whole-step device→host into
a staging slot and set `chunk.host` as views.
Committer waits that `step_d2h_event`, then writes
the pool.
JOIN_ON_FINISH committer copies the device clone → owned host
tensors, then writes the pool.
Async: high-priority then low-priority queues, then pool write.
Eager: submit() does wait+write inline (CPU tests / no CUDA).
"""
from __future__ import annotations
import logging
import threading
import time
from collections import deque
from collections.abc import Callable
from dataclasses import dataclass, field
from enum import Enum, auto
from typing import Literal, NamedTuple
import torch
from vllm_omni.core.prefix_cache.block_pool import PrefixBlockPool
from vllm_omni.core.prefix_cache.interface import (
OmniPrefixCacheStagingTimeoutError,
OmniPrefixCacheUnmatchError,
PrefixCacheConfig,
ReqId,
TensorName,
Tid,
WriteSchedule,
)
logger = logging.getLogger(__name__)
class StagingBufferHolder(NamedTuple):
"""One owner of a staging-buffer slot. The slot is free when none remain.
Not a buffer state — concurrent owners share the same slot:
- for_step: claimed at save, released when materialize/discard consumes the ctx
- for_task: bound before WriteTask submit, released when that task completes
"""
kind: Literal["step", "task"]
owner_id: int
@classmethod
def for_step(cls, step_id: int) -> StagingBufferHolder:
return cls("step", step_id)
@classmethod
def for_task(cls, tid: int) -> StagingBufferHolder:
return cls("task", tid)
@dataclass
class _BudgetTicket:
"""GPU-byte-budget charge for one device clone shared by several writes.
Charged once at ``reserve()``; freed when the last pinned tid releases.
Immediate: the whole-step clone, pinned by that step's JOIN_NEXT_STEP
tasks. Deferred: the shared deferred clone, pinned by every task a
chunk of it was appended to. Does not store tensors.
"""
nbytes: int
tids: set[Tid] = field(default_factory=set)
_freed: bool = False
def release(self, tid: Tid) -> int:
"""Drop `tid`; return the bytes to uncharge (nbytes once, when the
last holder leaves; 0 otherwise). Idempotent per tid."""
self.tids.discard(tid)
if self.tids or self._freed:
return 0
self._freed = True
return self.nbytes
@dataclass
class _WriteChunk:
"""One save's slots + tensors appended onto a WriteTask.
Controller identity is the parent ``tid``, not a request. Deferred
writes grow by appending one chunk per save.
"""
slots_cpu: torch.Tensor # int64 flat row ids, in token order
tensors: dict[TensorName, torch.Tensor] # freeze (device or eager CPU)
host: dict[TensorName, torch.Tensor] = field(default_factory=dict) # host view of rows [0:n)
# GPU-byte charge of the clone these tensors view; shared across the
# chunks cut from one save. None for chunks with no device clone.
budget: _BudgetTicket | None = None
class TaskState(Enum):
"""WriteTask lifecycle: a strict chain, plus FAILED from any non-terminal.
PENDING -> QUEUED -> COPYING -> HOST_READY -> WRITTEN
PENDING registered, not on a copy queue (deferred: waiting for
finish / budget pressure)
QUEUED on a copy queue
COPYING one thread owns the copy stage (staging: waits the step
device→host; deferred: copies the device clone)
HOST_READY host rows ready (`host_ready` set); device clone dropped
WRITTEN in the CPU pool (`done` set)
FAILED committer could not finish (`host_ready` + `done` set;
manager raises from every later entry)
Moves only through `WriteTask.transition`; skipping a step is illegal.
"""
PENDING = auto()
QUEUED = auto()
COPYING = auto()
HOST_READY = auto()
WRITTEN = auto()
FAILED = auto()
@property
def is_terminal(self) -> bool:
return self in (TaskState.WRITTEN, TaskState.FAILED)
# The one legal forward move from each non-terminal state.
_CHAIN: dict[TaskState, TaskState] = {
TaskState.PENDING: TaskState.QUEUED,
TaskState.QUEUED: TaskState.COPYING,
TaskState.COPYING: TaskState.HOST_READY,
TaskState.HOST_READY: TaskState.WRITTEN,
}
_NOT_YET_COPYING = frozenset({TaskState.PENDING, TaskState.QUEUED})
def _is_legal_move(frm: TaskState, to: TaskState) -> bool:
return (to is TaskState.FAILED and not frm.is_terminal) or _CHAIN.get(frm) is to
@dataclass
class WriteTask:
"""One write of (slot, key) rows for a single request.
Identity: `tid` is the handle in the manager's (slot, key) tables.
`req_id` + `write_n` mark whose write this is and the nth time that
request opened a write. One write may cover several keys.
`state` is the single source of truth for where the write is (see
`TaskState`). `host_ready` / `done` are wait primitives set by the
transitions into HOST_READY / WRITTEN / FAILED, never directly.
How HOST_READY is reached:
- JOIN_NEXT_STEP: `chunk.host` is a staging view set at save; the
copy stage only waits `step_d2h_event`.
- JOIN_ON_FINISH: the copy stage copies device→host into `chunk.host`.
`JOIN_NEXT_STEP` is queued at submit. `JOIN_ON_FINISH` stays
PENDING until finish or GPU-byte-budget pressure escalates it;
budget flush takes the unfinished task with the oldest `enqueued_time`.
Concurrent readers/writers:
- Staging readers (materialize clone, committer pool write) all wait
the same `step_d2h_event` before touching the view.
- A later task taking the same (slot, key) records those rows in
`reassigned`; the old pool write skips them.
- `append_chunk` is refused once the copy stage is claimed; the caller
opens a fresh task rather than mutating a closed one.
- `lock` covers `state` / `reassigned` / `append_chunk` / host↔freeze /
`slot_to_row`. `scatter_rows` snapshots `reassigned`.
"""
tid: Tid
req_id: ReqId
write_n: int # 1-based: nth write opened by this request
schedule: WriteSchedule
chunks: list[_WriteChunk]
# On-device clone finished on the compute stream. Copy/read streams
# must wait this before touching `chunks[].tensors`, or they can read
# the next CUDA-graph static-buffer overwrite.
freeze_event: torch.cuda.Event | None = None
state: TaskState = TaskState.PENDING
# Slots this write no longer owns (a newer write took them over).
reassigned: dict[TensorName, torch.Tensor] = field(default_factory=dict)
# Set on entering HOST_READY (or FAILED): this write's host rows are ready.
host_ready: threading.Event = field(default_factory=threading.Event)
# Set on entering WRITTEN (or FAILED): the CPU-pool write is over.
done: threading.Event = field(default_factory=threading.Event)
# Guards state / reassigned / append / host↔freeze.
lock: threading.Lock = field(default_factory=threading.Lock)
# time.monotonic() at submit; GPU-byte flush picks the oldest of these.
enqueued_time: float = field(default_factory=time.monotonic)
# Immediate path: this write's view into the shared step staging page.
staging_slot: int | None = None
# Immediate path: this step's CUDA device→host event (shared). None if deferred.
step_d2h_event: torch.cuda.Event | None = None
# slot -> (which chunk, row in that tensor). Built on demand
# when a write has more than one `_WriteChunk`; pool write uses slot, the tensor uses row.
_slot_to_row: dict[int, tuple[int, int]] | None = None
def add_reassigned(self, key: str, slots: torch.Tensor) -> None:
with self.lock:
prev = self.reassigned.get(key)
self.reassigned[key] = slots.clone() if prev is None else torch.cat([prev, slots])
def reassigned_intersects(self, chunk: _WriteChunk) -> bool:
"""True if this write already lost ownership of any (key, slot) the
chunk would add; the caller opens a fresh WriteTask instead."""
with self.lock:
if not self.reassigned:
return False
for key in chunk.tensors:
taken = self.reassigned.get(key)
if taken is not None and taken.numel() and bool(torch.isin(chunk.slots_cpu, taken).any()):
return True
return False
# ------------------------------------------------------------ lifecycle
def transition(self, to: TaskState) -> None:
"""Move to `to`; raises unless it is the next chain step or FAILED."""
with self.lock:
self._transition_locked(to)
def try_transition(self, to: TaskState) -> bool:
"""Move to `to` if legal from the current state. True if moved."""
with self.lock:
if not _is_legal_move(self.state, to):
return False
self._transition_locked(to)
return True
def _transition_locked(self, to: TaskState, wake: bool = True) -> None:
if not _is_legal_move(self.state, to):
raise OmniPrefixCacheUnmatchError(f"task {self.tid}: illegal transition {self.state.name} -> {to.name}")
self.state = to
if not wake:
return
if to is TaskState.HOST_READY:
self.host_ready.set()
elif to is TaskState.WRITTEN:
self.done.set()
elif to is TaskState.FAILED:
self.host_ready.set()
self.done.set()
def claim_copy(self) -> bool:
"""QUEUED -> COPYING. Only one thread may run the copy stage; True if this caller won."""
return self.try_transition(TaskState.COPYING)
@property
def is_terminal(self) -> bool:
return self.state.is_terminal
def append_chunk(self, chunk: _WriteChunk, freeze_event: torch.cuda.Event | None = None) -> TaskState | None:
"""Grow this write with one save's rows. Returns None when appended,
else the state that closed the task (COPYING or later).
`freeze_event` is stored in the same snapshot: events on one compute
stream are ordered, so the newest also covers every earlier clone.
"""
with self.lock:
if self.state not in _NOT_YET_COPYING:
return self.state
self.chunks.append(chunk)
self._slot_to_row = None
if freeze_event is not None:
self.freeze_event = freeze_event
return None
def get_host_tensor(self, si: int, key: str) -> torch.Tensor | None:
"""`chunks[si]` host if written, else device freeze. One snapshot."""
with self.lock:
chunk = self.chunks[si]
src = chunk.host.get(key)
if src is None:
src = chunk.tensors.get(key)
return src
def set_host_tensor(self, rows: list[tuple[_WriteChunk, str, torch.Tensor]]) -> None:
"""COPYING -> HOST_READY: write these host tensors, drop the device freeze."""
with self.lock:
for chunk, key, tensor in rows:
chunk.host[key] = tensor
self._clear_tensors()
self._transition_locked(TaskState.HOST_READY)
def mark_host_ready(self) -> None:
"""COPYING -> HOST_READY for staging views: wait the step device→host, drop freeze."""
if self.step_d2h_event is not None:
self.step_d2h_event.synchronize()
with self.lock:
self._clear_tensors()
self._transition_locked(TaskState.HOST_READY)
def mark_failed(self, *, defer_wake: bool = False) -> bool:
"""-> FAILED from any non-terminal state; unblocks joiners. False if
already terminal. ``defer_wake``: the caller publishes the failure
record first, then calls ``wake_failed_waiters``."""
with self.lock:
if self.state.is_terminal:
return False
self._clear_tensors()
self._transition_locked(TaskState.FAILED, wake=not defer_wake)
return True
def wake_failed_waiters(self) -> None:
"""Set the events after the failure record is published (see
``mark_failed(defer_wake=True)``): a woken joiner must find it."""
self.host_ready.set()
self.done.set()
def mark_done(self) -> None:
"""HOST_READY -> WRITTEN."""
self.transition(TaskState.WRITTEN)
def scatter_rows(self) -> list[tuple[TensorName, torch.Tensor, torch.Tensor]]:
"""`(key, slots, host)` to write, one entry per key. Omits slots in
`reassigned`. A slot written twice by this task (preempt + resume
onto the same block) keeps the later chunk's row.
"""
with self.lock:
reassigned = {k: s.clone() for k, s in self.reassigned.items()}
by_key: dict[TensorName, tuple[list[torch.Tensor], list[torch.Tensor]]] = {}
for chunk in self.chunks:
for k, host in chunk.host.items():
s, h = by_key.setdefault(k, ([], []))
s.append(chunk.slots_cpu)
h.append(host)
out: list[tuple[TensorName, torch.Tensor, torch.Tensor]] = []
for k, (s, h) in by_key.items():
slots = s[0] if len(s) == 1 else torch.cat(s)
host = h[0] if len(h) == 1 else torch.cat(h, dim=0)
if len(s) > 1 and torch.unique(slots).numel() != slots.numel():
last_pos = {int(slot): i for i, slot in enumerate(slots.tolist())}
keep_idx = torch.tensor(sorted(last_pos.values()), dtype=torch.int64)
slots, host = slots[keep_idx], host[keep_idx]
taken = reassigned.get(k)
if taken is not None and taken.numel():
keep = ~torch.isin(slots, taken)
if not bool(keep.any()):
continue
slots, host = slots[keep], host[keep]
out.append((k, slots, host))
return out
def clear_tensors(self) -> None:
"""Drop the device freeze. Host is unchanged (staging wait / fail)."""
with self.lock:
self._clear_tensors()
def _clear_tensors(self) -> None:
for chunk in self.chunks:
chunk.tensors = {}
def budget_tickets(self) -> list[_BudgetTicket]:
"""Distinct tickets across this task's chunks (by identity; one per save)."""
out: dict[int, _BudgetTicket] = {}
for chunk in self.chunks:
if chunk.budget is not None:
out.setdefault(id(chunk.budget), chunk.budget)
return list(out.values())
def slot_to_row(self) -> dict[int, tuple[int, int]]:
with self.lock:
if self._slot_to_row is None:
m: dict[int, tuple[int, int]] = {}
for si, chunk in enumerate(self.chunks):
for ri, slot in enumerate(chunk.slots_cpu.tolist()):
m[slot] = (si, ri)
self._slot_to_row = m
return self._slot_to_row
class StagingBufferPool:
"""Reusable pinned landing zone for ONE whole-step device→host at save.
Per-task `chunk.host` is a row-range view into a slot, so the committer
skips a per-task device→host. Slots recycle; this is not the CPU block pool.
A slot stays busy while anyone still holds it: the step (until
materialize/discard) and each immediate write that views the page
(until its pool write). Prefix hits do not hold a slot —
they wait for the pool write and read the durable pool.
Saves with only leftover mm still claim a slot (empty views) so
every step id shares this bound. A full pool waits; timeout then errors.
"""
def __init__(self, depth: int, capacity: int):
self.depth = depth
self.capacity = capacity # rows per slot; a larger step raises
self._bufs: dict[TensorName, torch.Tensor] = {} # [depth*capacity, width]
self._busy: list[set[StagingBufferHolder]] = [set() for _ in range(depth)]
self._slot_free_condition = threading.Condition()
self._closed = False
def _buf(self, key: str, width: int, dtype: torch.dtype, pin: bool) -> torch.Tensor:
buf = self._bufs.get(key)
if buf is None or buf.shape[-1] != width or buf.dtype != dtype:
buf = torch.empty((self.depth * self.capacity, width), dtype=dtype, pin_memory=pin)
self._bufs[key] = buf
return buf
def claim(self, holder: StagingBufferHolder, timeout: float) -> int:
"""Grab a free slot for `holder`. Waits until one is free, then
times out. ``timeout<=0`` fails immediately if none are free.
"""
deadline = time.monotonic() + max(0.0, timeout)
with self._slot_free_condition:
while True:
if self._closed:
raise OmniPrefixCacheUnmatchError("staging pool shut down while waiting for a slot")
for slot in range(self.depth):
if not self._busy[slot]:
self._busy[slot].add(holder)
return slot
remaining = deadline - time.monotonic()
if remaining <= 0:
raise OmniPrefixCacheStagingTimeoutError(f"timed out waiting for a staging slot after {timeout}s")
self._slot_free_condition.wait(timeout=remaining)
def bind(self, slot: int, holder: StagingBufferHolder) -> None:
with self._slot_free_condition:
self._busy[slot].add(holder)
def release(self, slot: int, holder: StagingBufferHolder) -> None:
with self._slot_free_condition:
self._busy[slot].discard(holder)
if not self._busy[slot]:
self._slot_free_condition.notify()
def close(self) -> None:
with self._slot_free_condition:
self._closed = True
self._slot_free_condition.notify_all()
def views(self, slot: int, key: str, n: int, width: int, dtype: torch.dtype, pin: bool) -> torch.Tensor:
base = slot * self.capacity
return self._buf(key, width, dtype, pin)[base : base + n]
@dataclass
class StepD2HClaim:
"""One whole-step landing in StagingBufferPool.
Return of ``stage_step_host``. The manager stores this on
``_StepContext`` until materialize/discard releases the step holder.
"""
staging_slot: int # StagingBufferPool index
views: dict[TensorName, torch.Tensor] # host rows [0:n)
event: torch.cuda.Event | None = None # torch.cuda.Event; None on eager/CPU
class OmniPrefixCacheController:
"""Staging pool + committer. Step device→host is launched at save;
this thread waits that event (JOIN_NEXT_STEP) or copies deferred
rows (JOIN_ON_FINISH), then writes into the CPU pool.
"""
def __init__(self, pool: PrefixBlockPool, config: PrefixCacheConfig, eager: bool | None = None):
self._pool = pool
self._config = config
self._eager = (not torch.cuda.is_available()) if eager is None else eager
self._tasks: dict[Tid, WriteTask] = {}
self._completed: deque[Tid] = deque() # pool write done, awaiting manager drain
self._failed: deque[Tid] = deque() # write failed; manager raises from every later entry (sticky)
self._staged_bytes = 0
self._lock = threading.Lock()
self._wake = threading.Condition(self._lock)
self._queue_hi: deque[Tid] = deque() # JOIN_NEXT_STEP + forced JOIN_ON_FINISH
self._queue_lo: deque[Tid] = deque() # JOIN_ON_FINISH waiting for finish/budget
self._blocked: list[Tid] = [] # host copy done, awaiting pool write
self._shutdown = False
self._copy_stream: torch.cuda.Stream | None = None
self._read_stream: torch.cuda.Stream | None = None
self._worker: threading.Thread | None = None
self._staging_pool = StagingBufferPool(config.staging_depth, config.staging_capacity_tokens)
if not self._eager:
self._copy_stream = torch.cuda.Stream()
self._read_stream = torch.cuda.Stream()
self._worker = threading.Thread(target=self._worker_loop, name="omni-prefix-cache-committer", daemon=True)
self._worker.start()
# --------------------------------------------------------- step device→host staging
def _d2h_on_stream(
self,
stream: torch.cuda.Stream,
freeze_event: torch.cuda.Event | None,
copy: Callable[[], None],
) -> torch.cuda.Event:
"""Issue device→host on `stream` after freeze; return the done event."""
with torch.cuda.stream(stream):
if freeze_event is not None:
stream.wait_event(freeze_event)
copy()
ev = torch.cuda.Event()
ev.record()
return ev
def stage_step_host(
self,
tensors: dict[str, torch.Tensor],
n: int,
freeze_event: torch.cuda.Event | None,
step_holder: StagingBufferHolder,
) -> StepD2HClaim:
"""Claim a staging slot and, when `tensors` is non-empty, launch
ONE whole-step device→host into it.
Leftover-only saves pass empty `tensors` and still take a slot
(empty views) so every step id shares this bound. A full pool
waits for materialize/discard; timeout then errors. A step
larger than the page overflows the next slot — that is a config
break. The caller binds tasks after submit; `step_holder` is
released by materialize/discard via staging_release.
"""
if tensors and n > self._staging_pool.capacity:
raise OmniPrefixCacheUnmatchError(
f"step has {n} tokens; staging capacity is {self._staging_pool.capacity} "
"(size staging_capacity_tokens to max_num_batched_tokens)"
)
slot = self._staging_pool.claim(step_holder, self._config.staging_claim_timeout_s)
try:
pin = not self._eager
views: dict[str, torch.Tensor] = {}
event: torch.cuda.Event | None = None
if self._eager or all(t.device.type == "cpu" for t in tensors.values()):
for key, src in tensors.items():
v = self._staging_pool.views(slot, key, n, int(src.shape[-1]), src.dtype, pin)
v.copy_(src)
views[key] = v
else:
def _copy_to_staging() -> None:
for key, src in tensors.items():
v = self._staging_pool.views(slot, key, n, int(src.shape[-1]), src.dtype, pin)
v.copy_(src, non_blocking=True)
views[key] = v
event = self._d2h_on_stream(self._copy_stream, freeze_event, _copy_to_staging)
return StepD2HClaim(staging_slot=slot, views=views, event=event)
except Exception:
self._staging_pool.release(slot, step_holder)
raise
def staging_bind(self, slot: int, holder: StagingBufferHolder) -> None:
self._staging_pool.bind(slot, holder)
def staging_release(self, slot: int, holder: StagingBufferHolder) -> None:
self._staging_pool.release(slot, holder)
def _release_task_slot(self, task: WriteTask) -> None:
"""Drop the task's staging hold at a terminal state (pool write or failure)."""
if task.staging_slot is not None:
self._staging_pool.release(task.staging_slot, StagingBufferHolder.for_task(task.tid))
def in_flight_tasks(self) -> int:
"""Registered tasks not yet drained (diagnostics only)."""
return len(self._tasks)
# ------------------------------------------------------------------ submit
def register(self, task: WriteTask, queued: bool = True) -> None:
"""Make a task visible (registry + QUEUED) without running anything;
safe under the manager's state lock. queued=False (deferred tasks)
stays PENDING on the GPU clone until finish/abort or the GPU-byte
budget forces a copy. Queued tasks must then go through
``dispatch``.
Caller must reserve() the task bytes first (budget flush can
block; the manager does that outside the state lock) and pin the
task on its budget ticket(s) before register.
"""
task.enqueued_time = time.monotonic()
with self._lock:
self._tasks[task.tid] = task
if queued:
task.transition(TaskState.QUEUED)
def dispatch(self, tasks: list[WriteTask]) -> None:
"""Hand registered, queued tasks to the copy path. Threaded: enqueue.
Eager: the copy + pool write run here, inline — never call this
under the manager's state lock."""
if not tasks:
return
if self._eager:
try:
for task in tasks:
self._run_eager(task)
except Exception:
# Later tasks were registered but will never be dispatched.
# Fail them too so shared budget owners and waiters release.
for task in tasks:
self._fail_task(task.tid)
raise
return
with self._wake:
for task in tasks:
(self._queue_hi if task.schedule is WriteSchedule.JOIN_NEXT_STEP else self._queue_lo).append(task.tid)
self._wake.notify_all()
def submit(self, task: WriteTask, queued: bool = True) -> None:
"""register + dispatch in one call (callers not holding the state lock)."""
self.register(task, queued)
if queued:
self.dispatch([task])
def append_chunk(
self, task: WriteTask, chunk: _WriteChunk, freeze_event: torch.cuda.Event | None = None
) -> TaskState | None:
"""Append to a pending task. None when appended, else the closing state."""
return task.append_chunk(chunk, freeze_event)
def pin_budget(self, ticket: _BudgetTicket, tid: int) -> None:
"""Record that ``tid`` holds a view of the clone ``ticket`` charges."""
with self._lock:
ticket.tids.add(tid)
def reserve(self, nbytes: int) -> None:
"""Reserve GPU-clone bytes; blocking flush happens here, so callers
must not hold the manager's state lock."""
self._reserve_bytes(nbytes)
def _release_staged_bytes(self, task: WriteTask) -> None:
"""Drop this task's hold on its budget ticket(s). Idempotent; a
ticket uncharges once, when its last holder leaves."""
tickets = task.budget_tickets()
with self._wake:
for ticket in tickets:
self._staged_bytes -= ticket.release(task.tid)
def _reserve_bytes(self, nbytes: int) -> None:
# GPU-byte budget: force-copy oldest pending tasks until under
# budget. Bounded wait: their device→host has usually long completed.
while True:
with self._lock:
pending = [tid for tid, t in self._tasks.items() if not t.is_terminal]
if self._staged_bytes + nbytes <= self._config.gpu_staging_bytes or not pending:
# Under budget or no pending tasks; admit reservation.
self._staged_bytes += nbytes
return
oldest = min(pending, key=lambda tid: self._tasks[tid].enqueued_time)
logger.warning("omni prefix cache: staging cap hit, force-flushing task %d", oldest)
self.escalate([oldest])
self.join([oldest])
# ------------------------------------------------------------- lifecycle
def escalate(self, tids: list[int]) -> None:
"""Move pending tasks to the front of the high-priority queue.
PENDING -> QUEUED at the head; QUEUED on the low-priority queue
moves up; QUEUED already high-priority is a no-op. COPYING and later
belong to the worker and are untouched: the worker claims under
`_wake` too, so a task cannot be popped and re-queued behind its back.
"""
if self._eager:
self.dispatch([task for tid in tids if (task := self._tasks.get(tid)) is not None and not task.is_terminal])
return
with self._wake:
for tid in tids:
task = self._tasks.get(tid)
if task is None:
continue
if task.try_transition(TaskState.QUEUED):
self._queue_hi.appendleft(tid)
elif task.state is TaskState.QUEUED and tid in self._queue_lo:
self._queue_lo.remove(tid)
self._queue_hi.appendleft(tid)
self._wake.notify_all()
def join(self, tids: list[int]) -> None:
"""Block until each task has finished the CPU-pool write (or failed)."""
self._wait_tasks(tids, "done")
def join_host_ready(self, tids: list[int]) -> None:
"""Block until each task's device→host is complete (`host_ready`).
Staging: committer has waited `step_d2h_event`. Deferred: committer
has written `chunk.host`. Does not wait for the CPU-pool write.
"""
self._wait_tasks(tids, "host_ready")
def _wait_tasks(self, tids: list[int], event: Literal["done", "host_ready"]) -> None:
timeout = self._config.staging_claim_timeout_s
deadline = time.monotonic() + timeout
for tid in tids:
task = self._tasks.get(tid)
if task is None:
continue
if not getattr(task, event).wait(max(0.0, deadline - time.monotonic())):
raise OmniPrefixCacheUnmatchError(
f"task {tid} ({task.schedule.value}) did not reach {event} within {timeout:g}s "
f"(state={task.state.name}, in_flight_tasks={len(self._tasks)})"
)
if task.state is TaskState.FAILED:
# FAILED sets both events; success here would read rows the
# write never produced. The one-shot failure record may have
# been drained by another thread — do not rely on it.
raise OmniPrefixCacheUnmatchError(f"task {tid} ({task.schedule.value}) write failed before {event}")
def drain_completed(self) -> list[int]:
"""Pop pool-written tasks from `_completed` and drop them from `_tasks`.
Staging holders were already released at the pool write."""
out: list[int] = []
with self._lock:
while self._completed:
out.append(self._completed.popleft())
for tid in out:
self._tasks.pop(tid, None)
return out
def drain_failed(self) -> list[int]:
"""Pop failed task ids from `_failed`. Does not drop `_tasks`."""
out: list[int] = []
with self._lock:
while self._failed:
out.append(self._failed.popleft())
return out
def get_task(self, tid: int) -> WriteTask | None:
return self._tasks.get(tid)
def shutdown(self) -> None:
self._staging_pool.close()
with self._wake:
self._shutdown = True
self._wake.notify_all()
if self._worker is not None:
self._worker.join(timeout=5.0)
# ------------------------------------------------------------ fetch_host
@torch.inference_mode()
def fetch_host(self, task: WriteTask, slots: torch.Tensor, key: str) -> torch.Tensor:
"""Rows for `slots` of one not-yet-done JOIN_ON_FINISH task.
`_slot_ref` puts JOIN_NEXT_STEP tids in `join_tids` (wait then
pool). This path reads committer-written `chunk.host`, or the
device clone if that device→host has not landed.
"""
if task.step_d2h_event is not None:
task.step_d2h_event.synchronize()
return self._rows_from(task, slots, key)
def _rows_from(self, task: WriteTask, slots: torch.Tensor, key: str) -> torch.Tensor:
"""Map `slots` to rows across one or more `_WriteChunk`s; preserve caller order."""
s2r = task.slot_to_row()
idx = [s2r[int(s)] for s in slots.tolist()]
parts: list[torch.Tensor] = []
order: list[int] = []
pos = 0
rows_groups: dict[int, list[tuple[int, int]]] = {}
for si, ri in idx:
rows_groups.setdefault(si, []).append((pos, ri))
pos += 1
for si, items in rows_groups.items():
src = task.get_host_tensor(si, key)
if src is None:
continue
rows_idx = torch.tensor([ri for _, ri in items], dtype=torch.long)
picked = self._slice_rows(task, src, rows_idx, host=(src.device.type == "cpu"))
parts.append(picked)
order.extend(p for p, _ in items)
if not parts:
raise KeyError(f"key {key} not present in task {task.tid}")
cat = torch.cat(parts, dim=0)
out = torch.empty_like(cat)
out[torch.tensor(order, dtype=torch.long)] = cat
return out
def _slice_rows(self, task: WriteTask, src: torch.Tensor, rows_idx: torch.Tensor, host: bool) -> torch.Tensor:
n = int(rows_idx.numel())
# Ascending-run check without materializing an arange: endpoints plus
# a monotonic diff are enough, and the common case is one long run.
contiguous = (
n > 0 and int(rows_idx[-1]) - int(rows_idx[0]) == n - 1 and (n < 2 or bool((rows_idx.diff() == 1).all()))
)
def _pick() -> torch.Tensor:
return src[rows_idx[0] : rows_idx[0] + n] if contiguous else src.index_select(0, rows_idx)
if host or src.device.type == "cpu":
return _pick()
if self._read_stream is None:
return _pick().detach().cpu()
copied: list[torch.Tensor] = []
def _copy_to_cpu() -> None:
copied.append(_pick().to("cpu", non_blocking=True))
ev = self._d2h_on_stream(self._read_stream, task.freeze_event, _copy_to_cpu)
ev.synchronize()
return copied[0]
# ------------------------------------------------------------ eager mode
@torch.inference_mode()
def _run_eager(self, task: WriteTask) -> None:
"""Eager stand-in for queue + worker: walk the whole chain inline."""
task.try_transition(TaskState.QUEUED) # no-op if submit already queued it
if not task.claim_copy():
# Copy stage already ran (a held stub, or a prior escalate); finish the write.
if task.state is TaskState.HOST_READY:
self._scatter(task)
return
if task.schedule is WriteSchedule.JOIN_NEXT_STEP:
# Host is already a staging view (copied at save). Drop freeze.
# `step_d2h_event` is None on CPU; `mark_host_ready` skips wait.
task.mark_host_ready()
else:
# JOIN_ON_FINISH: no copy stream; freeze → owned host inline.
task.set_host_tensor(
[
(chunk, k, t.detach().cpu() if t.device.type != "cpu" else t.clone())
for chunk in task.chunks
for k, t in chunk.tensors.items()
]
)
self._release_staged_bytes(task)
self._scatter(task)
# ---------------------------------------------------------- worker loop
def _worker_loop(self) -> None:
# A dying committer would strand every join() forever, so the loop
# never propagates: it fails the offending task and keeps serving.
while True:
tid = None
try:
task: WriteTask | None = None
with self._wake:
while not self._shutdown and not self._queue_hi and not self._queue_lo:
if self._blocked:
break
# submit / escalate / shutdown all notify.
self._wake.wait()
if self._shutdown and not self._queue_hi and not self._queue_lo and not self._blocked:
return
if self._queue_hi:
tid = self._queue_hi.popleft()
elif self._queue_lo:
tid = self._queue_lo.popleft()
if tid is not None:
task = self._tasks.get(tid)
if task is not None:
# Claim in the same critical section as the pop:
# escalate holds `_wake` and sees COPYING.
task.transition(TaskState.COPYING)
if task is not None:
self._copy_task(task)
with self._wake:
assert task.tid not in self._blocked, f"task {task.tid} reached HOST_READY twice"
self._blocked.append(task.tid)
self._scatter_host_ready()
except BaseException:
logger.exception("omni prefix cache committer failed on task %s; releasing waiters", tid)
self._fail_task(tid)
@torch.inference_mode()
def _copy_task(self, task: WriteTask) -> None:
"""COPYING -> HOST_READY. Staging: wait the save-time device→host event.
Deferred: this is the device→host into owned `chunk.host` tensors.
"""
if task.schedule is WriteSchedule.JOIN_NEXT_STEP:
# `chunk.host` is already a staging view; device→host ran at save.
# `mark_host_ready` waits `step_d2h_event` if one was recorded.
# No per-task copy. Pool write is `_scatter_host_ready`.
task.mark_host_ready()
self._release_staged_bytes(task)
return
# One device cat + one device→host per key, not one per chunk (a
# long request has one chunk per step).
chunk_bytes = self._config.copy_chunk_bytes
by_key: dict[str, list[_WriteChunk]] = {}
for chunk in task.chunks:
for k in chunk.tensors:
by_key.setdefault(k, []).append(chunk)
pending: list[tuple[str, list[_WriteChunk], list[torch.Tensor]]] = []
if self._copy_stream is None:
raise OmniPrefixCacheUnmatchError("deferred prefix cache copy requires a CUDA copy stream")
with torch.cuda.stream(self._copy_stream):
if task.freeze_event is not None:
self._copy_stream.wait_event(task.freeze_event)
for k, chunks in by_key.items():
srcs = [c.tensors[k] for c in chunks]
src = srcs[0] if len(srcs) == 1 else torch.cat(srcs, dim=0)
rows = int(src.shape[0])
row_bytes = max(1, src[:1].numel() * src.element_size())
step = max(1, rows if rows * row_bytes <= chunk_bytes else chunk_bytes // row_bytes)
parts = [src[s : s + step].to("cpu", non_blocking=True) for s in range(0, max(rows, 1), step)]
pending.append((k, chunks, parts))
ev = torch.cuda.Event()
ev.record()
ev.synchronize()
rows_out: list[tuple[_WriteChunk, str, torch.Tensor]] = []
for k, chunks, parts in pending:
host = parts[0] if len(parts) == 1 else torch.cat(parts, dim=0)
if len(chunks) == 1:
rows_out.append((chunks[0], k, host))
continue
for chunk, view in zip(chunks, torch.split(host, [int(c.tensors[k].shape[0]) for c in chunks], dim=0)):
rows_out.append((chunk, k, view))
task.set_host_tensor(rows_out)
self._release_staged_bytes(task)
def _fail_task(self, tid: int | None) -> None:
"""-> FAILED: release waiters for a task the committer could not complete.
No-op once the task is terminal. Budget release is idempotent, so a
raise after a successful copy stage does not uncharge twice.
"""
task = self._tasks.get(tid) if tid is not None else None
if task is None or not task.mark_failed(defer_wake=True):
return
self._release_staged_bytes(task)
with self._wake:
if tid in self._blocked:
self._blocked.remove(tid)
self._release_task_slot(task)
with self._lock:
# Publish the failure: rows behind already-published block hashes
# never landed, which the manager must raise on (hiding it would
# crash on every future hit that touches these slots).
self._failed.append(task.tid)
# Events last: a joiner woken by them must find the record on drain,
# not read never-written pool rows in a done-but-unpublished window.
task.wake_failed_waiters()
@torch.inference_mode()
def _scatter_host_ready(self) -> None:
"""HOST_READY -> WRITTEN for every `_blocked` task that has reached HOST_READY."""
with self._wake:
ready = [tid for tid in self._blocked if (t := self._tasks.get(tid)) and t.state is TaskState.HOST_READY]
for tid in ready:
self._blocked.remove(tid)
for tid in ready:
task = self._tasks[tid]
try:
self._scatter(task)
except BaseException:
# Attribute the failure to THIS task: letting it propagate
# would fail whichever entry the worker loop happened to be
# copying, double-release its bytes, and strand this one's
# join() forever.
logger.exception("omni prefix cache scatter failed on task %s; releasing waiters", tid)
self._fail_task(tid)
@torch.inference_mode()
def _scatter(self, task: WriteTask) -> None:
for key, slots, host in task.scatter_rows():
self._pool.write(key, slots, host)
# `done` is set LAST: a join(done) waiter (the save barrier) must find
# the completion record on its next drain and the staging slot free —
# a done-but-undrained window would leave occupancy IN_TRANSIT and
# make the COW preserve skip the reused rows.
self._release_task_slot(task)
with self._lock:
self._completed.append(task.tid)
task.mark_done()
+208
View File
@@ -0,0 +1,208 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
"""KV-cache group access for the omni prefix cache.
The sole path through which the prefix cache touches vLLM scheduler
internals (block tables, slot mappings). Group-spec rejection happens
at kv-cache init via ``check_prefix_cache_kv_groups``; the factory only
returns None when the input batch has no block table.
"""
from __future__ import annotations
from typing import TYPE_CHECKING
import torch
from vllm_omni.core.prefix_cache.interface import OmniPrefixCacheUnmatchError, PrefixCacheConfig
if TYPE_CHECKING:
from vllm.v1.worker.gpu_input_batch import InputBatch
class FullAttentionGroupView:
"""View over the first (full-attention) KV-cache group.
Step slots come from the CPU block table (`step_slots_cpu`), not the
device slot_mapping.
"""
def __init__(self, input_batch: InputBatch, block_size: int):
self._input_batch = input_batch
self.block_size = block_size
check_prefix_cache_block_layout(input_batch.block_table[0], block_size)
def _block_table_cpu(self) -> torch.Tensor:
return self._input_batch.block_table[0].block_table.cpu
def batch_req_ids(self) -> list[str]:
return list(self._input_batch.req_ids)
def step_slots_cpu(self, req_ids: list[str], num_scheduled: dict[str, int]) -> torch.Tensor:
"""This step's slot mapping, computed on CPU from the block table.
The device slot_mapping would need a stream sync to read back, which
stalls the whole forward; the CPU block table carries the same
information (positions are num_computed .. +num_scheduled per request).
"""
block_table = self._block_table_cpu()
bs = self.block_size
max_blocks = int(block_table.shape[1])
computed = self._input_batch.num_computed_tokens_cpu
parts: list[torch.Tensor] = []
for req_id in req_ids:
n = int(num_scheduled.get(req_id, 0))
if n <= 0:
continue
req_idx = self._input_batch.req_id_to_index[req_id]
start = int(computed[req_idx])
pos = torch.arange(start, start + n, dtype=torch.long)
offs = pos // bs
if int(offs[-1]) >= max_blocks:
keep = offs < max_blocks
pos, offs = pos[keep], offs[keep]
if pos.numel() == 0:
continue
parts.append(block_table[req_idx, offs].to(torch.long) * bs + (pos % bs))
return torch.cat(parts) if parts else torch.empty((0,), dtype=torch.long)
def check_prefix_cache_block_layout(block_table: object, block_size: int) -> None:
"""Reject block-table layouts ``step_slots_cpu`` cannot mirror.
The CPU slot math is ``table[req, pos // block_size] * block_size +
pos % block_size`` over allocator block ids. vLLM breaks that in two
cases: hybrid kernel blocks (the row holds ``blocks_per_kv_block``
kernel ids per allocator block, e.g. FlashInfer/FlashMLA with
``--block-size 128``) and decode context parallel (tokens are striped
across ranks and hashed at ``block_size * dcp_world_size``).
"""
if getattr(block_table, "use_hybrid_blocks", False) or int(getattr(block_table, "blocks_per_kv_block", 1)) != 1:
raise OmniPrefixCacheUnmatchError(
"omni prefix caching requires kernel_block_size == block_size; the attention backend "
f"splits each block into {getattr(block_table, 'blocks_per_kv_block', '?')} kernel blocks. "
"Pick a --block-size the backend supports natively or disable enable_prefix_caching"
)
if int(getattr(block_table, "dcp_world_size", 1)) != 1:
raise OmniPrefixCacheUnmatchError(
"omni prefix caching does not support decode context parallel "
f"(dcp_world_size={getattr(block_table, 'dcp_world_size', '?')}); disable enable_prefix_caching"
)
kv_bs = getattr(block_table, "kv_cache_block_size", block_size)
if int(kv_bs) != int(block_size):
raise OmniPrefixCacheUnmatchError(
f"omni prefix caching block_size {block_size} does not match the kv-cache block table ({kv_bs})"
)
def check_prefix_cache_kv_groups(kv_cache_groups: object) -> None:
"""Reject hybrid / multi-group models at kv-cache init, not first step.
Only needs ``kv_cache_config.kv_cache_groups``. ``FullAttentionSpec``
is imported here so ``tests/core`` can load this module without vllm.
"""
groups = list(kv_cache_groups or ())
if len(groups) != 1:
raise OmniPrefixCacheUnmatchError(
"omni prefix caching requires a single full-attention kv-cache group; "
f"found {len(groups)}. disable enable_prefix_caching for this model"
)
from vllm.v1.kv_cache_interface import FullAttentionSpec
spec = getattr(groups[0], "kv_cache_spec", None)
if not isinstance(spec, FullAttentionSpec):
raise OmniPrefixCacheUnmatchError(
"omni prefix caching requires a single full-attention kv-cache group; "
f"found {type(spec).__name__}. disable enable_prefix_caching for this model"
)
def check_prefix_cache_kv_transfer(kv_transfer_config: object) -> None:
"""Reject kv_consumer / kv_both stages.
KV loaded from a producer also shows up as ``num_computed_tokens``; the
manager would read it as a local hit with no rows behind it.
"""
if kv_transfer_config is not None and getattr(kv_transfer_config, "is_kv_consumer", False):
raise OmniPrefixCacheUnmatchError(
"omni prefix caching cannot tell locally cached tokens from KV received "
"through a KV connector; disable enable_prefix_caching on kv_consumer / "
"kv_both stages"
)
def check_prefix_cache_token_accounting(cache_config: object, speculative_config: object) -> None:
"""Reject configs where ``num_computed_tokens`` stops meaning "first
scheduled position, block aligned".
Speculative decoding: with async scheduling vLLM keeps
``num_computed_tokens_cpu`` optimistic (all drafts accepted) during the
forward and corrects it after; ``step_slots_cpu`` would read the
uncorrected value and mirror rows at the wrong slots. Refused as a
whole until that path is verified. ``prefix_match_unit`` smaller than
the block enables sub-block hits, which the hit registry rejects as
unaligned on the first hit; refuse at init instead.
"""
if speculative_config is not None:
raise OmniPrefixCacheUnmatchError(
"omni prefix caching is not supported together with speculative decoding; "
"disable enable_prefix_caching on this stage"
)
unit = getattr(cache_config, "prefix_match_unit", None)
block_size = getattr(cache_config, "block_size", None)
if unit is not None and block_size is not None and int(unit) != int(block_size):
raise OmniPrefixCacheUnmatchError(
f"omni prefix caching requires block-aligned hits; prefix_match_unit={unit} with block_size={block_size} "
"enables sub-block hits. Unset prefix_match_unit or disable enable_prefix_caching"
)
def stage_prefix_cache_config(
*,
kv_cache_config: object,
cache_config: object,
kv_transfer_config: object,
scheduler_config: object,
model_config: object,
is_pooling_model: bool,
speculative_config: object = None,
) -> PrefixCacheConfig | None:
"""Runner-side gate shared by the GPU and NPU model runners.
Returns None when the stage does not run an omni prefix cache
(``enable_prefix_caching`` off, or a pooling stage that never saves).
Otherwise refuses kv_consumer / kv_both, speculative decoding,
sub-block matching and hybrid kv groups, then sizes the config from
the scheduler. One place so a platform runner cannot silently skip a
refusal the other one has.
"""
if not getattr(cache_config, "enable_prefix_caching", False) or is_pooling_model:
return None
check_prefix_cache_kv_transfer(kv_transfer_config)
check_prefix_cache_token_accounting(cache_config, speculative_config)
check_prefix_cache_kv_groups(getattr(kv_cache_config, "kv_cache_groups", None))
return PrefixCacheConfig.from_vllm_config(
num_blocks=kv_cache_config.num_blocks, # type: ignore[attr-defined]
block_size=cache_config.block_size, # type: ignore[attr-defined]
scheduler_config=scheduler_config,
model_config=model_config,
)
def get_prefix_cache_group_view(
input_batch: InputBatch,
block_size: int,
kv_cache_groups: object = None,
) -> FullAttentionGroupView | None:
"""Build the group view; None only if the batch has no block table.
Group spec is checked first (raises). Selection is by spec, not by
counting block tables: a hybrid model's group 0 is not necessarily
full attention, and a narrower per-group table would make
step_slots_cpu silently clamp.
"""
check_prefix_cache_kv_groups(kv_cache_groups)
block_tables = getattr(input_batch.block_table, "block_tables", None)
if not block_tables:
return None
return FullAttentionGroupView(input_batch, block_size)
+171
View File
@@ -0,0 +1,171 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
"""Interface types for the omni prefix cache.
Naming aligns with vLLM's v1/core KV-cache design.
"""
from collections.abc import Iterable
from dataclasses import dataclass
from enum import Enum
from typing import Any, NamedTuple, TypeAlias
import torch
# Four identities (still str/int at runtime, not interchangeable):
# TensorName which cached tensor (hidden / mm), not a prefix hash
# ReqId vLLM request id
# Tid WriteTask handle
# StepId save_outputs id; materialize or discard exactly once
TensorName: TypeAlias = str
ReqId: TypeAlias = str
Tid: TypeAlias = int
StepId: TypeAlias = int
# Reserved pool key for hidden states (mm keys are flat dotted names).
# Identity only — whether a model caches it lives on ModelCachePolicy.
HIDDEN_KEY: TensorName = "__hidden_states__"
def is_hidden_key(key: TensorName) -> bool:
return key == HIDDEN_KEY
def without_hidden(keys: Iterable[TensorName]) -> set[TensorName]:
"""Drop the reserved hidden identity; the rest are mm names."""
return {k for k in keys if k != HIDDEN_KEY}
class WriteSchedule(Enum):
"""Write scheduling policy for one WriteTask."""
# Immediately-cached keys: device→host launched at save into the
# staging pool; committer waits that event and writes the CPU pool.
# The next save waits done (a reused slot must be pool-complete).
JOIN_NEXT_STEP = "join_next_step"
# Deferred mm: stays on the GPU clone until finish/abort (GPU-byte
# budget may force a copy earlier). One WriteTask per request.
JOIN_ON_FINISH = "join_on_finish"
@dataclass(frozen=True)
class PrefixCacheConfig:
"""Sizing and flow-control knobs (mirrors KVCacheConfig)."""
num_blocks: int
block_size: int
# GPU-clone byte budget for JOIN_ON_FINISH; exceeding it forces a copy.
gpu_staging_bytes: int = 512 * 1024 * 1024
# Device→host staging: circular slots, one whole step each (not per request).
# Host memory per key ≈ staging_depth * staging_capacity_tokens * width * dtype.
# Prefer from_vllm_config so staging_capacity_tokens tracks max_num_batched_tokens.
staging_depth: int = 4
staging_capacity_tokens: int = 1024
# How long save waits for a free staging slot, and join/join_host_ready
# for the committer; exceeding it raises.
staging_claim_timeout_s: float = 30.0
# Device→host chunk size for JOIN_ON_FINISH (copied a piece at a time).
copy_chunk_bytes: int = 16 * 1024 * 1024
@classmethod
def from_vllm_config(
cls,
*,
num_blocks: int,
block_size: int,
scheduler_config: Any = None,
model_config: Any = None,
) -> "PrefixCacheConfig":
"""Size device→host staging from the running scheduler.
A slot holds one *step* (the whole batch), not one request:
``staging_capacity_tokens`` is ``max_num_batched_tokens`` (falls back
to ``max_model_len``); ``staging_depth`` is how many unconsumed
step ids may exist at once, not ``max_num_seqs``.
Pinned staging is allocated lazily per key at
``depth * capacity_tokens * width * dtype``. There is no clamp: a
step larger than capacity raises. A 16k-token thinking batch at
hidden=2048 bf16 is ~256 MiB for hidden alone; each mm key adds
another pool tensor.
``staging_depth`` is the dataclass default (4). There is no CLI or
deploy YAML knob — changing it is a code change. Every save that
issues a step id claims one slot, including saves with only leftover
mm. A full pool waits for materialize/discard;
``staging_claim_timeout_s`` then errors.
"""
batched = getattr(scheduler_config, "max_num_batched_tokens", None)
model_len = getattr(scheduler_config, "max_model_len", None)
if not model_len and model_config is not None:
model_len = getattr(model_config, "max_model_len", None)
try:
capacity = int(batched or model_len or 1024)
except (TypeError, ValueError):
capacity = 1024
return cls(
num_blocks=num_blocks,
block_size=block_size,
staging_capacity_tokens=max(1, capacity),
)
class StageCacheOutputs(NamedTuple):
"""Plain value object: per-request merged stage outputs."""
# req -> full-prompt hidden states (None when policy skips them)
hidden_states: dict[ReqId, torch.Tensor] | None
# tensor name -> req -> payload element
mm_outputs: dict[TensorName, dict[ReqId, Any]]
class OmniPrefixCacheUnmatchError(RuntimeError):
"""Contract, config, or KV-occupancy error that must raise.
Includes hit spans that resolve to absent slots (omni cache diverged
from vLLM KV), a step id consumed twice or never saved, a step larger
than the staging page, and a failed write. Do not pretend these
were a miss.
"""
class OmniPrefixCacheStagingTimeoutError(OmniPrefixCacheUnmatchError):
"""Save waited for a free staging slot and timed out."""
@dataclass(frozen=True)
class ModelCachePolicy:
"""Replaces getattr probing on models for cache behavior decisions.
Hidden's *name* is ``HIDDEN_KEY`` (shared identity). This object
answers whether this model caches it, and which mm keys are deferred.
"""
needs_full_hidden_states: bool = True
# Mm whose first dim is this step's token count; stays on the GPU
# clone until finish/abort (JOIN_ON_FINISH). Also skipped by the
# immediate on-device clone / device→host path.
deferred_keys: frozenset[TensorName] = frozenset()
@property
def hidden_key(self) -> TensorName | None:
"""Pool key for hidden, or None when this model opts out."""
return HIDDEN_KEY if self.needs_full_hidden_states else None
def get_hit_keys(self, keys: Iterable[TensorName]) -> list[TensorName]:
"""Keys to plan/prefetch for a hit: hidden first (if cached), then mm."""
mm = sorted(without_hidden(keys))
return [HIDDEN_KEY, *mm] if self.needs_full_hidden_states else mm
def skip_immediate_mm(self, key: TensorName) -> bool:
"""Immediate on-device clone must not take hidden or deferred keys from mm."""
return is_hidden_key(key) or key in self.deferred_keys
@classmethod
def from_model(cls, model: Any) -> "ModelCachePolicy":
"""Shim over legacy per-model attributes (deprecation window)."""
deferred = frozenset(getattr(model, "deferred_prefix_cache_mm_keys", ()) or ())
return cls(
needs_full_hidden_states=bool(getattr(model, "requires_full_prefix_cached_hidden_states", True)),
deferred_keys=deferred,
)
File diff suppressed because it is too large. Load diff
+148
View File
@@ -0,0 +1,148 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
"""Runner-facing access layer for the omni prefix cache.
One copy of the integration rules shared by the GPU and NPU runners:
one-shot manager construction, the model cache-policy snapshot, and the
per-step lifecycle entry. The manager/controller state machines and the
platform output-building flows stay in their owners.
Not exported from the package ``__init__``; runners import this module
directly. No module-level vLLM import (``get_pp_group`` is resolved at
call time) so ``tests/core`` keeps loading the package without vLLM.
"""
from __future__ import annotations
from typing import TYPE_CHECKING, Any
from vllm_omni.core.prefix_cache.group_view import get_prefix_cache_group_view
from vllm_omni.core.prefix_cache.interface import (
ModelCachePolicy,
OmniPrefixCacheUnmatchError,
PrefixCacheConfig,
)
from vllm_omni.core.prefix_cache.manager import OmniPrefixCacheManager
from vllm_omni.data_entry_keys import flatten_payload
if TYPE_CHECKING:
import torch
from vllm.v1.core.sched.output import SchedulerOutput
class PrefixCacheRunnerMixin:
"""Prefix-cache integration for a model runner (no state machine here).
Host-class contract — the runner must provide:
``input_batch`` live batch with the group-0 block table
``kv_cache_config`` for ``kv_cache_groups`` at construction
``omni_prefix_cache`` set here; ``None`` until first-step build
``_omni_prefix_cache_cfg`` staged by the runner's kv-cache init
(``stage_prefix_cache_config``), consumed here
No cooperative ``__init__``: the class attributes below are safe
defaults (``ModelCachePolicy`` is frozen); runners keep initializing
their instance fields as before.
"""
# Host-class fields (annotation only; the runner initializes them).
input_batch: Any
kv_cache_config: Any
is_pooling_model: bool
omni_prefix_cache: OmniPrefixCacheManager | None = None
_omni_prefix_cache_cfg: PrefixCacheConfig | None = None
# Snapshotted once at load_model; the hot path must not re-probe model
# attributes every step. The same object is registered on the manager.
_omni_cache_policy: ModelCachePolicy = ModelCachePolicy()
def _snapshot_prefix_cache_model_policy(self, model) -> None:
"""Freeze the model's cache policy at load_model."""
self._omni_cache_policy = ModelCachePolicy.from_model(model)
def _model_needs_full_prefix_hidden_states(self) -> bool:
"""Opt-out hook for models whose postprocess only consumes the tail.
When False, we skip both the per-step hidden-state write into the
omni prefix cache and the merged-tensor reconstruction on hits;
postprocess receives the normal scheduled-token slice instead. Models
that need the full cached_prefix + new_tail span (default) are not
affected.
"""
return self._omni_cache_policy.needs_full_hidden_states
def _ensure_omni_prefix_cache(self) -> None:
"""One-shot construction (caller gates on the staged config).
Only the last PP rank writes and materializes the cache. Other ranks
would register hits they can never serve (save_outputs is last-rank
only), so skip construction there.
"""
from vllm.distributed.parallel_state import get_pp_group
cfg = self._omni_prefix_cache_cfg
self._omni_prefix_cache_cfg = None
if cfg is None or not get_pp_group().is_last_rank:
return
view = get_prefix_cache_group_view(
self.input_batch,
cfg.block_size,
kv_cache_groups=getattr(self.kv_cache_config, "kv_cache_groups", None),
)
if view is None:
raise OmniPrefixCacheUnmatchError(
"omni prefix caching requires a block table on the input batch; "
"disable enable_prefix_caching for this model"
)
manager = OmniPrefixCacheManager(cfg, view)
manager.register_policy(self._omni_cache_policy)
self.omni_prefix_cache = manager
def _prefix_cache_step_begin(self, scheduler_output: SchedulerOutput) -> None:
"""Per-step lifecycle entry; must run before ``_update_states``
removes finished requests (finished/abort escalation happens inside
``new_step_starts``). Builds the manager on the first real step."""
if self.omni_prefix_cache is None and self._omni_prefix_cache_cfg is not None:
self._ensure_omni_prefix_cache()
if self.omni_prefix_cache is not None:
self.omni_prefix_cache.new_step_starts(scheduler_output)
def _prefix_cache_save_step(
self,
hidden_states: torch.Tensor,
multimodal_outputs: dict | None,
*,
num_tokens_unpadded: int,
num_tokens_padded: int,
) -> int | None:
"""Write this step's outputs into the cache.
None when this stage/rank does not write (pooling, cache off, or not
the last PP rank). The returned step id must be consumed exactly once
— materialize or discard_step — by the output path.
"""
from vllm.distributed.parallel_state import get_pp_group
if self.is_pooling_model or self.omni_prefix_cache is None or not get_pp_group().is_last_rank:
return None
return self.omni_prefix_cache.save_outputs(
hidden_states,
flatten_payload(multimodal_outputs) if multimodal_outputs else {},
num_tokens_unpadded=num_tokens_unpadded,
num_tokens_padded=num_tokens_padded,
)
def _prefix_cache_materialize(
self, step_id: int | None, req_ids: list[str]
) -> tuple[dict[str, torch.Tensor] | None, dict | None]:
"""Per-request merged outputs for a saved step.
``req_ids`` must be the save-time snapshot, never the live
``input_batch`` (under async output this runs a step late).
``step_id`` None means save did not run — nothing to consume.
``mm_outputs`` is all-or-nothing; empty only when the step had no mm.
"""
if step_id is None or self.omni_prefix_cache is None:
return None, None
outs = self.omni_prefix_cache.materialize(step_id, list(req_ids))
return outs.hidden_states, (outs.mm_outputs or None)