Skip to content

[ASM] [HIP] [JIT] [Perf] Replace gfx950 MLA v4 prefill 16mx8 kernels with 32mx4 kernels - #7784

Merged
shay-li77 merged 3 commits into
mainfrom
kaiyang-1/mla_v4_32mx4
Nov 12, 2026
Merged

shay-li77 merged 3 commits into
mainfrom
kaiyang-1/mla_v4_32mx4

Conversation

@kaiyang-1

@kaiyang-1 kaiyang-1 commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Summary

For H > 32, gfx950 MLA v4 sparse prefill (mla_sparse_prefill for bf16, mla_sparse_prefill_v4 for fp8 NoPE) now runs two new 32mx4_32nx1 kernels instead of the 16mx8_32nx1 kernels. H ≤ 32 still runs the 16mx1 kernels.

Motivation

Every H = 64 and H = 128 shape ran on the 16mx8 kernels, where each of 8 waves owns 16 heads. In the 32mx4 kernels each of 4 waves owns 32 heads in QK and a 128-wide slice of dv in PV, and P is exchanged through LDS. This keeps the matrix cores busier: in the main loop of the bf16 kernel, MFMA is busy about 92% of the cycles (thread trace on MI355X). Over the shapes below, the new kernels are 1.21x (bf16) and 1.29x (fp8) faster (geomean).

Changes

  • Add mla_v4_prefill_{a16w16,a8w8}_32mx4_32nx1.{hpp,cu} under csrc/kernels/mla/opus/gfx950/ and their code objects in hsa/gfx950/mla_opus/.
    • The kernels use named-register pins, so opus/build_co.sh builds them with the amdgpu-named-reg-pin-std LLVM. The first line of each .cu lists its extra build flags.
    • Each code object has a plain and a split-K kernel.
  • Remove the 16mx8 kernels (mla_v4_prefill_{a16w16,a8w8}_16mx8_32nx1.hpp), their traits and their JIT entries.
  • Add split-K fits for the 32mx4 kernels, used when num_kv_splits is auto.
  • The opus backend of mla_sparse_prefill accepts bf16 only. gfx950 also accepted fp16 before; gfx1250 was already bf16-only.
  • opus_mla_prefill_fwd.cu is grouped by arch, then by MLA version. The gfx950 and gfx1250 entries share their argument validation: one helper for a16w16 and one for a8w8.

Performance (if applicable)

python op_tests/test_mla_sparse_prefill.py --backend opus --no-verify --prec <dtype> with N ∈ {1, 64, 256, 1024, 4096}, H ∈ {64, 128}, topk ∈ {384, 1024, 2048}, total_pages 65536 and auto split: 30 shapes per dtype. Before is the 16mx8 kernels on main, After is this PR. Before and After are mean times over the shapes of each row. Change is the geomean speedup over the same shapes.

Hardware Workload/configuration Before After Change
MI355X bf16, N = 1 20.9 µs 19.9 µs 1.05x
MI355X bf16, N = 64 51.3 µs 43.3 µs 1.21x
MI355X bf16, N = 256 107.9 µs 85.7 µs 1.29x
MI355X bf16, N = 1024 452.5 µs 365.5 µs 1.25x
MI355X bf16, N = 4096 2168.0 µs 1739.2 µs 1.25x
MI355X bf16, all 30 shapes 1.21x (min 1.00x, max 1.66x)
MI355X fp8, N = 1 20.6 µs 18.9 µs 1.09x
MI355X fp8, N = 64 50.6 µs 42.8 µs 1.19x
MI355X fp8, N = 256 109.0 µs 78.0 µs 1.41x
MI355X fp8, N = 1024 456.5 µs 329.4 µs 1.39x
MI355X fp8, N = 4096 2196.1 µs 1583.6 µs 1.40x
MI355X fp8, all 30 shapes 1.29x (min 1.05x, max 1.48x)

Testing

  • Unit tests added or updated
  • Performance benchmarks run
  • Tested on MI250X
  • Tested on MI300X

Documentation

  • Docstrings updated
  • User guide updated
  • Performance guide updated
  • No documentation change is needed, with the reason stated below

Dependencies

  • No new third-party dependencies added
  • New dependencies are listed and justified below

Breaking Changes

On gfx950, the opus backend of mla_sparse_prefill now raises RuntimeError for fp16 inputs. Callers must pass bf16 q, unified_kv and kv. gfx1250 already required bf16.

PR Self-Review

Reviewed revision

dc06a62

Behavior and intent

On gfx950, MLA v4 sparse prefill with H > 32 (bf16 and fp8 NoPE) now launches the prebuilt 32mx4_32nx1 code objects instead of the JIT-compiled 16mx8_32nx1 kernels. The 16mx8 kernels were 1.21x (bf16) and 1.29x (fp8) slower (geomean) on the benchmark shapes above. The opus bf16 entry now rejects fp16, so the a16w16 path has one dtype on both architectures. The opus_mla_prefill_fwd.cu restructure and the shared validation do not change which kernel runs.

Validation and unverified scope

MI355X (gfx950), ROCm 7.2.3, PyTorch 2.10:

  • pytest op_tests/test_mla_sparse_prefill.py: 38 passed.
  • python op_tests/test_mla_sparse_prefill.py -n 1 64 1024 --h_q 16 32 64 128 --prec bf16 v4_fp8 v41_fp8 v41_fp4 --backend opus --num_kv_splits 1 3 auto --no-bench: 576/576 passed.
  • -n 0 with H 16 and 128, bf16 and v4_fp8, splits 1 and 3: passed.
  • CI on this revision: OPUS Tests (MI35X) and Aiter Test Gate passed.
  • Performance: see the table above.

Unverified: the gfx1250 entries, whose validation moved into the shared helpers, are compile-checked only because no gfx1250 hardware was available. MI250X and MI300X are not applicable: the opus prefill runs only on gfx950 and gfx1250. End-to-end model accuracy (for example DeepSeek-V4 under vLLM or ATOM) was not run.

PR-specific question

  • Architecture: only the gfx950 entries (opus_mla_v4_prefill_a16w16_gfx950_fwd, opus_mla_v4_prefill_a8w8_gfx950_fwd) load the new code objects. _opus_fwd picks them from get_gfx_runtime(), and architectures other than gfx950 and gfx1250 raise. gfx1250 dispatch is unchanged.
  • dtype: a16w16 takes bf16 q, unified_kv, kv and out. a8w8 takes fp8 NoPE rows (448 values, 14 E8M0 scales and padding in 512 bytes), bf16 RoPE (64) and bf16 out. Indices and indptr are int32.
  • Shape: D = 512. H > 32 selects 32mx4 (128 heads per workgroup, grid y = ceil(H / 128)); H ≤ 32 keeps 16mx1. The buffer bounds mask a partial last head block (for example H = 96). num_kv_splits > 1 selects the split-K kernel and the reduce. N = 0 returns before any launch.
  • Invariant: every gfx950 and gfx1250 entry builds its kernel arguments through opus_mla_v4_prefill_a16w16_kargs or opus_mla_v4_prefill_a8w8_kargs. These check dtypes, D, shapes, strides and index layout before any launch. The Python wrapper rejects non-bf16 q and num_kv_splits outside 1–32, and the a16w16 kernel has a static_assert that its data type is bf16.

Changed op tests

op_tests/test_mla_sparse_prefill.py: removes the fp16 parametrization of test_opus_csr_boundaries, test_opus_csr_without_sink and test_opus_csr_one_key_scale_one, because the op now rejects fp16. Their bf16 cases are unchanged. No test is added: the existing test_mla_sparse_prefill matrix already runs the new kernels for bf16 and v4_fp8. Its H = 64 case picks 4 auto splits, which exercises the split-K kernel and the reduce. Its H = 96 and H = 128 cases run unsplit, and H = 96 leaves a partial head block.

@kaiyang-1
kaiyang-1 requested a review from a team November 11, 2026 01:09
Copilot AI balanced review requested due to automatic review settings November 11, 2026 01:09
@github-actions

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

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

Extended tests (opt-in via labels):

Label Tests
ci:mi300x Run Aiter standard tests on MI300X in PRs; main branch always runs them
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 Radeon R9700 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 7784 --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.

Reviews:
A reviewer is usually assigned within one working day after the PR is ready for review (see Code Review Process). If nobody has responded for about a week, mention the area contact listed in CONTACTS.md. Want to help review? See REVIEWERS.md.

@aiter-bot

aiter-bot commented Nov 11, 2026 •

Copy link
Copy Markdown

WAITING ON AUTHOR

PR Self-Review

Revision 17d97bb662de · 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. Which architecture, dtype and shape boundaries select the changed kernel path, and what invariant prevents unsupported inputs from reaching it?
  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 — Reviewed revision must contain at least the first 7 characters of the current head SHA

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

Copilot AI 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.

🔵 Needs a closer look

The named-register gfx950 kernels and prebuilt objects require GPU correctness and base-versus-head performance validation unavailable here, while the shared gfx1250 changes are reported as compile-checked only.

No open issues 🎉

What changed in this PR

Updates AITER’s OPUS MLA v4 sparse prefill on gfx950 to use 32mx4_32nx1 kernels for H > 32, while retaining the existing kernels for H ≤ 32.

Changes:

  • Adds BF16 and FP8 kernel implementations with split-K variants and prebuilt-code-object dispatch.
  • Updates automatic split-K fits and consolidates argument validation across gfx950 and gfx1250.
  • Removes FP16 support for unscaled prefill and updates the affected tests and documentation to BF16.
File Description
op_tests/​test_mla_sparse_prefill.py Changes affected unscaled prefill tests to BF16 only.
csrc/​kernels/​mla/​opus/​gfx950/​mla_v4_prefill_traits.h Defines tile, register operand, and shared-memory configuration for the new kernels.
csrc/​kernels/​mla/​opus/​gfx950/​mla_v4_prefill_a8w8_32mx4_32nx1.hpp Implements FP8 prefill with BF16 RoPE and split-K support.
csrc/​kernels/​mla/​opus/​gfx950/​mla_v4_prefill_a8w8_32mx4_32nx1.cu Exports FP8 kernel entry points and records build flags.
csrc/​kernels/​mla/​opus/​gfx950/​mla_v4_prefill_a16w16_32mx4_32nx1.hpp Implements BF16 prefill with split-K support.
csrc/​kernels/​mla/​opus/​gfx950/​mla_v4_prefill_a16w16_32mx4_32nx1.cu Exports BF16 kernel entry points and records build flags.
csrc/​kernels/​mla/​opus/​gfx950/​mla_v4_prefill_a16w16_16mx8_32nx1.hpp Removes the superseded BF16 kernel implementation.
csrc/​kernels/​mla/​opus_mla_prefill_fwd.cu Adds prebuilt-kernel dispatch, shares validation, and groups launchers by architecture and version.
aiter/​ops/​mla_sparse_prefill.py Updates split-K selection and enforces the BF16-only unscaled API.
aiter/​jit/​optCompilerConfig.json Removes obsolete 16mx8 headers from JIT source dependencies.

Review effort: Balanced

@github-actions github-actions Bot changed the title [OPUS] [Perf] Replace gfx950 MLA v4 prefill 16mx8 kernels with 32mx4 kernels [ASM] [HIP] [JIT] [Perf] Replace gfx950 MLA v4 prefill 16mx8 kernels with 32mx4 kernels Nov 11, 2026
@github-actions github-actions Bot added ASM JIT and removed OPUS labels Nov 11, 2026
Copilot AI balanced review requested due to automatic review settings November 11, 2026 01:45

Copilot AI 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.

🟡 Changes recommended

Missing regression tests for fp16 rejection on gfx950 across both H ≤ 32 and H > 32 leave the new BF16-only contract unprotected against regression.

1 open issue

Review effort: Balanced

Comment thread aiter/ops/mla_sparse_prefill.py Resolved
Copilot AI balanced review requested due to automatic review settings November 11, 2026 07:14

Copilot AI 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.

🔵 Needs a closer look

The register-pinned GPU pipelines and prebuilt code objects need AMD-kernel specialist review, as independent GPU validation is unavailable and the gfx1250 validation refactor has only compile evidence.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@shay-li77 shay-li77 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.

LGTM

@shay-li77
shay-li77 merged commit 784cbda into main Nov 12, 2026
@shay-li77
shay-li77 deleted the kaiyang-1/mla_v4_32mx4 branch November 12, 2026 01:54
eppaneamd added a commit that referenced this pull request Nov 13, 2026
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants