[Kimi K3] Support internal prefix checkpoints with partial prefix caching and spec-decoding (#53614)
Signed-off-by: Ziming Huang <zelda.huanghuang@gmail.com> Co-authored-by: OpenAI Codex <codex@openai.com>
This commit is contained in:
co-authored by
OpenAI Codex
parent
1970f3ed4b
commit
144e79c810
@@ -609,6 +609,9 @@ class KimiK3DeltaAttention(GatedDeltaNetAttention):
|
||||
return replace(
|
||||
spec,
|
||||
num_prefill_checkpoint_blocks=int(self.kda_prefill_backend == "flashkda"),
|
||||
prefill_checkpoint_alignment=(
|
||||
16 if self.kda_prefill_backend == "flashkda" else None
|
||||
),
|
||||
)
|
||||
|
||||
def forward(
|
||||
|
||||
@@ -20,6 +20,7 @@ import torch
|
||||
from vllm.config import VllmConfig
|
||||
from vllm.platforms import current_platform
|
||||
from vllm.triton_utils import tl, triton
|
||||
from vllm.utils.math_utils import cdiv
|
||||
from vllm.utils.torch_utils import async_tensor_h2d
|
||||
from vllm.v1.attention.backend import CommonAttentionMetadata
|
||||
from vllm.v1.attention.backends.gdn_attn import (
|
||||
@@ -36,7 +37,11 @@ from vllm.v1.attention.backends.utils import (
|
||||
compute_causal_conv1d_metadata,
|
||||
split_decodes_and_prefills,
|
||||
)
|
||||
from vllm.v1.kv_cache_interface import MambaSpec
|
||||
from vllm.v1.kv_cache_interface import (
|
||||
MambaSpec,
|
||||
get_mamba_prefill_checkpoint_position,
|
||||
is_mamba_prefill_checkpoint_valid,
|
||||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from vllm.models.kimi_k3.nvidia.ops.recoverssm import (
|
||||
@@ -44,9 +49,6 @@ if TYPE_CHECKING:
|
||||
)
|
||||
|
||||
|
||||
FLASHKDA_CHUNK_SIZE = 16
|
||||
|
||||
|
||||
@cache
|
||||
def _metadata_launch_pdl() -> bool:
|
||||
return current_platform.is_arch_support_pdl()
|
||||
@@ -582,11 +584,11 @@ class KimiK3KDAMetadataBuilder(GDNAttentionMetadataBuilder):
|
||||
has_initial_state = None
|
||||
|
||||
checkpoint = None
|
||||
if (
|
||||
num_prefills > 0
|
||||
checkpoint_enabled = (
|
||||
self.vllm_config.cache_config.mamba_cache_mode == "align"
|
||||
and self.kv_cache_spec.num_prefill_checkpoint_blocks > 0
|
||||
and self.vllm_config.cache_config.mamba_cache_mode == "align"
|
||||
):
|
||||
)
|
||||
if num_prefills > 0 and checkpoint_enabled:
|
||||
# prepare checkpoint metadata
|
||||
assert m.seq_lens_cpu_upper_bound is not None
|
||||
request_rows = list(range(m.num_reqs))
|
||||
@@ -597,21 +599,39 @@ class KimiK3KDAMetadataBuilder(GDNAttentionMetadataBuilder):
|
||||
query_lens = [all_query_lens[row] for row in request_rows]
|
||||
seq_lens = m.seq_lens_cpu_upper_bound.tolist()
|
||||
block_size = self.kv_cache_spec.block_size
|
||||
hash_block_size = (
|
||||
self.vllm_config.cache_config.prefix_match_unit or block_size
|
||||
)
|
||||
speculative_config = self.vllm_config.speculative_config
|
||||
drop_eagle_block = (
|
||||
speculative_config is not None
|
||||
and speculative_config.use_eagle_block_drop()
|
||||
)
|
||||
checkpoint_splits = []
|
||||
checkpoint_cols = []
|
||||
for row, query_len in zip(request_rows, query_lens):
|
||||
seq_len = seq_lens[row]
|
||||
offset = seq_len // block_size * block_size - (seq_len - query_len)
|
||||
# offset should be less than query_len
|
||||
valid = (
|
||||
seq_len % block_size != 0
|
||||
and 0 < offset < query_len
|
||||
and offset % FLASHKDA_CHUNK_SIZE == 0
|
||||
query_start = seq_len - query_len
|
||||
checkpoint_position = get_mamba_prefill_checkpoint_position(
|
||||
seq_len,
|
||||
hash_block_size,
|
||||
drop_eagle_block=drop_eagle_block,
|
||||
)
|
||||
offset = checkpoint_position - query_start
|
||||
valid = is_mamba_prefill_checkpoint_valid(
|
||||
query_start=query_start,
|
||||
query_end=seq_len,
|
||||
checkpoint_position=checkpoint_position,
|
||||
hash_block_size=hash_block_size,
|
||||
mamba_block_size=block_size,
|
||||
checkpoint_alignment=(
|
||||
self.kv_cache_spec.prefill_checkpoint_alignment
|
||||
),
|
||||
)
|
||||
offset = offset if valid else 0
|
||||
first_len = offset or query_len
|
||||
checkpoint_splits.append((first_len, query_len - first_len))
|
||||
checkpoint_cols.append(seq_len // block_size - 1 if valid else -1)
|
||||
checkpoint_cols.append(cdiv(seq_len, block_size) - 2 if valid else -1)
|
||||
if any(tail for _, tail in checkpoint_splits):
|
||||
checkpoint_offsets_tensor = async_tensor_h2d(
|
||||
[first if tail else 0 for first, tail in checkpoint_splits],
|
||||
|
||||
@@ -450,6 +450,7 @@ class BlockPool:
|
||||
num_tokens: int,
|
||||
kv_cache_group_id: int,
|
||||
block_size: int,
|
||||
replace_existing_hashes: bool = False,
|
||||
) -> BlockHashWithGroupId | None:
|
||||
"""Register a partial prefix-cache entry for an existing block.
|
||||
|
||||
@@ -477,6 +478,9 @@ class BlockPool:
|
||||
entry hash itself is always the prefix-chain hash at
|
||||
``num_tokens``; ``block_size`` is used to assert that the
|
||||
entry is partial within the owning cache block.
|
||||
replace_existing_hashes: Whether the block contents were replaced
|
||||
and all existing cache entries must be removed before the new
|
||||
entry is registered.
|
||||
|
||||
Returns:
|
||||
The hash key with group ID if a partial entry can be registered;
|
||||
@@ -485,9 +489,10 @@ class BlockPool:
|
||||
if block.is_null:
|
||||
return None
|
||||
|
||||
assert block_size > self.hash_block_size
|
||||
assert block_size % self.hash_block_size == 0
|
||||
assert num_tokens % block_size != 0
|
||||
assert replace_existing_hashes or (
|
||||
block_size > self.hash_block_size and num_tokens % block_size != 0
|
||||
)
|
||||
block_hash = self._get_partial_block_hash(request, num_tokens)
|
||||
num_hash_blocks = num_tokens // self.hash_block_size
|
||||
block_hash_with_group_id = make_block_hash_with_group_id(
|
||||
@@ -498,7 +503,11 @@ class BlockPool:
|
||||
block_hash_with_group_id, block.block_id
|
||||
)
|
||||
)
|
||||
if (
|
||||
if replace_existing_hashes:
|
||||
removed_hashes = self._remove_cached_block_hashes(block)
|
||||
self._emit_block_removed_events(removed_hashes)
|
||||
already_cached = False
|
||||
elif (
|
||||
not already_cached
|
||||
and block.block_hash is not None
|
||||
and block.block_hash_num_tokens is not None
|
||||
|
||||
@@ -15,6 +15,7 @@ from vllm.v1.core.kv_cache_utils import (
|
||||
)
|
||||
from vllm.v1.core.single_type_kv_cache_manager import (
|
||||
CrossAttentionManager,
|
||||
MambaManager,
|
||||
SingleTypeKVCacheManager,
|
||||
get_manager_for_kv_cache_spec,
|
||||
)
|
||||
@@ -151,6 +152,11 @@ class KVCacheCoordinator(ABC):
|
||||
)
|
||||
for i, kv_cache_group in enumerate(self.kv_cache_config.kv_cache_groups)
|
||||
)
|
||||
# Match Mamba checkpoints to Eagle's attention replay boundary.
|
||||
if use_eagle:
|
||||
for manager in self.single_type_managers:
|
||||
if isinstance(manager, MambaManager):
|
||||
manager.drop_eagle_checkpoint_block = True
|
||||
|
||||
# A positive retention interval must be a multiple of the base hit granularity
|
||||
# (``scheduler_block_size``) to land on real cache-hit boundaries.
|
||||
|
||||
@@ -54,7 +54,12 @@ from vllm.v1.core.sched.request_queue import (
|
||||
)
|
||||
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, MambaSpec
|
||||
from vllm.v1.kv_cache_interface import (
|
||||
KVCacheConfig,
|
||||
MambaSpec,
|
||||
get_mamba_prefill_checkpoint_position,
|
||||
is_mamba_prefill_checkpoint_valid,
|
||||
)
|
||||
from vllm.v1.metrics.perf import ModelMetrics, PerfStats
|
||||
from vllm.v1.metrics.stats import (
|
||||
PrefixCacheStats,
|
||||
@@ -334,15 +339,20 @@ class Scheduler(SchedulerInterface):
|
||||
self.need_mamba_block_aligned_split = (
|
||||
self.has_mamba_layers and self.cache_config.mamba_cache_mode == "align"
|
||||
)
|
||||
self.mamba_has_prefill_checkpoint_blocks = (
|
||||
self.has_mamba_layers
|
||||
# TODO: support spec decoding
|
||||
and not self.use_eagle
|
||||
and all(
|
||||
not isinstance(group.kv_cache_spec, MambaSpec)
|
||||
or group.kv_cache_spec.num_prefill_checkpoint_blocks > 0
|
||||
# TODO: Support models with multiple Mamba specs that require different
|
||||
# prefill checkpoint alignments instead of selecting the first one.
|
||||
self.mamba_prefill_checkpoint_alignment = next(
|
||||
(
|
||||
group.kv_cache_spec.prefill_checkpoint_alignment
|
||||
for group in kv_cache_config.kv_cache_groups
|
||||
)
|
||||
if isinstance(group.kv_cache_spec, MambaSpec)
|
||||
),
|
||||
None,
|
||||
)
|
||||
self.mamba_has_prefill_checkpoint_blocks = self.has_mamba_layers and all(
|
||||
not isinstance(group.kv_cache_spec, MambaSpec)
|
||||
or group.kv_cache_spec.num_prefill_checkpoint_blocks > 0
|
||||
for group in kv_cache_config.kv_cache_groups
|
||||
)
|
||||
# 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
|
||||
@@ -426,8 +436,22 @@ class Scheduler(SchedulerInterface):
|
||||
last_cache_position = max(last_cache_position - block_size, 0)
|
||||
|
||||
end = start + num_new_tokens
|
||||
checkpoint_position = get_mamba_prefill_checkpoint_position(
|
||||
prefill_end,
|
||||
self.hash_block_size,
|
||||
drop_eagle_block=self.use_eagle_block_drop,
|
||||
)
|
||||
use_internal_checkpoint = (
|
||||
self.mamba_has_prefill_checkpoint_blocks and start % block_size == 0
|
||||
self.mamba_has_prefill_checkpoint_blocks
|
||||
and end >= prefill_end
|
||||
and is_mamba_prefill_checkpoint_valid(
|
||||
query_start=start,
|
||||
query_end=end,
|
||||
checkpoint_position=checkpoint_position,
|
||||
hash_block_size=self.hash_block_size,
|
||||
mamba_block_size=block_size,
|
||||
checkpoint_alignment=self.mamba_prefill_checkpoint_alignment,
|
||||
)
|
||||
)
|
||||
if use_internal_checkpoint:
|
||||
last_cache_position = 0
|
||||
@@ -436,7 +460,7 @@ class Scheduler(SchedulerInterface):
|
||||
# aligned. Exempt: the prompt's last chunk, whose slot decode advances
|
||||
# to the boundary. A block too wide for one chunk advances sub-block
|
||||
# and re-aligns at the next boundary.
|
||||
if end < prefill_end and not use_internal_checkpoint:
|
||||
if end < prefill_end:
|
||||
max_prefill_tokens = self.max_num_scheduled_tokens
|
||||
long_prefill_threshold = self.scheduler_config.long_prefill_token_threshold
|
||||
if long_prefill_threshold > 0:
|
||||
@@ -448,13 +472,15 @@ class Scheduler(SchedulerInterface):
|
||||
next_block_boundary = (start // block_size + 1) * block_size
|
||||
tail_boundary = (
|
||||
request.num_prompt_tokens // self.hash_block_size * self.hash_block_size
|
||||
if self.mamba_partial_cache_hit
|
||||
if self.mamba_partial_cache_hit and not use_internal_checkpoint
|
||||
else 0
|
||||
)
|
||||
stops = (
|
||||
# Same invariant: a chunk starting mid-block stops at the boundary
|
||||
# rather than running past it.
|
||||
next_block_boundary if start % block_size != 0 else 0,
|
||||
next_block_boundary
|
||||
if start % block_size != 0 and not use_internal_checkpoint
|
||||
else 0,
|
||||
# Never run past the last cacheable block boundary mid-chunk.
|
||||
last_cache_position,
|
||||
# Fine-grained hits: the prompt's partial-tail entry can only be
|
||||
|
||||
@@ -31,6 +31,8 @@ from vllm.v1.kv_cache_interface import (
|
||||
SinkFullAttentionSpec,
|
||||
SlidingWindowMLASpec,
|
||||
SlidingWindowSpec,
|
||||
get_mamba_prefill_checkpoint_position,
|
||||
is_mamba_prefill_checkpoint_valid,
|
||||
)
|
||||
from vllm.v1.kv_cache_spec_registry import KVCacheSpecRegistry
|
||||
from vllm.v1.request import Request
|
||||
@@ -1399,6 +1401,12 @@ class MambaManager(SingleTypeKVCacheManager):
|
||||
self.block_size = kv_cache_spec.block_size
|
||||
self.mamba_cache_mode = kv_cache_spec.mamba_cache_mode
|
||||
self.num_speculative_blocks: int = kv_cache_spec.num_speculative_blocks
|
||||
self.has_prefill_checkpoint_blocks = (
|
||||
self.mamba_cache_mode == "align"
|
||||
and kv_cache_spec.num_prefill_checkpoint_blocks > 0
|
||||
)
|
||||
# Mamba checkpoints follow Eagle's global replay boundary.
|
||||
self.drop_eagle_checkpoint_block = False
|
||||
self.cached_blocks_this_step: set[BlockHashWithGroupId] = set()
|
||||
if self.mamba_cache_mode == "align":
|
||||
# Mapping from request ID to the index of the block
|
||||
@@ -1406,9 +1414,9 @@ class MambaManager(SingleTypeKVCacheManager):
|
||||
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()
|
||||
# Number of internal checkpoint blocks required by each request's
|
||||
# Absolute checkpoint position selected by the scheduler for the
|
||||
# current allocation.
|
||||
self._num_checkpoint_blocks: dict[str, int] = {}
|
||||
self._checkpoint_positions: dict[str, int] = {}
|
||||
# Requests that registered their own last-prompt-boundary partial
|
||||
# tail (producers). A later CoW hands its private copy to the
|
||||
# connector; a request that finishes first hands off this table
|
||||
@@ -1592,18 +1600,32 @@ class MambaManager(SingleTypeKVCacheManager):
|
||||
def _needs_internal_checkpoint(
|
||||
self,
|
||||
request_id: str,
|
||||
num_tokens: int,
|
||||
num_computed_tokens: int,
|
||||
query_start: int,
|
||||
query_end: int,
|
||||
checkpoint_position: int,
|
||||
) -> bool:
|
||||
assert isinstance(self.kv_cache_spec, MambaSpec)
|
||||
checkpoint_idx = cdiv(num_tokens, self.block_size) - 2
|
||||
checkpoint_idx = cdiv(query_end, self.block_size) - 2
|
||||
blocks = self.req_to_blocks[request_id]
|
||||
return (
|
||||
self.kv_cache_spec.num_prefill_checkpoint_blocks > 0
|
||||
and num_tokens % self.block_size != 0
|
||||
and num_computed_tokens % self.block_size == 0
|
||||
self.has_prefill_checkpoint_blocks
|
||||
and is_mamba_prefill_checkpoint_valid(
|
||||
query_start=query_start,
|
||||
query_end=query_end,
|
||||
checkpoint_position=checkpoint_position,
|
||||
hash_block_size=self.block_pool.hash_block_size,
|
||||
mamba_block_size=self.block_size,
|
||||
checkpoint_alignment=(self.kv_cache_spec.prefill_checkpoint_alignment),
|
||||
)
|
||||
and checkpoint_idx >= 0
|
||||
and (checkpoint_idx >= len(blocks) or blocks[checkpoint_idx].is_null)
|
||||
and (
|
||||
checkpoint_idx >= len(blocks)
|
||||
or blocks[checkpoint_idx].is_null
|
||||
or (
|
||||
request_id in self._allocated_block_reqs
|
||||
and checkpoint_idx >= len(blocks) - self.num_speculative_blocks
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
def get_num_blocks_to_allocate(
|
||||
@@ -1668,17 +1690,27 @@ class MambaManager(SingleTypeKVCacheManager):
|
||||
)
|
||||
if has_partial_hit:
|
||||
num_new_blocks = max(num_new_blocks, 0) + 1
|
||||
checkpoint_block = int(
|
||||
self._needs_internal_checkpoint(
|
||||
request_id, num_tokens, total_computed_tokens
|
||||
)
|
||||
checkpoint_position = get_mamba_prefill_checkpoint_position(
|
||||
num_tokens,
|
||||
self.block_pool.hash_block_size,
|
||||
self.drop_eagle_checkpoint_block,
|
||||
)
|
||||
if not self._needs_internal_checkpoint(
|
||||
request_id,
|
||||
total_computed_tokens,
|
||||
num_tokens,
|
||||
checkpoint_position,
|
||||
):
|
||||
checkpoint_position = 0
|
||||
checkpoint_block = int(checkpoint_position > 0)
|
||||
if not apply_admission_cap:
|
||||
self._num_checkpoint_blocks[request_id] = checkpoint_block
|
||||
self._checkpoint_positions[request_id] = checkpoint_position
|
||||
if num_new_blocks > 0:
|
||||
num_new_blocks = 1 + int(has_partial_hit) + checkpoint_block
|
||||
if request_id not in self._allocated_block_reqs:
|
||||
num_new_blocks += self.num_speculative_blocks
|
||||
blocks_allocated = request_id in self._allocated_block_reqs
|
||||
if not (checkpoint_block and blocks_allocated):
|
||||
num_new_blocks = 1 + int(has_partial_hit) + checkpoint_block
|
||||
if not blocks_allocated:
|
||||
num_new_blocks += self.num_speculative_blocks
|
||||
|
||||
num_evictable_computed_blocks = self._get_num_evictable_blocks(
|
||||
new_computed_blocks
|
||||
@@ -1710,12 +1742,17 @@ class MambaManager(SingleTypeKVCacheManager):
|
||||
num_required_blocks = (
|
||||
cdiv(num_tokens, self.block_size) + self.num_speculative_blocks
|
||||
)
|
||||
checkpoint_block = self._num_checkpoint_blocks.get(request_id, 0)
|
||||
checkpoint_position = self._checkpoint_positions.get(request_id, 0)
|
||||
checkpoint_block = int(checkpoint_position > 0)
|
||||
partial_hit = self._partial_hit_reqs.get(request_id)
|
||||
has_partial_hit = partial_hit is not None
|
||||
# `num_required_blocks` might be less than `len(req_blocks)` if blocks are
|
||||
# over-allocated at last round.
|
||||
if num_required_blocks <= len(req_blocks) and not has_partial_hit:
|
||||
if (
|
||||
num_required_blocks <= len(req_blocks)
|
||||
and not has_partial_hit
|
||||
and not checkpoint_block
|
||||
):
|
||||
self._allocated_block_reqs.add(request_id)
|
||||
return []
|
||||
else:
|
||||
@@ -1745,7 +1782,7 @@ class MambaManager(SingleTypeKVCacheManager):
|
||||
[self._null_block for _ in range(prev_block_len, null_end)]
|
||||
)
|
||||
|
||||
if blocks_allocated:
|
||||
if blocks_allocated and not checkpoint_block:
|
||||
# Relocate exclusively owned speculative scratch blocks.
|
||||
for block_idx in range(
|
||||
prev_block_len - self.num_speculative_blocks, prev_block_len
|
||||
@@ -1754,11 +1791,11 @@ class MambaManager(SingleTypeKVCacheManager):
|
||||
self._relocate_speculative_block(req_blocks, block_idx)
|
||||
else:
|
||||
break
|
||||
num_new_blocks = num_required_blocks - len(req_blocks)
|
||||
num_new_blocks = max(num_required_blocks - len(req_blocks), 0)
|
||||
if has_partial_hit:
|
||||
num_new_blocks = max(num_new_blocks, 0) + 1
|
||||
max_new_blocks = 1 + int(has_partial_hit) + checkpoint_block
|
||||
if not blocks_allocated:
|
||||
if not blocks_allocated or checkpoint_block:
|
||||
max_new_blocks += self.num_speculative_blocks
|
||||
assert num_new_blocks <= max_new_blocks
|
||||
new_blocks = self.block_pool.get_new_blocks(num_new_blocks)
|
||||
@@ -1838,7 +1875,7 @@ class MambaManager(SingleTypeKVCacheManager):
|
||||
if self.mamba_cache_mode == "align":
|
||||
self._allocated_block_reqs.discard(request_id)
|
||||
self.last_state_block_idx.pop(request_id, None)
|
||||
self._num_checkpoint_blocks.pop(request_id, None)
|
||||
self._checkpoint_positions.pop(request_id, None)
|
||||
self._producer_partial_tail_reqs.pop(request_id, None)
|
||||
# An offer is only guaranteed to hold committed bytes until the end
|
||||
# of the pass that made it. This request's blocks are going back to
|
||||
@@ -1885,6 +1922,7 @@ class MambaManager(SingleTypeKVCacheManager):
|
||||
continue
|
||||
self.cached_blocks_this_step.add(block.block_hash)
|
||||
if self.mamba_cache_mode == "align":
|
||||
assert block.block_hash_num_tokens is not None
|
||||
# Offer every retained boundary with its exact block.
|
||||
# The connector filters against its save window, which may
|
||||
# extend past the original prompt during resumed prefill.
|
||||
@@ -1893,7 +1931,7 @@ class MambaManager(SingleTypeKVCacheManager):
|
||||
request.request_id,
|
||||
self.kv_cache_group_id,
|
||||
block,
|
||||
(idx + 1) * self.block_size,
|
||||
block.block_hash_num_tokens,
|
||||
)
|
||||
)
|
||||
|
||||
@@ -1906,6 +1944,24 @@ class MambaManager(SingleTypeKVCacheManager):
|
||||
num_tokens: int,
|
||||
) -> BlockHashWithGroupId | None:
|
||||
hash_block_size = self.block_pool.hash_block_size
|
||||
# Re-key the reserved block at its exported checkpoint boundary.
|
||||
checkpoint_position = self._checkpoint_positions.get(request.request_id, 0)
|
||||
if checkpoint_position > 0:
|
||||
# TODO: Store the reserved slot explicitly for multi-module MTP.
|
||||
checkpoint_idx = cdiv(num_tokens, self.block_size) - 2
|
||||
blocks = self.req_to_blocks[request.request_id]
|
||||
assert 0 <= checkpoint_idx < len(blocks)
|
||||
checkpoint_block = blocks[checkpoint_idx]
|
||||
if checkpoint_block.block_hash_num_tokens == checkpoint_position:
|
||||
return None
|
||||
return self.block_pool.cache_partial_block(
|
||||
request=request,
|
||||
block=checkpoint_block,
|
||||
num_tokens=checkpoint_position,
|
||||
kv_cache_group_id=self.kv_cache_group_id,
|
||||
block_size=self.block_size,
|
||||
replace_existing_hashes=True,
|
||||
)
|
||||
if self.block_size == hash_block_size:
|
||||
return None
|
||||
if num_tokens % self.block_size == 0:
|
||||
|
||||
@@ -890,6 +890,7 @@ class MambaSpec(KVCacheSpec):
|
||||
mamba_cache_mode: str = "none"
|
||||
num_speculative_blocks: int = 0
|
||||
num_prefill_checkpoint_blocks: int = 0
|
||||
prefill_checkpoint_alignment: int | None = None
|
||||
num_heads: int = 1
|
||||
tokens_per_state: int = -1
|
||||
# False: the state is sharded across TP ranks (e.g. GDN). True: every TP
|
||||
@@ -950,12 +951,49 @@ class MambaSpec(KVCacheSpec):
|
||||
isinstance(spec, MambaSpec)
|
||||
and spec.num_speculative_blocks == self.num_speculative_blocks
|
||||
and spec.num_prefill_checkpoint_blocks == self.num_prefill_checkpoint_blocks
|
||||
and spec.prefill_checkpoint_alignment == self.prefill_checkpoint_alignment
|
||||
and spec.page_size_bytes == self.page_size_bytes
|
||||
and spec.tp_replicated == self.tp_replicated
|
||||
for spec in kv_cache_specs.values()
|
||||
)
|
||||
|
||||
|
||||
def get_mamba_prefill_checkpoint_position(
|
||||
num_tokens: int,
|
||||
hash_block_size: int,
|
||||
drop_eagle_block: bool,
|
||||
) -> int:
|
||||
"""Return the reusable Mamba checkpoint boundary for a prefill."""
|
||||
checkpoint_position = (num_tokens - 1) // hash_block_size * hash_block_size
|
||||
if drop_eagle_block:
|
||||
checkpoint_position -= hash_block_size
|
||||
return max(checkpoint_position, 0)
|
||||
|
||||
|
||||
def is_mamba_prefill_checkpoint_valid(
|
||||
query_start: int,
|
||||
query_end: int,
|
||||
checkpoint_position: int,
|
||||
hash_block_size: int,
|
||||
mamba_block_size: int,
|
||||
checkpoint_alignment: int | None,
|
||||
) -> bool:
|
||||
"""Whether a backend can export the checkpoint in this query."""
|
||||
if checkpoint_alignment is None:
|
||||
return False
|
||||
assert checkpoint_alignment > 0
|
||||
|
||||
initial_state_col = (query_start - 1) // mamba_block_size
|
||||
checkpoint_col = cdiv(query_end, mamba_block_size) - 2
|
||||
return (
|
||||
query_start % hash_block_size == 0
|
||||
and checkpoint_col > initial_state_col
|
||||
and query_start + hash_block_size <= checkpoint_position
|
||||
and query_start < checkpoint_position < query_end
|
||||
and (checkpoint_position - query_start) % checkpoint_alignment == 0
|
||||
)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class EncoderOnlyAttentionSpec(AttentionSpec):
|
||||
def max_memory_usage_bytes(self, vllm_config: VllmConfig) -> int:
|
||||
|
||||
Reference in New Issue
Block a user