Skip to content

[Triton/Gluon] Take sparse MLA's native fp8 dtype from get_fp8_e4m3_dtype() - #6955

Draft
jin-amd wants to merge 11 commits into
ROCm:mainfrom
jin-amd:sparse-mla-fp8-native-dtype
Draft

jin-amd wants to merge 11 commits into
ROCm:mainfrom
jin-amd:sparse-mla-fp8-native-dtype

Conversation

@jin-amd

@jin-amd jin-amd commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

Follow-up to #6199, from @azaidy's review there: take the native fp8 dtype of the flat sparse MLA cache from get_fp8_e4m3_dtype().

Rebased onto #6199's current head (f5d031a4), so this branch carries its commits too. The change here is the last commit only; I will rebase onto main once #6199 is in.

Technical details

_classify_flat worked out the arch's e4m3 dtype from FNUZ_ARCHS. It now calls get_fp8_e4m3_dtype(), which other Triton ops and the sparse MLA tests already use. Both give e4m3fnuz on gfx942 and OCP e4m3 on gfx950, so nothing changes on the supported archs. FNUZ_ARCHS still picks the encoding the kernel decodes.

Test plan

op_tests/triton_tests/attention/test_sparse_mla.py on gfx942 (MI325X, rocm/pytorch rocm7.2.4 image, triton 3.8.0): 83 passed, 69 skipped. The same counts on the f5d031a4 base this sits on, and on the branch merged into main, so the change moves nothing.

On gfx942 both expressions give torch.float8_e4m3fnuz. gfx950 is not run here; there get_fp8_dtypes() returns the OCP type and FNUZ_ARCHS excludes gfx950, so both give torch.float8_e4m3fn.

@github-actions

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

  • ✅ Pre-checks (submodule verification, code formatting)
  • ✅ Aiter op tests (gfx942 + gfx950)
  • ✅ Triton tests on MI35X (only when aiter/ops/triton/** or related paths are changed)

Extended tests (opt-in via labels):

Label Tests
ci:gfx1250-ffm-triton Run the five-shard gfx1250 FFM Triton test suite
ci:triton-300x Run an additional Triton test job on MI300X in PRs (added automatically when gfx942 configs change); main branch always runs both MI35X and MI300X
ci:triton-355 Run the full Triton test suite on MI35X, not only the tests the change affects
ci:windows Windows JIT op smoke test on the gfx1201 ROCm runner
ci:multi-gpu Aiter multi-GPU tests on the 8-GPU runner
ci:sglang SGLang integration tests: DeepSeek-R1-MXFP4 accuracy, Qwen 3.5 accuracy
ci:atom ATOM benchmark: DeepSeek-R1-0528, GPT-OSS-120B
ci:atom_full ATOM accuracy suite for PR and main models from ATOM models_accuracy.json
ci:vllm vLLM benchmark: DeepSeek-V4-Pro, MiniMax-M3-MXFP4
ci:all All standard extended tests (excludes ci:atom_full)

Only add ci:atom_full for FlyDSL or Triton upgrades.
Add labels via the sidebar or gh pr edit 6955 --add-label <label>

One backend per PR:
A PR changes one kernel backend: [Triton/Gluon] (Triton and Gluon count as one), [HIP], [ASM], [CK], [OPUS] or [FlyDSL]. If the title ends up with two backend tags, split the PR -- as stacked pull requests when one part cannot merge without the other.

PR title tags & labels:
Component tags ([Triton/Gluon], [HIP], [CK], [ASM], ...) are added to the PR title and as PR labels automatically from the changed files and re-synced on every push — change-type tags like [fix]/[Perf], op tags like [MLA], and human labels (ci:*) are left untouched. Add the no-auto-title label to stop the title rewrites; labels stay in sync either way.

jin-amd and others added 11 commits November 11, 2026 20:46
The kernel decoded every fp8 byte as OCP e4m3, so gfx942, whose native fp8 (and
vLLM's fp8 KV cache) is e4m3fnuz, took bf16 caches only. Add an FP8_FNUZ
constexpr that picks the fp8 element type the dequant helpers read, and set it
from the arch in the wrapper. gfx942 now takes the per-tensor fp8_scalar cache
under bf16 dots; fp8 q, fp8 dots and fp8_dsv32_mla stay gfx950-only.

A typed cache in the other arch's encoding is refused instead of misread; a
uint8 view is taken to be the arch's own fp8.

gfx942 (MI325X), GLM-5.3-Flash shape (16 heads, rope-free 512, top-k 2048),
fp8 vs bf16 cache: decode 31.0 vs 33.2 us at 2 tokens, 33.1 vs 35.5 us at 16;
16K-token prefill 9.34 vs 10.54 ms. test_sparse_mla.py: 61 passed, 13 skipped
(fp8 dots, fp8_dsv32_mla).
dot_precision="fp8" was gfx950-only because the kernel fed the matrix core OCP
e4m3. The fp8 staging, the in-kernel Q quantization (now to the arch's fp8
range, 240 on fnuz) and P now use the arch's fp8 type, and the MFMA layouts take
version 3 for fnuz operands, which only have CDNA3 intrinsics.

P is quantized as p * 128 (exact; p <= 1 stays under 240) and the epilogue
divides it out with the V-side scale. Unscaled, softmax tails below fnuz's
smallest subnormal flush to zero: with one key scoring 9 above 2047 others that
carry a fifth of the output, rel-L2 error is 14.5% unscaled and 1.7% scaled
(split-K off). gfx950 keeps its numerics (P scale 1, OCP, version 4).

gfx942 publishes a _sparse_mla_fp8 config at BLOCK_K 64: one-byte tiles fit
twice the bf16 tile's rows. The LDS model takes the element size, and matches
the compiled kernels (bf16 34304/34816/38912/39424 B, fp8 34816/39936 B).

MI325X, GLM-5.3-Flash shape (16 heads, rope-free 512, top-k 2048), fp8 cache
under fp8 vs bf16 dots, and the bf16 cache: decode at 2 tokens 23.7 / 30.7 /
33.2 us, at 16 tokens 25.4 / 32.7 / 37.4 us; 16K-token prefill 5.78 / 9.35 /
10.55 ms. BLOCK_K 32 and 8 warps were slower.

Rel-L2 vs f32 attention over the same fp8 cache, fp8 dots: 3.4% on N(0,1)
inputs, 4.5% on peaky ones (bf16 dots: 0.2-0.3%).
test_sparse_mla.py on gfx942: 77 passed, 1 skipped (fp8_dsv32_mla).
…ench fp8 dots on gfx942

_sparse_mla_repr left out FP8_MFMA and FP8_FNUZ, so bf16 and fp8 dots over the
same fp8_scalar cache got the same trace name whenever they ran at the same
BLOCK_K, which gfx950 does for prefill-sized grids and with the async path off.
Add both keys.

bench_sparse_mla.py gated its fp8-dot series on FP8_ARCHS and pre-quantized Q,
so gfx942 skipped the path with a stale note that the kernel reads OCP e4m3.
Gate on FP8_SCALAR_ARCHS, pre-quantize Q only on FP8_ARCHS (gfx942 quantizes
bf16 Q inside the kernel), and count Q at its own element size in the
bandwidth metric.

MI325X decode at 1/8/64 sequences (16 heads, context 8192, top-k 2048): bf16
dots 39.5/43.6/103.8 us, fp8 dots 25.0/27.0/72.3 us. test_sparse_mla.py: 77
passed, 1 skipped.
… softmax tail

_classify_flat took any one-byte dtype other than the other arch's e4m3, and
the kernel then read the bytes as this arch's e4m3, so a typed e5m2 (or int8,
bool) cache was decoded with the wrong encoding. Accept only the native e4m3
dtype or a uint8 view of it, as the sparse_mla_fwd docstring says.

The fp8 accuracy cases use mild random inputs, where every p stays near 1, and
they all pass with P_SCALE forced to 1. test_fp8_dots_keep_the_softmax_tail
gives each query a first key scoring 9 above 2047 others that carry a fifth of
the output at p ~ 1e-4, with split-K off. On MI325X the fp8-dot output is 1.8%
off its f32 reference (max-rel); with P_SCALE = 1 the tail's share comes out
zero and the error is 100%. It runs on gfx942, the arch with a P_SCALE.

test_sparse_mla.py on gfx942: 82 passed, 1 skipped.
…dle series

An fp8_scalar cache plus extra_kv skipped the gfx950-only two-loop check and
launched the gluon kernel. Reject every extra_kv outside PACKED_ARCHS, and
test that through sparse_mla_fwd.

The benchmark's fp8 series was the fp8 cache with fp8 dots, so the fp8 cache
with bf16 dots could not be reproduced. Add that series.

The softmax-tail test skipped gfx950, where P_SCALE stays 1 and a p ~ 1e-4
tail flushes in OCP e4m3 too. Run the same case there and expect the flush,
and note on the MFMA version line that a false FP8_FNUZ is the old gfx950
kernel.
…pool

The rejection test built a 2-D fp8 cache. Dropping the arch gate would still
raise "the two-loop needs a 3-D block cache", which matched "two-loop", so
the test passed without the gate. View both segments as a 3-D block pool and
match the arch error instead.
test_fp8_dots_keep_the_softmax_tail drew its inputs from a CPU generator
into tensors of the default device and dtype, then moved them with .cuda().
CI's triton shard 6 also collects test_gather_kv_b_proj.py, which imports
attention/test_mla.py, and that module's torch.set_default_device("cuda")
holds for the whole pytest process. torch.randn then made a CUDA tensor from
the CPU generator and raised "Expected a 'cuda' device type for generator
but found 'cpu'" before any assert, on MI300X and MI35X alike. main has
since dropped that call (ROCm#6783), but the test should not depend on it.

Draw from a CUDA generator and make every tensor on the GPU with an
explicit dtype, as main's op_tests/triton_tests/README.md asks. The case is
unchanged: each query's first key scores 9 above the other 2047.

MI325X, CI's rocm/pytorch rocm7.2.4 image and triton 3.8.0: the fp8-dot
output is 1.5% off its f32 reference (max-rel), and 94% with P_SCALE forced
to 1, so the test still fails without the scale. Shard 6 replayed in one
process: the test failed before and passes now (3381 passed, 2595 skipped).
test_sparse_mla.py: 83 passed, 1 skipped, also with the default device set
to cuda and the default dtype to bf16.

Not run on gfx950. Flushing every tail p, which is what P_SCALE 1 does
there, puts this input 100% off in OCP e4m3; the same estimate gives the 94%
measured on gfx942. The gfx950 branch asserts more than 50%.
main's ROCm#7429 added gfx950's custom fp8 kernel and two tests for it,
test_sparse_mla_fp8_kernel and test_sparse_mla_fp8_two_caches. Both ask
for fp8 dots on a tensor-scale cache, which only gfx950 accepted when
they were written.

This branch makes gfx942 accept that pair, so after the rebase the two
tests stop skipping on gfx942. They test the gfx950 kernel, and they
pass fp8 q and a two-loop. sparse_mla_fwd refuses both on gfx942, so
each case raises ValueError instead of running.

Hold the two tests on FP8_ARCHS with _skip_unless_fp8_kernel(). gfx942's
fp8 dots keep their coverage in the matrix above and in
test_fp8_dots_keep_the_softmax_tail.

Not run: no gfx942 or gfx950 hardware available for this rebase.
main's ROCm#7429 added a custom fp8 kernel for gfx950. This test's geometry
goes to it: a flat fp8_scalar cache, 16 heads, 512-wide rows, no rope
and no epilogue pass every gate in _sparse_mla_fwd_gfx950_fp8, and the
flat-cache early return does not fire on a contiguous pool. The gfx950
case then measured a kernel that this PR does not change.

Pass has_invalid=True. The dispatch keeps an index stream that can carry
sentinels on the generic kernel, so both archs run the kernel this PR
changes, and the gfx950 case tests the OCP casts and a P scale of 1
again. Every index here is valid, so the masked path reads the same rows
and the result does not move.

Not run: no gfx942 or gfx950 hardware available.

Co-authored-by: Jin Tao <[email protected]>
…rule

aiter/ops/triton/README.md rule 18 asks for one or two sentences that
say what the code does, or why. Eight comments and docstrings that this
PR adds or expands went past that.

Cut each to the rule: the arch restriction and the fp8_scalar exception
on PACKED_ARCHS, the config-selection rule in _get_config, the reason
_check_packed_arch runs early, the bypass condition on the extra_kv
gate, the MFMA version reason, the benchmark's arch gate, and the two
test docstrings. No code changed.

Co-authored-by: Jin Tao <[email protected]>
…type()

_classify_flat worked out the arch's e4m3 dtype itself, from FNUZ_ARCHS.
Use get_fp8_e4m3_dtype(), which other Triton ops and the sparse MLA tests
already use, as asked in the ROCm#6199 review. It gives the same dtype on both
supported archs: e4m3fnuz on gfx942 and OCP e4m3 on gfx950. FNUZ_ARCHS
still picks the encoding the kernel decodes.

test_sparse_mla.py on gfx942 (MI325X, rocm/pytorch rocm7.2.4 image, triton
3.8.0): 83 passed, 69 skipped, the same counts on the ROCm#6199 base this sits
on and on the branch merged into main.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants