[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:
JohnQinAMD
2026-10-08 14:15:18 +08:00
committed by GitHub
co-authored by Claude Opus 5 Ren Yuzhou mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Douglas Lehr
parent 95307ac6d4
commit fb2ac82458
4 changed files with 185 additions and 170 deletions
@@ -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)