Skip to content

[MHA] Tune MHA forward across every backend on the shared tuning contract - #6580

Draft
amd-bartgips wants to merge 5 commits into
users/bartgips/fmha-single-storefrom
users/bartgips/fmha-tuner-family
Draft

amd-bartgips wants to merge 5 commits into
users/bartgips/fmha-single-storefrom
users/bartgips/fmha-tuner-family

Conversation

@amd-bartgips

@amd-bartgips amd-bartgips commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

op_tests/tuners/tune_mha_fwd.py writes the rows that #5762 reads, and this PR lets those rows name Triton and Gluon. For each shape the tuner times every MHA forward backend as shipped and writes the fastest correct one. It updates the tuned CSV through the existing --compare --update_improved flow, using #5846's dispatch verification and auto-select baseline.

Stack. main → #5762 → #6580 (this) → #5849. #5846 has merged. No kernel changes and no Triton code changes. Four commits: Triton and Gluon rows in dispatch, the tuner, a build-time check that keeps per-model rows out of compiled tables, then the tuner's own gpu_model key.

Triton and Gluon rows (first commit)

  • A row calls Triton with config=None and stores config_digest, a hash of the tile Triton resolved at tuning time. If Triton now resolves another tile, dispatch skips the row with a warning, and the tuner treats it as stale and retunes it.
  • A row is skipped if its key needs what Triton's varlen entry lacks: soft-cap, FP8 inputs, sink tokens, LSE in CK's layout, backward, bias or a right window. It is also skipped where Triton would compute something else than CK: min_seqlen_q (Triton ignores it), a left window of 0 (Triton reads it as no window), and ALiBi or sink logits (no tuner run measures them on Triton).
  • Triton returns a tensor of its own, so when the caller passes out, dispatch copies the result into it: one extra copy per call on a Triton row.

The tuner (second commit)

  • One candidate per backend. Each backend picks its own kernel or tile. There is no tile grid.
  • Only what it can judge. A backend is skipped where it can't serve the shape correctly; on soft-capped shapes only CK is measured. A shape left with no backend is skipped rather than failing the run. Inputs the tuner can't synthesize are refused, such as min_seqlen_q, dropout or gfx1250.
  • Kernel time decides. The candidates and the probe that --compare uses to time a row both go through run_perftest with their inputs as arguments, so both rotate copies of the inputs and neither reads them warm from cache. A row is kept for its device kernel time. Under CUDA/HIP graphs, as in vLLM and SGLang, host-side launch cost disappears and kernel time is what the caller sees.
  • Numerics. Results are checked against a chunked fp32 reference with rtol 0.02, and no element may fall outside it. atol is 0.02 up to 1,024 keys and shrinks as 1/√keys beyond that.
  • Updating the tuned CSV. With --compare --update_improved, a row is kept only if it beats what serves today, auto-select included, by --min_improvement_pct (3% by default), and a fresh process verifies that dispatch selects it.
  • Duplicate shapes. untuned_mha_fwd.csv gives the shared duplicate-shape check its key, which now includes gpu_model when a table has it: gfx and cu_num cannot tell an MI300X row from an MI325X row for the same shape. test_config_shape_collision now checks the MHA table too.

Compiled tables (third commit)

  • The duplicate-shape check above now lets two rows differ only in gpu_model. That is right for a table read at runtime, such as tuned_mha_fwd.csv. The CK GEMM tables, however, are compiled into a C++ lookup keyed on gfx, cu_num and shape only, so the last of two such rows would silently serve both GPUs.
  • filter_tune_df, which every CK GEMM codegen goes through, now raises when a row names a gpu_model. No compiled table ships the column and no CK GEMM tuner writes it, so no build changes today. A row with the cell blank still passes. The check can go once codegen keys on the model.

The tuner's gpu_model key (fourth commit)

Not covered

  • Padded calls (cu_seqlens_*_padded) are not tuned, and [FlyDSL] [CI] [JIT] [MHA] Route MHA forward through a tuned backend row #5762 rejects their rows. The key would need the real token count, which is cu_seqlens_q[-1], a GPU tensor: reading it per call costs a device sync, and in a graph-captured call the value cannot be read at all. q.shape[0] counts padded rows and would map different workloads to one row.
  • Paged KV (block_table) is also excluded: k.shape[0] is the KV pool size, not the sequence length. It needs a different key and will get its own PR.
  • Eager callers. A row chosen on kernel time can be slower in eager mode. Triton's Python launch is the main case: in an earlier run timed end to end, the Triton row below lost to CK (74.5 vs 54.4 µs).

Questions for the Triton owners (@azaidy, @vgokhale)

  • The digest goes through Triton's private _get_config today. #6613 proposes a public get_mha_fwd_config for it. Until then, a change to the private function makes rows be skipped, not wrongly run.
  • A triton row sends a CK build into Triton for that one shape. Is that acceptable?
  • A Gluon row is checked when the table loads, but a kernel that then fails to compile only fails at call time; with Triton 3.6.0 here, Gluon does not compile at all. Should dispatch fall back to auto-select when a Triton or Gluon row raises, or is failing loudly better?

Result (MI355X, 8 shapes, with --compare). Two rows were written, both Triton on hd64, non-causal: 1,024 keys at 40.6 µs vs 51.5 µs from auto-select (21%), and 4,096 keys at 381.9 vs 399.3 µs (4.4%). The other six shapes were within 3% or slower, so auto-select keeps them.

Open: timing robustness. With both sides timed on rotated inputs, the probe and the candidate loop agree within about 1% for every backend except Triton on the 1,024-key shape: 40.6 µs in the probe vs 42.8 µs in the candidates. The us column in the CSV is the candidate time. Run to run, CK has moved more than the 3% gate on one shape (64.1 vs 52.1 µs in earlier runs, same GPU). I plan to follow up on this separately, possibly by adopting the timers from #6715; until then, I would not read 3–5% margins from a single run as real.

Reproduce: python3 op_tests/tuners/tune_mha_fwd.py -i untuned.csv -o tuned.csv --mp 1 --compare --update_improved, with untuned.csv in the format of aiter/configs/untuned_mha_fwd.csv.

Tested. Head 9683e2f42 on #5762 5e2e1e056 (main 1863bb8e4, which includes #5846), on one MI355X:

  • python3 -m unittest op_tests.tuning_tests.{test_mha_tuner_logic,test_mha_fwd_dispatch,test_compare_logic,test_dispatch_verification,test_csv_validation,test_tuner_infra,test_config_shape_collision,test_mp_tuner_logic}: 225 run, 217 passed. 7 were skipped and 1 failed for one reason: main's Gluon MHA kernel ([Triton/Gluon] [GFX950] Productize gluon mha kernel from GFX950-gluon-tutorials #5153) imports warp_pipeline_stage, which this container's Triton 3.6.0 lacks, so aiter.ops.triton.attention.mha does not import here.
  • test_tune_pipeline.TestTunePipeline.test_mha_fwd_mp1 (a real tune of one shape on two backends): passed. test_run_config.TestRunConfig.test_mha_fwd: skipped, because the shipped CSV has no rows for this GPU.
  • The tests for the --run_config fallback, for verification leaving the model to dispatch, and for filter_tune_df refusing model rows fail on the code without them.

The sweep above ran on head d80c2d5cf (main bbef5a6f2), before the rebases.

Sweep (µs, kernel time; best candidate per backend)
Shape asm_v3 CK Triton --compare (auto-select → row)
hd64, 1024, non-causal – 51.7 42.8 51.5 → 40.6, written
hd64, 1024, causal – 41.2 43.5 41.3 → 41.0, skipped
hd64, 4096, non-causal – 399.0 381.8 399.3 → 381.9, written
hd64, 4096, causal – 243.9 255.6 246.5 → 247.5, skipped
hd128, 1024, non-causal 43.4 56.1 87.1 42.6 → 41.6, skipped (2.3%)
hd128, 1024, causal 44.1 49.9 84.4 44.6 → 44.4, skipped
hd128, 4096, non-causal 467.0 570.5 966.1 466.7 → 469.5, skipped
hd128, 4096, causal 270.2 345.1 610.8 270.3 → 270.8, skipped

These were bf16, batch 4, 16 heads; "1024" and "4096" are the keys per sequence. asm_v3 rejects hd64. Opus has no hd64 varlen kernel; its hd128 varlen kernel (#7584, merged after the sweep) is opt-in behind AITER_ENABLE_FMHA_OPUS. FlyDSL declined every shape, and Gluon does not compile with Triton 3.6.0.

Size. 17 files, +1,919 / −19 (about 880 lines of tests). Owners: @yzhou103 (tuning), @valarLip (MHA), @azaidy and @vgokhale (Triton rows).

@amd-bartgips
amd-bartgips force-pushed the users/bartgips/fmha-single-store branch from 5e212c8 to 2057f17 Compare October 15, 2026 13:47
@amd-bartgips
amd-bartgips force-pushed the users/bartgips/fmha-tuner-family branch from 2cfd4e2 to cea9f92 Compare October 15, 2026 13:47
@amd-bartgips
amd-bartgips force-pushed the users/bartgips/fmha-single-store branch from 2057f17 to f0621a0 Compare October 16, 2026 08:01
@amd-bartgips
amd-bartgips force-pushed the users/bartgips/fmha-tuner-family branch from cea9f92 to 41ec951 Compare October 16, 2026 08:01
@amd-bartgips
amd-bartgips force-pushed the users/bartgips/fmha-single-store branch from f0621a0 to 98674fd Compare October 16, 2026 08:03
@amd-bartgips
amd-bartgips force-pushed the users/bartgips/fmha-tuner-family branch from 41ec951 to 8ca1329 Compare October 16, 2026 08:03
@amd-bartgips amd-bartgips changed the title [MHA] Tune MHA forward through the shared tuning contract [MHA] Tune MHA forward across every backend on the shared tuning contract Oct 16, 2026
@amd-bartgips
amd-bartgips force-pushed the users/bartgips/fmha-tuner-family branch from 8ca1329 to 52c49b0 Compare October 16, 2026 12:16
@amd-bartgips
amd-bartgips force-pushed the users/bartgips/fmha-single-store branch from 98674fd to 7ad4275 Compare October 16, 2026 12:16
@amd-bartgips
amd-bartgips force-pushed the users/bartgips/fmha-single-store branch from 7ad4275 to e10cf33 Compare October 26, 2026 08:54
@amd-bartgips
amd-bartgips force-pushed the users/bartgips/fmha-tuner-family branch from af4c60d to dcdf0e5 Compare October 26, 2026 08:54
@amd-bartgips
amd-bartgips force-pushed the users/bartgips/fmha-single-store branch from e10cf33 to eba1d35 Compare October 26, 2026 12:33
@amd-bartgips
amd-bartgips force-pushed the users/bartgips/fmha-tuner-family branch from dcdf0e5 to 543795a Compare October 26, 2026 12:33
@amd-bartgips
amd-bartgips force-pushed the users/bartgips/fmha-single-store branch from eba1d35 to f350402 Compare October 26, 2026 14:42
@amd-bartgips
amd-bartgips force-pushed the users/bartgips/fmha-tuner-family branch 2 times, most recently from cfbfea8 to ca442bd Compare October 26, 2026 16:18
@amd-bartgips
amd-bartgips force-pushed the users/bartgips/fmha-single-store branch from f350402 to 8d4326e Compare October 26, 2026 16:18
@amd-bartgips
amd-bartgips force-pushed the users/bartgips/fmha-tuner-family branch from ca442bd to 4ed3bd0 Compare October 27, 2026 08:29
@amd-bartgips
amd-bartgips force-pushed the users/bartgips/fmha-single-store branch from 8d4326e to 40499c9 Compare October 27, 2026 08:29
@amd-bartgips
amd-bartgips force-pushed the users/bartgips/fmha-tuner-family branch 2 times, most recently from 6c90856 to 353a718 Compare October 27, 2026 10:51
@amd-bartgips
amd-bartgips force-pushed the users/bartgips/fmha-single-store branch 3 times, most recently from 1911985 to 3984c97 Compare October 28, 2026 08:39
@amd-bartgips
amd-bartgips force-pushed the users/bartgips/fmha-tuner-family branch from 353a718 to d80c2d5 Compare October 28, 2026 08:39
@amd-bartgips
amd-bartgips force-pushed the users/bartgips/fmha-single-store branch from 3984c97 to d6e8acd Compare October 28, 2026 14:49
@amd-bartgips
amd-bartgips force-pushed the users/bartgips/fmha-tuner-family branch from d80c2d5 to c204d35 Compare October 28, 2026 14:49
@amd-bartgips
amd-bartgips force-pushed the users/bartgips/fmha-single-store branch from d6e8acd to 3edb3a5 Compare November 3, 2026 16:03
@amd-bartgips
amd-bartgips force-pushed the users/bartgips/fmha-tuner-family branch from c204d35 to 2d30c99 Compare November 3, 2026 16:03
@amd-bartgips
amd-bartgips force-pushed the users/bartgips/fmha-single-store branch 2 times, most recently from 7afdcbc to 4a86817 Compare November 4, 2026 12:18
@amd-bartgips
amd-bartgips force-pushed the users/bartgips/fmha-tuner-family branch 2 times, most recently from ed20d45 to 3af5360 Compare November 9, 2026 15:49
@amd-bartgips
amd-bartgips force-pushed the users/bartgips/fmha-single-store branch from 4a86817 to b39ee86 Compare November 9, 2026 15:49
@amd-bartgips
amd-bartgips force-pushed the users/bartgips/fmha-tuner-family branch from 3af5360 to 6dde747 Compare November 9, 2026 15:59
@amd-bartgips
amd-bartgips force-pushed the users/bartgips/fmha-single-store branch from b39ee86 to 3c4a532 Compare November 9, 2026 15:59
…tile

A triton or gluon row calls Triton's varlen entry with config=None, so
Triton resolves its own tile from its JSON as today, and dispatch copies
the result into the caller's out. The row stores config_digest, a hash of
the tile Triton resolved when it was tuned. At load the hash is recomputed,
and the row is skipped with a warning naming the tuner if Triton now
resolves another tile, if the digest is missing, or if the key needs what
Triton's varlen entry lacks (soft-cap, FP8 inputs, sinks, ...).

The hash goes through Triton's private _get_config until a public resolver
exists; if that changes, the rows are skipped, never run with a stale tile.
op_tests/tuners/tune_mha_fwd.py times every MHA forward backend as shipped
for each shape (one candidate per backend; each picks its own kernel or
tile) and writes the fastest correct one. With --compare --update_improved
a row is kept only when it beats what serves the shape today, including
auto-select, by --min_improvement_pct and a fresh process verifies that
dispatch selects it.

- Backends that cannot serve a shape correctly are skipped (on soft-capped
  shapes only CK is measured); inputs it cannot synthesize are refused. A
  shape left with no backend is skipped rather than failing the run.
- Kernel time through run_perftest on rotated inputs, for the candidates
  and for the probe that --compare and run_config time a row with, so
  neither host-side dispatch, which graph-captured serving does not pay,
  nor inputs left warm in cache decide a row.
- A chunked fp32 reference with rtol 0.02 and no element outside it.
  atol is 0.02 up to 1024 keys and shrinks as 1/sqrt(keys) beyond, or a
  kernel that drops a long sequence's last keys would pass.
- A Triton row whose tile changed is stale: a plain rerun retunes it.

The untuned CSV gives the shared duplicate-shape check its key, which now
includes gpu_model when the table has it: gfx and cu_num cannot tell an
MI300X row from an MI325X row for the same shape.
Tables compiled into C++ are looked up by (gfx, cu_num, shape) only, so
rows tuned on two products that share those (MI300X and MI325X) would
collapse into whichever row comes last. The merge-time duplicate check now
accepts rows that differ only in gpu_model, which is right for tables
dispatched at runtime such as tuned_mha_fwd.csv; the build refuses them
until codegen can key on the model.
The shared tuner no longer keys rows on gpu_model, so the MHA tuner adds it
to its live hardware and stamps its rows with it. --run_config reads the
rows dispatch serves this GPU, by the same fallback: its own model's, else
rows without a model (or a table without the column), else another
model's with the same gfx and cu_num. Verification therefore leaves the
model to the fresh process, whose recorded selection shows whether the row
served. The --run_config filter goes through _rows_for_this_gpu, which the
MHA tuner overrides; for other tuners it still checks gfx and cu_num.

mp_tuner(return_status=True) now returns (results, statuses); tune() joins
them into the records result_to_df reads.
@amd-bartgips
amd-bartgips force-pushed the users/bartgips/fmha-tuner-family branch from 6dde747 to 579db97 Compare November 12, 2026 09:58
@amd-bartgips
amd-bartgips force-pushed the users/bartgips/fmha-single-store branch from 3c4a532 to 4cc8ead Compare November 12, 2026 09:58
amd-bartgips added a commit that referenced this pull request Nov 19, 2026

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.

1 participant