[ROCm][Perf] Kimi-K3 enable prefill checkpoints on ROCm (#58344)
Signed-off-by: kliuae <kuanfu.liu@embeddedllm.com>
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
"""Conv half of the ROCm Kimi-K3 prefill checkpoint."""
|
||||
|
||||
from dataclasses import dataclass
|
||||
|
||||
import torch
|
||||
|
||||
from vllm.model_executor.layers.mamba.checkpoint import (
|
||||
MambaPrefillCheckpointMetadata,
|
||||
)
|
||||
from vllm.model_executor.layers.mamba.kda_checkpoint import (
|
||||
FlashKDAPrefillCheckpointExporter,
|
||||
)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class KimiK3ROCmKDAPrefillCheckpointExporter(FlashKDAPrefillCheckpointExporter):
|
||||
"""Conv window only; `fused_kda_chunk` writes the recurrent half itself.
|
||||
|
||||
`state_len` is `conv_kernel_size - 1`, not the spec-widened conv row.
|
||||
"""
|
||||
|
||||
def export( # type: ignore[override]
|
||||
self,
|
||||
checkpoint: MambaPrefillCheckpointMetadata,
|
||||
*,
|
||||
raw_qkv: torch.Tensor,
|
||||
conv_state: torch.Tensor,
|
||||
cu_seqlens: torch.Tensor,
|
||||
) -> None:
|
||||
assert self.state_len is not None
|
||||
# An empty recurrent checkpoint masks the kernel's recurrent half off.
|
||||
super().export(
|
||||
checkpoint,
|
||||
raw_qkv=raw_qkv,
|
||||
conv_state=conv_state,
|
||||
recurrent_checkpoint=conv_state[:, :0],
|
||||
recurrent_state=conv_state,
|
||||
cu_seqlens=cu_seqlens,
|
||||
)
|
||||
@@ -16,6 +16,7 @@ from vllm.third_party.flash_linear_attention.ops.index import (
|
||||
prepare_chunk_indices,
|
||||
prepare_chunk_offsets,
|
||||
)
|
||||
from vllm.v1.attention.backends.utils import NULL_BLOCK_ID
|
||||
|
||||
CHUNK_SIZE = 64
|
||||
HEAD_DIM = 128
|
||||
@@ -28,6 +29,15 @@ _DEEP_LEN = 12
|
||||
_MAX_GROUPS = 32
|
||||
|
||||
|
||||
def _hip_checkpoint_state_indices(state_indices: torch.Tensor) -> torch.Tensor:
|
||||
"""NULL_BLOCK_ID marks no checkpoint; the HIP kernel skips negative rows."""
|
||||
return (
|
||||
torch.where(state_indices != NULL_BLOCK_ID, state_indices, -1)
|
||||
.to(torch.int32)
|
||||
.contiguous()
|
||||
)
|
||||
|
||||
|
||||
@cache
|
||||
def is_fused_kda_chunk_supported() -> bool:
|
||||
from vllm.platforms.rocm import on_gfx950
|
||||
@@ -295,7 +305,7 @@ def fused_kda_chunk(
|
||||
None if checkpoint_offsets is None else checkpoint_offsets.to(torch.int32),
|
||||
None
|
||||
if checkpoint_state_indices is None
|
||||
else checkpoint_state_indices.to(torch.int32).contiguous(),
|
||||
else _hip_checkpoint_state_indices(checkpoint_state_indices),
|
||||
state_cache,
|
||||
None if state_indices is None else state_indices.to(torch.int32).contiguous(),
|
||||
has_initial_state,
|
||||
|
||||
@@ -155,7 +155,13 @@ def chunk_kda_prefill(
|
||||
aqk=ws["aqk"],
|
||||
decay=ws["decay"],
|
||||
# v is dead by this point, so it doubles as the output buffer.
|
||||
out=out if out is not None else v,
|
||||
out=out
|
||||
if out is not None
|
||||
else (
|
||||
v
|
||||
if v.is_contiguous()
|
||||
else torch.empty_like(v, memory_format=torch.contiguous_format)
|
||||
),
|
||||
scale=scale,
|
||||
cu_seqlens=cu_seqlens,
|
||||
initial_state=initial_state,
|
||||
|
||||
Reference in New Issue
Block a user