Add DCP + Eagle support for Tokenspeed MLA backends (#48180)
Signed-off-by: Pavani Majety <pmajety@nvidia.com> Signed-off-by: Jingyi Yang <girasoleyang@gmail.com> Co-authored-by: Jingyi Yang <girasoleyang@gmail.com>
This commit is contained in:
co-authored by
Jingyi Yang
parent
18c4067a54
commit
7fc97042c3
@@ -752,12 +752,32 @@ class FlashInferMetadataBuilder(AttentionMetadataBuilder[FlashInferMetadata]):
|
||||
if can_use_xqa_or_trtllm_gen_decode
|
||||
else None
|
||||
)
|
||||
if (
|
||||
self.use_dcp
|
||||
and self.flashinfer_trtllm_api_decode_kernel == FlashInferDecodeKernel.XQA
|
||||
):
|
||||
logger.warning_once(
|
||||
"FlashInfer XQA decode does not support returning LSE and "
|
||||
"therefore does not support DCP, reverting to native FlashInfer "
|
||||
"decode."
|
||||
)
|
||||
self.use_trtllm_decode_attention = False
|
||||
self.flashinfer_trtllm_api_decode_kernel = None
|
||||
supports_spec_as_decode = (
|
||||
self.flashinfer_trtllm_api_decode_kernel
|
||||
== FlashInferDecodeKernel.TRTLLM_GEN
|
||||
)
|
||||
self._init_reorder_batch_threshold(
|
||||
1, supports_spec_as_decode=supports_spec_as_decode
|
||||
1,
|
||||
supports_spec_as_decode=supports_spec_as_decode,
|
||||
# trtllm-gen decode receives no cp_rank/global-seq-len information,
|
||||
# so its end-aligned causal mask is wrong for q_len > 1 over the
|
||||
# DCP-interleaved local KV shard (spec token i misses up to
|
||||
# (dcp_world_size - 1) * (q_len - 1 - i) KV entries, including its
|
||||
# own). Keep the threshold at 1 under DCP so spec queries take the
|
||||
# DCP-aware prefill path, until the kernel is CP-aware (compare
|
||||
# flash_attn_varlen_func's cp_world_size/cp_rank/cp_tot_seqused_k).
|
||||
supports_dcp_with_varlen=False,
|
||||
)
|
||||
|
||||
self._cascade_wrapper = None # Wrapper for cascade attention
|
||||
@@ -1127,9 +1147,7 @@ class FlashInferMetadataBuilder(AttentionMetadataBuilder[FlashInferMetadata]):
|
||||
has_sinks=self.has_sinks,
|
||||
has_spec=uses_spec_reorder,
|
||||
)
|
||||
decode_with_flashinfer_trtllm_api = (
|
||||
causal and self.use_trtllm_decode_attention and self.dcp_world_size <= 1
|
||||
)
|
||||
decode_with_flashinfer_trtllm_api = causal and self.use_trtllm_decode_attention
|
||||
|
||||
if not causal and self.use_dcp:
|
||||
raise NotImplementedError(
|
||||
@@ -1230,7 +1248,13 @@ class FlashInferMetadataBuilder(AttentionMetadataBuilder[FlashInferMetadata]):
|
||||
# Compute paged_kv_indices if necessary
|
||||
# paged_kv_indices is only needed for FlashInfer native paths;
|
||||
# XQA/trtllm-gen paths use block_tables directly on GPU.
|
||||
needs_paged_kv_indices = use_cascade or not all_uses_trtllm
|
||||
needs_native_paged_prefill = num_prefills > 0 and not prefill_use_trtllm
|
||||
needs_native_paged_decode = (
|
||||
num_decodes > 0 and not decode_with_flashinfer_trtllm_api
|
||||
)
|
||||
needs_paged_kv_indices = (
|
||||
use_cascade or needs_native_paged_prefill or needs_native_paged_decode
|
||||
)
|
||||
if needs_paged_kv_indices:
|
||||
assert num_blocks_np is not None
|
||||
assert seq_lens_np is not None
|
||||
@@ -1306,6 +1330,9 @@ class FlashInferMetadataBuilder(AttentionMetadataBuilder[FlashInferMetadata]):
|
||||
assert qo_indptr_prefill_cpu.shape[0] == num_prefills + 1
|
||||
|
||||
if prefill_use_trtllm:
|
||||
# TRTLLM prefill has no cross-rank combine for DCP-sharded KV;
|
||||
# use_trtllm_attention never selects it when DCP is enabled.
|
||||
assert not self.use_dcp
|
||||
# Create GPU versions
|
||||
qo_indptr_prefill_gpu = (
|
||||
qo_indptr[prefill_start:] - qo_indptr[prefill_start]
|
||||
@@ -1332,7 +1359,7 @@ class FlashInferMetadataBuilder(AttentionMetadataBuilder[FlashInferMetadata]):
|
||||
max_q_len_prefill = int(query_lens_prefill_cpu.max().item())
|
||||
attn_metadata.prefill = TRTLLMPrefill(
|
||||
block_tables=block_table_tensor[prefill_start:],
|
||||
seq_lens=seq_lens[prefill_start:],
|
||||
seq_lens=prefill_seq_lens,
|
||||
cum_seq_lens_q=qo_indptr_prefill_gpu,
|
||||
cum_seq_lens_kv=paged_kv_indptr_prefill_gpu,
|
||||
max_q_len=max_q_len_prefill,
|
||||
@@ -1409,10 +1436,16 @@ class FlashInferMetadataBuilder(AttentionMetadataBuilder[FlashInferMetadata]):
|
||||
f"Got {num_decode_tokens=} and {num_decodes=}."
|
||||
)
|
||||
assert self.flashinfer_trtllm_api_decode_kernel is not None
|
||||
seq_lens_decode = seq_lens[:num_decodes]
|
||||
if self.use_dcp:
|
||||
assert common_attn_metadata.dcp_local_seq_lens is not None
|
||||
seq_lens_decode = common_attn_metadata.dcp_local_seq_lens[
|
||||
:num_decodes
|
||||
]
|
||||
attn_metadata.decode = FlashInferTrtllmAPIDecode(
|
||||
kernel=self.flashinfer_trtllm_api_decode_kernel,
|
||||
block_tables=block_table_tensor[:num_decodes],
|
||||
seq_lens=seq_lens[:num_decodes],
|
||||
seq_lens=seq_lens_decode,
|
||||
max_seq_len=max_seq_len,
|
||||
)
|
||||
else:
|
||||
@@ -2086,6 +2119,18 @@ class FlashInferImpl(AttentionImpl):
|
||||
f"contiguous, got strides {kv_strides}"
|
||||
)
|
||||
|
||||
if use_dcp:
|
||||
assert decode_with_trtllm_gen
|
||||
if output.dtype == FP4_DTYPE:
|
||||
raise NotImplementedError(
|
||||
"DCP decode with FlashInfer trtllm-gen does not support "
|
||||
"FP4 attention output yet."
|
||||
)
|
||||
decode_query = get_dcp_group().all_gather(
|
||||
decode_query.contiguous(), dim=-2
|
||||
)
|
||||
decode_query = canonicalize_singleton_dim_strides(decode_query)
|
||||
|
||||
if output.dtype == FP4_DTYPE:
|
||||
assert self.o_sf_scale is not None
|
||||
out = FP4Tensor(
|
||||
@@ -2124,6 +2169,19 @@ class FlashInferImpl(AttentionImpl):
|
||||
else self.bmm1_scale
|
||||
)
|
||||
|
||||
lse = None
|
||||
if use_dcp:
|
||||
out = torch.empty(
|
||||
decode_query.shape,
|
||||
dtype=output.dtype,
|
||||
device=decode_query.device,
|
||||
)
|
||||
lse = torch.empty(
|
||||
(decode_query.size(0), decode_query.size(1)),
|
||||
dtype=torch.float32,
|
||||
device=decode_query.device,
|
||||
)
|
||||
|
||||
trtllm_batch_decode_with_kv_cache(
|
||||
query=decode_query,
|
||||
kv_cache=(
|
||||
@@ -2145,9 +2203,19 @@ class FlashInferImpl(AttentionImpl):
|
||||
kv_cache_sf=(
|
||||
nvfp4_kv_block_scales if self.is_kvcache_nvfp4 else None
|
||||
),
|
||||
lse=lse,
|
||||
return_lse=self.need_to_return_lse_for_decode,
|
||||
)
|
||||
|
||||
if needs_fp8_out:
|
||||
if use_dcp:
|
||||
assert isinstance(out, torch.Tensor)
|
||||
assert lse is not None
|
||||
output[:num_decode_tokens] = self.dcp_combine(
|
||||
out,
|
||||
lse,
|
||||
get_dcp_group(),
|
||||
)
|
||||
elif needs_fp8_out:
|
||||
output[:num_decode_tokens].copy_(out.to(output.dtype))
|
||||
return output_padded
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
"""TokenSpeed CuTe DSL MLA decode backend (Blackwell, FP8 KV cache only)."""
|
||||
|
||||
from typing import ClassVar
|
||||
from typing import TYPE_CHECKING, ClassVar
|
||||
|
||||
import torch
|
||||
|
||||
@@ -25,6 +25,10 @@ from vllm.v1.attention.backend import (
|
||||
)
|
||||
from vllm.v1.attention.backends.utils import KVCacheLayoutType
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from vllm.config import VllmConfig
|
||||
from vllm.v1.kv_cache_interface import AttentionSpec
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
# Workspace upper bound for tokenspeed_mla_decode (per-device, lazy):
|
||||
@@ -55,6 +59,22 @@ class TokenspeedMLAMetadataBuilder(MLACommonMetadataBuilder[MLACommonMetadata]):
|
||||
_cudagraph_support: ClassVar[AttentionCGSupport] = AttentionCGSupport.UNIFORM_BATCH
|
||||
query_len_support: ClassVar[QueryLenSupport] = QueryLenSupport.UNIFORM
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
kv_cache_spec: "AttentionSpec",
|
||||
layer_names: list[str],
|
||||
vllm_config: "VllmConfig",
|
||||
device: torch.device,
|
||||
) -> None:
|
||||
super().__init__(
|
||||
kv_cache_spec,
|
||||
layer_names,
|
||||
vllm_config,
|
||||
device,
|
||||
MLACommonMetadata,
|
||||
supports_dcp_with_varlen=True,
|
||||
)
|
||||
|
||||
|
||||
class TokenspeedMLABackend(MLACommonBackend):
|
||||
supported_dtypes: ClassVar[list[torch.dtype]] = [torch.float16, torch.bfloat16]
|
||||
@@ -130,6 +150,11 @@ class TokenspeedMLABackend(MLACommonBackend):
|
||||
|
||||
|
||||
class TokenspeedMLAImpl(MLACommonImpl[MLACommonMetadata]):
|
||||
can_return_lse_for_decode: bool = True
|
||||
# tokenspeed_mla_decode returns LSE in log2 units; its own DCP test merges
|
||||
# partial outputs with exp2(lse).
|
||||
lse_base_on_e: bool = False
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
num_heads: int,
|
||||
@@ -228,9 +253,15 @@ class TokenspeedMLAImpl(MLACommonImpl[MLACommonMetadata]):
|
||||
f"q_scale={layer._q_scale_float}, k_scale={layer._k_scale_float}."
|
||||
)
|
||||
|
||||
num_decodes = attn_metadata.num_decodes
|
||||
num_decode_tokens = attn_metadata.num_decode_tokens
|
||||
block_tables = attn_metadata.decode.block_table
|
||||
seq_lens = attn_metadata.decode.seq_lens
|
||||
causal_seqs = attn_metadata.decode.dcp_tot_seq_lens
|
||||
|
||||
# tokenspeed_mla_decode expects query shape
|
||||
# (num_decodes, q_len_per_request, num_heads, head_dim).
|
||||
if attn_metadata.num_decode_tokens % attn_metadata.num_decodes != 0:
|
||||
if num_decode_tokens % num_decodes != 0:
|
||||
logger.warning_once(
|
||||
"""TokenspeedMLAImpl got a query of uneven length.
|
||||
This usually indicates an issue in batch reordering
|
||||
@@ -238,7 +269,7 @@ class TokenspeedMLAImpl(MLACommonImpl[MLACommonMetadata]):
|
||||
)
|
||||
q = q.unsqueeze(1)
|
||||
else:
|
||||
q = q.view(attn_metadata.num_decodes, -1, q.shape[-2], q.shape[-1])
|
||||
q = q.view(num_decodes, -1, q.shape[-2], q.shape[-1])
|
||||
|
||||
if self.softmax_scale is None:
|
||||
# FP8 KV cache is mandatory for this backend, so q_scale/k_scale
|
||||
@@ -257,22 +288,29 @@ class TokenspeedMLAImpl(MLACommonImpl[MLACommonMetadata]):
|
||||
|
||||
# vLLM kv_c_and_k_pe_cache is already (num_blocks, block_size, head_size).
|
||||
# tokenspeed_mla_decode wants 3D — pass as-is (no unsqueeze, unlike trtllm).
|
||||
o = tokenspeed_mla_decode(
|
||||
return_lse = self.need_to_return_lse_for_decode
|
||||
kernel_out = tokenspeed_mla_decode(
|
||||
query=q,
|
||||
kv_cache=kv_c_and_k_pe_cache,
|
||||
workspace_buffer=self._workspace_buffer,
|
||||
kv_lora_rank=self.kv_lora_rank,
|
||||
qk_rope_head_dim=self.qk_rope_head_dim,
|
||||
block_tables=attn_metadata.decode.block_table,
|
||||
seq_lens=attn_metadata.decode.seq_lens,
|
||||
block_tables=block_tables,
|
||||
seq_lens=seq_lens,
|
||||
max_seq_len=attn_metadata.max_seq_len,
|
||||
softmax_scale=self.softmax_scale,
|
||||
output_scale=self.output_scale,
|
||||
enable_pdl=False,
|
||||
return_lse=return_lse,
|
||||
causal_seqs=causal_seqs if self.dcp_world_size > 1 else None,
|
||||
cp_world=self.dcp_world_size,
|
||||
cp_rank=self.dcp_rank,
|
||||
)
|
||||
if return_lse:
|
||||
o, lse = kernel_out
|
||||
lse = lse.view(-1, lse.shape[-1])
|
||||
else:
|
||||
o, lse = kernel_out, None
|
||||
|
||||
# Flatten the output for consistent shape
|
||||
o = o.view(-1, o.shape[-2], o.shape[-1])
|
||||
|
||||
# tokenspeed_mla_decode does not return LSE.
|
||||
return o, None
|
||||
return o, lse
|
||||
|
||||
Reference in New Issue
Block a user