Repository navigation
[Triton/Gluon] mla_sparse_prefill: triton backend on gfx950, the default at H=32 without a sink - #7681
Conversation
…ult at H=32 without a sink mla_sparse_prefill gains backend="triton": pa_prefill_sparse on gfx950 for bf16 q and unified_kv, H = 32, an empty extend list, no split-K and out None or contiguous; anything else raises RuntimeError. backend=None takes it for such calls without a sink when pa_prefill_sparse's single-pass kernel serves them (its MIN_TOKENS query rows and unit inner strides), and logs the switch once. Other calls keep opus. Tests: the bf16 sweep's triton candidate also runs on gfx950, through mla_sparse_prefill(backend="triton"), for H = 32 cases with no extend rows, plus one such case; a guard checks that backend=None gives the triton output without a sink and the opus output with one. Signed-off-by: Rony Leppänen <[email protected]>
There was a problem hiding this comment.
🟡 Changes recommended
Forced Triton calls can produce incorrect attention results for non-unit inner strides and small-query sink inputs, and normal CI does not exercise the added regressions.
4 open issues
What changed in this PR
This PR connects mla_sparse_prefill to the gfx950 single-pass backend introduced in #7680, making it the default for eligible sink-free bf16 calls while retaining OPUS for other configurations.
Changes:
- Adds explicit
backend="triton"support with configuration checks. - Selects Triton automatically for supported H=32, single-source calls with enough query rows.
- Extends correctness tests and CLI coverage for gfx950, including default-backend selection with and without a sink.
| File | Description |
|---|---|
op_tests/test_mla_sparse_prefill.py |
Adds gfx950 Triton cases, default-selection regression coverage, and CLI documentation. |
aiter/ops/mla_sparse_prefill.py |
Adds Triton eligibility checks, dispatch, one-time logging, and backend documentation. |
Review effort: Balanced
…d/mla-sparse-prefill-triton-backend Signed-off-by: Rony Leppänen <[email protected]>
…kernel serves, H=64 too, and opus when pa_prefill_sparse cannot load backend="triton" now takes exactly the calls pa_prefill_sparse's gfx950 single-pass kernel serves (_single_pass_unsupported) within its own contract (one KV source, no split-K, out None or contiguous), and raises RuntimeError for the rest. Before, it also accepted q or unified_kv with an inner stride other than 1, and a unified_kv that is not 2-D. pa_prefill_sparse sent those to mla_gluon, which ignores those strides: on MI355X every row of a T=256, H=32 call came back wrong. Calls with a sink below MIN_TOKENS rows now run the kernel inside pa_prefill_sparse (merged from #7680), where mla_gluon added the sink once per key split. With #7680's H=64 support the triton backend serves H=64 too, and backend=None takes it for sink-free H=64 calls with at least MIN_TOKENS rows, as at H=32. Its choice at H=32 is unchanged. backend=None raised ImportError where pa_prefill_sparse's module cannot be imported (its gluon kernels need a Triton with gluon). It now runs opus there, and logs why once. Tests: test_triton_backend_rejects (q with inner stride 2, a 3-D unified_kv), test_default_backend_without_gluon, test_default_backend_gfx950 at H=32 and 64, and an H=64 one-source case in the sweep. Signed-off-by: Rony Leppänen <[email protected]>
…en its sweep CI ran the file as a script, whose entry point runs the backend sweep only, so none of its pytest functions ran: the precision sweep, the OPUS CSR boundary cases and the backend-choice tests. The file now runs under pytest first, then as a script. On MI35X the script took 85 s in CI. Signed-off-by: Rony Leppänen <[email protected]>
…d/mla-sparse-prefill-triton-backend Signed-off-by: Rony Leppänen <[email protected]>
…its own 64-row bound backend=None took triton from pa_prefill_sparse's MIN_TOKENS query rows, which pa_prefill_sparse is about to drop. The bound stays at 64 rows, now as this module's _TRITON_MIN_ROWS: below it the single-pass kernel would run one program per query row, while opus splits each row's keys. Signed-off-by: Rony Leppänen <[email protected]>
…d/mla-sparse-prefill-triton-backend Signed-off-by: Rony Leppänen <[email protected]>
…ingle-pass kernel cannot serve; tests in tests/ backend="triton" raises RuntimeError unless pa_prefill_sparse's single-pass kernel serves the call (unit inner strides, at least MIN_TOKENS query rows). Before, such a call reached mla_gluon, which is not the kernel the backend names. _use_triton returns False where pa_prefill_sparse cannot be imported (Triton < 3.6), so backend=None keeps opus there instead of raising ImportError; a forced "triton" call still raises it. The default-backend test and the two guards above move to tests/test_mla_sparse_prefill_backend.py, which CI's `pytest -q tests` step runs once a pull request targets main; op_tests/test_mla_sparse_prefill.py runs there as its default sweep, which never called them. On MI355X (torch 2.13.0+rocm10.0.0): on the sources of the previous head the two guard tests fail (DID NOT RAISE; the import halted) and the default-backend tests pass; with this commit all four pass, 0 skipped; dropping the raise fails only the first guard, dropping the ImportError guard only the second. op_tests/test_mla_sparse_prefill.py passes 46 on both, and its default sweep completes on both. Signed-off-by: Rony Leppänen <[email protected]>
…ppaneamd/mla-sparse-prefill-triton-backend Two fixes for the same review findings meet here. The resolution: - aiter/ops/mla_sparse_prefill.py is this branch's. backend="triton" raises where pa_prefill_sparse's single-pass kernel cannot serve the call (H, D, dtypes, a 2-D unified_kv, unit inner strides), at any number of query rows, since pa_prefill_sparse now sends every call that kernel serves to it; the module is imported once, and backend=None keeps opus where it cannot be. - The backend tests live in tests/test_mla_sparse_prefill_backend.py, as in cd4d853, where CI's `pytest -q tests` step collects them, in this branch's form (H 32 and 64, the 2-D check, no row bound), plus test_triton_backend: backend="triton" against the op test's reference, in place of the op test's two one-source pytest rows. - .github/scripts/aiter_test.sh stays as on main: with these tests in tests/, the op test's pytest functions need no CI case. Signed-off-by: Rony Leppänen <[email protected]>
8e5164b to
3e40348
Compare
…d/mla-sparse-prefill-triton-backend Signed-off-by: Rony Leppänen <[email protected]>
…d/mla-sparse-prefill-triton-backend Signed-off-by: Rony Leppänen <[email protected]>
…ram per query row and 32 heads Signed-off-by: Rony Leppänen <[email protected]>
…d/mla-sparse-prefill-triton-backend Brings in main up to 85ccf17 through #7680. Conflicts: - aiter/ops/mla_sparse_prefill.py: main routes the op through the plugin interface (#7791). mla_sparse_prefill now dispatches as mla_sparse_prefill_v41 does: enabled providers first, then a native fallback that picks triton or opus from backend=. The routed op (_mla_sparse_prefill_routed, opus) and its contract are unchanged. - op_tests/test_mla_sparse_prefill.py: docstrings. main replaced --total_tokens with --window and passes total_tokens=n, so the gfx950 triton candidate now keys on an empty kv_indices_extend (what the triton backend requires) instead of total_tokens == 0. tests/test_plugin_routed_ops.py: the backend-pin test also covers backend="triton". Signed-off-by: Rony Leppänen <[email protected]>
…2 rows Measured on MI355X against main's OPUS, whose H > 32 kernel #7784 and #7897 sped up: - H=64: OPUS takes 0.90x the triton backend's time on index lists captured from GLM-5.3-Flash prefill and 0.83-0.94x on uniformly random ones, so backend=None keeps OPUS there. backend="triton" still serves H=64. - H=32: OPUS takes 1.09x the triton backend's time on the captured lists and 1.75-2.07x on the random ones, but below 512 query rows 0.39-0.95x, so the bound rises from 64 to 512. test_default_backend_gfx950 runs at the bound, expects OPUS at H=64, and adds a case one row below the bound, which expects OPUS. Signed-off-by: Rony Leppänen <[email protected]>
|
No activity for 15 days, so this is now labelled |
msaffari-amd
left a comment
There was a problem hiding this comment.
Thanks for this! I ran it on an MI355X (gfx950). The triton path gave the right answers on everything I tried: ragged and empty rows, no sink, 32 and 64 heads, a 1M-row pool, strided q and unified_kv views, a NaN-filled out, and an empty pool. Comparing base against head for backend=None at 32 heads without a sink, it was 43 to 50 % faster from 512 to 16,384 rows, which matches your table. Nothing changed where it still uses OPUS (64 heads, with a sink, or under 512 rows).
Two things I'd like to see before merge (details in the inline comments):
- Needs updating for the new plugin routing on
main - These tests don't run in CI on
mainanymore
One open question: with backend=None, existing callers at 32 heads without a sink will now get triton instead of OPUS, and the only hint is a one-time log line. The two drafts you mention, sglang#49282 and vllm#66424, don't pass backend= and were tested on OPUS. Would a note in the PR or release text pointing to backend="opus" as the way to opt out be enough? Or would a small helper like get_mla_sparse_prefill_v41_backend, so callers can check which kernel they'll get, be useful here too?
| out, | ||
| num_kv_splits, | ||
| ) | ||
| operation = _mla_sparse_prefill_routed.__aiter_plugin_op__ |
There was a problem hiding this comment.
Needs updating for the new plugin routing on main
Heads-up for when this moves onto main after #7680: #8291 changed how mla_sparse_prefill hooks into the plugin system there, so this part won't work as it is.
What changed on main:
- The function plugins see is now
_mla_sparse_prefill_contract, and it no longer takesout. _mla_sparse_prefill_routedisn't decorated anymore, so this line would raise anAttributeError.- Git also shows a merge conflict in this file.
mla_sparse_prefill_v41 on main already does the same thing the new way, so copying that is probably the easiest fix. If the caller passes out, skip the plugins and run our own code directly. If not, go through the new function and add out=None back before picking triton or opus.
Roughly like this (written against main, not tested):
def native_fallback(
q_, unified_kv_, kv_indices_prefix_, kv_indptr_prefix_, kv_,
kv_indices_extend_, kv_indptr_extend_, attn_sink_, softmax_scale_,
out_=None, num_kv_splits_=None,
):
if backend == "triton" or (
backend is None
and _use_triton(
q_, unified_kv_, kv_indices_extend_, attn_sink_, out_, num_kv_splits_
)
):
return _triton_sparse_prefill(
q_, unified_kv_, kv_indices_prefix_, kv_indptr_prefix_,
kv_indices_extend_, attn_sink_, softmax_scale_, out_, num_kv_splits_,
)
return _mla_sparse_prefill_native(
q_, unified_kv_, kv_indices_prefix_, kv_indptr_prefix_, kv_,
kv_indices_extend_, kv_indptr_extend_, attn_sink_, softmax_scale_,
out_, num_kv_splits_,
)
args = (
q, unified_kv, kv_indices_prefix, kv_indptr_prefix, kv,
kv_indices_extend, kv_indptr_extend, attn_sink, softmax_scale,
)
if out is not None:
return native_fallback(*args, out, num_kv_splits)
def contract_fallback(*contract_args):
*inputs, num_kv_splits_ = contract_args
return native_fallback(*inputs, None, num_kv_splits_)
operation = _mla_sparse_prefill_contract.__aiter_plugin_op__
registered = _plugin._routed_ops[operation.name]
return _plugin._dispatch(
operation, registered.canonicalize, contract_fallback,
(*args, num_kv_splits), {},
)| Lives in ``tests/`` because CI runs ``python3 -m pytest -q tests`` on pull requests | ||
| to main, while ``op_tests/test_mla_sparse_prefill.py`` runs there only as its | ||
| default sweep, which never calls these tests. The inputs, the reference and the | ||
| arch query are the op test's. |
There was a problem hiding this comment.
These tests don't run in CI on main anymore
Agreeing with Copilot's earlier comment here. Moving the tests to tests/ used to be enough, but #8352 removed the pytest -q tests step from aiter-test.yaml, and pre-checks.yaml only runs the test_plugin* files. So right now nothing in CI runs this file. The op test's default run doesn't cover it either: its default --window 128 gives every call extend rows, so the gfx950 triton path never gets picked.
The way in on main is the explicit list in .github/scripts/split_tests.sh (it already has tests/test_flydsl_aot_cache.py), and aiter_test.sh already runs tests/test_*.py through pytest. Could you add the file there?
"tests/test_flydsl_aot_cache.py" \
- "tests/test_flydsl_aot_launch.py"
+ "tests/test_flydsl_aot_launch.py" \
+ "tests/test_mla_sparse_prefill_backend.py"And the docstring would then need a small update:
| Lives in ``tests/`` because CI runs ``python3 -m pytest -q tests`` on pull requests | |
| to main, while ``op_tests/test_mla_sparse_prefill.py`` runs there only as its | |
| default sweep, which never calls these tests. The inputs, the reference and the | |
| arch query are the op test's. | |
| Runs in CI from the explicit list in ``.github/scripts/split_tests.sh`` | |
| (``aiter_test.sh`` runs ``tests/test_*.py`` through pytest), because | |
| ``op_tests/test_mla_sparse_prefill.py``'s default sweep never reaches these | |
| cases. The inputs, the reference and the arch query are the op test's. |


Stacked on #7680, and based on its branch: this PR's diff is its own commits.
Summary
mla_sparse_prefillacceptsbackend="triton":pa_prefill_sparse's gfx950 single-pass kernel from #7680, at H=32or 64. With
backend=Noneit becomes the default on gfx950 for sink-free bf16 calls with H=32, one KV source and atleast 512 query rows. Other calls keep OPUS.
Motivation
OPUS's time against the triton backend's on MI355X (Performance):
test_mla_sparse_prefill.py)At H=64 OPUS is faster, so
backend=Nonekeeps it there.Callers that reach the new default: GLM-5.3-Flash at attention TP 2 (32 heads per rank), bf16 KV cache, gfx950,
through two open drafts that call
mla_sparse_prefillwithout a sink:--dsa-prefill-backend aiter): calls from 128 rows at TP 2, of which the default takesthose from 512;
VLLM_ROCM_USE_AITER=1): calls from 1,024 rows.Not affected:
main, which callpa_sparse_prefill_opusor pass a sink.End to end, GLM-5.3-Flash at TP 2 in SGLang with [Perf] [AMD] dsa: Prefill rope-free bf16 KV pools with aiter's OPUS sparse kernel sgl-project/sglang#49282: output throughput +1.5 to +1.9 % against
the older OPUS in the SGLang image's AITER, an estimated +0.4 to +0.6 % against
main's (Performance).Changes
aiter/ops/mla_sparse_prefill.py:backend="triton"callspa_prefill_sparsewithhas_invalid=False, at any number of query rows, and alwaysruns [Triton/Gluon] pa_prefill_sparse: single-pass gluon kernel for gfx950 at H=32 and 64 #7680's single-pass kernel, never
mla_gluon:kv_indices_extend,num_kv_splitsNoneor 1 andoutNoneor contiguous;_single_pass_unsupported: bf16 q andunified_kv, H=32 or 64, D=512, a 2-Dunified_kv, unit inner strides);RuntimeError, the module's error for a configuration no backend serves.backend=Nonetakes it for such calls with H=32, without a sink and with at least 512 query rows. Otherwise OPUS,as before.
OPUS splits each row's keys.
pa_prefill_sparseis tried once. If it fails (its Gluon kernels need a Triton with Gluon),backend=Nonekeeps OPUS and"triton"raisesRuntimeErrornaming theImportError.mla_sparse_prefilldispatches the routed op asmla_sparse_prefill_v41does. Enabledproviders are tried first and never see
backend=; the native fallback then runs the triton backend or OPUS. Therouted op
_mla_sparse_prefill_routed(OPUS), its contract andpa_sparse_prefill_opusare unchanged.op_tests/test_mla_sparse_prefill.pyandtests/test_mla_sparse_prefill_backend.py(new): see "Changed op tests".tests/test_plugin_routed_ops.py: the test that keeps the backend pin out of the provider contract also runs withbackend="triton".Performance (if applicable)
Kernel,
test_mla_sparse_prefill.py, forcedtritonagainstopus:opus, µs)triton, µs)--total_pages 1048576 -n 4096 16384), H=32: 2,501.61 → 1,392.05 µs (−44.4 %) and10,108.61 → 5,767.11 µs (−42.9 %).
tritonis slower: +6.3 to +9.9 % with a 64 MiB pool, +19.8 to +20.7 % with a 1 GiB pool.Kernel, without a sink, through
mla_sparse_prefill(backend=...):Index lists captured from GLM-5.3-Flash prefill in SGLang (3 layers × 8,499 and 16,179 rows): OPUS takes 1.09× the
triton backend's time at H=32 and 0.90× at H=64.
main1eab5ae12c): 1.31× at H=64.mainhas since sped up OPUS for H > 32 ([ASM] [HIP] [JIT] [Perf] Replace gfx950 MLA v4 prefill 16mx8 kernels with 32mx4 kernels #7784,[ASM] [HIP] Walk prefix and extend as one tile stream in gfx950 MLA sparse prefill #7897).
Synthetic lists (rows choose up to 2,048 keys from runs of 4 adjacent slots), H=32: OPUS's time over the triton
backend's, the minimum over 3 processes.
512 is the fewest rows from which the triton backend is faster in both rows of the table.
Harness outside this PR: the backends interleaved in one process, 512 MiB written before each call; 3 processes per
input set.
End to end, GLM-5.3-Flash at TP 2 (H=32) on MI355X in SGLang with sgl-project/sglang#49282:
lmsysorg/sglang-rocm:v0.5.23-rocm724-mi35x-20261106with #49282 applied; model revisionb5688f25.285579b621) has nomla_sparse_prefill.1c902a4d25reproduced in a test-only shim (the same contract and
_use_tritoncode, with its 64-row bound). Other calls go tothat AITER's OPUS.
SGLANG_USE_AITER=1):patched, baseline, patched, baseline.
0.11 pp over the three runs.
backend; in the baseline runs, OPUS.
main's OPUS: about +0.4 to +0.6 %, estimated, not measured. On synthetic lists of 8,192 and 16,384rows the image's OPUS takes 1.31× the triton backend's time and
main's 1.09×; the gain is scaled by(1.09 − 1) / (1.31 − 1).
Testing
Documentation
Dependencies
Breaking Changes
None in the API. On gfx950, sink-free bf16 calls with H=32, one source and at least 512 rows run the triton backend by
default. Their outputs differ from OPUS's within the test tolerance;
backend="opus"keeps the previous kernel.PR Self-Review
Reviewed revision
20443284ed112d9872cd2937ffca30b636e62f06Behavior and intent
backend=Noneon gfx950 runspa_prefill_sparse's single-pass kernel instead of OPUS for sink-free bf16 calls withH=32, one KV source and at least 512 rows.
backend="triton"forces that kernel at H=32 or 64 and any number of rows. It raisesRuntimeErrorwhere the kernelcannot serve the call or cannot be imported; on
main,"triton"raisesValueError.Validation and unverified scope
vllm/vllm-openai-rocm:nightly-rocm100-1648fefab91204f3b15bf8062217316f07f2c4bb(Triton 3.8.0), with FlyDSL0.5.0, which
main'saiterrequires at import.2718e0df2b(main85ccf1794dmerged).
python -m pytest op_tests/test_mla_sparse_prefill.py tests/test_mla_sparse_prefill_backend.py:mla_sparse_prefill.pyismain's, with this PR's three test files: 42 passed, 8 failed.ValueError: ... backend must be one of ('opus',) or None, got 'triton':[64-32-0-0-128-bf16]in the op test,test_triton_backend× 4,test_default_backend_gfx950[no-sink-32],test_triton_backend_rejects.AttributeError(mainhas no_pa_prefill_sparse_module):test_default_backend_without_gluon.test_default_backend_gfx950[no-sink-64],[sink-*]and[short-*]pass on both.python -m pytest tests/test_plugin_routed_ops.py -k sparse_prefill: this branch 3 passed; [Triton/Gluon] pa_prefill_sparse: single-pass gluon kernel for gfx950 at H=32 and 64 #7680's head with thisPR's version 2 passed,
[triton]failed (ValueError).python -m pytest op_tests/triton_tests/attention/test_pa_prefill_sparse.py: 29 passed, 43 skipped, as in [Triton/Gluon] pa_prefill_sparse: single-pass gluon kernel for gfx950 at H=32 and 64 #7680._use_tritonalwaysFalse:test_default_backend_gfx950[no-sink-32];_use_tritoncalled withattn_sink=None:[sink-32];_TRITON_MIN_ROWS0:[short-32]; 513:[no-sink-32];_TRITON_DEFAULT_HEADS(32, 64):[no-sink-64];_triton_unsupportedwithout the single-pass kernel's check:test_triton_backend_rejects;ImportErrorfallback:test_default_backend_without_gluon;backend="opus"read asNone:test_default_backend_without_gluon;2 * softmax_scalepassed topa_prefill_sparse:test_triton_backend[8-rows],[h32],[h64]([empty-pool]has no keys: zeros at any scale).
test_default_backend_gfx950's inputs without a sink, the forcedopusandtritonoutputs differ in 2,490 of8,388,608 elements at H=32 and 7,626,216 of 16,777,216 at H=64, so the test tells the backends apart.
741ac525d2, this revision's parent, with the same kernel and calls;there
backend=Nonetook the triton backend from 64 rows at H=32 and 64;1c902a4d25. At this revision those calls compile to the same kernel instructions.and is left out of
main's 1.09× there; the captured lists and the row-count table use all 3."triton"raises andNonekeeps OPUS;main's AITER (measured on the image's AITER, see Performance) and vLLM with[ROCm][Perf] Route sink-less sparse MLA prefill to AITER OPUS on gfx950 vllm-project/vllm#66424;
Aiter Testhas not run on it. Thetests/file first runs inCI after [Triton/Gluon] pa_prefill_sparse: single-pass gluon kernel for gfx950 at H=32 and 64 #7680 merges and this pull request targets
main.PR-specific question
main, which rejectsbackend="triton"withValueError: every case that forces it (the op test's[64-32-0-0-128-bf16], its gfx950tritoncandidate;test_triton_backend× 4;test_triton_backend_rejects;test_the_backend_pin_is_not_part_of_the_sparse_prefill_contract[triton]) andtest_default_backend_gfx950[no-sink-32], whose reference is the forcedtritonoutput.mainfor lack of the import fallback (_pa_prefill_sparse_module):test_default_backend_without_gluon.main:test_default_backend_gfx950[no-sink-64],[sink-*]and[short-*]. They pin wherebackend=Nonekeeps OPUS: each fails under one of the mutations above.
mla_sparse_prefill(backend=None), where the defaultis chosen, and had no gfx950
tritoncandidate.--windowdefaults to 128) keep the gfx950candidate out; hence the backend tests in
tests/.Changed op tests
op_tests/test_mla_sparse_prefill.py:tritoncandidate also runs on gfx950, asmla_sparse_prefill(backend="triton"):(64, 32, 0, 0, 128)(empty pools, sink only) runs it; it fails onmain.tests/test_mla_sparse_prefill_backend.py(new), with the op test's inputs, reference and arch query:test_triton_backend: forcedtritonagainst the reference at 8 rows, with empty pools, at H=32 and at H=64.test_default_backend_gfx950(H=32 and 64; without a sink, with one, and without a sink at 511 rows):backend=Nonegives the forced
tritonoutput at H=32 without a sink at 512 rows, else the forcedopusoutput.test_triton_backend_rejects: forcedtritonraisesRuntimeErrorfor aqwith inner stride 2 and for a 3-Dunified_kv.test_default_backend_without_gluon: withpa_prefill_sparseunimportable,backend=Nonegives the forcedopusoutput.
AI assistance (Claude) was used for this PR; the change, tests and measurements were reviewed and run by the submitter.