Repository navigation
feat(qsa):Extend QSA prefill to support 1M context - #81
Conversation
- Prefixed chunk prefill: PyHIP attention reads the layer's K/V pool in place through the packed slot table (kv_slots) instead of gathering the full-context K/V with index_select; pools PyHIP cannot read in place keep the gather path. - Graph/eager decode: the PyHIP indexer accepts compressed page tables up to the runtime's MAX_COMPRESSED_KEYS (262144 keys, a 1M-token context) instead of 65536, bounded by the 32-bit offsets of its FP32 logits. - Tests: in-place reads match the gather fallback, and graph decode with a 1M-context page table still runs PyHIP. Co-authored-by: Copilot <[email protected]> (cherry picked from commit 0a10105)
The 1M-context launch overrides RoPE to YaRN, which get_rope builds as YaRNScalingMRotaryEmbedding. PyHIPIndexer.create matched MRotaryEmbedding by exact type, so it returned None and the native indexer silently ran (TP2, ~1M-token prompt: TTFT 229 s vs 87 s with PyHIP). YaRN only rescales the cos/sin cache that the PyHIP prep reads; its lookup and MRoPE axis composition are MRotaryEmbedding's. The new test builds the RoPE through get_rope with the launch-style YaRN parameters and checks that PyHIP is selected and matches native bit for bit in prefill, graph decode and verify. Co-authored-by: Copilot <[email protected]> (cherry picked from commit 010b0e2)
…-TP ranks The PyHIP prefill indexer is replicated on every TP rank; at 1M context its last chunk costs 45.3 ms per layer. With SGLANG_OPT_PYHIP_QSA_SPLIT_INDEXER=1 (off by default), a chunk of at least 2048 rows averaging at least 32768 compressed keys per row is cut into equal contiguous shares: each attention-TP rank selects its share with prefill_indexer(row_range, out) into a shared [tp * share, 2051] frame, and an in-place all_gather_into_tensor completes it. The thresholds come from MI308X measurements (indexer ~1.06e-5 us per row and key, TP2 all-gather ~0.087 us per row) with about a 2x margin. Validated on TP2 with YaRN at --context-length 1000000: SGLANG_TEST_PYHIP_QSA=1 passes with the split on (including a 999648-token request), and TTFT changes by +0.1% / -1.3% / -5.0% / -11.5% at 131k / 262k / 524k / 1M inputs (1M: 86.1 s -> 76.2 s); the decode path is unchanged. Needs pyhip with prefill_indexer(row_range, out). Co-authored-by: Copilot <[email protected]> (cherry picked from commit b1abbb0)
Gather and score compressed keys in page-aligned chunks with a 256 MiB FP32 K budget. Preserve FP32 scoring, full logits, masking and capture-static shapes without materializing all per-query keys at once. Handle empty batches/capacity and truncated or padded output lengths.
Add the opt-in SGLANG_LOG_QSA_INDEXER_BACKEND diagnostic flag. Log the selected PyHIP, Torch or TileLang path after the indexer returns, with layer, forward mode and query row count. Deduplicate by layer instance, mode and backend. Keep logging disabled by default and avoid import hooks or GPU synchronization.
There was a problem hiding this comment.
🟡 Changes recommended
The pinned PyHIP revision does not support the newly passed kv_slots argument, causing the first attention call to fail on installations using the declared dependencies.
4 open issues
What changed in this PR
This PR extends SGLang’s PyHIP Qwen Sparse Attention integration toward 1M-token contexts on TP2 and TP4 deployments.
Changes:
- Adds YaRN support, optional prefill indexer splitting across attention-TP ranks, and direct KV-pool reads.
- Bounds the Torch decode fallback’s key workspace and adds indexer backend logging.
- Adds long-context launch/client scripts and tests for the expanded PyHIP paths.
| File | Description |
|---|---|
test/registered/kernels/test_pyhip_qsa.py |
Tests YaRN, split prefill, KV-slot reads, and larger graph page tables. |
scripts/test_qwen38_long_request.py |
Sends exact-length requests and reports token counts and latency. |
scripts/launch_qwen38_flash_next_fp8_mi308x_pure_tp_4_or_8_or_2.sh |
Enables indexer backend logging by default. |
scripts/launch_qwen38_flash_next_fp8_mi308x_long_server_1m.sh |
Adds a configurable 1M-context ROCm launcher. |
scripts/generate_qwen38_long_prompts.py |
Generates long token-ID prompt fixtures. |
python/sglang/srt/layers/attention/qwen_sparse_attn_backend.py |
Uses direct KV-pool reads when supported. |
python/sglang/srt/layers/attention/qsa/qsa_indexer.py |
Logs the selected indexer backend. |
python/sglang/srt/layers/attention/qsa/pyhip_backend.py |
Integrates extended PyHIP attention and indexer capabilities. |
python/sglang/srt/layers/attention/qsa/mqa.py |
Chunks gathered decode keys to limit workspace size. |
python/sglang/srt/environ.py |
Defines backend-logging and split-indexer controls. |
Review effort: Balanced
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Lower the FP32 key budget to force multiple page chunks and compare against a dense reference. Cover reordered pages, different context lengths, partial-page truncation, padded output and empty inputs. Add a 1M-capacity CUDA Graph workspace bound and replay checks with changed query, cache, page-table and length inputs.
There was a problem hiding this comment.
🟡 Changes recommended
Both ROCm extras still pin an incompatible PyHIP revision, so the new unconditional kv_slots argument causes TypeError on every PyHIP attention call in standard installations.
3 open issues
2 resolved since last review
Review effort: Balanced
Refactor TestQsaDecodeWorkspace to inherit from CustomTestCase. Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Compare real compressed lengths of 66049 and 250001 against an independent Torch reference, with reordered pages, masked tail keys, and changing lengths during CUDA graph replay.
Use the built-in serving benchmark with token-ID prompts, zero client warmups, and an explicit cache flush for long-context reproduction. Validated a cold 1000000-token input and 350-token output on TP2 with YaRN.
There was a problem hiding this comment.
🔵 Needs a closer look
Long-context TP4 correctness needs gfx942 maintainer validation because the split test mocks the in-place collective and the reported TP4 accuracy test covers only short contexts.
No open issues 🎉
3 resolved since last review
Review effort: Balanced
286cd46
into
qwen38-flash-next-amd-current-fix-install
* feat(qsa): read K/V in place and allow 1M-context decode in PyHIP QSA - Prefixed chunk prefill: PyHIP attention reads the layer's K/V pool in place through the packed slot table (kv_slots) instead of gathering the full-context K/V with index_select; pools PyHIP cannot read in place keep the gather path. - Graph/eager decode: the PyHIP indexer accepts compressed page tables up to the runtime's MAX_COMPRESSED_KEYS (262144 keys, a 1M-token context) instead of 65536, bounded by the 32-bit offsets of its FP32 logits. - Tests: in-place reads match the gather fallback, and graph decode with a 1M-context page table still runs PyHIP. Co-authored-by: Copilot <[email protected]> (cherry picked from commit 0a10105) * fix(qsa): accept YaRN MRoPE in the PyHIP QSA indexer The 1M-context launch overrides RoPE to YaRN, which get_rope builds as YaRNScalingMRotaryEmbedding. PyHIPIndexer.create matched MRotaryEmbedding by exact type, so it returned None and the native indexer silently ran (TP2, ~1M-token prompt: TTFT 229 s vs 87 s with PyHIP). YaRN only rescales the cos/sin cache that the PyHIP prep reads; its lookup and MRoPE axis composition are MRotaryEmbedding's. The new test builds the RoPE through get_rope with the launch-style YaRN parameters and checks that PyHIP is selected and matches native bit for bit in prefill, graph decode and verify. Co-authored-by: Copilot <[email protected]> (cherry picked from commit 010b0e2) * feat(qsa): split long PyHIP QSA prefill indexer rows across attention-TP ranks The PyHIP prefill indexer is replicated on every TP rank; at 1M context its last chunk costs 45.3 ms per layer. With SGLANG_OPT_PYHIP_QSA_SPLIT_INDEXER=1 (off by default), a chunk of at least 2048 rows averaging at least 32768 compressed keys per row is cut into equal contiguous shares: each attention-TP rank selects its share with prefill_indexer(row_range, out) into a shared [tp * share, 2051] frame, and an in-place all_gather_into_tensor completes it. The thresholds come from MI308X measurements (indexer ~1.06e-5 us per row and key, TP2 all-gather ~0.087 us per row) with about a 2x margin. Validated on TP2 with YaRN at --context-length 1000000: SGLANG_TEST_PYHIP_QSA=1 passes with the split on (including a 999648-token request), and TTFT changes by +0.1% / -1.3% / -5.0% / -11.5% at 131k / 262k / 524k / 1M inputs (1M: 86.1 s -> 76.2 s); the decode path is unchanged. Needs pyhip with prefill_indexer(row_range, out). Co-authored-by: Copilot <[email protected]> (cherry picked from commit b1abbb0) * add 1m script * fix(qsa): bound Torch decode fallback key workspace Gather and score compressed keys in page-aligned chunks with a 256 MiB FP32 K budget. Preserve FP32 scoring, full logits, masking and capture-static shapes without materializing all per-query keys at once. Handle empty batches/capacity and truncated or padded output lengths. * feat(qsa): log actual indexer backend dispatch Add the opt-in SGLANG_LOG_QSA_INDEXER_BACKEND diagnostic flag. Log the selected PyHIP, Torch or TileLang path after the indexer returns, with layer, forward mode and query row count. Deduplicate by layer instance, mode and backend. Keep logging disabled by default and avoid import hooks or GPU synchronization. * update scripts * update test * Update default model path in argument parser Co-authored-by: Copilot Autofix powered by AI <[email protected]> * test(qsa): cover chunked decode reference workspace Lower the FP32 key budget to force multiple page chunks and compare against a dense reference. Cover reordered pages, different context lengths, partial-page truncation, padded output and empty inputs. Add a 1M-capacity CUDA Graph workspace bound and replay checks with changed query, cache, page-table and length inputs. * Change TestQsaDecodeWorkspace base class Refactor TestQsaDecodeWorkspace to inherit from CustomTestCase. Co-authored-by: Copilot Autofix powered by AI <[email protected]> * revert(qsa): drop chunked Torch decode fallback and workspace tests Revert the chunked reference implementation and its dedicated workspace regressions. Keep the PyHIP 1M/YaRN/TP-split integration and actual backend dispatch logging unchanged. Reverts 14c5729, 7edc822, and e255acb. * update script * test(qsa): validate graph selection beyond 65536 compressed keys Compare real compressed lengths of 66049 and 250001 against an independent Torch reference, with reordered pages, masked tail keys, and changing lengths during CUDA graph replay. * chore(scripts): remove redundant long-context request helpers Use the built-in serving benchmark with token-ID prompts, zero client warmups, and an explicit cache flush for long-context reproduction. Validated a cold 1000000-token input and 350-token output on TP2 with YaRN. * build(rocm): pin PyHIP to merged QSA API revision --------- Co-authored-by: Luo Cheng <[email protected]> Co-authored-by: Copilot <[email protected]> Co-authored-by: Copilot Autofix powered by AI <[email protected]> (cherry picked from commit 286cd46)
…#90) * feat(qsa): Support pyhip qsa indexer for prefill/decode, sparse gqa for prefill (#43) * add pyhip qsa support * enable pyhip for mtp verify * fix error overwriting for ring buffer * align new indexer interface * remove potential sync in qsa * cache packed QSA KV slots per forward * update scripts * update mtp method * update pyhip version * clean qsa attn conditions * rename condition function * rename indexer support * support qsa indexer in decode eager mode * rename ring to ring_slots * fix ring slots * Apply suggestion from @yicyang Co-authored-by: Ethan Yang <[email protected]> --------- Co-authored-by: Luo Cheng <[email protected]> Co-authored-by: Ethan Yang <[email protected]> (cherry picked from commit d14f81d) * feat(qsa):Extend QSA prefill to support 1M context (#81) * feat(qsa): read K/V in place and allow 1M-context decode in PyHIP QSA - Prefixed chunk prefill: PyHIP attention reads the layer's K/V pool in place through the packed slot table (kv_slots) instead of gathering the full-context K/V with index_select; pools PyHIP cannot read in place keep the gather path. - Graph/eager decode: the PyHIP indexer accepts compressed page tables up to the runtime's MAX_COMPRESSED_KEYS (262144 keys, a 1M-token context) instead of 65536, bounded by the 32-bit offsets of its FP32 logits. - Tests: in-place reads match the gather fallback, and graph decode with a 1M-context page table still runs PyHIP. Co-authored-by: Copilot <[email protected]> (cherry picked from commit 0a10105) * fix(qsa): accept YaRN MRoPE in the PyHIP QSA indexer The 1M-context launch overrides RoPE to YaRN, which get_rope builds as YaRNScalingMRotaryEmbedding. PyHIPIndexer.create matched MRotaryEmbedding by exact type, so it returned None and the native indexer silently ran (TP2, ~1M-token prompt: TTFT 229 s vs 87 s with PyHIP). YaRN only rescales the cos/sin cache that the PyHIP prep reads; its lookup and MRoPE axis composition are MRotaryEmbedding's. The new test builds the RoPE through get_rope with the launch-style YaRN parameters and checks that PyHIP is selected and matches native bit for bit in prefill, graph decode and verify. Co-authored-by: Copilot <[email protected]> (cherry picked from commit 010b0e2) * feat(qsa): split long PyHIP QSA prefill indexer rows across attention-TP ranks The PyHIP prefill indexer is replicated on every TP rank; at 1M context its last chunk costs 45.3 ms per layer. With SGLANG_OPT_PYHIP_QSA_SPLIT_INDEXER=1 (off by default), a chunk of at least 2048 rows averaging at least 32768 compressed keys per row is cut into equal contiguous shares: each attention-TP rank selects its share with prefill_indexer(row_range, out) into a shared [tp * share, 2051] frame, and an in-place all_gather_into_tensor completes it. The thresholds come from MI308X measurements (indexer ~1.06e-5 us per row and key, TP2 all-gather ~0.087 us per row) with about a 2x margin. Validated on TP2 with YaRN at --context-length 1000000: SGLANG_TEST_PYHIP_QSA=1 passes with the split on (including a 999648-token request), and TTFT changes by +0.1% / -1.3% / -5.0% / -11.5% at 131k / 262k / 524k / 1M inputs (1M: 86.1 s -> 76.2 s); the decode path is unchanged. Needs pyhip with prefill_indexer(row_range, out). Co-authored-by: Copilot <[email protected]> (cherry picked from commit b1abbb0) * add 1m script * fix(qsa): bound Torch decode fallback key workspace Gather and score compressed keys in page-aligned chunks with a 256 MiB FP32 K budget. Preserve FP32 scoring, full logits, masking and capture-static shapes without materializing all per-query keys at once. Handle empty batches/capacity and truncated or padded output lengths. * feat(qsa): log actual indexer backend dispatch Add the opt-in SGLANG_LOG_QSA_INDEXER_BACKEND diagnostic flag. Log the selected PyHIP, Torch or TileLang path after the indexer returns, with layer, forward mode and query row count. Deduplicate by layer instance, mode and backend. Keep logging disabled by default and avoid import hooks or GPU synchronization. * update scripts * update test * Update default model path in argument parser Co-authored-by: Copilot Autofix powered by AI <[email protected]> * test(qsa): cover chunked decode reference workspace Lower the FP32 key budget to force multiple page chunks and compare against a dense reference. Cover reordered pages, different context lengths, partial-page truncation, padded output and empty inputs. Add a 1M-capacity CUDA Graph workspace bound and replay checks with changed query, cache, page-table and length inputs. * Change TestQsaDecodeWorkspace base class Refactor TestQsaDecodeWorkspace to inherit from CustomTestCase. Co-authored-by: Copilot Autofix powered by AI <[email protected]> * revert(qsa): drop chunked Torch decode fallback and workspace tests Revert the chunked reference implementation and its dedicated workspace regressions. Keep the PyHIP 1M/YaRN/TP-split integration and actual backend dispatch logging unchanged. Reverts 14c5729, 7edc822, and e255acb. * update script * test(qsa): validate graph selection beyond 65536 compressed keys Compare real compressed lengths of 66049 and 250001 against an independent Torch reference, with reordered pages, masked tail keys, and changing lengths during CUDA graph replay. * chore(scripts): remove redundant long-context request helpers Use the built-in serving benchmark with token-ID prompts, zero client warmups, and an explicit cache flush for long-context reproduction. Validated a cold 1000000-token input and 350-token output on TP2 with YaRN. * build(rocm): pin PyHIP to merged QSA API revision --------- Co-authored-by: Luo Cheng <[email protected]> Co-authored-by: Copilot <[email protected]> Co-authored-by: Copilot Autofix powered by AI <[email protected]> (cherry picked from commit 286cd46) * fix(qsa): adapt backports to qwen4 v0.5.21 interfaces * chore(scripts): move Qwen3.8 1M launcher to evaluation * docs: add Qwen3.8 TP1 and 1M launch examples --------- Co-authored-by: Luo Cheng <[email protected]> Co-authored-by: Ethan Yang <[email protected]> Co-authored-by: Copilot <[email protected]> Co-authored-by: Copilot Autofix powered by AI <[email protected]>


Motivation
This PR is based on tingqli/pyhip#62 and it extends the QSA prefill (attention + indexer) to 1M-token contexts (MAX_COMPRESSED_KEYS 65536 → 262144 compressed keys) for TP2 TP4.
Co-authored-by: Cheng Luo [email protected]
Modifications
kv_slots, avoiding the creation of contiguouspacked_k/packed_vcopies; The indexer supports both 1M and YaRN, and it partitions prefill scoring across TP ranks usingrow_range/outbefore performing anall-gatherto get the final selection results.SGLANG_OPT_PYHIP_QSA_SPLIT_INDEXERwithSGLANG_USE_PYHIP_QSA=1SGLANG_LOG_QSA_INDEXER_BACKENDfor logAccuracy Tests
1M context
This is just a smoke test and it passed (both yarn and default rope type).
server
client
Test shorter example
gsm8k tp2
gsm8k tp4
Speed Tests and Profiling
evalscope test
server
client example
Comparison between pyhip and the torch ref on the same batch of random inputs: all 64 input hashes were identical, the per-request cache counts also matched, and the average cache hit rate was 89.9994% for both runs.
TP2, concurrency 1 with mtp; only the indexer path was changed, while all other inference settings were kept the same.
PyHIP reduced TTFT by 56.48%, reduced TPOT by 26.91%, and improved QPS by 58.77%.
Checklist
Review and Merge Process
/tag-and-rerun-ci,/tag-run-ci-label,/rerun-failed-ci