Skip to content

[FlyDSL] [gfx1250] FMHA prefill m32x8: skip query tiles past a varlen sequence's end - #7098

Merged
coderfeli merged 3 commits into
mainfrom
vgokhale/fmha-m32x8-skip-empty-q-tiles
Nov 10, 2026
Merged

coderfeli merged 3 commits into
mainfrom
vgokhale/fmha-m32x8-skip-empty-q-tiles

Conversation

@vgokhale

Copy link
Copy Markdown
Contributor

Problem

The varlen kernel sets grid.x from max_seqlen_q. Each sequence in the batch gets the same number of tiles as the longest sequence.

A short sequence has tiles that start after its last query row. The kernel does not stop these tiles. Each of these tiles reads all the KV of its sequence. The masks remove all the results, and the tile writes no output.

Prefix-cached agentic prefill has this problem. One batch can have tool-result turns of 1 to 16 tokens and turns of 4,000 tokens. For these batches, the kernel launches 5.7 to 8.6 times the necessary tiles.

Fix

A tile that starts after the last packed query row of its sequence does not enter the core. The kernel already skips the core in the same location for an empty sequence. All waves of the workgroup get the same result, so they skip together.

first_row = _m_tile_idx() * fx.Int32(_block_m(NUM_Q_TILES))
tile_has_rows = first_row < q_len * fx.Int32(GQA_RATIO)
if (q_len > fx.Int32(0)) & (kv_len > fx.Int32(0)) & tile_has_rows:

Results

gfx1250, bf16, 16 query heads, 2 KV heads, head dim 256, one call. A1 and A2 are two prefill steps from a Qwen3.5 MLPerf agentic run (c=2048, DP attention).

Case Tiles launched / needed Before (ms) After (ms) Output
A1: 20 seqs, 1 to 4,640 new tokens over 1k to 38k 5.7 21.80 3.61 bit-identical
A2: 30 seqs, 2 to 4,704 new tokens over 1k to 46k 8.6 39.55 5.28 bit-identical
A1, non-causal, with LSE 5.7 22.65 3.89 bit-identical, LSE too
A1 seqs, equal lengths 1.0 4.03 3.84 bit-identical
2 seqs × 8,192 new tokens 1.0 2.18 2.11 bit-identical
4 query / 1 KV heads, 7 to 1,040 new tokens 2.2 1.58 1.52 bit-identical
1 seq, 1 token 1.0 0.37 0.36 bit-identical
16, 32, 2,048, 2,047 new tokens 2.0 1.60 1.27 bit-identical, LSE too

op_tests/test_mha_flydsl.py -l thd -dq bf16 -k flydsl: 256 of 256 cases pass, with two new skewed varlen shapes.

… sequence's end

The varlen launch sizes grid.x from max_seqlen_q, so every sequence of the
batch gets the longest sequence's tile count. The entry only kept an empty
sequence out of the core (q_len == 0 or kv_len == 0). A shorter sequence's
trailing tiles, which own no packed query row, went through the full core:
`_wg_kv_span` clamps their last row to the sequence's last query, so each
streamed the whole KV band with every row masked and stored nothing.

Prefix-cached agentic prefill batches mix 1-16 token turns (tool results)
with turns of thousands of tokens, and launched 5.7-8.6x the tiles they
need. Such a tile now skips the core like an empty sequence does; the
condition is uniform across the workgroup's waves.

Outputs and LSE are bit-identical to the current kernel. Adds two skewed
varlen shapes to op_tests/test_mha_flydsl.py.
@vgokhale
vgokhale requested a review from a team October 28, 2026 15:36
@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 7098 --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.

vgokhale added a commit that referenced this pull request Oct 28, 2026
… sequence's end (#7098 on the Qwen branch)

The varlen launch sizes grid.x from max_seqlen_q, so every sequence of the
batch gets the longest sequence's tile count. The entry only kept an empty
sequence out of the core (q_len == 0 or kv_len == 0). A shorter sequence's
trailing tiles, which own no packed query row, went through the full core:
`_wg_kv_span` clamps their last row to the sequence's last query, so each
streamed the whole KV band with every row masked and stored nothing.

Prefix-cached agentic prefill batches mix 1-16 token turns (tool results)
with turns of thousands of tokens, and launched 5.7-8.6x the tiles they
need. Such a tile now skips the core like an empty sequence does; the
condition is uniform across the workgroup's waves.

Outputs and LSE are bit-identical to the current kernel. Adds two skewed
varlen shapes to op_tests/test_mha_flydsl.py on main (#7098); this branch
version carries the kernel change only.
@zufayu
zufayu requested review from coderfeli and yadaish October 29, 2026 01:02
@zufayu
zufayu removed the request for review from yadaish October 29, 2026 10:16
ruanjm

This comment was marked as outdated.

@aiter-bot

aiter-bot commented Nov 5, 2026 •

Copy link
Copy Markdown

WAITING ON AUTHOR

PR Self-Review

Revision c97ba472700d · mode shadow

Waiting for the PR author. The readiness report will appear after the self-review answers are complete for this revision.

Questions to answer in the PR description

  1. What behavior does this PR change, and why is the existing behavior insufficient?
  2. What did you validate, and what remains unverified? Include commands, hardware/configuration and results.
  3. 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?
  4. For every changed op_tests/ file, what behavior does it cover and why is it not redundant? Write None if no op test changed.

Edit the PR Self-Review section in the PR description. Include the current revision SHA; this comment refreshes automatically.

Missing or invalid answers

  • FAIL — missing ### Reviewed revision in the PR description
  • FAIL — missing ### Behavior and intent in the PR description
  • FAIL — missing ### Validation and unverified scope in the PR description
  • FAIL — missing ### PR-specific question in the PR description
  • FAIL — missing ### Changed op tests in the PR description
  • FAIL — Reviewed revision must contain at least the first 7 characters of the current head SHA
  • FAIL — Behavior and intent has no substantive answer
  • FAIL — Validation and unverified scope has no substantive answer
  • FAIL — PR-specific question has no substantive answer
  • FAIL — changed op test not explained in Changed op tests: op_tests/test_mha_flydsl.py
  • FAIL — Changed op tests must explain why each changed op test is necessary and non-redundant

Shadow mode reports what would block but keeps the status successful. Set AITER_PR_SELF_REVIEW_MODE=enforce only after reviewing real false positives.

coderfeli
coderfeli previously approved these changes Nov 5, 2026

@ruanjm ruanjm left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a similar function to the a8w8 varlen kernel. The rest LGTM.

@ruanjm
ruanjm dismissed coderfeli’s stale review November 5, 2026 02:56

Addtional requirement to PR owner.

@vgokhale
vgokhale requested review from coderfeli and ruanjm November 9, 2026 16:13
…arlen sequence's end

The a8w8 varlen launch sizes grid.x from max_seqlen_q, as the a16w16 one does.
A shorter sequence's trailing tiles own no packed query row, but they streamed
the whole KV band with every row masked and stored nothing. Such a tile now
skips the core, as in the a16w16 kernel.

In both kernels, a tile with no row also skips the kv_len == 0 zero fill.
Before, it ran the zero fill with every store masked off.

Outputs and LSE are bit-identical to the current kernels (fp8 and bf16, causal
and not, 6 shapes). gfx1250, 16 q / 2 kv heads, d 256, fp8, causal, median of
100 launches:
- 15 x 8-token turns + 1 x 4096, ~20k-token prefixes: 5538.6 -> 623.5 us
- 8 turns of 1-2048 tokens, 2k-90k prefixes: 2566.3 -> 605.2 us
- 4 x 2048 (no skew): 77.8 -> 78.4 us

Adds a skewed varlen shape with seqlen_k == 0 to op_tests/test_mha_flydsl.py.
@vgokhale
vgokhale force-pushed the vgokhale/fmha-m32x8-skip-empty-q-tiles branch from e89add1 to c97ba47 Compare November 9, 2026 16:28

@coderfeli coderfeli left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@coderfeli
coderfeli merged commit 0793327 into main Nov 10, 2026
@coderfeli
coderfeli deleted the vgokhale/fmha-m32x8-skip-empty-q-tiles branch November 10, 2026 02:49
ruanjm pushed a commit that referenced this pull request Nov 10, 2026
… sequence's end (#7098)

* [FlyDSL] [gfx1250] FMHA prefill m32x8: skip query tiles past a varlen sequence's end

The varlen launch sizes grid.x from max_seqlen_q, so every sequence of the
batch gets the longest sequence's tile count. The entry only kept an empty
sequence out of the core (q_len == 0 or kv_len == 0). A shorter sequence's
trailing tiles, which own no packed query row, went through the full core:
`_wg_kv_span` clamps their last row to the sequence's last query, so each
streamed the whole KV band with every row masked and stored nothing.

Prefix-cached agentic prefill batches mix 1-16 token turns (tool results)
with turns of thousands of tokens, and launched 5.7-8.6x the tiles they
need. Such a tile now skips the core like an empty sequence does; the
condition is uniform across the workgroup's waves.

Outputs and LSE are bit-identical to the current kernel. Adds two skewed
varlen shapes to op_tests/test_mha_flydsl.py.

* [FlyDSL] [gfx1250] FMHA prefill a8w8 m32x8: skip query tiles past a varlen sequence's end

The a8w8 varlen launch sizes grid.x from max_seqlen_q, as the a16w16 one does.
A shorter sequence's trailing tiles own no packed query row, but they streamed
the whole KV band with every row masked and stored nothing. Such a tile now
skips the core, as in the a16w16 kernel.

In both kernels, a tile with no row also skips the kv_len == 0 zero fill.
Before, it ran the zero fill with every store masked off.

Outputs and LSE are bit-identical to the current kernels (fp8 and bf16, causal
and not, 6 shapes). gfx1250, 16 q / 2 kv heads, d 256, fp8, causal, median of
100 launches:
- 15 x 8-token turns + 1 x 4096, ~20k-token prefixes: 5538.6 -> 623.5 us
- 8 turns of 1-2048 tokens, 2k-90k prefixes: 2566.3 -> 605.2 us
- 4 x 2048 (no skew): 77.8 -> 78.4 us

Adds a skewed varlen shape with seqlen_k == 0 to op_tests/test_mha_flydsl.py.
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.

4 participants