[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
@@ -302,8 +302,13 @@ def combine_case(
with_image: bool,
replay_starts: list[int] | None = None,
combine_fn=combine_topk_swa_indices,
in_range_topk: bool = False,
):
"""Run combine_topk_swa_indices and return (indices, lens, expected)."""
"""Run combine_topk_swa_indices and return (indices, lens, expected).
``in_range_topk`` draws top-k indices below N, for combiners that drop
out-of-range ones.
"""
device = torch.device("cuda")
num_reqs = len(seq_lens)
replay_starts = replay_starts or [0] * num_reqs
@@ -326,7 +331,11 @@ def combine_case(
M = N + int(gather_lens.max()) + 8
gen = torch.Generator(device="cpu").manual_seed(0)
topk_indices = torch.randint(
0, 4096, (num_tokens, max(topk, 1)), generator=gen, dtype=torch.int32
0,
N if in_range_topk else 4096,
(num_tokens, max(topk, 1)),
generator=gen,
dtype=torch.int32,
).to(device)
topk_indices = topk_indices[:, : max(topk, 1)]
@@ -415,24 +424,26 @@ def test_combine_topk_swa_with_image_spans(cfg):
@pytest.mark.skipif(not torch.cuda.is_available(), reason="requires CUDA")
@pytest.mark.parametrize("cfg", COMBINE_CASES)
def test_v41_combine_topk_swa_stops_at_replay_start(cfg):
@pytest.mark.parametrize("query_len", [24, 20])
def test_v41_combine_topk_swa_stops_at_replay_start(cfg, query_len):
"""SWA bounded replay: the gathered buffer starts at replay_start, so the
window never indexes below it."""
from vllm.models.deepseek_v41.common.ops.cache_utils import (
combine_topk_swa_indices as combine_v41,
)
# Request 0 replays [16, 40): the windows of its first rows would
# otherwise reach below 16.
# Request 0 replays from 16: a 24-token chunk starts there, a 20-token
# chunk leaves only 4 context tokens above it (fewer than the window).
indices, lens, rows, exp_lens = combine_case(
cfg["compress_ratio"],
cfg["topk"],
seq_lens=[40, 12],
query_lens=[24, 12],
query_lens=[query_len, 12],
spans=[[], []],
with_image=False,
replay_starts=[16, 0],
combine_fn=combine_v41,
in_range_topk=True,
)
assert lens.cpu().tolist() == exp_lens
assert indices.cpu().tolist() == rows