Repository navigation
[Perf] [AMD] Qwen3.8: Serve QSA decode with AITER's fused FlyDSL sparse decode on gfx950 #45196
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
Open
amd-nprotaso
wants to merge
8
commits into
sgl-project:main
Choose a base branch
from
amd-nprotaso:pr/flydsl_qsa_kernels
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+400
−1
Open
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
71e6ff5
[AMD] Serve QSA decode with AITER's fused FlyDSL sparse decode on gfx950
amd-nprotaso 0fe0903
Merge branch 'main' into pr/flydsl_qsa_kernels
1am9trash e033c49
remove SGLANG_ENABLE_QSA_FLYDSL_DECODE flag
amd-nprotaso 52ebdf5
Merge branch 'main' into pr/flydsl_qsa_kernels
amd-nprotaso b7fa53d
Merge branch 'main' into pr/flydsl_qsa_kernels
amd-nprotaso d0d0e41
Merge branch 'main' into pr/flydsl_qsa_kernels
amd-nprotaso 4ff5871
add qsa kernels in aiter in tests check
amd-nprotaso a66926b
Merge branch 'main' into pr/flydsl_qsa_kernels
amd-nprotaso 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
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
146 changes: 146 additions & 0 deletions
146
test/registered/kernels/benchmark/attention/bench_qsa_sparse_decode.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,146 @@ | ||
| """Benchmark: QSA sparse decode attention on gfx950. | ||
|
|
||
| ``fused_flydsl`` (``aiter.ops.flydsl.qsa_sparse_decode``) reads the selected KV | ||
| straight from the pool and splits each row across CTAs. ``gather_triton`` is the ROCm path it replaces in | ||
| ``QwenSparseAttnBackend._forward_paged_attention``: a Triton gather into packed | ||
| scratch, then ``sparse_gqa_packed_decode_triton`` with one program per row and | ||
| KV head. | ||
|
|
||
| Cold cache: each captured call reads a different random slice of a pool that is | ||
| several times larger than MI355X's 256 MB last-level cache. | ||
| """ | ||
|
|
||
| import itertools | ||
|
|
||
| import torch | ||
|
|
||
| from sglang.kernels.jit.benchmark import marker | ||
| from sglang.test.ci.ci_register import register_amd_ci | ||
|
|
||
| register_amd_ci(est_time=120, stage="jit-kernel-benchmark", runner_config="amd") | ||
|
|
||
| DIM = 256 | ||
| TOPK = 2051 # Qwen3.8-Flash-Next: 512 compressed blocks x ratio 4 + 3 | ||
| MAX_CTX = 8192 | ||
| POOL_SLOTS = 1 << 21 # 2 GiB of bf16 K+V at Hkv=2 | ||
| # Touch at least 4x the last-level cache across the rotated calls. | ||
| COLD_BYTES = 4 << 30 | ||
|
|
||
|
|
||
| def _fused_flydsl(q, k, v, req_to_token, row_req, indices, seq_lens, splits, ws): | ||
| from aiter.ops.flydsl.qsa_sparse_decode import flydsl_qsa_sparse_decode | ||
|
|
||
| return flydsl_qsa_sparse_decode( | ||
| q, | ||
| k, | ||
| v, | ||
| req_to_token, | ||
| row_req, | ||
| indices, | ||
| seq_lens, | ||
| DIM**-0.5, | ||
| num_splits=splits, | ||
| workspace=ws, | ||
| ) | ||
|
|
||
|
|
||
| def _gather_triton(q, k, v, req_to_token, row_req, indices, seq_lens, splits, ws): | ||
| from sglang.srt.layers.attention.qsa.sparse_attn import ( | ||
| qwen_sparse_fa2_cu_seqlens_triton, | ||
| qwen_sparse_kv_extraction_compact_triton, | ||
| sparse_gqa_packed_decode_triton, | ||
| ) | ||
|
|
||
| batch, topk = indices.shape | ||
| counts = torch.empty(batch, dtype=torch.int32, device=q.device) | ||
| cu_k = torch.empty(batch + 1, dtype=torch.int32, device=q.device) | ||
| qwen_sparse_fa2_cu_seqlens_triton(seq_lens, indices, counts, cu_k, batch, topk) | ||
| packed_k = torch.empty( | ||
| batch * topk, k.shape[1], DIM, dtype=q.dtype, device=q.device | ||
| ) | ||
| packed_v = torch.empty_like(packed_k) | ||
| qwen_sparse_kv_extraction_compact_triton( | ||
| k, | ||
| v, | ||
| req_to_token, | ||
| row_req, | ||
| indices, | ||
| seq_lens, | ||
| cu_k, | ||
| packed_k, | ||
| packed_v, | ||
| batch, | ||
| topk, | ||
| ) | ||
| relative = torch.arange(topk, dtype=torch.int32, device=q.device).expand(batch, -1) | ||
| relative = relative.masked_fill(relative >= counts[:, None], -1).contiguous() | ||
| cu_q = torch.arange(batch + 1, dtype=torch.int32, device=q.device) | ||
| return sparse_gqa_packed_decode_triton( | ||
| q, packed_k, packed_v, relative, cu_q, cu_k, counts, DIM**-0.5 | ||
| ) | ||
|
|
||
|
|
||
| FN_MAP = {"fused_flydsl": _fused_flydsl, "gather_triton": _gather_triton} | ||
|
|
||
|
|
||
| @marker.parametrize( | ||
| "num_kv_heads,group", [(2, 12), (1, 3)], [(2, 12)] | ||
| ) # TP1 and the cookbook's TP8 per-rank shape | ||
| @marker.parametrize("batch", [1, 4, 16, 64, 128], [1, 64]) | ||
| @marker.benchmark("impl", ["fused_flydsl", "gather_triton"]) | ||
| def benchmark(num_kv_heads: int, group: int, batch: int, impl: str): | ||
| from aiter.ops.flydsl.qsa_sparse_decode import recommended_num_splits | ||
|
|
||
| gen = torch.Generator(device="cuda").manual_seed(0) | ||
| q = torch.randn(batch, num_kv_heads * group, DIM, generator=gen, device="cuda") | ||
| q = q.bfloat16() | ||
| k = torch.randn(POOL_SLOTS, num_kv_heads, DIM, generator=gen, device="cuda") | ||
| k = k.bfloat16() | ||
| v = torch.randn_like(k) | ||
| touched = batch * num_kv_heads * TOPK * DIM * 2 * 2 | ||
| rotations = max(2, min(256, -(-COLD_BYTES // touched))) | ||
| req_to_token = torch.randint( | ||
| 1, | ||
| POOL_SLOTS, | ||
| (rotations * batch, MAX_CTX), | ||
| generator=gen, | ||
| device="cuda", | ||
| dtype=torch.int32, | ||
| ) | ||
| # Each rotation maps the same positions through different requests' slots. | ||
| row_reqs = [ | ||
| torch.arange(r * batch, (r + 1) * batch, device="cuda") | ||
| for r in range(rotations) | ||
| ] | ||
| seq_lens = torch.full((batch,), MAX_CTX, dtype=torch.int32, device="cuda") | ||
| indices = torch.stack( | ||
| [ | ||
| torch.randperm(MAX_CTX, generator=gen, device="cuda")[:TOPK].sort().values | ||
| for _ in range(batch) | ||
| ] | ||
| ).int() | ||
| splits = recommended_num_splits(batch, num_kv_heads, TOPK, q.device) | ||
| partials = batch * num_kv_heads * group * splits | ||
| workspace = ( | ||
| torch.empty(partials, device="cuda"), | ||
| torch.empty(partials, device="cuda"), | ||
| torch.empty(partials * DIM, device="cuda"), | ||
| ) | ||
| fn = FN_MAP[impl] | ||
| rotation = itertools.cycle(row_reqs) | ||
|
|
||
| def call(): | ||
| row_req = next(rotation) | ||
| return fn(q, k, v, req_to_token, row_req, indices, seq_lens, splits, workspace) | ||
|
|
||
| return marker.do_bench( | ||
| call, | ||
| graph_clone_args=None, | ||
| memory_args=None, | ||
| memory_output=None, | ||
| extra_memory_footprint=touched, | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| benchmark.run() |
Oops, something went wrong.
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.