Skip to content

feat(plugins): GLM-5.2 DSA sparse MLA prefill: op, route and MI355X kernels - #83

Merged
kyle-256 merged 5 commits into
mainfrom
feat/dsa-sparse-mla-prefill
Nov 17, 2026
Merged

kyle-256 merged 5 commits into
mainfrom
feat/dsa-sparse-mla-prefill

Conversation

@ChengYao-amd

@ChengYao-amd ChengYao-amd commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

Why

GLM-5.2's DSA prefill on HIP runs triton_sparse_mla_fwd from DeepseekSparseAttnBackend.forward_extend, and no AKLR op covers it. A new sequence's token i < 2048 names only i + 1 of its 2048 top-k slots; the rest are -1. This PR adds:

  • the op and its route;
  • a distribution that draws such rows the way sglang's indexer hands them over;
  • MI355X kernels that skip the -1 slots and read sglang's fp8 q directly. On the workloads the route takes they are 1.25–1.77x faster than triton_sparse_mla_fwd, and 7–12% faster in TTFT end to end.

Scope

  • Op [email protected] in plugins/sglang/layers/dsa_backend:

    • inputs and output are those of [email protected]: fp8 q [n_tokens, n_heads, kv_dim], fp8 KV pool, int32 top-k with -1 for none, bf16 output, SqnrBound 20 dB;
    • reference: the decode's;
    • baseline: triton_sparse_mla_fwd as forward_extend calls it.
  • Route on sglang.kernels.ops.attention.dsa.triton_sparse_mla.triton_sparse_mla_fwd, which forward_extend imports at each call. It takes a call only when:

    • no sequence in the forward has a cached prefix: seq_lens_sum == sum(dsa_extend_seq_lens_list) on the backend's host metadata, so no device sync;
    • q's two views are one buffer;
    • speculative decoding is off;
    • topk_length is None and max_topk_length is None or > 512, so sglang's own short-sequence path stays.

    Calls during stream capture stay on sglang. So do tokens after a cached prefix (later chunks of a chunked prefill, radix hits): every row is full there, and skipping -1s gains nothing.

  • aklr/core: CausalTopkDistribution (causal_topk):

    • Draw: one sequence's slots, a contiguous run of the pool at a random offset. A row with at most k visible positions names all of them in order, then -1. Any other row names the top k of its positions under a fixed random permutation, in random order, so adjacent rows share all but about one slot.
    • Domain: any distinct slots and any number of -1s, since a batch mixes sequences.
    • Repeatable bit for bit for one seed: the keys are distinct and the shuffle uses a stable sort. Otherwise the bench's determinism check, which redraws the inputs, flags the input order, not the kernel.
    • magnified_inputs skips it like int_range.
  • Kernels: Gluon schemas for kv_dim 576, top-k 2048, fp8 q and KV, n_slots 1835008. n_slots is the pool of --kv-cache-dtype fp8_e4m3 --max-total-tokens 1835008; it must stay below the kernels' 2 GiB addressing.

    • 8 heads (TP8): n_tokens 3072, 4096 (one kernel) and 5120, 6144, 8192, 10240, 12288, 16384 (another).
    • 16 heads (TP4): 3072, 4096 (one kernel each) and 5120–16384 (one kernel).
    • One TP4 kernel repacked the whole KV pool on every call. It now reads the pool in place, because at serving size the copy costs more than the attention. The output is bit-identical.
  • Not covered:

    • sequences of ≤ 2048 tokens: sglang runs dense attention for them and never calls this op;
    • n_tokens values not listed (dispatch matches exactly);
    • calls with a prefix;
    • bf16 KV caches, which are sglang's default.

Tradeoffs

  • Route target: triton_sparse_mla_fwd, not forward_extend (about 370 lines over many backends). The prefix check reads get_attn_backend().forward_metadata, as the qwen3_5 route does.
  • Fresh sequences only: the distribution draws a fresh sequence, so a kernel's measured speedup holds only for calls the route takes; that is why the route takes no call with a prefix. With a prefix the same kernels are 0.82–0.95x.
  • Three TP4 kernels left out, all below the bar against this baseline:
heads kernel (n_tokens) speedup (lower bound)
16 repacks the pool every call (5120 / 6144 / 8192 / 10240 / 12288) 0.78 / 0.83 / 0.90 / 0.95 / 1.01
16 repacks the pool every call (6144) 0.86
16 persistent (16384) 0.93

Verification

Environment: head 02ee530 (main merged in; kernels and validation on 275b7d1, full pytest on 2e6a1d7), MI355X, lmsysorg/sglang:v0.5.22-rocm10-mi35x (ROCm 7.15, torch 2.11.0+rocm10.0.0, Triton 3.8.0, AITER dbd8bf5)
Gate overrides: none

Contracts

Workloads come from GLM-5.2-MXFP4 on sglang 0.5.22, TP8, --kv-cache-dtype fp8_e4m3:

  • In every sparse prefill call, q_nope/q_rope are views of one [M, 8, 576] fp8 buffer, indices is [M, 1, 2048] int32, and max_topk_length is 2048.
  • A new sequence's rows hold exactly min(i + 1, 2048) slots, -1 last.
  • Sequences of ≤ 2048 tokens never reach this call: sglang runs dense attention for them (SGLANG_DSA_PREFILL_DENSE_ATTN_KV_LEN_THRESHOLD).
$ python -m aklr export check
(no output, exit 0)
$ python -m aklr validate --base origin/main --stages audit
audit: passed, findings: 0

Kernels

contract baseline measured accuracy (bound) speedup
[email protected] (8 heads) dsa_sparse_mla_prefill_baseline (sglang triton_sparse_mla_fwd) 8/8 8/8 (SqnrBound 20 dB) 1.25 – 1.77
[email protected] (16 heads) same 8/8 8/8 (SqnrBound 20 dB) 1.28 – 1.58
heads schema n_tokens baseline ms kernel ms speedup (lower bound) verdict
8 4f6bc6be 3072 0.353 0.199 1.774 passed
8 4f6bc6be 4096 0.475 0.288 1.649 passed
8 671af60f 5120 0.587 0.397 1.478 passed
8 671af60f 6144 0.699 0.490 1.426 passed
8 671af60f 8192 0.923 0.680 1.357 passed
8 671af60f 10240 1.144 0.862 1.327 passed
8 671af60f 12288 1.370 1.050 1.303 passed
8 671af60f 16384 1.815 1.444 1.255 passed
16 91beae1b 3072 0.364 0.230 1.579 passed
16 7ad4baa3 4096 0.503 0.355 1.417 passed
16 45bc6f5d 5120 0.607 0.402 1.509 passed
16 45bc6f5d 6144 0.722 0.499 1.447 passed
16 45bc6f5d 8192 0.957 0.694 1.379 passed
16 45bc6f5d 10240 1.195 0.891 1.341 passed
16 45bc6f5d 12288 1.433 1.093 1.309 passed
16 45bc6f5d 16384 1.910 1.490 1.279 passed

Accuracy, by how many slots a row holds: the inputs of 6 layers (0, 3, 20, 40, 60, 77) were dumped from real GLM-5.2 prefills of 4096 / 8192 / 16384 tokens. Below is the SQNR against fp32, min–max over layers and lengths.

  • The kernels hold 54–60 dB in every bucket.
  • sglang's kernel is lower, and lower still on rows with fewer slots (one layer: 32 dB on 1–16-slot rows, 40 dB on full rows).
TP slots in the row these kernels (dB) sglang triton_sparse_mla_fwd (dB)
TP8 1–16 55.8–60.2 32.0–41.4
TP8 17–256 55.5–55.7 34.8–44.8
TP8 257–2047 55.5–55.7 31.9–51.3
TP8 2048 55.5–55.9 33.0–54.2
TP4 1–16 53.6–60.9 33.0–40.8
TP4 17–256 54.1–55.6 35.0–45.2
TP4 257–2047 53.9–55.6 33.6–51.5
TP4 2048 54.3–55.8 34.4–54.1
Validation log
$ python -m aklr validate --base <this PR's code commit> --stages audit,bench <the 5 schemas>
[06:58:03] validate: collecting schemas and platform
[06:58:04] validate: 2026-11-06, head 275b7d1862be, base 025ec0e2e277, MI355X gfx950 x8
[06:58:04] audit: started (stage 1/2)
[06:58:04] audit: checking 5 schemas
[06:58:04] audit: checking changes against base 025ec0e2e277
[06:58:04] audit: passed, findings: 0, 0:00:00
[06:58:04] bench: started (stage 2/2)
[06:58:04] bench: reading 5 schemas
[06:58:04] bench: 5 batches (5 schemas), 16 workloads, 7 devices
[06:58:29] bench: 1/5 passed triton/dsa_sparse_mla_prefill_n_heads16_kv_dim576_topk2048/7ad4baa3: 1 wl, speedup 1.42x, acc ok
[06:58:29] bench: 2/5 passed triton/dsa_sparse_mla_prefill_n_heads16_kv_dim576_topk2048/91beae1b: 1 wl, speedup 1.58x, acc ok
[06:58:38] bench: 3/5 passed triton/dsa_sparse_mla_prefill_n_heads8_kv_dim576_topk2048/4f6bc6be: 2 wl, speedup 1.65x-1.77x, acc ok
[06:59:21] bench: 4/5 passed triton/dsa_sparse_mla_prefill_n_heads8_kv_dim576_topk2048/671af60f: 6 wl, speedup 1.25x-1.48x, acc ok
[06:59:22] bench: 5/5 passed triton/dsa_sparse_mla_prefill_n_heads16_kv_dim576_topk2048/45bc6f5d: 6 wl, speedup 1.28x-1.51x, acc ok
[06:59:22] bench: 16 workloads
[06:59:22] bench:   outcome                         workloads
[06:59:22] bench:   passed                                 16
[06:59:22] bench:   speedup  best 1.77x, median 1.40x, worst 1.25x  (workloads with a speedup value)
[06:59:22] bench:   accuracy FAIL 0, ok 16, not measured 0
[06:59:22] bench:   per-schema table: report.md
[06:59:22] bench: passed, findings: 0, 0:01:18

End to end

GLM-5.2-MXFP4, sglang 0.5.22, --kv-cache-dtype fp8_e4m3 --max-total-tokens 1835008. TTFT is the median of 10 single requests of random tokens (new sequences), in ms.

  • "AKLR, op off": AKLR_SGLANG_PLUGIN=1 and [email protected], so AKLR's other ops stay on.
  • "op on": the same without AKLR_DISABLE.
  • Route hits, from AKLR_EXPORT call records: at TP8 and TP4, n_tokens 4096 / 8192 / 16384 are served in every layer; other lengths record no_workload.
ISL 1024 2048 4096 8192 16384
TP8 native (no AKLR) 100.9 100.7 160.4 297.9 578.0
TP8 AKLR, op off 77.8 76.5 150.1 297.2 578.7
TP8 AKLR, op on 76.9 75.7 131.3 270.2 535.7
TP8 on vs off −1.1% −1.0% −12.5% −9.1% −7.4%
TP4 native (no AKLR) 100.6 112.1 219.5 371.3 715.2
TP4 AKLR, op off 77.3 104.1 208.3 368.9 712.0
TP4 AKLR, op on 77.6 104.0 192.6 340.2 663.1
TP4 on vs off +0.3% −0.1% −7.5% −7.8% −6.9%

ISL ≤ 2048 runs sglang's dense attention and is the control.

Correctness: chat prompts of exactly 4096 / 8192 / 16384 tokens (a number stated first, asked for last).

  • Greedy answers are identical and correct for native, op off and op on, at TP8 and TP4.
  • Prompt-token logprobs move only in the first 2048 positions, where the op serves rows short of top-k:
    • mean |Δ| on vs off is 0.10–0.27 there and 0.01–0.02 (noise) after;
    • two "off" runs agree bit for bit there.
  • Those positions move as much when only another op changes: native vs AKLR off, where AKLR's MoE kernels serve n_tokens 4096, gives mean |Δ| 0.25 (TP8) and 0.15 (TP4) at ISL 4096.
  • The shift is not one-signed (TP8 at 4096: −0.176 on vs −0.256 off).

Code

  • pytest tests on 2e6a1d7, in the CI image: 745 passed, 123 skipped. After merging main (02ee530): export check and the audit against main pass; without a GPU, tests/core/contract, tests/ops/test_contracts.py and tests/plugins fail only where main's do (routes whose targets need a GPU at import).
  • New tests:
    • tests/core/contract/test_causal_topk.py: row counts and order, slow change of full rows, domain, same-seed repeatability, a sequence longer than the pool;
    • the op's small Point in tests/plugins/sglang/cases.py (n_tokens 96 > top-k 64, so it draws both kinds of row);
    • names.txt regenerated.
  • aklr/core changes:
    • no schema or measurement version changes;
    • causal_topk is new and only this op uses it;
    • magnified_inputs skipping it changes no committed schema.

Not run

  • A CI run of this head (its validation job validates every schema, since aklr/core changes).
  • EP8 / DP-attention (64 heads) and speculative decoding: the route falls back there.

Risk

Affects: GLM-5.2 TP4 / TP8 prefill of new sequences on MI355X with an fp8 KV cache, at the listed n_tokens. aklr/core gains a distribution kind.
If wrong: a wrong attention output in those prefills, or a crash in the route.
Undo: [email protected]; otherwise git revert.

🤖 Generated with Claude Code

ChengYao-amd and others added 2 commits November 6, 2026 06:57
Add [email protected] to layers/dsa_backend: the sparse MLA that
DeepseekSparseAttnBackend.forward_extend runs through triton_sparse_mla_fwd
on HIP, with its reference, baseline and route.

- The route hooks triton_sparse_mla_fwd and takes only new sequences' tokens
  (no cached prefix, read from the backend's host lengths): a new sequence's
  first topk tokens name fewer than topk slots, where kernels can skip the -1s.
  Speculative decoding, sglang's short-sequence path (max_topk_length <= 512)
  and stream capture stay on sglang.
- aklr/core: a causal_topk distribution draws the top-k slots of one new
  sequence as sglang's indexer hands them over (rows short of topk name all
  their positions in order, then -1; full rows change by about one slot per
  token); its domain is any distinct slots and any number of -1s.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… on MI355X

Five Gluon schemas of [email protected] for GLM-5.2's absorbed MLA
prefill (kv_dim 576, top-k 2048, fp8 q and KV): 8 heads (TP8) and 16 heads
(TP4), n_tokens 3072-16384 of a new sequence, the KV pool at serving size
(1835008 rows).

- The kernels skip a row's -1 slots, which a new sequence's first 2048
  tokens hold, and read sglang's fp8 q directly, converting it to bf16 as
  they load it.
- The TP4 5120-16384 kernel reads the pool in place instead of repacking
  all of it on every call.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
zhenhuang12 added a commit that referenced this pull request Nov 16, 2026
…ts call

A schema's workload now carries `models`: the models whose serving makes that very call, sorted,
each once, lowercase (`kimi-k3`, `deepseek-v4-pro`, `glm-5.2`). The file's `models`, right after
`op_type`, is their union, written by `schema_text` and so checked on parse: the file shows at a
glance which models it is for, and each workload which of them calls it. The uuid hashes neither,
so no file moves; the schema_version stays 5.

A workload without `models` still parses (a base from before this change is read as it is), and
`schema_errors` refuses it (E10), so `export check` and audit require it of every committed and
changed file. `OpContract.narrow` keeps its signature; `workload_for` takes the models.

`python -m aklr export` takes `--model`: a new file's workloads carry it, and a committed file the
calls reach is listed with the called workloads that do not name it.

All 698 committed schemas (4606 workloads) are labelled. A workload takes the model named by the
commit that added it, in AKLR or, for AKLR's initial commit, in SIKL (whose main is that commit);
a kernel added later at an existing Point takes the Point's models. Where one scenario holds
workloads of several commits, they are told apart:
- flash_mla sparse decode and fp8_paged_mqa_logits (64 heads): SIKL #71's are deepseek-v4-flash,
  SIKL #83's deepseek-v4-pro; the three 262144-row ones of #96 ("DSV4") name both.
- deepseek_v2_fp8_moe at model_dim 6144: #46's are glm-5.2, #124's and #181's glm-5.3.
- gemm_n7168_k512: #159's multiples of 7 (DSPARK's draft) are kimi-k3.
Workloads from SIKL #52, which gave the pre-existing definitions their default sweep, take the
model of the commit that added the definition: SIKL #50 (DeepSeek-V4-Flash's schema set), #35 and
#47 (Kimi-K3). The definitions of SIKL #18 and #36 name no model and are labelled by their shapes,
as the owner reviewed: hidden-6144 GEMMs and MoE as glm-5.2, gemm_a8w8_blockwise_scaled_n3072_k1536
as deepseek-r1, gemm_n7168_k512's sweep as glm-5.2 and kimi-k3, the hidden-6144 norms as glm-5.2
and glm-5.3. By the owner's choice, DeepSeek-R1's workloads also name deepseek-v3, the DSA indexer
ones name glm-5.2 and glm-5.3, and gemm_n4096_k2048 and gemm_n512_k4096 also name qwen3.5. The
Qwen3.5 gated-attention decode workloads (qkv_dim 4608) also name qwen3-next, whose TP2 route
falls through to that op (#78).
zhenhuang12 added a commit that referenced this pull request Nov 17, 2026
…ts call

A schema's workload now carries `models`: the models whose serving makes that very call, sorted,
each once, lowercase (`kimi-k3`, `deepseek-v4-pro`, `glm-5.2`). The file's `models`, right after
`op_type`, is their union, written by `schema_text` and so checked on parse: the file shows at a
glance which models it is for, and each workload which of them calls it. The uuid hashes neither,
so no file moves; the schema_version stays 5.

A workload without `models` still parses (a base from before this change is read as it is), and
`schema_errors` refuses it (E10), so `export check` and audit require it of every committed and
changed file. `OpContract.narrow` keeps its signature; `workload_for` takes the models.

`python -m aklr export` takes `--model`: a new file's workloads carry it, and a committed file the
calls reach is listed with the called workloads that do not name it.

All 705 committed schemas (4668 workloads) are labelled. A workload takes the model named by the
commit that added it, in AKLR or, for AKLR's initial commit, in SIKL (whose main is that commit);
a kernel added later at an existing Point takes the Point's models. Where one scenario holds
workloads of several commits, they are told apart:
- flash_mla sparse decode and fp8_paged_mqa_logits (64 heads): SIKL #71's are deepseek-v4-flash,
  SIKL #83's deepseek-v4-pro; the three 262144-row ones of #96 ("DSV4") name both.
- deepseek_v2_fp8_moe at model_dim 6144: #46's are glm-5.2, #124's and #181's glm-5.3.
- gemm_n7168_k512: #159's multiples of 7 (DSPARK's draft) are kimi-k3.
Workloads from SIKL #52, which gave the pre-existing definitions their default sweep, take the
model of the commit that added the definition: SIKL #50 (DeepSeek-V4-Flash's schema set), #35 and
#47 (Kimi-K3). The definitions of SIKL #18 and #36 name no model and are labelled by their shapes,
as the owner reviewed: hidden-6144 GEMMs and MoE as glm-5.2, gemm_a8w8_blockwise_scaled_n3072_k1536
as deepseek-r1, gemm_n7168_k512's sweep as glm-5.2 and kimi-k3, the hidden-6144 norms as glm-5.2
and glm-5.3. By the owner's choice, DeepSeek-R1's workloads also name deepseek-v3, the DSA indexer
ones name glm-5.2 and glm-5.3, and gemm_n4096_k2048 and gemm_n512_k4096 also name qwen3.5. The
Qwen3.5 gated-attention decode workloads (qkv_dim 4608) also name qwen3-next, whose TP2 route
falls through to that op (#78).

Rebased onto main at #235: the files main rewrote (#169, #197, #217) keep their Points' models
under the new uuids and paths, and #212's seven GEMM files, at Points new to the tree, are glm-5.2.
@kyle-256
kyle-256 merged commit 43c5db0 into main Nov 17, 2026
39 of 45 checks passed

This branch was previously deployed

1 inactive deployment
gpu-trusted — 75a80ba5 Deployed Nov 11, 2026 by ChengYao-amd via prepare-gpu / prepare #556
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants