[fix] Remove trtllm ragged mla prefills (#36540)

Signed-off-by: Olya Kozlova <okozlova@nvidia.com>
This commit is contained in:
Olya Kozlova
2026-03-31 12:30:27 -07:00
committed by GitHub
parent b779eb3363
commit 598190aac3
8 changed files with 185 additions and 35 deletions
+8 -1
View File
@@ -264,9 +264,16 @@ def merge_attn_states(
suffix_output: torch.Tensor,
suffix_lse: torch.Tensor,
output_lse: torch.Tensor | None = None,
prefill_tokens_with_context: int | None = None,
) -> None:
torch.ops._C.merge_attn_states(
output, output_lse, prefix_output, prefix_lse, suffix_output, suffix_lse
output,
output_lse,
prefix_output,
prefix_lse,
suffix_output,
suffix_lse,
prefill_tokens_with_context,
)
@@ -1181,6 +1181,7 @@ class MLACommonPrefillMetadata:
padded_local_cu_seq_lens: torch.Tensor | None = None
cu_seq_lens_lst: list[list[int]] | None = None
chunk_size: int | None = None
prefill_tokens_with_context: int | None = None
block_table: torch.Tensor
query_start_loc: torch.Tensor
@@ -1743,6 +1744,9 @@ class MLACommonMetadataBuilder(AttentionMetadataBuilder[M]):
prefill_query_start_loc = (
query_start_loc[reqs_start:] - query_start_loc[reqs_start]
)
prefill_query_start_loc_cpu = (
query_start_loc_cpu[reqs_start:] - query_start_loc_cpu[reqs_start]
)
chunked_context_metadata = None
if max_context_len_cpu > 0:
@@ -1864,6 +1868,11 @@ class MLACommonMetadataBuilder(AttentionMetadataBuilder[M]):
if self._use_cudnn_prefill
else MLACommonPrefillMetadata.ChunkedContextMetadata
)
prefill_tokens_with_context = None
if num_prefills_with_context_cpu > 0:
prefill_tokens_with_context = prefill_query_start_loc_cpu[
num_prefills_with_context_cpu
].item()
if self.dcp_world_size > 1:
chunked_context_metadata = chunked_context_metadata_cls(
cu_seq_lens=cu_seq_lens_cpu.to(device, non_blocking=True),
@@ -1883,6 +1892,7 @@ class MLACommonMetadataBuilder(AttentionMetadataBuilder[M]):
),
cu_seq_lens_lst=cu_seq_lens_cpu.tolist(),
chunk_size=padded_local_max_context_chunk_across_ranks,
prefill_tokens_with_context=prefill_tokens_with_context,
)
else:
chunked_context_metadata = chunked_context_metadata_cls(
@@ -1896,6 +1906,7 @@ class MLACommonMetadataBuilder(AttentionMetadataBuilder[M]):
),
chunk_total_token=chunk_total_token,
workspace=self.chunked_prefill_workspace,
prefill_tokens_with_context=prefill_tokens_with_context,
)
if self._use_cudnn_prefill:
@@ -2382,14 +2393,13 @@ class MLACommonImpl(MLAAttentionImpl[M], Generic[M]):
assert prefill.chunked_context.seq_lens[chunk_idx] is not None
assert prefill.workspace_buffer is not None
out = torch.zeros(
out = torch.empty(
q.shape[0],
q.shape[1],
v.shape[2],
device=q.device,
dtype=prefill.output_dtype,
)
prefill.workspace_buffer.fill_(0)
attn_out, lse = trtllm_ragged_attention_deepseek(
query=q,
@@ -2691,6 +2701,7 @@ class MLACommonImpl(MLAAttentionImpl[M], Generic[M]):
)
if has_context:
assert prefill_metadata.chunked_context is not None
suffix_output, suffix_lse = output_prefill
if self.dcp_world_size > 1:
context_output, context_lse = (
@@ -2719,6 +2730,7 @@ class MLACommonImpl(MLAAttentionImpl[M], Generic[M]):
prefix_lse=context_lse,
suffix_output=suffix_output,
suffix_lse=suffix_lse,
prefill_tokens_with_context=prefill_metadata.chunked_context.prefill_tokens_with_context,
)
else:
output_prefill = output_prefill[..., : v.shape[-1]].flatten(start_dim=-2)
+43 -2
View File
@@ -13,7 +13,36 @@ def merge_attn_states(
suffix_output: torch.Tensor,
suffix_lse: torch.Tensor,
output_lse: torch.Tensor | None = None,
prefill_tokens_with_context: int | None = None,
) -> None:
"""Merge partial attention outputs from prefix (KV cache) and suffix
(new tokens) into a single output tensor using the log-sum-exp (LSE)
rescaling method described in section 2.2 of
https://www.arxiv.org/pdf/2501.01005.
For tokens that have prefix context (token index < prefill_tokens_with_context),
the prefix and suffix partial outputs are combined as a weighted sum.
For tokens without prefix context, the suffix output is copied directly.
Args:
output: Output tensor of shape [NUM_TOKENS, NUM_HEADS, HEAD_SIZE].
prefix_output: Partial attention output over the prefix (KV cache),
shape [NUM_TOKENS, NUM_HEADS, HEAD_SIZE].
prefix_lse: Log-sum-exp values for the prefix attention,
shape [NUM_HEADS, NUM_TOKENS].
suffix_output: Partial attention output over the suffix (new KV),
shape [NUM_TOKENS, NUM_HEADS, HEAD_SIZE].
suffix_lse: Log-sum-exp values for the suffix attention,
shape [NUM_HEADS, NUM_TOKENS].
output_lse: Optional tensor to store the merged LSE values,
shape [NUM_HEADS, NUM_TOKENS]. If None, LSE is not written out.
prefill_tokens_with_context: Number of prefill tokens that have
prefix context and therefore require merging. Tokens at indices
>= this value are decode or context-free prefill tokens whose
output is taken directly from suffix_output. If None, all tokens
are treated as having context.
"""
# NOTE(DefTruth): Currently, custom merge_attn_states CUDA kernel
# does not support FP8 dtype, fallback to use Triton kernel.
def supported_dtypes(o: torch.Tensor) -> bool:
@@ -37,11 +66,23 @@ def merge_attn_states(
from vllm._custom_ops import merge_attn_states
return merge_attn_states(
output, prefix_output, prefix_lse, suffix_output, suffix_lse, output_lse
output,
prefix_output,
prefix_lse,
suffix_output,
suffix_lse,
output_lse,
prefill_tokens_with_context,
)
else:
from vllm.v1.attention.ops.triton_merge_attn_states import merge_attn_states
return merge_attn_states(
output, prefix_output, prefix_lse, suffix_output, suffix_lse, output_lse
output,
prefix_output,
prefix_lse,
suffix_output,
suffix_lse,
output_lse,
prefill_tokens_with_context,
)
@@ -15,6 +15,7 @@ def merge_attn_states(
suffix_output: torch.Tensor,
suffix_lse: torch.Tensor,
output_lse: torch.Tensor | None = None,
prefill_tokens_with_context: int | None = None,
) -> None:
num_tokens = output.shape[0]
num_query_heads = output.shape[1]
@@ -25,6 +26,11 @@ def merge_attn_states(
# backend.
prefix_head_stride = prefix_output.stride(1)
output_head_stride = output.stride(1)
# If prefill_tokens_with_context is None, all tokens should use prefix context
if prefill_tokens_with_context is None:
prefill_tokens_with_context = num_tokens
# TODO(woosuk): Use CUDA kernel instead of Triton to minimize CPU overhead.
merge_attn_states_kernel[(num_tokens, num_query_heads)](
output,
@@ -38,6 +44,7 @@ def merge_attn_states(
head_size,
padded_head_size,
output_lse is not None,
prefill_tokens_with_context,
)
@@ -54,12 +61,44 @@ def merge_attn_states_kernel(
HEAD_SIZE: tl.constexpr,
PADDED_HEAD_SIZE: tl.constexpr,
OUTPUT_LSE: tl.constexpr,
prefill_tokens_with_context: tl.constexpr,
):
token_idx = tl.program_id(0)
num_tokens = tl.num_programs(0)
head_idx = tl.program_id(1)
num_heads = tl.num_programs(1)
prefix_mask = token_idx < prefill_tokens_with_context
head_arange = tl.arange(0, PADDED_HEAD_SIZE)
head_mask = head_arange < HEAD_SIZE
# For tokens without context (token_idx >= prefill_tokens_with_context),
# directly copy from suffix_output
if not prefix_mask:
s_lse = tl.load(suffix_lse + head_idx * num_tokens + token_idx)
if OUTPUT_LSE:
tl.store(output_lse + head_idx * num_tokens + token_idx, s_lse)
s_out = tl.load(
suffix_output
+ token_idx * num_heads * prefix_head_stride
+ head_idx * prefix_head_stride
+ head_arange,
mask=head_mask,
)
tl.store(
output
+ token_idx * num_heads * output_head_stride
+ head_idx * output_head_stride
+ head_arange,
s_out,
mask=head_mask,
)
return
# For tokens with context (token_idx < prefill_tokens_with_context),
# perform normal merge operation
p_lse = tl.load(prefix_lse + head_idx * num_tokens + token_idx)
s_lse = tl.load(suffix_lse + head_idx * num_tokens + token_idx)
@@ -83,8 +122,6 @@ def merge_attn_states_kernel(
out_lse = tl.log(out_se) + max_lse
tl.store(output_lse + head_idx * num_tokens + token_idx, out_lse)
head_arange = tl.arange(0, PADDED_HEAD_SIZE)
head_mask = head_arange < HEAD_SIZE
p_out = tl.load(
prefix_output
+ token_idx * num_heads * prefix_head_stride