Skip to content

feat(rocke): free-stride forward attention for arbitrary Q/K/V/O layouts - #13946

Closed
arai713 wants to merge 4 commits into
developfrom
users/arai/attn_free_stride
Closed

arai713 wants to merge 4 commits into
developfrom
users/arai/attn_free_stride

Conversation

@arai713

@arai713 arai713 commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

JIRA ID: AICK-2618

Motivation

hipDNN needs attention forward to read Q/K/V from arbitrary memory layouts, not one fixed contiguous order. The production forward kernel (fmha_mfma) only served the BSHD-contiguous case: it folded the batch axis in as batch_idx * seqlen_q rows, which is expressible only when the batch stride is a whole number of token strides. BHSD, general B/H/S strides and packed QKV could not be served without a host-side repack. This generalizes fmha_mfma so one kernel reads any layout.

Technical Details

  • Add an opt-in free_stride: bool = False field to FmhaMfmaSpec (defaulted, so existing descriptors / AOT packs hydrate to the current behavior).
  • When set, the kernel declares four per-tensor batch strides and rebases the Q/K/V/O base pointers to each CTA's batch slice (global_ptr_add(ptr, batch_idx * stride_*_batch * elem_bytes)). The shared body then addresses head and token through the runtime strides it already takes. Batch, head and token offsets are all runtime, so BSHD, BHSD, general strides and packed QKV all build the same kernel with nothing baked.
  • The default (free_stride=False) path is left verbatim and is byte-identical: the field defaults off, the ABI (15 params) and kernel name are unchanged, and only the free-stride path appends the four batch strides (19 params) and tags the name _fs.
  • The free-stride path is Python-only for now and is not added to the byte-identity parity fixtures
  • New host helper _sdpa_fwd_strides.contiguous_strides single-sources the (batch, head, token) element strides the launcher passes for the bhsd/bshd orders. The earlier standalone free-stride scaffold (fmha_sdpa_fwd) is retired, now subsumed by fmha_mfma.

Test Plan

  • CPU ABI contract test (test_fmha_mfma_free_stride.py): default ABI unchanged (15 params, no batch strides), free_stride appends exactly the four batch strides (19), name tagging, builds for gfx942/gfx950 and GQA.
  • Host layout contract (test_sdpa_free_stride_layouts.py): every layout (BHSD/BSHD/padded/packed) recovers the same values and attention output via the kernel's scalar address formula; contiguous_strides matches numpy.
  • On-GPU numeric parity (test_fmha_mfma_free_stride_numeric.py, CDNA-gated): fmha_mfma(free_stride=True) matches the numpy reference for BSHD and BHSD.
  • Default-path byte-identity: lowered LLVM IR compared before/after for gfx950 and gfx942, none and causal.
  • Byte-identity gate green at every flavor (tools/check_byte_identity.py).

Test Result

  • Default-path LLVM IR is byte-identical to the pre-change kernel for gfx950 and gfx942 across the none and causal masks (the C++ twin and golden are untouched).
  • On-GPU numeric parity passes on gfx942, gfx950 for BSHD and BHSD (GQA, within the attention parity tolerance), confirming the per-batch pointer rebase reads and writes every layout correctly.
  • CPU ABI, host-contract and layering suites pass.

Submission Checklist

@therock-pr-bot

therock-pr-bot Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

✅ All Checks Passed — Ready for Review

Check Status Details
📝 PR Description ✅ Pass —
⛔ Forbidden Files ✅ Pass —
🧪 Unit Test ✅ Pass —
🔎 pre-commit ✅ Pass —
🚫 Draft PR 🔜 To Be Enabled —
🚩 Feature Flag 🔜 To Be Enabled —
📊 Code Coverage 🔜 To Be Enabled —
🤖 therock-pr-bot ✅ Pass —

🎉 All checks passed! This PR is ready for review.

📖 Need help? See the Policy FAQ for details on every check and how to fix failures.

🙋 Wish to Override Policy?

@therock-pr-bot

therock-pr-bot Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

🎉 All checks passed! This PR is ready for review.

@arai713 arai713 changed the title Users/arai/attn free stride feat(rocke): free-stride forward attention for arbitrary Q/K/V/O layouts Oct 21, 2026
@arai713
arai713 marked this pull request as ready for review October 21, 2026 16:14
@arai713
arai713 requested a review from a team as a code owner October 21, 2026 16:14
@arai713
arai713 force-pushed the users/arai/attn_free_stride branch from cd39c1d to f9b8478 Compare October 26, 2026 17:46
Add fmha_sdpa_fwd, the net-new MFMA-body layout shell (hipDNN
requirement 2), reusing mfma_attention_fwd_inner_body for the attention
math and adding only the addressing layer. This scaffold covers the
contiguous BHSD dense case: batch is threaded through the grid z-axis
plus the inner body's K/V offset hooks, with Q/O batch folded into the
tile base. BSHD, general strides, and packed QKV are declared on the
spec but gated off in is_valid_spec as Phase 3. CPU-only build/smoke
tests cover the validator gates, ABI signature, grid, and that a valid
spec lowers to a KernelDef.
Make fmha_sdpa_fwd read Q/K/V/O from any memory layout by rebasing each
tensor's base pointer to the CTA's batch slice (batch_idx * stride_*_batch)
and letting the shared body address head/token through its runtime strides.
Batch, head and token offsets are all runtime, so BSHD, BHSD, general B/H/S
strides and packed QKV build the same kernel, with nothing baked and the
contiguous Q/O fold removed.

Add _sdpa_fwd_strides.contiguous_strides as the launcher's single source of
truth for the (batch, head, token) element strides of the bhsd/bshd orders,
plus host tests: the layout contract and the updated validator/build smoke tests.
Add an opt-in free_stride mode that rebases Q/K/V/O base pointers per batch
and declares per-tensor batch strides, so one kernel reads any layout
(BSHD/BHSD/general/packed) instead of the BSHD-contiguous seqlen fold.The
free-stride path is Python-only for now. Retire the fmha_sdpa_fwd scaffold,
add a CPU ABI test and a GPU verify harness.
Extract the verify harness core into a reusable run_verify() and add a
CDNA-gated pytest (skips off gfx942/gfx950) that checks fmha_mfma(free_stride=
True) against the numpy reference for BSHD and BHSD, confirming layout
invariance of the per-batch pointer rebase.
@arai713
arai713 force-pushed the users/arai/attn_free_stride branch from f9b8478 to 0bdfdac Compare October 27, 2026 17:53
@arai713

arai713 commented Oct 28, 2026

Copy link
Copy Markdown
Contributor Author

Moved to PR 13953

@arai713 arai713 closed this Oct 28, 2026
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.

1 participant