[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],
|
||||
|
||||
Reference in New Issue
Block a user