Repository navigation
[Triton] fp8_mqa_logits: add a deterministic option - #7471
aarnetalman wants to merge 2 commits into
Conversation
fp8_mqa_logits picks its kernel configuration from seq_len: on gfx950 the row tile (block_m 1 or 2) and a single-warp variant above 4,096 rows, on the non-Gluon path the MFMA shape (16 up to 1,024 rows, else 32). These change the order of the head reduction, so a query row's logits depend on how many rows share the call. Callers that split the rows into chunks (the DSA k-pool indexer sizes its chunks from free device memory) then get logits that change with the chunking, and a top-k over near-tied logits can pick different keys. deterministic=True keeps one configuration for every seq_len: block_m 1 with two warps on gfx950, the 32-wide MFMA shape on the non-Gluon path. The default is unchanged. The op test checks that rows are bitwise equal across 12 sub-calls of an 8,192-row call on both paths, and that the result matches the default's accuracy.
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: |
NOT READY FOR HUMAN REVIEWPR Self-ReviewRevision Author self-review accepted. Readiness report: Author answers
Show the author's self-review answersBehavior and intentValidation and unverified scopePR-specific questionChanged op testsPR state
Copilot findings
Code and op-test hygiene
Required CI
Shadow mode reports what would block but keeps the status successful. Set |
vpietila-amd
left a comment
There was a problem hiding this comment.
Looks good to me. This is safe change as it is opt in. Consider extending the test cases and the possible performance improvement.
| @@ -250,3 +251,58 @@ def test_fp8_mqa_logits_nonzero_cu_start( | |||
| logits = logits.masked_fill(neginf_mask, 0) | |||
| diff = calc_diff(logits, ref_logits) | |||
| assert diff < 1e-4, f"{diff=}" | |||
|
|
|||
|
|
|||
| @pytest.mark.parametrize("s_k", [8192, 32768]) | |||
There was a problem hiding this comment.
We probably should add the s_k=32768 case to cover also the KV-splitting path. There is MIN_SPLIT_KV = 16384 value used in method _gfx950_kv_splits.
There was a problem hiding this comment.
Thanks, it's already there: s_k is parametrized over 8,192 and 32,768. At 32,768 keys, calls of up to 4,096 rows get 2 KV splits from _gfx950_kv_splits, and the full 8,192-row call gets 1, so the row ranges in the test compare split and unsplit calls.
Looking at the split path more closely did turn up a problem at 16 heads, which the test doesn't cover yet (it runs 32 and 64). I'll push a fix with a 16-head case shortly.
| @@ -243,7 +248,8 @@ def fp8_mqa_logits( | |||
| seq_len, seq_len_kv, 2, num_warps, waves_per_eu | |||
| ) | |||
| if ( | |||
| num_heads <= 32 | |||
| not deterministic | |||
There was a problem hiding this comment.
If performance is concern for the deterministic mode, you could remove here the condition not deterministic to allow block_m = 2 and set later num_chains = 1 for the deterministic case. I tested that this combination has better perf in the H <= 32 case and it passes the determinism test.
There was a problem hiding this comment.
Thanks, I tried it: with one head-sum chain, BLOCK_M 1 and 2 give the same logits and every test passes (I applied it only above 16 heads, where the head sum is folded). At 32 heads on MI350X it is 4-18% faster at 8,192 rows with 32k-64k keys, but 12-14% slower at 512-2,048 rows with 8k or 64k keys, and the same elsewhere. Since it's mixed, I'd keep this PR as is and leave the BLOCK_M=2 choice for a follow-up with a better crossover. Happy to add it here if you prefer.
…e folded head reduction On gfx950, a call split over KV is not repeatable at 16 heads or fewer (no folded head reduction): the logits next to the split boundary change from run to run (2,048 rows, 32k / 64k keys: 19 of 20 calls differ). Deterministic mode now runs those calls unsplit; with the folded reduction (more than 16 heads) the split is repeatable and stays. The row-invariance test also covers 16 heads.
|
@vpietila-amd Pushed a fix for the 16-head problem I mentioned: on gfx950, a call that is split over KV is not repeatable at 16 heads or fewer, where there is no folded head reduction. The logits next to the split boundary change from run to run (2,048 rows, 32k or 64k keys: 19 of 20 calls differ). The default configuration was repeatable in every case I ran. Deterministic mode now runs those calls without the split. With more than 16 heads the split is repeatable and stays. The row-invariance test now covers 16 heads as well, and all 170 tests in the file pass on MI350X. The cost is only at 16 heads, with long KV and few rows: for example 1 row with 65,536 keys goes from 65 to 305 µs. 32 and 64 heads are unchanged. |
Summary
fp8_mqa_logits(..., deterministic=True)keeps one kernel configuration for every number of query rows, so a row's logits are bitwise the same whichever rows share the call. The default is unchanged.Motivation
fp8_mqa_logitscomputes the indexer logits of DeepSeek-style sparse attention (DeepSeek-V3.2, GLM-5). It picks its configuration fromseq_len, the number of query rows in the call: on gfx950 the row tile (block_m1 or 2) and a single-warp variant above 4,096 rows, on the non-Gluon path the MFMA shape (16 up to 1,024 rows, otherwise 32). These change the order of the reduction over heads, so the same row gets slightly different logits depending on how many rows share the call. On MI350X at GLM-5.3's indexer shape (32 heads x 128, fp8), a row's logits from calls of up to about 1,500 rows differ from the same row in a 4,096-row call (largest difference 1.5e-5); calls of 2,048 rows and more agree.The differences are small, but the logits feed a top-k, and near-tied keys can be picked differently. Inference engines call this kernel per chunk of rows, and the chunking follows the batch (and, in SGLang, free device memory), so in deterministic inference the same prompt can attend to different keys depending on its batchmates or from run to run. We saw GLM-5.3 give different tokens from run to run this way under memory pressure.
Changes
aiter/ops/triton/attention/fp8_mqa_logits.py: newdeterministic=Falseargument. When True: on gfx950 alwaysblock_m1 with two warps (no 2-row tile, no single-warp variant above 4,096 rows); on the non-Gluon path always the 32-wide MFMA shape. The KV split still follows the shape (it only divides the keys among workgroups and does not change a logit's arithmetic). Other architectures' Gluon path already walks one row per program with a fixed configuration and is unchanged.op_tests/triton_tests/attention/test_fp8_mqa_logits.py:test_fp8_mqa_logits_deterministic_rows.deterministic=False): unchanged.Performance (if applicable)
MI350X, GLM-5.3's indexer shape (32 heads x 128, fp8), median of 20 calls (ms):
The extra time is on large prefill calls, where the default uses the 2-row tile. Only callers that pass
deterministic=Trueare affected.Testing
Documentation
Dependencies
Breaking Changes
None. The new argument defaults to the current behaviour.
PR Self-Review
Reviewed revision
660a4d0
Behavior and intent
With
deterministic=True, a query row's logits no longer depend on the number of rows in the call, so callers that chunk the rows (by batch or by memory) get reproducible logits and top-k picks. Without it, nothing changes.Validation and unverified scope
MI350X (gfx950), ROCm 7.2.4,
rocm/sgl-dev:v0.5.23-rocm724-mi35x-20261030with this file:166 passed (all existing tests, and the 8 cases of
test_fp8_mqa_logits_deterministic_rowson the Gluon and non-Gluon paths). On main the new test fails (nodeterministicargument). With the default configuration, sub-calls of up to about 1,500 rows differ bitwise from a 4,096-row call (6-9 of 14 row ranges at 8k / 32k / 64k keys); withdeterministic=Truenone differ, also against calls of 16,384 (8k keys), 16,383 (32k) and 8,191 rows (64k).End to end (SGLang, GLM-5.3-Flash TP2, deterministic inference,
--mem-fraction-static 0.9, 32,768- / 60,000-token prompts alone twice and among batchmates): with the free-memory chunking left as is, 0 / 6 identical; with fixed chunking plus this configuration, 6 / 6.Unverified: gfx942 and gfx1250 (their Gluon path has no
seq_len-dependent choice; the non-Gluon change is tested on gfx950 by disabling the Gluon kernel), and accuracy beyond the comparison with the default (the test requires a cosine-style difference below 1e-6 against the default; the deterministic configuration is one the default already uses for smaller inputs).PR-specific question
What behavior would each changed op test fail on before this PR, and why is that coverage not already provided by an existing canonical test?
test_fp8_mqa_logits_deterministic_rowsfails before this PR in two ways. On main all 8 cases raiseTypeError, becausefp8_mqa_logitshas nodeterministicargument. Run against the default configuration, the same check fails because calls of up to about 1,500 rows use a different tile and warp count: it recomputes row ranges of an 8,192-row call as separate calls and compares them bitwise, and on MI350X 6-9 of 14 row ranges differ at 8k-64k keys. No existing test covers this. The canonical tests compare each call against a reference with a tolerance, and never compare the same rows across calls with different row counts.Changed op tests
op_tests/triton_tests/attention/test_fp8_mqa_logits.py: addstest_fp8_mqa_logits_deterministic_rows(8k / 32k keys, 32 / 64 heads, Gluon and non-Gluon): withdeterministic=True, 12 row ranges of an 8,192-row call (1 row to 8,185 rows, crossing everyseq_lenthreshold the default uses) are bitwise equal to the same rows of the full call, run to run equal, and as accurate as the default. Not redundant: the existing tests compare against a reference with a tolerance and never compare calls with different row counts.