Skip to content

[ASM] [gfx1250] asm mha bf16 fwd: allow D64 without a sink - #7291

Merged
shay-li77 merged 3 commits into
mainfrom
lxx/dev/gfx1250_asm_fwd_d64_nosink
Nov 10, 2026
Merged

shay-li77 merged 3 commits into
mainfrom
lxx/dev/gfx1250_asm_fwd_d64_nosink

Conversation

@shay-li77

@shay-li77 shay-li77 commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Let the gfx1250 BF16 D64 asm forward wrappers (fmha_fwd_with_sink_asm, fmha_fwd_with_sink_varlen_asm) run without a sink. When sink is None and the head dim is 64, the wrappers now pass the kernel a per-head -inf sink. Its softmax term is exp(-inf) = 0, so the result is plain attention.

Motivation

The gfx1250 D64 forward kernels (fmha_bf16_pertokenBf16_hd64_128x256*.co) are built with ENABLE_SINK=1 and always read sink[head]. With sink=None, both wrappers forwarded a null pointer to them, which the D64 binaries dereference (the router comments in mha.py already note this). Callers that want plain attention on the D64 asm path had to build their own sink buffer, and the public routers avoid the issue only by sending D64 to asm when a sink is given.

Changes

  • aiter/ops/mha.py
    • New helper _fmha_fwd_asm_sink(): for D64 with sink is None, returns a [q_head_num] fp32 -inf tensor; otherwise returns sink unchanged.
    • fmha_fwd_with_sink_asm and fmha_fwd_with_sink_varlen_asm call it before launching; docstrings updated.
    • Router comments in _flash_attn_forward / _flash_attn_varlen_forward updated. The routing logic is unchanged: flash_attn_func / flash_attn_varlen_func still send D64 to the asm path only when a sink is given.
  • op_tests/test_fmha_fwd_with_sink_asm.py, op_tests/test_fmha_fwd_with_sink_varlen_asm.py: D64 correctness rows also run without a sink. In the fixed-batch test both candidates (asm through the ops wrapper, public through flash_attn_func) are checked on those rows; the varlen test calls the ops wrapper for them. New D64 cases with more queries than keys (fixed-batch, and varlen with separate q / k offsets) check that causal rows without a key give exactly O = 0 and LSE = -inf.

Impact: only D64 calls with sink=None to the two ops wrappers change, from passing a null sink to a -inf sink. Calls with a sink, D128, D192x128, D256, kernels, launch parameters and routing are unchanged. The -inf tensor is allocated per call, like out / lse, with no cached tensor, so CUDA graph capture behaves as for the existing buffers.

Performance (if applicable)

No kernel or launch change; the only added work is one fill of q_head_num fp32 values per D64 call without a sink. Measured on one shape (fwd only, interleaved windows: 2 s warm-up, 10 x 0.5 s, median; output checked against FP32 on 16 rows x 3 heads):

Hardware Workload/configuration Before After Change
gfx1250 fmha_fwd_with_sink_asm, D64, B 4, S 16000, H 128 (MHA), non-causal, BF16, BSHD. Before: the caller passes a -inf sink (the only way to run the D64 asm kernel without a sink). After: sink=None 30.82 ms (1089 TFLOPS) 30.68 ms (1094 TFLOPS) none (within noise)

For reference, the public routers keep sending D64 without a sink to FlyDSL m32x8 (this PR does not change routing): on the same shape flash_attn_func takes 21.03 ms and flash_attn_varlen_func 20.69 ms (flydsl 0.4.1, ENABLE_CK=0).

Testing

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

Tested on gfx1250 only; the changed code path is gfx1250-only (require_gfx1250_asm). See "Validation and unverified scope".

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

None. D64 calls without a sink, which previously handed the kernel a null pointer, now compute plain attention; every other call behaves as before.

PR Self-Review

Reviewed revision

776e7f9

Behavior and intent

  • Before: fmha_fwd_with_sink_asm(..., sink=None) and fmha_fwd_with_sink_varlen_asm(..., sink=None) with D64 forwarded a null sink pointer to kernels that always read it.
  • After: they pass a -inf sink, i.e. plain attention. The output is bit-identical to passing an explicit -inf sink.
  • Unchanged: calls with a sink, D128 / D192x128 / D256 (their kernels do not read a sink, and the D256 entry rejects one; None is still passed through), and the public routers.

Validation and unverified scope

Hardware: gfx1250, single GPU. aiter via PYTHONPATH, JIT modules rebuilt from the tested revision; torch 2.11. Items 1-5 ran on earlier revisions of this change (same code) with flydsl 0.4.1; items 6-8 with flydsl 0.4.2.

  1. python3 op_tests/test_fmha_fwd_with_sink_varlen_asm.py (all head dims): rc 0; 260/260 checkAllclose passed, including all 20 new D64 no-sink rows.
  2. python3 op_tests/test_fmha_fwd_with_sink_asm.py -d 64 128: the 42 rows before (64, 64, 8, 8192, 8192, 1) passed (84/84 checkAllclose), including all 14 new D64 no-sink rows. That row then aborts with a GPU memory access fault inside the test's torch reference (run_torch, an fp32 einsum over [1, 64, 8192, 8192]), which runs before the kernel. Running the reference alone on those inputs reproduces the fault, while the D64 asm kernel alone on the same tensors (20 launches) completes, so the fault is in the reference and independent of this change.
  3. Direct ops-wrapper check, D64 with/without a sink and D128; fixed-batch (sbhd view, sq 300, sk 2048, B 2, hq 64 / hk 8) and varlen (seqlens 2048 / 512 / 1280 / 256), causal and non-causal: relative L2 ≤ 2.4e-3 against the op tests' torch references. D64 sink=None is bit-identical to an explicit -inf sink.
  4. Large shape for the new path (covers the rows skipped by the abort in 2): D64 without a sink, sq = sk = 8192, B 1, hq 64 / hk 8, bshd and sbhd, causal and non-causal, checked on 16 rows x 3 heads against an fp32 reference: relative L2 ≤ 2.41e-3.
  5. Causal with sq > sk (bottom-right aligned, so the first sq - sk rows see no key), sq/sk 384/256 and 512/128, fixed-batch and varlen: with the -inf sink those rows come out as 0 with no NaN/Inf, the same as with a real sink and as D128. The rows with keys match the torch reference (relative L2 ≤ 2.0e-3). LSE on those rows was not checked here; the op tests now check it (item 8).
  6. Re-run on f6e610b72 (this change stacked on [ASM] [HIP] [gfx1250] asm mha bf16 hd256: batch and varlen forward #7559's head b7aa8fae8), flydsl 0.4.2, torch GEMMs on rocBLAS (TORCH_BLAS_PREFER_HIPBLASLT=0 ROCBLAS_USE_HIPBLASLT=0, which avoids the hipBLASLt fault in the test's reference noted in 2): op_tests/test_fmha_fwd_with_sink_asm.py -d 64: 36 correctness rows, 144/144 checks passed, including all 18 D64 no-sink rows; gate and empty-input tables ran. op_tests/test_fmha_fwd_with_sink_varlen_asm.py -d 64 --dtype bf16: 40 correctness rows passed, including all 20 D64 no-sink rows; gate and perf tables ran. No failures, no faults.
  7. Re-run on 94f0d34735de (rebased onto main 2ec8c26c9 after [ASM] [HIP] [gfx1250] asm mha bf16 hd256: batch and varlen forward #7559 merged as 5c003a5e3; the diff of this change is identical to f6e610b72), same environment as 6, each run under the node's GPU lock with no other process on the GPU:
    • op_tests/test_fmha_fwd_with_sink_asm.py -d 64: 36 correctness rows, 144/144 checks passed (18 D64 no-sink rows).
    • op_tests/test_fmha_fwd_with_sink_varlen_asm.py -d 64 --dtype bf16, run twice back to back: 40 correctness rows, 80/80 checks passed in each run (20 D64 no-sink rows).
    • op_tests/test_fmha_fwd_with_sink_varlen_asm.py -d 128 192 256 --dtype bf16: 456/456 checks passed.
    • op_tests/test_fmha_fwd_with_sink_asm.py -d 128 256: 50 correctness rows, 200/200 checks passed; gate, empty-input and perf tables ran.
  8. This head 776e7f9a374a (adds the D64 cases with more queries than keys from review; mha.py unchanged since 7), same environment, one GPU-lock run:
    • op_tests/test_fmha_fwd_with_sink_asm.py -d 64 256: 304/304 checks passed. The new (64, 64, 8, 300, 256, 2) rows (causal and non-causal, with and without a sink, both candidates) all match the reference, and the exact checks on rows without a key pass, including the existing D256 (1000, 300) rows.
    • op_tests/test_fmha_fwd_with_sink_varlen_asm.py -d 64 --dtype bf16: 88/88 checks passed. test_fmha_fwd_with_sink_varlen_asm_d64_qk, causal: 88 rows without a key per case; O = 0 and LSE = -inf without a sink, LSE = the sink logit with one.
    • Another user's process outside the GPU lock opened the GPU for about 18 s during this run; every check passed.

Unverified:

  • MI250X / MI300X: not applicable (gfx1250-only path).
  • Performance: measured on one shape only (see Performance); other shapes not measured.
  • CUDA graph capture of the new per-call allocation: not tested explicitly (same pattern as out / lse).
  • In the test environment ENABLE_CK=0, so the via="public" rows (D64 with a sink, D128) ran on FlyDSL / Triton rather than asm. The asm D64-with-sink path was covered through the ops wrapper in 3.
  • The rows after the aborted one in 2 (D128 4096 correctness row and the perf table) did not run there; the D64 no-sink large shape is covered by 4.
  • The new no-sink rows were not run against the base revision.
  • Observed while re-running 7, not caused by this change: two attempts overlapped with another user's GPU job that ran outside the GPU lock. In those attempts a small fraction (up to 0.3%) of asm forward outputs O held garbage (NaN, up to 3e35), on D64 varlen rows with and without a sink and on D128 fixed-batch rows (which this change does not touch); LSE stayed correct. The same tests passed on an idle GPU (the results in 7). Cause not investigated here.

PR-specific question

What each changed op test would fail on before this PR, and why existing tests do not cover it: every new case calls an ops wrapper with sink=None for D64. Before this PR that passed a null sink pointer to D64 kernels that always read it, so these cases are expected to fault or return wrong results (not run on the base revision). Existing cases always pass a sink for D64, and flash_attn_func / flash_attn_varlen_func never send D64 without a sink to the asm path, so no existing test calls the D64 asm kernels without a sink. The new sq > sk cases also check the boundary this change creates: on causal rows that see no key, the -inf sink meets a -inf row maximum, and the rows must still give O = 0 and LSE = -inf rather than NaN.

Changed op tests

  • op_tests/test_fmha_fwd_with_sink_asm.py:
    • test_fmha_fwd_with_sink_asm takes a d64_sink argument. D64 correctness shapes run with a sink (as before) and without one; on the no-sink rows the asm candidate goes through the ops wrapper and the public candidate through flash_attn_func, both compared with the torch reference without a sink.
    • New shape (64, 64, 8, 300, 256, 2): causal, the first 44 rows of each batch see no key.
    • For the asm candidate, rows that see no key are checked exactly (O = 0, LSE = -inf), because assertAllclose tolerates up to 5% mismatching elements.
    • Not redundant: the only coverage of the fixed-batch D64 asm kernel with sink=None, and of D64 rows without keys.
  • op_tests/test_fmha_fwd_with_sink_varlen_asm.py:
    • The same d64_sink rows for test_fmha_fwd_with_sink_varlen_asm (equal q and k lengths) through the varlen ops wrapper.
    • New test_fmha_fwd_with_sink_varlen_asm_d64_qk: D64 with separate q and k lengths (q [300, 300], k [256, 256], so the q and k offsets differ), causal and non-causal, with and without a sink, through the ops wrapper; compared with the torch reference, and the rows without a key are checked exactly as above.
    • Not redundant: the only coverage of the varlen D64 asm kernel with sink=None; the existing q > k cases (test_fmha_fwd_with_sink_varlen_asm_qk) cover D192x128 and D256 only.

@shay-li77
shay-li77 requested review from a team and a balanced review from Copilot November 2, 2026 03:18
@github-actions

github-actions Bot commented Nov 2, 2026

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 7291 --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.

@github-actions github-actions Bot added the ASM label Nov 2, 2026
@aiter-bot

aiter-bot commented Nov 2, 2026 •

Copy link
Copy Markdown

WAITING ON AUTHOR

PR Self-Review

Revision e906a17cbc8b · 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 — 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 new -inf sentinel’s numerical correctness and performance impact in precompiled gfx1250 D64 kernels remain unverified because the required GPU tests and benchmarks could not run in this review environment.

No open issues 🎉

What changed in this PR

This PR enables the gfx1250 D64 assembly attention wrappers to handle calls without a sink by supplying a per-head -inf tensor, which contributes zero to the softmax denominator.

Changes:

  • Add shared fallback-sink handling for fixed-length and variable-length wrappers.
  • Extend both op tests to exercise D64 without a sink through the wrappers.
  • Clarify documentation while leaving public dispatcher routing unchanged.
File Description
op_tests/​test_fmha_fwd_with_sink_varlen_asm.py Add variable-length D64 coverage without a sink.
op_tests/​test_fmha_fwd_with_sink_asm.py Add fixed-length D64 coverage without a sink.
aiter/​ops/​mha.py Supply fallback sinks in both wrappers and clarify sink behavior.

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 2, 2026 06:21

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 sink behavior depends on gfx1250 binaries unavailable as source, and numerical and CUDA graph replay results lack an attached head-bound validation report, requiring human confirmation.

No open issues 🎉

Review effort: Balanced

shengnxu
shengnxu previously approved these changes Nov 3, 2026
@amd-ruitang3
amd-ruitang3 removed their request for review November 3, 2026 05:36
@shay-li77
shay-li77 force-pushed the lxx/dev/gfx1250_asm_fwd_d64_nosink branch from 1ee988a to f6e610b Compare November 8, 2026 16:47
Copilot AI balanced review requested due to automatic review settings November 8, 2026 16:47
@github-actions github-actions Bot changed the title [ASM] [gfx1250] asm mha bf16 fwd: allow D64 without a sink [ASM] [HIP] [gfx1250] asm mha bf16 fwd: allow D64 without a sink Nov 8, 2026
@github-actions github-actions Bot added the HIP label Nov 8, 2026

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

The description and validation scope do not cover the patch’s expanded routing, stride handling, gradient paths, and launch options.

1 open issue

Review effort: Balanced

Comment thread aiter/ops/mha.py Resolved
The gfx1250 D64 fwd kernels are built with ENABLE_SINK=1 and always read
the per-head sink, so fmha_fwd_with_sink_asm / fmha_fwd_with_sink_varlen_asm
handed them a null pointer when called without one. The wrappers now pass a
-inf sink in that case: its softmax term exp(-inf) is 0, so the result is
plain attention.

Routing is unchanged: flash_attn_func / flash_attn_varlen_func still send
D64 to the asm path only when the caller gives a sink. The op tests now also
run D64 without a sink through the ops wrappers.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@shay-li77
shay-li77 force-pushed the lxx/dev/gfx1250_asm_fwd_d64_nosink branch from f6e610b to 94f0d34 Compare November 9, 2026 10:43
Copilot AI balanced review requested due to automatic review settings November 9, 2026 10:43
@github-actions github-actions Bot changed the title [ASM] [HIP] [gfx1250] asm mha bf16 fwd: allow D64 without a sink [ASM] [gfx1250] asm mha bf16 fwd: allow D64 without a sink Nov 9, 2026
@github-actions github-actions Bot removed the HIP label Nov 9, 2026

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

The D64 no-sink tests omit fully masked causal rows in both ops wrappers, leaving a potential NaN boundary unverified instead of asserting zero output and LSE=-inf.

2 open issues

Review effort: Balanced

Comment thread aiter/ops/mha.py Resolved
Add D64 cases with more queries than keys, through both ops wrappers,
causal and non-causal, with and without a sink:
- fixed-batch: shape (64, 64, 8, 300, 256, 2);
- varlen: test_fmha_fwd_with_sink_varlen_asm_d64_qk, q [300, 300] and
  k [256, 256], so the q and k offsets differ.

Causal rows that see no key are found by position (bottom-right aligned)
and checked on every element: O = 0, and LSE = -inf without a sink or
the sink logit with one.  assertAllclose tolerates up to 5% mismatches,
so a few NaN rows would otherwise pass.  The fixed-batch public
candidate keeps checking LSE only on rows that see a key, now by
position too: with a sink the reference LSE is finite on every row.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 9, 2026 17:19

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

Correctness of the -inf sink for fully masked causal rows and graph replay depends on prebuilt gfx1250 kernels, but the required hardware validation was unavailable.

1 open issue
1 resolved since last review

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 9, 2026 17:23

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

Human verification is needed because the source-only review could not confirm the precompiled gfx1250 D64 kernels’ handling of the -inf sink sentinel, especially for fully masked causal rows.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@shengnxu
shengnxu self-requested a review November 10, 2026 03:21
@shay-li77
shay-li77 merged commit 6a6539b into main Nov 10, 2026
@shay-li77
shay-li77 deleted the lxx/dev/gfx1250_asm_fwd_d64_nosink branch November 10, 2026 05:28
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.

5 participants