[2/N][Core] support partial prefix cache hit for hybrid model (#46384)
Signed-off-by: zjy0516 <riverclouds.zhu@qq.com> Signed-off-by: Yifan Qiao <yifanqiao@inferact.ai> Co-authored-by: Yifan Qiao <yifanqiao@inferact.ai>
This commit is contained in:
co-authored by
Yifan Qiao
parent
8e981630c9
commit
481e481be7
@@ -16,10 +16,12 @@ if TYPE_CHECKING:
|
||||
from vllm.multimodal.inputs import MultiModalFeatureSpec
|
||||
from vllm.pooling_params import PoolingParams
|
||||
from vllm.sampling_params import SamplingParams
|
||||
from vllm.v1.core.kv_cache_utils import KVCacheBlockCopy
|
||||
from vllm.v1.request import Request
|
||||
else:
|
||||
ECConnectorMetadata = object
|
||||
KVConnectorMetadata = object
|
||||
KVCacheBlockCopy = object
|
||||
LoRARequest = object
|
||||
MultiModalFeatureSpec = object
|
||||
PoolingParams = object
|
||||
@@ -240,6 +242,9 @@ class SchedulerOutput:
|
||||
# preventing stale NaN/data from corrupting attention or SSM computation.
|
||||
new_block_ids_to_zero: list[int] | None = None
|
||||
|
||||
# CoW copies to apply after zeroing new blocks and before forward.
|
||||
kv_cache_block_copies: list[KVCacheBlockCopy] | None = None
|
||||
|
||||
# Dynamic speculative decoding: optimal K chosen by scheduler.
|
||||
# Number of spec tokens to schedule for the next step.
|
||||
num_spec_tokens_to_schedule: int = 0
|
||||
|
||||
@@ -259,6 +259,7 @@ class Scheduler(SchedulerInterface):
|
||||
# Create the KV cache manager.
|
||||
if hash_block_size is None:
|
||||
hash_block_size = block_size
|
||||
self.hash_block_size = hash_block_size
|
||||
self.kv_cache_manager = KVCacheManager(
|
||||
kv_cache_config=kv_cache_config,
|
||||
max_model_len=self.max_model_len,
|
||||
@@ -297,6 +298,13 @@ class Scheduler(SchedulerInterface):
|
||||
self.need_mamba_block_aligned_split = (
|
||||
self.has_mamba_layers and self.cache_config.mamba_cache_mode == "align"
|
||||
)
|
||||
# A finer prefix_match_unit is configured: a mamba partial tail entry
|
||||
# can only be registered by a step ending exactly at the prompt's last
|
||||
# hash boundary, so the split adds that stop.
|
||||
self.mamba_partial_cache_hit = (
|
||||
self.need_mamba_block_aligned_split
|
||||
and self.hash_block_size < self.block_size
|
||||
)
|
||||
|
||||
# Counts of non-empty steps scheduled / processed. update_from_output
|
||||
# is called once per scheduled step in FIFO order, so these stay in sync.
|
||||
@@ -368,9 +376,23 @@ class Scheduler(SchedulerInterface):
|
||||
if self.use_eagle:
|
||||
last_cache_position = max(last_cache_position - block_size, 0)
|
||||
num_computed_tokens_after_sched = num_computed_tokens + num_new_tokens
|
||||
if num_computed_tokens_after_sched < last_cache_position:
|
||||
# align to block_size
|
||||
num_new_tokens = num_new_tokens // block_size * block_size
|
||||
next_boundary = (num_computed_tokens // block_size + 1) * block_size
|
||||
if (
|
||||
num_computed_tokens % block_size != 0
|
||||
and next_boundary <= last_cache_position
|
||||
and num_computed_tokens_after_sched > next_boundary
|
||||
):
|
||||
# Resumed mid-block (partial hash hit): stop the first chunk
|
||||
# at the next block boundary so chunk ends re-align to blocks
|
||||
# and the boundary state is materialized before running past.
|
||||
num_new_tokens = next_boundary - num_computed_tokens
|
||||
elif num_computed_tokens_after_sched < last_cache_position:
|
||||
# Align the chunk END (not its length) to block_size;
|
||||
# identical to flooring the length when the start is
|
||||
# block-aligned. May yield 0 (insufficient budget to reach
|
||||
# the next boundary); the caller then skips the request.
|
||||
aligned_end = num_computed_tokens_after_sched // block_size * block_size
|
||||
num_new_tokens = max(aligned_end - num_computed_tokens, 0)
|
||||
elif (
|
||||
num_computed_tokens
|
||||
< last_cache_position
|
||||
@@ -378,9 +400,23 @@ class Scheduler(SchedulerInterface):
|
||||
):
|
||||
# force to cache the last chunk
|
||||
num_new_tokens = last_cache_position - num_computed_tokens
|
||||
else:
|
||||
# prefill the last few tokens
|
||||
pass
|
||||
elif self.mamba_partial_cache_hit:
|
||||
# Prefill of the final partial block: stop once at the
|
||||
# prompt's last hash boundary so the mamba partial tail entry
|
||||
# can be registered.
|
||||
tail_boundary = (
|
||||
request.num_prompt_tokens
|
||||
// self.hash_block_size
|
||||
* self.hash_block_size
|
||||
)
|
||||
if (
|
||||
num_computed_tokens
|
||||
< tail_boundary
|
||||
< num_computed_tokens_after_sched
|
||||
and tail_boundary < request.num_prompt_tokens
|
||||
and tail_boundary > last_cache_position
|
||||
):
|
||||
num_new_tokens = tail_boundary - num_computed_tokens
|
||||
|
||||
# Marconi cache admission optimization:
|
||||
# cache common prefixes by scheduling num_new_tokens = common prefix length
|
||||
@@ -1084,6 +1120,16 @@ class Scheduler(SchedulerInterface):
|
||||
new_block_ids_to_zero = (
|
||||
(new_attn_block_ids or None) if self.needs_kv_cache_zeroing else None
|
||||
)
|
||||
kv_cache_block_copies, cow_retained_blocks = (
|
||||
self.kv_cache_manager.take_kv_cache_block_copies()
|
||||
)
|
||||
if kv_cache_block_copies:
|
||||
# The copies run with this step's execution; the first non-empty
|
||||
# step at or after it gets seq `sched_step_seq + 1` (0-token steps
|
||||
# do not advance the seq), and its completion implies the copies
|
||||
# have run.
|
||||
self._free_cow_retained_blocks(cow_retained_blocks, self.sched_step_seq + 1)
|
||||
pending_kv_cache_block_copies = kv_cache_block_copies or None
|
||||
|
||||
# Dynamic speculative decoding: compute optimal K
|
||||
num_spec_tokens_to_schedule = self.num_spec_tokens
|
||||
@@ -1108,6 +1154,7 @@ class Scheduler(SchedulerInterface):
|
||||
finished_req_ids=self.finished_req_ids,
|
||||
free_encoder_mm_hashes=self.encoder_cache_manager.get_freed_mm_hashes(),
|
||||
new_block_ids_to_zero=new_block_ids_to_zero,
|
||||
kv_cache_block_copies=pending_kv_cache_block_copies,
|
||||
num_spec_tokens_to_schedule=num_spec_tokens_to_schedule,
|
||||
)
|
||||
|
||||
@@ -2148,11 +2195,23 @@ class Scheduler(SchedulerInterface):
|
||||
if blocks:
|
||||
self.deferred_frees.append((self.sched_step_seq, blocks))
|
||||
|
||||
def _free_cow_retained_blocks(
|
||||
self, blocks: list[KVCacheBlock], fence_seq: int
|
||||
) -> None:
|
||||
"""Release CoW copy retentions, deferring their return to the block
|
||||
pool while the step that runs the copy may still be in flight.
|
||||
"""
|
||||
if not self.defer_block_free or fence_seq <= self.processed_step_seq:
|
||||
self.kv_cache_manager.block_pool.free_blocks(blocks)
|
||||
return
|
||||
self.deferred_frees.append((fence_seq, blocks[::-1]))
|
||||
|
||||
def _drain_deferred_frees(self):
|
||||
"""Return deferred blocks whose fence step has completed.
|
||||
|
||||
Entries are appended with monotonically non-decreasing fences, so
|
||||
stop at the first one that is still pending.
|
||||
Fences are appended in near-monotonic order (a CoW retention fence
|
||||
can lead request-free fences by one step), so stop at the first
|
||||
pending one; any satisfied entry behind it is merely freed later.
|
||||
"""
|
||||
while self.deferred_frees:
|
||||
fence, _ = self.deferred_frees[0]
|
||||
@@ -2401,6 +2460,7 @@ class Scheduler(SchedulerInterface):
|
||||
new_computed_blocks=self.kv_cache_manager.empty_kv_cache_blocks.blocks,
|
||||
num_encoder_tokens=0,
|
||||
total_computed_tokens=request.num_computed_tokens,
|
||||
num_local_computed_tokens=request.num_computed_tokens,
|
||||
num_tokens_main_model=full_num_tokens,
|
||||
apply_admission_cap=True,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user