Repository navigation
[AMD] Reuse KV gather indices across ASM context prefill layers #39901
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
HaiShaw
merged 5 commits into
sgl-project:main
from
zijiecode:pr/qwen35-asm-prefill-gather
Sep 22, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
03940fc
Reuse ASM context-prefill gather indices across attention layers
zijiecode dd2824e
Merge branch 'main' into pr/qwen35-asm-prefill-gather
yichiche 3c6350c
Address ASM context prefill review comments
zijiecode 8a35b16
Clarify current Qwen3.5 ASM prefill scope
zijiecode 6294712
fix(amd): rename ASM context gather check environment variable
zijiecode File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
test/registered/amd/test_asm_context_prefill_gather_indices.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| """Per-batch gather indices for the ROCm ASM context-chunk prefill. | ||
|
|
||
| For a plain extend batch the prefill indices updater lays kv_indices out token by | ||
| token with kv_indptr = cumsum(seq_lens), so the ASM context prefill can take the | ||
| first sum(seq_lens) entries and kv_indptr itself without any device sync. Guards: | ||
| the fast path equals the generic (syncing) gather, is computed once per batch, | ||
| and batches that break the invariant fall back to the generic gather. | ||
| """ | ||
|
|
||
| import types | ||
| import unittest | ||
| from unittest.mock import patch | ||
|
|
||
| import torch | ||
|
|
||
| from sglang.test.ci.ci_register import register_amd_ci | ||
| from sglang.test.test_utils import CustomTestCase | ||
|
|
||
| register_amd_ci(est_time=30, suite="stage-b-test-1-gpu-small-amd") | ||
|
|
||
|
|
||
| @unittest.skipUnless(torch.version.hip, "ROCm attention backend") | ||
| class TestAsmContextPrefillGatherIndices(CustomTestCase): | ||
| @classmethod | ||
| def setUpClass(cls): | ||
| from sglang.srt.layers.attention import aiter_backend as ab | ||
|
|
||
| cls.ab = ab | ||
| cls.device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | ||
|
|
||
| def _batch(self, seq_lens, pad=256, spec_info=None, kv_indptr=None): | ||
| seq_lens_cpu = torch.tensor(seq_lens, dtype=torch.int32) | ||
| seq_lens_dev = seq_lens_cpu.to(self.device) | ||
| bs = len(seq_lens) | ||
| if kv_indptr is None: | ||
| kv_indptr = torch.zeros(bs + 1, dtype=torch.int32, device=self.device) | ||
| kv_indptr[1:] = torch.cumsum(seq_lens_dev, 0) | ||
| total = int(kv_indptr[bs].item()) | ||
| # pool slots in a shuffled order, padded the way the updater pads | ||
| pool = torch.randperm(total + pad, device=self.device)[:total].to(torch.int32) | ||
| kv_indices = torch.cat([pool, pool[:1].expand(pad)]) | ||
| fm = self.ab.ForwardMetadata( | ||
| kv_indptr=kv_indptr, | ||
| kv_indices=kv_indices, | ||
| qo_indptr=None, | ||
| kv_last_page_len=None, | ||
| max_q_len=1, | ||
| max_kv_len=max(seq_lens), | ||
| ) | ||
| backend = types.SimpleNamespace(forward_metadata=fm) | ||
| fb = types.SimpleNamespace( | ||
| seq_lens=seq_lens_dev, | ||
| seq_lens_cpu=seq_lens_cpu, | ||
| spec_info=spec_info, | ||
| forward_mode=types.SimpleNamespace(is_extend=lambda: True), | ||
| ) | ||
| return backend, fb, bs, total + pad | ||
|
|
||
| def test_fast_path_matches_generic_gather(self): | ||
| torch.manual_seed(0) | ||
| for seq_lens in ([5], [7, 3, 12], [1, 1, 1, 1], [1024, 17, 300]): | ||
| with self.subTest(seq_lens=seq_lens): | ||
| backend, fb, bs, num_slots = self._batch(seq_lens) | ||
| fm = backend.forward_metadata | ||
| tok_idx, cu_k = self.ab.AiterAttnBackend._asm_context_prefill_indices( | ||
| backend, fb, bs, num_slots | ||
| ) | ||
| ref_tok, ref_cu = self.ab._asm_context_prefill_gather_indices( | ||
| fm.kv_indptr[: bs + 1], fm.kv_indices, fb.seq_lens, num_slots | ||
| ) | ||
| self.assertTrue(torch.equal(tok_idx.to(torch.long), ref_tok)) | ||
| self.assertTrue(torch.equal(cu_k, ref_cu.to(torch.int32))) | ||
| self.assertEqual(cu_k.dtype, torch.int32) | ||
| self.assertEqual(tok_idx.numel(), sum(seq_lens)) | ||
| # cached: the second call for the same batch returns the same tensors | ||
| again = self.ab.AiterAttnBackend._asm_context_prefill_indices( | ||
| backend, fb, bs, num_slots | ||
| ) | ||
| self.assertIs(again[0], tok_idx) | ||
| self.assertIs(again[1], cu_k) | ||
|
|
||
| def test_spec_batch_uses_generic_gather(self): | ||
| backend, fb, bs, num_slots = self._batch([9, 4], spec_info=object()) | ||
| fm = backend.forward_metadata | ||
| tok_idx, cu_k = self.ab.AiterAttnBackend._asm_context_prefill_indices( | ||
| backend, fb, bs, num_slots | ||
| ) | ||
| ref_tok, ref_cu = self.ab._asm_context_prefill_gather_indices( | ||
| fm.kv_indptr[: bs + 1], fm.kv_indices, fb.seq_lens, num_slots | ||
| ) | ||
| self.assertTrue(torch.equal(tok_idx.to(torch.long), ref_tok)) | ||
| self.assertTrue(torch.equal(cu_k, ref_cu.to(torch.int32))) | ||
|
|
||
| def test_short_table_falls_back(self): | ||
| # Host lengths exceed the table; the generic helper clamps them. | ||
| backend, fb, bs, num_slots = self._batch([6, 6], pad=0) | ||
| fb.seq_lens_cpu = torch.tensor([6, 100], dtype=torch.int32) | ||
| fb.seq_lens = fb.seq_lens_cpu.to(self.device) | ||
| tok_idx, cu_k = self.ab.AiterAttnBackend._asm_context_prefill_indices( | ||
| backend, fb, bs, num_slots | ||
| ) | ||
| # the generic gather clamps the lengths to the table, so it still returns | ||
| self.assertIsNotNone(tok_idx) | ||
| self.assertEqual(tok_idx.numel(), 12) | ||
|
|
||
| def test_missing_host_lengths_uses_and_caches_generic_gather(self): | ||
| for speculative in (False, True): | ||
| with self.subTest(speculative=speculative): | ||
| backend, fb, bs, num_slots = self._batch( | ||
| [9, 4], spec_info=object() if speculative else None | ||
| ) | ||
| fm = backend.forward_metadata | ||
| fb.seq_lens_cpu = None | ||
| ref_tok, ref_cu = self.ab._asm_context_prefill_gather_indices( | ||
| fm.kv_indptr[: bs + 1], fm.kv_indices, fb.seq_lens, num_slots | ||
| ) | ||
| with patch.object( | ||
| self.ab, | ||
| "_asm_context_prefill_gather_indices", | ||
| wraps=self.ab._asm_context_prefill_gather_indices, | ||
| ) as generic: | ||
| tok_idx, cu_k = ( | ||
| self.ab.AiterAttnBackend._asm_context_prefill_indices( | ||
| backend, fb, bs, num_slots | ||
| ) | ||
| ) | ||
| again = self.ab.AiterAttnBackend._asm_context_prefill_indices( | ||
| backend, fb, bs, num_slots | ||
| ) | ||
| self.assertEqual(generic.call_count, 1) | ||
| self.assertTrue(torch.equal(tok_idx.to(torch.long), ref_tok)) | ||
| self.assertTrue(torch.equal(cu_k, ref_cu.to(torch.int32))) | ||
| self.assertIs(again[0], tok_idx) | ||
| self.assertIs(again[1], cu_k) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.