Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 36 additions & 15 deletions vllm/models/kimi_k3/nvidia/kda.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
from vllm.third_party.flash_linear_attention.ops.kda import FusedRMSNormGated
from vllm.transformers_utils.configs.kimi_linear import KimiLinearConfig
from vllm.v1.attention.backend import AttentionBackend
from vllm.v1.worker.workspace import current_workspace_manager

logger = init_logger(__name__)

Expand Down Expand Up @@ -187,20 +188,12 @@ def _flashkda_prefill(
lower_bound: float,
initial_state: torch.Tensor,
cu_seqlens: torch.Tensor,
out: torch.Tensor,
final_state: torch.Tensor,
workspace: torch.Tensor,
) -> tuple[torch.Tensor, torch.Tensor]:
import vllm._flashkda_C # noqa: F401

out = torch.empty(v.shape, dtype=v.dtype, device=v.device)
final_state = torch.empty_like(initial_state)
workspace = torch.empty(
torch.ops._flashkda_C.get_workspace_size(
q.shape[0] * q.shape[1],
q.shape[2],
cu_seqlens.numel() - 1,
),
dtype=torch.uint8,
device=q.device,
)
# FlashKDA hardcodes dense Q/K/V/G strides. Beta may be row-strided because
# FlashKDA materializes its transposed [H, T] layout internally.
# TODO: Teach FlashKDA to consume beta in [T, H] layout directly instead
Expand Down Expand Up @@ -436,6 +429,21 @@ def __init__(
vllm_config.model_config.dtype,
self.gate_lower_bound,
)
self._flashkda_buffer_specs: (
tuple[tuple[tuple[int, ...], torch.dtype], ...] | None
) = None
if self.kda_prefill_backend == "flashkda":
T = vllm_config.scheduler_config.max_num_batched_tokens
N = vllm_config.scheduler_config.max_num_seqs
H, D = self.local_num_heads, self.head_dim
import vllm._flashkda_C # noqa: F401

workspace_size = torch.ops._flashkda_C.get_workspace_size(T, H, N)
self._flashkda_buffer_specs = (
((1, T, H, D), self.model_config.dtype),
((N, H, D, D), self.get_state_dtype()[1]),
((workspace_size,), torch.uint8),
)

self.o_norm = FusedRMSNormGated(self.head_dim, activation="sigmoid")
decode_norm_weight = None
Expand Down Expand Up @@ -696,6 +704,15 @@ def _prefill_conv(
)
if self.kda_prefill_backend == "flashkda":
assert self.gate_lower_bound is not None
assert self._flashkda_buffer_specs is not None
workspace_out, final_state, workspace = (
current_workspace_manager().get_simultaneous(
*self._flashkda_buffer_specs
)
)
flashkda_out = (
workspace_out if has_spec_decode else core_attn_out
)[:, : q_ns.shape[1]]
Comment on lines +713 to +715

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FlashKDA requires contiguous output buffer

https://github.com/vllm-project/FlashKDA/blob/053de1b716ef3255873e02d2d28f4adf09951978/csrc/flash_kda.cpp#L49

The slicing here might cause runtime errors.

(
core_attn_out_non_spec,
last_recurrent_state,
Expand All @@ -710,6 +727,9 @@ def _prefill_conv(
lower_bound=self.gate_lower_bound,
initial_state=initial_state,
cu_seqlens=non_spec_query_start_loc,
out=flashkda_out,
final_state=final_state[: initial_state.shape[0]],
workspace=workspace,
)
else:
(
Expand Down Expand Up @@ -766,10 +786,11 @@ def _prefill_conv(
core_attn_out.index_copy_(1, spec_token_indx, core_attn_out_spec)
core_attn_out.index_copy_(1, non_spec_token_indx, core_attn_out_non_spec)
elif core_attn_out_non_spec is not None:
# TODO: prefill and decode kernels write directly to core_attn_out
core_attn_out[0, :num_actual_tokens] = core_attn_out_non_spec[
0, :num_actual_tokens
]
if self.kda_prefill_backend != "flashkda" or m.num_prefills == 0:
# TODO: decode kernels write directly to core_attn_out
core_attn_out[0, :num_actual_tokens] = core_attn_out_non_spec[
0, :num_actual_tokens
]
else:
assert core_attn_out_spec is not None
# Triton normalizes in place, so this is a self-copy with no device
Expand Down
Loading