[V1][Hybrid] Mamba Prefix Caching with align mode (#30877)
Signed-off-by: huanghaoyan.hhy <huanghaoyan.hhy@alibaba-inc.com> Signed-off-by: Chen Zhang <zhangch99@outlook.com> Co-authored-by: Chen Zhang <zhangch99@outlook.com>
This commit is contained in:
co-authored by
Chen Zhang
parent
fec9da0af4
commit
5206e5e28c
@@ -255,7 +255,8 @@ class BlockPool:
|
||||
)
|
||||
for i, blk in enumerate(new_full_blocks):
|
||||
# Some blocks may be null blocks when enabling sparse attention like
|
||||
# sliding window attention. We skip null blocks here.
|
||||
# sliding window attention, or Mamba models with prefix-caching in
|
||||
# align mode. We skip null blocks here.
|
||||
if blk.is_null:
|
||||
continue
|
||||
assert blk.block_hash is None
|
||||
|
||||
@@ -75,6 +75,7 @@ class KVCacheCoordinator(ABC):
|
||||
new_computed_blocks: tuple[Sequence[KVCacheBlock], ...],
|
||||
num_encoder_tokens: int,
|
||||
total_computed_tokens: int,
|
||||
num_tokens_main_model: int,
|
||||
) -> int:
|
||||
"""
|
||||
Get the number of blocks needed to be allocated for the request.
|
||||
@@ -88,6 +89,9 @@ class KVCacheCoordinator(ABC):
|
||||
num_encoder_tokens: The number of encoder tokens for allocating
|
||||
blocks for cross-attention.
|
||||
total_computed_tokens: Include both local and external tokens.
|
||||
num_tokens_main_model: The number of tokens for the main model (aka target
|
||||
model in spec decode). w/o spec decode, it is num_tokens;
|
||||
with spec decode, it is num_tokens - num_lookahead_tokens.
|
||||
|
||||
Returns:
|
||||
The number of blocks to allocate.
|
||||
@@ -98,7 +102,7 @@ class KVCacheCoordinator(ABC):
|
||||
# For cross-attention, we issue a single static allocation
|
||||
# of blocks based on the number of encoder input tokens.
|
||||
num_blocks_to_allocate += manager.get_num_blocks_to_allocate(
|
||||
request_id, num_encoder_tokens, [], 0
|
||||
request_id, num_encoder_tokens, [], 0, num_encoder_tokens
|
||||
)
|
||||
else:
|
||||
num_blocks_to_allocate += manager.get_num_blocks_to_allocate(
|
||||
@@ -106,6 +110,7 @@ class KVCacheCoordinator(ABC):
|
||||
num_tokens,
|
||||
new_computed_blocks[i],
|
||||
total_computed_tokens,
|
||||
num_tokens_main_model,
|
||||
)
|
||||
return num_blocks_to_allocate
|
||||
|
||||
@@ -139,6 +144,7 @@ class KVCacheCoordinator(ABC):
|
||||
self,
|
||||
request_id: str,
|
||||
num_tokens: int,
|
||||
num_tokens_main_model: int,
|
||||
num_encoder_tokens: int = 0,
|
||||
) -> tuple[list[KVCacheBlock], ...]:
|
||||
"""
|
||||
@@ -149,6 +155,9 @@ class KVCacheCoordinator(ABC):
|
||||
request_id: The request ID.
|
||||
num_tokens: The total number of tokens that need a slot (including
|
||||
tokens that are already allocated).
|
||||
num_tokens_main_model: The number of tokens for the main model (aka target
|
||||
model in spec decode). w/o spec decode, it is num_tokens;
|
||||
with spec decode, it is num_tokens - num_lookahead_tokens.
|
||||
num_encoder_tokens: The number of encoder tokens for allocating
|
||||
blocks for cross-attention.
|
||||
|
||||
@@ -161,6 +170,7 @@ class KVCacheCoordinator(ABC):
|
||||
num_encoder_tokens
|
||||
if isinstance(manager, CrossAttentionManager)
|
||||
else num_tokens,
|
||||
num_tokens_main_model,
|
||||
)
|
||||
for manager in self.single_type_managers
|
||||
)
|
||||
|
||||
@@ -307,8 +307,9 @@ class KVCacheManager:
|
||||
num_local_computed_tokens + num_external_computed_tokens,
|
||||
self.max_model_len,
|
||||
)
|
||||
num_tokens_main_model = total_computed_tokens + num_new_tokens
|
||||
num_tokens_need_slot = min(
|
||||
total_computed_tokens + num_new_tokens + num_lookahead_tokens,
|
||||
num_tokens_main_model + num_lookahead_tokens,
|
||||
self.max_model_len,
|
||||
)
|
||||
|
||||
@@ -329,6 +330,7 @@ class KVCacheManager:
|
||||
num_encoder_tokens=num_encoder_tokens,
|
||||
total_computed_tokens=num_local_computed_tokens
|
||||
+ num_external_computed_tokens,
|
||||
num_tokens_main_model=num_tokens_main_model,
|
||||
)
|
||||
|
||||
if num_blocks_to_allocate > self.block_pool.get_num_free_blocks():
|
||||
@@ -349,7 +351,10 @@ class KVCacheManager:
|
||||
)
|
||||
|
||||
new_blocks = self.coordinator.allocate_new_blocks(
|
||||
request.request_id, num_tokens_need_slot, num_encoder_tokens
|
||||
request.request_id,
|
||||
num_tokens_need_slot,
|
||||
num_tokens_main_model,
|
||||
num_encoder_tokens,
|
||||
)
|
||||
|
||||
# P/D: delay caching blocks if we have to recv from
|
||||
|
||||
@@ -47,7 +47,7 @@ from vllm.v1.core.sched.output import (
|
||||
from vllm.v1.core.sched.request_queue import SchedulingPolicy, create_request_queue
|
||||
from vllm.v1.core.sched.utils import check_stop, remove_all
|
||||
from vllm.v1.engine import EngineCoreEventType, EngineCoreOutput, EngineCoreOutputs
|
||||
from vllm.v1.kv_cache_interface import KVCacheConfig
|
||||
from vllm.v1.kv_cache_interface import KVCacheConfig, MambaSpec
|
||||
from vllm.v1.metrics.perf import ModelMetrics, PerfStats
|
||||
from vllm.v1.metrics.stats import (
|
||||
PrefixCacheStats,
|
||||
@@ -226,6 +226,17 @@ class Scheduler(SchedulerInterface):
|
||||
)
|
||||
self.use_pp = self.parallel_config.pipeline_parallel_size > 1
|
||||
self.use_v2_model_runner = envs.VLLM_USE_V2_MODEL_RUNNER
|
||||
|
||||
def has_mamba_layers(kv_cache_config: KVCacheConfig) -> bool:
|
||||
return any(
|
||||
isinstance(group_spec.kv_cache_spec, MambaSpec)
|
||||
for group_spec in kv_cache_config.kv_cache_groups
|
||||
)
|
||||
|
||||
self.has_mamba_layers = has_mamba_layers(kv_cache_config)
|
||||
self.need_mamba_block_aligned_split = (
|
||||
self.has_mamba_layers and self.cache_config.mamba_cache_mode == "align"
|
||||
)
|
||||
self.perf_metrics: ModelMetrics | None = None
|
||||
if self.log_stats and vllm_config.observability_config.enable_mfu_metrics:
|
||||
self.perf_metrics = ModelMetrics(vllm_config)
|
||||
@@ -250,6 +261,53 @@ class Scheduler(SchedulerInterface):
|
||||
vllm_config=self.vllm_config,
|
||||
)
|
||||
|
||||
def _mamba_block_aligned_split(
|
||||
self,
|
||||
request: Request,
|
||||
num_new_tokens: int,
|
||||
num_new_local_computed_tokens: int = 0,
|
||||
num_external_computed_tokens: int = 0,
|
||||
) -> int:
|
||||
assert num_external_computed_tokens == 0, (
|
||||
"External KV connector is not verified yet"
|
||||
)
|
||||
# TODO: need check for resume requests
|
||||
if request.num_output_tokens == 0: # prefill
|
||||
# To enable block-aligned caching of the Mamba state, `num_new_tokens`
|
||||
# must be a multiple of `block_size`.
|
||||
# As an exception, if `num_new_tokens` is less than `block_size`, the
|
||||
# state is simply not cached, requiring no special handling.
|
||||
# Additionally, when Eagle mode is enabled, FullAttn prunes the last
|
||||
# matching block. To prevent this from causing a Mamba cache miss, the
|
||||
# last chunk must be larger than `block_size`.
|
||||
block_size = self.cache_config.block_size
|
||||
last_cache_position = (
|
||||
request.num_prompt_tokens - request.num_prompt_tokens % block_size
|
||||
)
|
||||
# eagle prune
|
||||
if self.use_eagle:
|
||||
last_cache_position = max(last_cache_position - block_size, 0)
|
||||
num_computed_tokens = (
|
||||
request.num_computed_tokens
|
||||
+ num_new_local_computed_tokens
|
||||
+ num_external_computed_tokens
|
||||
)
|
||||
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
|
||||
elif (
|
||||
num_computed_tokens
|
||||
< last_cache_position
|
||||
< num_computed_tokens_after_sched
|
||||
):
|
||||
# force to cache the last chunk
|
||||
num_new_tokens = last_cache_position - num_computed_tokens
|
||||
else:
|
||||
# prefill the last few tokens
|
||||
pass
|
||||
return num_new_tokens
|
||||
|
||||
def schedule(self) -> SchedulerOutput:
|
||||
# NOTE(woosuk) on the scheduling algorithm:
|
||||
# There's no "decoding phase" nor "prefill phase" in the scheduler.
|
||||
@@ -340,6 +398,11 @@ class Scheduler(SchedulerInterface):
|
||||
shift_computed_tokens=1 if self.use_eagle else 0,
|
||||
)
|
||||
|
||||
if self.need_mamba_block_aligned_split:
|
||||
num_new_tokens = self._mamba_block_aligned_split(
|
||||
request, num_new_tokens
|
||||
)
|
||||
|
||||
if num_new_tokens == 0:
|
||||
# The request cannot be scheduled because one of the following
|
||||
# reasons:
|
||||
@@ -350,6 +413,8 @@ class Scheduler(SchedulerInterface):
|
||||
# its max_total_tokens or max_model_len.
|
||||
# 2. The encoder budget is exhausted.
|
||||
# 3. The encoder cache is exhausted.
|
||||
# 4. Insufficient budget for a block-aligned chunk in hybrid
|
||||
# models with mamba cache mode \"align\".
|
||||
# NOTE(woosuk): Here, by doing `continue` instead of `break`,
|
||||
# we do not strictly follow the FCFS scheduling policy and
|
||||
# allow the lower-priority requests to be scheduled.
|
||||
@@ -608,6 +673,16 @@ class Scheduler(SchedulerInterface):
|
||||
# The request cannot be scheduled.
|
||||
break
|
||||
|
||||
if self.need_mamba_block_aligned_split:
|
||||
num_new_tokens = self._mamba_block_aligned_split(
|
||||
request,
|
||||
num_new_tokens,
|
||||
num_new_local_computed_tokens,
|
||||
num_external_computed_tokens,
|
||||
)
|
||||
if num_new_tokens == 0:
|
||||
break
|
||||
|
||||
# Handles an edge case when P/D Disaggregation
|
||||
# is used with Spec Decoding where an
|
||||
# extra block gets allocated which
|
||||
|
||||
@@ -66,12 +66,17 @@ class SingleTypeKVCacheManager(ABC):
|
||||
self.kv_cache_group_id = kv_cache_group_id
|
||||
self._null_block = block_pool.null_block
|
||||
|
||||
@classmethod
|
||||
def _get_num_evictable_blocks(cls, blocks: Sequence[KVCacheBlock]):
|
||||
return sum(blk.ref_cnt == 0 and not blk.is_null for blk in blocks)
|
||||
|
||||
def get_num_blocks_to_allocate(
|
||||
self,
|
||||
request_id: str,
|
||||
num_tokens: int,
|
||||
new_computed_blocks: Sequence[KVCacheBlock],
|
||||
total_computed_tokens: int,
|
||||
num_tokens_main_model: int,
|
||||
) -> int:
|
||||
"""
|
||||
Get the number of blocks needed to be allocated for the request.
|
||||
@@ -84,6 +89,9 @@ class SingleTypeKVCacheManager(ABC):
|
||||
prefix caching.
|
||||
total_computed_tokens: Include both local and external computed
|
||||
tokens.
|
||||
num_tokens_main_model: The number of tokens for the main model (aka target
|
||||
model in spec decode). w/o spec decode, it is num_tokens;
|
||||
with spec decode, it is num_tokens - num_lookahead_tokens.
|
||||
|
||||
Returns:
|
||||
The number of blocks to allocate.
|
||||
@@ -121,9 +129,8 @@ class SingleTypeKVCacheManager(ABC):
|
||||
# If a computed block is an eviction candidate (in the free queue and
|
||||
# ref_cnt == 0), it will be removed from the free queue when touched by
|
||||
# the allocated request, so we must count it in the free-capacity check.
|
||||
num_evictable_blocks = sum(
|
||||
blk.ref_cnt == 0 and not blk.is_null
|
||||
for blk in new_computed_blocks[num_skipped_new_computed_blocks:]
|
||||
num_evictable_blocks = self._get_num_evictable_blocks(
|
||||
new_computed_blocks[num_skipped_new_computed_blocks:]
|
||||
)
|
||||
return num_new_blocks + num_evictable_blocks
|
||||
|
||||
@@ -199,7 +206,7 @@ class SingleTypeKVCacheManager(ABC):
|
||||
req_blocks.extend(allocated_blocks)
|
||||
|
||||
def allocate_new_blocks(
|
||||
self, request_id: str, num_tokens: int
|
||||
self, request_id: str, num_tokens: int, num_tokens_main_model: int
|
||||
) -> list[KVCacheBlock]:
|
||||
"""
|
||||
Allocate new blocks for the request to give it at least `num_tokens`
|
||||
@@ -209,7 +216,9 @@ class SingleTypeKVCacheManager(ABC):
|
||||
request_id: The request ID.
|
||||
num_tokens: The total number of tokens that need a slot (including
|
||||
tokens that are already allocated).
|
||||
|
||||
num_tokens_main_model: The number of tokens for the main model (aka target
|
||||
model in spec decode). w/o spec decode, it is num_tokens;
|
||||
with spec decode, it is num_tokens - num_lookahead_tokens.
|
||||
Returns:
|
||||
The new allocated blocks.
|
||||
"""
|
||||
@@ -450,12 +459,9 @@ class FullAttentionManager(SingleTypeKVCacheManager):
|
||||
|
||||
|
||||
class SlidingWindowManager(SingleTypeKVCacheManager):
|
||||
def __init__(
|
||||
self, kv_cache_spec: SlidingWindowSpec, block_pool: BlockPool, **kwargs
|
||||
) -> None:
|
||||
super().__init__(kv_cache_spec, block_pool, **kwargs)
|
||||
def __init__(self, kv_cache_spec: SlidingWindowSpec, **kwargs) -> None:
|
||||
super().__init__(kv_cache_spec, **kwargs)
|
||||
self.sliding_window = kv_cache_spec.sliding_window
|
||||
self._null_block = block_pool.null_block
|
||||
|
||||
@classmethod
|
||||
def find_longest_cache_hit(
|
||||
@@ -586,12 +592,9 @@ class SlidingWindowManager(SingleTypeKVCacheManager):
|
||||
|
||||
|
||||
class ChunkedLocalAttentionManager(SingleTypeKVCacheManager):
|
||||
def __init__(
|
||||
self, kv_cache_spec: ChunkedLocalAttentionSpec, block_pool: BlockPool, **kwargs
|
||||
) -> None:
|
||||
super().__init__(kv_cache_spec, block_pool, **kwargs)
|
||||
def __init__(self, kv_cache_spec: ChunkedLocalAttentionSpec, **kwargs) -> None:
|
||||
super().__init__(kv_cache_spec, **kwargs)
|
||||
self.attention_chunk_size = kv_cache_spec.attention_chunk_size
|
||||
self._null_block = block_pool.null_block
|
||||
|
||||
@classmethod
|
||||
def find_longest_cache_hit(
|
||||
@@ -739,6 +742,17 @@ class ChunkedLocalAttentionManager(SingleTypeKVCacheManager):
|
||||
|
||||
|
||||
class MambaManager(SingleTypeKVCacheManager):
|
||||
def __init__(self, kv_cache_spec: MambaSpec, **kwargs) -> None:
|
||||
super().__init__(kv_cache_spec, **kwargs)
|
||||
self.mamba_cache_mode = kv_cache_spec.mamba_cache_mode
|
||||
self.num_speculative_blocks: int = kv_cache_spec.num_speculative_blocks
|
||||
if self.mamba_cache_mode == "align":
|
||||
# Mapping from request ID to the index of the block
|
||||
# allocated in the previous step
|
||||
self.last_state_block_idx: dict[str, int] = {}
|
||||
# The set of the requests that have been allocated blocks
|
||||
self._allocated_block_reqs: set[str] = set()
|
||||
|
||||
@classmethod
|
||||
def find_longest_cache_hit(
|
||||
cls,
|
||||
@@ -787,6 +801,28 @@ class MambaManager(SingleTypeKVCacheManager):
|
||||
|
||||
return computed_blocks
|
||||
|
||||
def remove_skipped_blocks(self, request_id: str, num_computed_tokens: int) -> None:
|
||||
assert isinstance(self.kv_cache_spec, MambaSpec)
|
||||
super().remove_skipped_blocks(request_id, num_computed_tokens)
|
||||
if self.mamba_cache_mode == "align":
|
||||
# `last_state_block_idx` refers to the block index allocated two steps ago.
|
||||
# The block allocated in the previous step is used to copy Mamba states
|
||||
# into the block allocated in the current step; the earlier block is
|
||||
# no longer needed and should be freed here.
|
||||
last_state_block_idx = self.last_state_block_idx.get(request_id)
|
||||
# Blocks allocated during prefill may be non-contiguous. Use
|
||||
# `last_state_block_idx` to free the appropriate block and replace it
|
||||
# with a null block.
|
||||
if (
|
||||
last_state_block_idx is not None
|
||||
and last_state_block_idx
|
||||
< cdiv(num_computed_tokens, self.block_size) - 1
|
||||
):
|
||||
blocks = self.req_to_blocks[request_id]
|
||||
if blocks[last_state_block_idx] != self._null_block:
|
||||
self.block_pool.free_blocks([blocks[last_state_block_idx]])
|
||||
blocks[last_state_block_idx] = self._null_block
|
||||
|
||||
def get_num_common_prefix_blocks(self, running_request_id: str) -> int:
|
||||
"""
|
||||
cascade attention is not supported by mamba
|
||||
@@ -799,31 +835,134 @@ class MambaManager(SingleTypeKVCacheManager):
|
||||
num_tokens: int,
|
||||
new_computed_blocks: Sequence[KVCacheBlock],
|
||||
total_computed_tokens: int,
|
||||
num_tokens_main_model: int,
|
||||
) -> int:
|
||||
# Allocate extra `num_speculative_blocks` blocks for
|
||||
# speculative decoding (MTP/EAGLE) with linear attention.
|
||||
assert isinstance(self.kv_cache_spec, MambaSpec)
|
||||
if self.kv_cache_spec.num_speculative_blocks > 0:
|
||||
num_tokens += (
|
||||
self.kv_cache_spec.block_size
|
||||
* self.kv_cache_spec.num_speculative_blocks
|
||||
if self.mamba_cache_mode != "align":
|
||||
# Allocate extra `num_speculative_blocks` blocks for
|
||||
# speculative decoding (MTP/EAGLE) with linear attention.
|
||||
if self.num_speculative_blocks > 0:
|
||||
num_tokens += (
|
||||
self.kv_cache_spec.block_size * self.num_speculative_blocks
|
||||
)
|
||||
return super().get_num_blocks_to_allocate(
|
||||
request_id,
|
||||
num_tokens,
|
||||
new_computed_blocks,
|
||||
total_computed_tokens,
|
||||
num_tokens_main_model,
|
||||
)
|
||||
return super().get_num_blocks_to_allocate(
|
||||
request_id, num_tokens, new_computed_blocks, total_computed_tokens
|
||||
)
|
||||
else:
|
||||
# We don't allocate blocks for lookahead tokens in align mode, because if
|
||||
# x * block_size tokens are scheduled, num_tokens is
|
||||
# x * block_size + num_lookahead_tokens and breaks the alignment.
|
||||
# We can ignore lookahead tokens because current draft models don't have
|
||||
# mamba layers.
|
||||
num_tokens = num_tokens_main_model
|
||||
num_required_blocks = (
|
||||
cdiv(num_tokens, self.block_size) + self.num_speculative_blocks
|
||||
)
|
||||
num_new_blocks = (
|
||||
num_required_blocks
|
||||
- len(new_computed_blocks)
|
||||
- len(self.req_to_blocks[request_id])
|
||||
)
|
||||
if num_new_blocks > 0:
|
||||
if request_id in self._allocated_block_reqs:
|
||||
# Old request. Needs at most 1 more blocks as we can reuse the
|
||||
# speculative blocks in previous step.
|
||||
num_new_blocks = 1
|
||||
else:
|
||||
# First prefill. Allocate 1 block for running state and the
|
||||
# speculative blocks.
|
||||
num_new_blocks = 1 + self.num_speculative_blocks
|
||||
|
||||
num_evictable_computed_blocks = self._get_num_evictable_blocks(
|
||||
new_computed_blocks
|
||||
)
|
||||
return num_new_blocks + num_evictable_computed_blocks
|
||||
|
||||
def allocate_new_blocks(
|
||||
self, request_id: str, num_tokens: int
|
||||
self, request_id: str, num_tokens: int, num_tokens_main_model: int
|
||||
) -> list[KVCacheBlock]:
|
||||
# Allocate extra `num_speculative_blocks` blocks for
|
||||
# speculative decoding (MTP/EAGLE) with linear attention.
|
||||
assert isinstance(self.kv_cache_spec, MambaSpec)
|
||||
if self.kv_cache_spec.num_speculative_blocks > 0:
|
||||
num_tokens += (
|
||||
self.kv_cache_spec.block_size
|
||||
* self.kv_cache_spec.num_speculative_blocks
|
||||
if self.mamba_cache_mode != "align":
|
||||
# Allocate extra `num_speculative_blocks` blocks for
|
||||
# speculative decoding (MTP/EAGLE) with linear attention.
|
||||
if self.num_speculative_blocks > 0:
|
||||
num_tokens += self.block_size * self.num_speculative_blocks
|
||||
return super().allocate_new_blocks(
|
||||
request_id, num_tokens, num_tokens_main_model
|
||||
)
|
||||
return super().allocate_new_blocks(request_id, num_tokens)
|
||||
else:
|
||||
# We don't allocate blocks for lookahead tokens in align mode, because if
|
||||
# x * block_size tokens are scheduled, num_tokens is
|
||||
# x * block_size + num_lookahead_tokens and breaks the alignment.
|
||||
# We can ignore lookahead tokens because current draft models don't have
|
||||
# mamba layers.
|
||||
num_tokens = num_tokens_main_model
|
||||
req_blocks: list[KVCacheBlock] = self.req_to_blocks[request_id]
|
||||
num_required_blocks = (
|
||||
cdiv(num_tokens, self.block_size) + self.num_speculative_blocks
|
||||
)
|
||||
if num_required_blocks == len(req_blocks):
|
||||
return []
|
||||
else:
|
||||
assert num_required_blocks > len(req_blocks), (
|
||||
"num_required_blocks "
|
||||
f"{num_required_blocks} < len(req_blocks) {len(req_blocks)}"
|
||||
)
|
||||
prev_block_len = len(req_blocks)
|
||||
blocks_allocated = request_id in self._allocated_block_reqs
|
||||
# Record the last state block
|
||||
if blocks_allocated:
|
||||
# We always save the running state at the last
|
||||
# (1 + num_speculative_blocks) block
|
||||
self.last_state_block_idx[request_id] = (
|
||||
prev_block_len - 1 - self.num_speculative_blocks
|
||||
)
|
||||
elif prev_block_len > 0:
|
||||
# When a new request hits the prefix cache, the last block
|
||||
# saves the hit state.
|
||||
self.last_state_block_idx[request_id] = prev_block_len - 1
|
||||
|
||||
num_skipped_blocks = (
|
||||
num_required_blocks - self.num_speculative_blocks - 1
|
||||
)
|
||||
# null blocks
|
||||
if prev_block_len < num_skipped_blocks:
|
||||
req_blocks.extend(
|
||||
[
|
||||
self._null_block
|
||||
for _ in range(prev_block_len, num_skipped_blocks)
|
||||
]
|
||||
)
|
||||
|
||||
if blocks_allocated:
|
||||
# reuse previous speculative blocks in this step
|
||||
for block_idx in range(
|
||||
prev_block_len - self.num_speculative_blocks, prev_block_len
|
||||
):
|
||||
if block_idx < num_skipped_blocks:
|
||||
req_blocks.append(req_blocks[block_idx])
|
||||
req_blocks[block_idx] = self._null_block
|
||||
else:
|
||||
break
|
||||
num_new_blocks = num_required_blocks - len(req_blocks)
|
||||
if blocks_allocated:
|
||||
assert num_new_blocks <= 1
|
||||
else:
|
||||
assert num_new_blocks <= self.num_speculative_blocks + 1
|
||||
new_blocks = self.block_pool.get_new_blocks(num_new_blocks)
|
||||
req_blocks.extend(new_blocks)
|
||||
self._allocated_block_reqs.add(request_id)
|
||||
return req_blocks[prev_block_len:]
|
||||
|
||||
def free(self, request_id: str) -> None:
|
||||
if self.mamba_cache_mode == "align":
|
||||
self._allocated_block_reqs.discard(request_id)
|
||||
self.last_state_block_idx.pop(request_id, None)
|
||||
super().free(request_id)
|
||||
|
||||
def get_num_skipped_tokens(self, num_computed_tokens: int) -> int:
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user