[Perf][Attention] Reduce GLM sparse MLA preparation overhead (#57458)

Signed-off-by: Summer Yang <girasoleyang@gmail.com>
Co-authored-by: Codex <noreply@openai.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Co-authored-by: Jiangyun Zhu <riverclouds.zhu@qq.com>
This commit is contained in:
Summer Yang
2026-09-21 17:24:25 -07:00
committed by GitHub
co-authored by Codex mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Jiangyun Zhu
parent c64b15cde5
commit a369a7becc
7 changed files with 246 additions and 92 deletions
@@ -700,9 +700,8 @@ def test_dcp_filter_compacts_valid_slots_for_sparse_kernel(
@pytest.mark.skipif(not current_platform.is_cuda(), reason="This test requires CUDA")
@pytest.mark.parametrize("interleave", [1, 2])
@pytest.mark.parametrize("dcp_rank", [0, 1])
# 384 is not a power of two, so it exercises the multi-tile atomic allocator
# rather than the single-tile path 1024 takes.
@pytest.mark.parametrize("num_topk", [1024, 384])
# Include padded single-tile compaction and the multi-tile atomic fallback.
@pytest.mark.parametrize("num_topk", [1024, 2176, 4224])
def test_dcp_filter_compaction_matches_reference(
interleave: int, dcp_rank: int, num_topk: int
):
@@ -729,7 +728,7 @@ def test_dcp_filter_compaction_matches_reference(
for r in range(num_rows):
# Ids are distinct, so a row holds at most `seq` valid entries.
hi = min(num_topk * 3 // 4, seq)
n_valid = int(torch.randint(num_topk // 4, hi, (1,)).item())
n_valid = int(torch.randint(hi // 3, hi, (1,)).item())
perm = torch.randperm(seq, device=device)[:n_valid].to(torch.int32)
token_indices[r, :n_valid] = perm
+18 -7
View File
@@ -1663,10 +1663,12 @@ def test_split_indexer_prefill_chunks_single_request_overflow():
assert out == expected
# 384 is not a power of two, so it counts via the tiled atomic accumulation
# rather than the single-tile path 128 takes.
@pytest.mark.parametrize("num_topk_tokens", [128, 384])
def test_triton_convert_returns_valid_counts(num_topk_tokens: int):
# Power-of-two, GLM's padded tile, and atomic fallback, with reused buffers.
@pytest.mark.parametrize(
"num_topk_tokens,reuse_buffers",
[(128, False), (2176, False), (2176, True), (4224, False), (4224, True)],
)
def test_triton_convert_returns_valid_counts(num_topk_tokens: int, reuse_buffers: bool):
"""Test that return_valid_counts correctly counts non-negative indices."""
device = torch.device(DEVICE_TYPE)
num_tokens = 8
@@ -1679,8 +1681,7 @@ def test_triton_convert_returns_valid_counts(num_topk_tokens: int):
num_requests * max_blocks_per_req, dtype=torch.int32, device=device
).view(num_requests, max_blocks_per_req)
# Create token indices with varying numbers of valid entries: half the row,
# a quarter of it, the whole row, then a single valid entry -- twice over.
# Include partly valid, fully valid, and empty selections for each request.
token_indices = torch.full(
(num_tokens, num_topk_tokens), -1, dtype=torch.int32, device=device
)
@@ -1688,7 +1689,7 @@ def test_triton_convert_returns_valid_counts(num_topk_tokens: int):
num_topk_tokens // 2,
num_topk_tokens // 4,
num_topk_tokens,
1,
0,
] * 2
expected_valid = []
for i in range(num_tokens):
@@ -1702,6 +1703,15 @@ def test_triton_convert_returns_valid_counts(num_topk_tokens: int):
expected_valid, dtype=torch.int32, device=device
)
buffers = {}
if reuse_buffers:
buffers = dict(
out=torch.full_like(token_indices, -99),
valid_counts_out=torch.full(
(num_tokens,), 99, dtype=torch.int32, device=device
),
)
# Test with return_valid_counts=True
result, valid_counts = triton_convert_req_index_to_global_index(
req_id,
@@ -1710,6 +1720,7 @@ def test_triton_convert_returns_valid_counts(num_topk_tokens: int):
BLOCK_SIZE=block_size,
NUM_TOPK_TOKENS=num_topk_tokens,
return_valid_counts=True,
**buffers,
)
torch.testing.assert_close(valid_counts, expected_valid_tensor, rtol=0, atol=0)