Remove CP indexer syncs around MQA logits

The CP in-seq NSA indexer path was constructing a CUDA validity mask around each fp8_mqa_logits call, synchronizing it back with .item(), and then rebuilding small CUDA tensors before topk. The valid rows are always a contiguous prefix because ke_offset is monotonic, so the count can be derived from existing CP metadata and the path can use slices instead of boolean gather/scatter.

RAGGED topk now reuses the zero row-start vector as the per-row offset override for the single-segment case to avoid cumsum/repeat_interleave metadata kernels after MQA.

Constraint: CP shared KV prefill still needs to preserve prev/next causal visibility and topk transform semantics.
Rejected: Add a new Triton range-builder first | the Python-side synchronization and boolean indexing were the larger immediate risk, and kernel work should be profiled after this smaller change.
Confidence: medium
Scope-risk: moderate
Directive: Do not reintroduce GPU mask .item() or CPU-created CUDA tensors in the fp8_mqa_logits hot path without profiler evidence.
Tested: python3 -m py_compile python/sglang/srt/layers/attention/nsa/nsa_indexer.py test/registered/unit/layers/test_nsa_cp_utils.py
Tested: g0034 docker /sgl-workspace/sglang-tai python3 test/registered/unit/layers/test_nsa_cp_utils.py -> Ran 22 tests OK
Not-tested: full GLM5 PD server throughput/profiler run
This commit is contained in:
laoyao0822
2026-05-03 05:11:58 +08:00
parent 9fec89ba09
commit 9eb9d82b51
2 changed files with 118 additions and 21 deletions
@@ -19,6 +19,44 @@ register_cpu_ci(est_time=1, suite="stage-a-test-cpu")
class TestNSAInSeqCPUtils(unittest.TestCase):
def test_contiguous_valid_cp_query_count(self):
from sglang.srt.layers.attention.nsa.nsa_indexer import (
_compute_contiguous_valid_cp_query_count,
)
self.assertEqual(
_compute_contiguous_valid_cp_query_count(
cp_kv_end=1024,
actual_seq_q=128,
logical_kv_limit=1024,
),
128,
)
self.assertEqual(
_compute_contiguous_valid_cp_query_count(
cp_kv_end=1100,
actual_seq_q=128,
logical_kv_limit=1024,
),
52,
)
self.assertEqual(
_compute_contiguous_valid_cp_query_count(
cp_kv_end=1100,
actual_seq_q=64,
logical_kv_limit=1000,
),
0,
)
self.assertEqual(
_compute_contiguous_valid_cp_query_count(
cp_kv_end=100,
actual_seq_q=0,
logical_kv_limit=100,
),
0,
)
def assert_page_aligned_boundaries(
self, split_list, *, extend_prefix_len, extend_len, page_size
):
@@ -415,11 +453,13 @@ class TestNSAInSeqCPUtils(unittest.TestCase):
current_index_kv=None,
shared_index_buffer=None,
shared_block_tables=None,
actual_seq_q_tensor=None,
):
topk_calls.append(
{
"kv_len": kv_len,
"actual_seq_q": actual_seq_q,
"actual_seq_q_tensor": actual_seq_q_tensor,
"shared_index_buffer": shared_index_buffer,
"shared_block_tables": shared_block_tables,
"current_index_kv": current_index_kv,
@@ -497,10 +537,12 @@ class TestNSAInSeqCPUtils(unittest.TestCase):
current_index_kv=None,
shared_index_buffer=None,
shared_block_tables=None,
actual_seq_q_tensor=None,
):
topk_calls.append(
{
"current_index_kv": current_index_kv,
"actual_seq_q_tensor": actual_seq_q_tensor,
"shared_index_buffer": shared_index_buffer,
"shared_block_tables": shared_block_tables,
}