[ROCm][DSv4.1][Perf] Replace the Torch prefill combine with the shared combine_topk_swa_indices kernel (#56638)
Signed-off-by: JohnQinAMD <yanyuan.qin@amd.com> Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com> Co-authored-by: Ren Yuzhou <54501155+yuzhouo7@users.noreply.github.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Co-authored-by: Douglas Lehr <91553416+dllehr-amd@users.noreply.github.com>
This commit is contained in:
co-authored by
Claude Opus 5
Ren Yuzhou
mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Douglas Lehr
parent
95307ac6d4
commit
fb2ac82458
@@ -26,7 +26,10 @@ from vllm.models.deepseek_v41.attention import (
|
||||
DeepseekV4IndexerCache,
|
||||
_replace_layer_index,
|
||||
)
|
||||
from vllm.models.deepseek_v41.common.ops import dequantize_and_gather_k_cache
|
||||
from vllm.models.deepseek_v41.common.ops import (
|
||||
combine_topk_swa_indices,
|
||||
dequantize_and_gather_k_cache,
|
||||
)
|
||||
from vllm.models.deepseek_v41.sparse_mla import (
|
||||
DeepseekV4FlashMLAMetadata,
|
||||
DeepseekV4SparseMLABackend,
|
||||
@@ -129,165 +132,6 @@ def apply_pre_quantized_block_scaled_mm(
|
||||
return out.to(dtype=kernel.config.out_dtype)
|
||||
|
||||
|
||||
# ROCm sparse prefill keeps this dense combine local so AMD-specific SWA changes
|
||||
# do not touch the shared DeepSeek V4 cache utilities.
|
||||
_SPARSE_PREFILL_TOPK_ALIGNMENT = 128
|
||||
|
||||
|
||||
@triton.jit
|
||||
def _combine_topk_swa_indices_kernel(
|
||||
combined_indices_ptr,
|
||||
combined_indices_stride,
|
||||
combined_lens_ptr,
|
||||
topk_indices_ptr,
|
||||
topk_indices_stride,
|
||||
query_start_loc_ptr,
|
||||
seq_lens_ptr,
|
||||
gather_lens_ptr,
|
||||
M,
|
||||
N,
|
||||
TOP_K: tl.constexpr,
|
||||
COMPRESS_RATIO: tl.constexpr,
|
||||
WINDOW_SIZE: tl.constexpr,
|
||||
TOPK_WIDTH: tl.constexpr,
|
||||
PADDED_TOP_K: tl.constexpr,
|
||||
):
|
||||
batch_idx = tl.program_id(0)
|
||||
worker_id = tl.program_id(1)
|
||||
num_workers = tl.num_programs(1)
|
||||
|
||||
base = tl.load(query_start_loc_ptr)
|
||||
query_start = tl.load(query_start_loc_ptr + batch_idx) - base
|
||||
query_end = tl.load(query_start_loc_ptr + batch_idx + 1) - base
|
||||
query_len = query_end - query_start
|
||||
seq_len = tl.load(seq_lens_ptr + batch_idx)
|
||||
gather_len = tl.load(gather_lens_ptr + batch_idx)
|
||||
start_pos = seq_len - query_len
|
||||
gather_start = seq_len - gather_len
|
||||
|
||||
for token_idx in range(query_start + worker_id, query_end, num_workers):
|
||||
token_idx_in_query = token_idx - query_start
|
||||
pos = start_pos + token_idx_in_query
|
||||
topk_len = tl.minimum((pos + 1) // COMPRESS_RATIO, TOP_K)
|
||||
swa_len = tl.minimum(pos + 1, WINDOW_SIZE)
|
||||
|
||||
topk_offset = tl.arange(0, PADDED_TOP_K)
|
||||
topk_mask = topk_offset < topk_len
|
||||
safe_topk_offset = tl.where(topk_offset < TOPK_WIDTH, topk_offset, 0)
|
||||
topk_indices = tl.load(
|
||||
topk_indices_ptr + token_idx * topk_indices_stride + safe_topk_offset,
|
||||
mask=topk_mask,
|
||||
other=-1,
|
||||
)
|
||||
valid_topk = (topk_indices >= 0) & (topk_indices < N)
|
||||
topk_indices = tl.where(valid_topk, topk_indices + M * batch_idx, -1)
|
||||
tl.store(
|
||||
combined_indices_ptr + token_idx * combined_indices_stride + topk_offset,
|
||||
topk_indices,
|
||||
mask=topk_mask,
|
||||
)
|
||||
|
||||
swa_offset = tl.arange(0, WINDOW_SIZE)
|
||||
tl.store(
|
||||
combined_indices_ptr
|
||||
+ token_idx * combined_indices_stride
|
||||
+ topk_len
|
||||
+ swa_offset,
|
||||
M * batch_idx + N + swa_offset + pos - swa_len + 1 - gather_start,
|
||||
mask=swa_offset < swa_len,
|
||||
)
|
||||
|
||||
tl.store(combined_lens_ptr + token_idx, topk_len + swa_len)
|
||||
|
||||
|
||||
def combine_topk_swa_indices(
|
||||
topk_indices: torch.Tensor,
|
||||
query_start_loc: torch.Tensor,
|
||||
seq_lens: torch.Tensor,
|
||||
gather_lens: torch.Tensor,
|
||||
window_size: int,
|
||||
compress_ratio: int,
|
||||
topk: int,
|
||||
M: int,
|
||||
N: int,
|
||||
) -> tuple[torch.Tensor, torch.Tensor]:
|
||||
"""Combine compressed-attention and sliding-window indices with Torch.
|
||||
|
||||
The Triton implementation inherited from DeepSeek V4 launches a
|
||||
two-dimensional grid with 128 workers per request. On gfx950 it can issue
|
||||
an out-of-bounds access for V4.1's mixed prefill metadata (including the
|
||||
synthetic mixed-token warmup). This path is prefill-only and the tensors
|
||||
are small, so use ordinary Torch indexing until a gfx950-safe fused kernel
|
||||
is available.
|
||||
"""
|
||||
topk_indices = topk_indices.reshape(topk_indices.shape[0], -1).contiguous()
|
||||
num_tokens = topk_indices.shape[0]
|
||||
combined_topk = (
|
||||
(topk + window_size + _SPARSE_PREFILL_TOPK_ALIGNMENT - 1)
|
||||
// _SPARSE_PREFILL_TOPK_ALIGNMENT
|
||||
* _SPARSE_PREFILL_TOPK_ALIGNMENT
|
||||
)
|
||||
combined_indices = torch.full(
|
||||
(num_tokens, combined_topk),
|
||||
fill_value=-1,
|
||||
dtype=torch.int32,
|
||||
device=topk_indices.device,
|
||||
)
|
||||
combined_lens = torch.empty(
|
||||
num_tokens, dtype=torch.int32, device=topk_indices.device
|
||||
)
|
||||
|
||||
# query_start_loc may have a non-zero base for a narrowed mixed batch.
|
||||
query_lens = query_start_loc[1:] - query_start_loc[:-1]
|
||||
req_ids = torch.repeat_interleave(
|
||||
torch.arange(seq_lens.shape[0], device=seq_lens.device), query_lens
|
||||
)
|
||||
query_starts = query_start_loc[:-1] - query_start_loc[0]
|
||||
token_offsets = torch.arange(num_tokens, device=seq_lens.device) - (
|
||||
torch.repeat_interleave(query_starts, query_lens)
|
||||
)
|
||||
positions = seq_lens[req_ids] - query_lens[req_ids] + token_offsets
|
||||
|
||||
logical_topk_width = min(topk, topk_indices.shape[1])
|
||||
topk_lens = torch.minimum(
|
||||
(positions + 1) // compress_ratio,
|
||||
torch.full_like(positions, logical_topk_width),
|
||||
).clamp_min(0)
|
||||
topk_offsets = torch.arange(logical_topk_width, device=seq_lens.device)
|
||||
topk_mask = topk_offsets[None, :] < topk_lens[:, None]
|
||||
topk_values = topk_indices[:, :logical_topk_width].to(torch.int32)
|
||||
topk_valid = topk_mask & (topk_values >= 0) & (topk_values < N)
|
||||
combined_indices[:, :logical_topk_width] = torch.where(
|
||||
topk_valid,
|
||||
topk_values + (M * req_ids).to(torch.int32)[:, None],
|
||||
-1,
|
||||
)
|
||||
|
||||
swa_lens = torch.minimum(
|
||||
positions + 1, torch.full_like(positions, window_size)
|
||||
).clamp_min(0)
|
||||
swa_offsets = torch.arange(window_size, device=seq_lens.device)
|
||||
swa_mask = swa_offsets[None, :] < swa_lens[:, None]
|
||||
swa_columns = topk_lens[:, None] + swa_offsets[None, :]
|
||||
gather_starts = seq_lens - gather_lens
|
||||
swa_values = (
|
||||
M * req_ids[:, None]
|
||||
+ N
|
||||
+ swa_offsets[None, :]
|
||||
+ positions[:, None]
|
||||
- swa_lens[:, None]
|
||||
+ 1
|
||||
- gather_starts[req_ids, None]
|
||||
).to(torch.int32)
|
||||
rows = torch.arange(num_tokens, device=seq_lens.device)[:, None].expand_as(
|
||||
swa_columns
|
||||
)
|
||||
flat_dst = rows[swa_mask] * combined_topk + swa_columns[swa_mask]
|
||||
combined_indices.view(-1).index_copy_(0, flat_dst, swa_values[swa_mask])
|
||||
combined_lens.copy_((topk_lens + swa_lens).to(torch.int32))
|
||||
return combined_indices, combined_lens
|
||||
|
||||
|
||||
@triton.jit
|
||||
def _compute_topk_lens_kernel(
|
||||
topk_lens_ptr,
|
||||
|
||||
@@ -1089,7 +1089,8 @@ class CombineTopkSwaIndicesKernel(
|
||||
token_idx_in_query = token_idx - query_start
|
||||
pos = start_pos + token_idx_in_query
|
||||
if COMPRESS_RATIO > 0:
|
||||
topk_len = tl.minimum((pos + 1) // COMPRESS_RATIO, TOP_K)
|
||||
# Warmup batches can put pos below zero; keep the length >= 0.
|
||||
topk_len = tl.maximum(tl.minimum((pos + 1) // COMPRESS_RATIO, TOP_K), 0)
|
||||
else:
|
||||
# SWA-only layers pass TOP_K=0; skip the division entirely
|
||||
# (integer div by constexpr 0 is UB and yields garbage, which
|
||||
@@ -1100,7 +1101,7 @@ class CombineTopkSwaIndicesKernel(
|
||||
# start (SWA bounded replay), so the window cannot start before
|
||||
# the gathered buffer does.
|
||||
swa_start = tl.maximum(swa_start, gather_start)
|
||||
swa_len = pos - swa_start + 1
|
||||
swa_len = tl.maximum(pos - swa_start + 1, 0)
|
||||
|
||||
offset = tl.arange(0, PADDED_TOP_K)
|
||||
mask = offset < topk_len
|
||||
@@ -1108,9 +1109,12 @@ class CombineTopkSwaIndicesKernel(
|
||||
topk_indices_ptr + token_idx * topk_indices_stride + offset,
|
||||
mask=mask,
|
||||
)
|
||||
# A failed (-1) or out-of-pool candidate stays -1 instead of landing
|
||||
# in the previous request's rows or this request's SWA rows.
|
||||
valid = (topk_indices >= 0) & (topk_indices < N)
|
||||
tl.store(
|
||||
combined_indices_ptr + token_idx * combined_indices_stride + offset,
|
||||
topk_indices + M * batch_idx,
|
||||
tl.where(valid, topk_indices + M * batch_idx, -1),
|
||||
mask=mask,
|
||||
)
|
||||
# Index into gathered buffer: N + (position - gather_start)
|
||||
|
||||
Reference in New Issue
Block a user