[Attention][MLA] Fuse Kimi-K3 chunked-context K/V packing (#51772)
Signed-off-by: Yongye Zhu <zyy1102000@gmail.com> Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
co-authored by
Claude Opus 5
mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
parent
63344913a6
commit
903d2efe7e
@@ -106,7 +106,12 @@ class AiterFlashAttnPrefillBackend(MLAPrefillBackend):
|
||||
q: torch.Tensor,
|
||||
k: torch.Tensor,
|
||||
v: torch.Tensor,
|
||||
out: torch.Tensor | None = None,
|
||||
) -> tuple[torch.Tensor, torch.Tensor]:
|
||||
assert out is None, (
|
||||
"AiterFlashAttnPrefillBackend does not report supports_out(), so it "
|
||||
"is never given a context-chunk `out` to write into."
|
||||
)
|
||||
out, lse = self.flash_attn_varlen_func(
|
||||
q=q,
|
||||
k=k,
|
||||
|
||||
@@ -74,13 +74,16 @@ class MLAPrefillBackend(ABC):
|
||||
return False
|
||||
|
||||
def supports_out(self) -> bool:
|
||||
"""Whether `run_prefill_new_tokens` honors a caller-provided `out`
|
||||
tensor of shape `[num_tokens, num_heads, v_head_dim]`, writing the
|
||||
final result into it in place.
|
||||
"""Whether `run_prefill_new_tokens` and `run_prefill_context_chunk` honor
|
||||
a caller-provided `out` tensor of shape
|
||||
`[num_tokens, num_heads, v_head_dim]`, writing the result into it in place
|
||||
and returning it.
|
||||
|
||||
When True, callers may pass `out` and skip the post-hoc
|
||||
slice/flatten/copy. False for backends that ignore `out` or emit a
|
||||
padded (`qk_head_dim`) output. Overridden by backends that support it.
|
||||
slice/flatten/copy -- and, for context chunks, size the accumulating
|
||||
partial before running any chunk. False for backends that ignore `out` or
|
||||
emit a padded (`qk_head_dim`) output. Overridden by backends that support
|
||||
it.
|
||||
"""
|
||||
return False
|
||||
|
||||
@@ -179,5 +182,6 @@ class MLAPrefillBackend(ABC):
|
||||
q: torch.Tensor,
|
||||
k: torch.Tensor,
|
||||
v: torch.Tensor,
|
||||
out: torch.Tensor | None = None,
|
||||
) -> tuple[torch.Tensor, torch.Tensor]:
|
||||
raise NotImplementedError
|
||||
|
||||
@@ -477,6 +477,7 @@ class FlashAttnPrefillBackend(MLAPrefillBackend):
|
||||
q: torch.Tensor,
|
||||
k: torch.Tensor,
|
||||
v: torch.Tensor,
|
||||
out: torch.Tensor | None = None,
|
||||
) -> tuple[torch.Tensor, torch.Tensor]:
|
||||
return self._flash_attn_varlen_diff_headdims(
|
||||
q=q,
|
||||
@@ -489,6 +490,7 @@ class FlashAttnPrefillBackend(MLAPrefillBackend):
|
||||
softmax_scale=self.scale,
|
||||
causal=False, # Context is unmasked
|
||||
return_softmax_lse=True,
|
||||
out=out,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -228,11 +228,13 @@ class FlashInferPrefillBackend(MLAPrefillBackend):
|
||||
q: torch.Tensor,
|
||||
k: torch.Tensor,
|
||||
v: torch.Tensor,
|
||||
out: torch.Tensor | None = None,
|
||||
) -> tuple[torch.Tensor, torch.Tensor]:
|
||||
attn_out, lse = self._prefill_chunks[chunk.index].run(
|
||||
q=q,
|
||||
k=k,
|
||||
v=v,
|
||||
out=out,
|
||||
return_lse=True,
|
||||
)
|
||||
|
||||
|
||||
@@ -166,11 +166,13 @@ class TokenspeedMLAPrefillBackend(MLAPrefillBackend):
|
||||
q: torch.Tensor,
|
||||
k: torch.Tensor,
|
||||
v: torch.Tensor,
|
||||
out: torch.Tensor | None = None,
|
||||
) -> tuple[torch.Tensor, torch.Tensor]:
|
||||
from tokenspeed_mla import tokenspeed_mla_prefill
|
||||
|
||||
# See note in run_prefill_new_tokens — `v` is a split-view of `kv_nope`
|
||||
# in `_compute_prefill_context` and arrives non-contiguous.
|
||||
# in the generic `_compute_prefill_context` and arrives non-contiguous.
|
||||
# (K3's fused fp8 K/V pack already hands over a contiguous `v`.)
|
||||
v = v.contiguous()
|
||||
|
||||
attn_out, lse = tokenspeed_mla_prefill(
|
||||
@@ -187,6 +189,7 @@ class TokenspeedMLAPrefillBackend(MLAPrefillBackend):
|
||||
cum_seq_lens_q=chunk.query_start_loc,
|
||||
max_seq_len_q=chunk.max_query_len,
|
||||
enable_pdl=False,
|
||||
out=out,
|
||||
)
|
||||
|
||||
# Convert from (q_len, num_heads) to (num_heads, q_len)
|
||||
|
||||
@@ -147,16 +147,18 @@ class TrtllmRaggedPrefillBackend(MLAPrefillBackend):
|
||||
q: torch.Tensor,
|
||||
k: torch.Tensor,
|
||||
v: torch.Tensor,
|
||||
out: torch.Tensor | None = None,
|
||||
) -> tuple[torch.Tensor, torch.Tensor]:
|
||||
from flashinfer.prefill import trtllm_ragged_attention_deepseek
|
||||
|
||||
out = torch.empty(
|
||||
q.shape[0],
|
||||
q.shape[1],
|
||||
v.shape[2],
|
||||
device=q.device,
|
||||
dtype=self._prefill_metadata.output_dtype,
|
||||
)
|
||||
if out is None:
|
||||
out = torch.empty(
|
||||
q.shape[0],
|
||||
q.shape[1],
|
||||
v.shape[2],
|
||||
device=q.device,
|
||||
dtype=self._prefill_metadata.output_dtype,
|
||||
)
|
||||
|
||||
attn_out, lse = trtllm_ragged_attention_deepseek(
|
||||
query=q,
|
||||
|
||||
Reference in New Issue
Block a user