FMHA Backward SBHD LSE/D Fix
When to Use
Use this skill when working on backward-v3 FMHA sp3 kernels and any of these are true:
SBHDfails whileBSHDorBHSDpasses.QGrad Incorrect results,KGrad Incorrect results, orVGrad Incorrect resultsappear only for one layout family.D/O*dOchecks pass, but the main backward outputs fail.- You are editing files under
poc_kl/mi300/fmha_bwd_asm/shaders/. - You need to port the same class of fix to
gfx950.
Core Rule
If the failure is layout-specific and D passes, suspect a side-buffer address bug first:
Lse/Dbasedq_acc/ 32-bitdQtemporary base- sometimes
dK/dVside-buffer descriptors
Do not assume the visible sp3 file is the active runtime source. Always map the live source family first.
Workflow
Map the active runtime source family.
- Inspect
aiter/csrc/cpp_itfs/mha_bwd.cu. - Inspect
aiter/hsa/gfx942/fmha_v3_bwd/fmha_bwd_dqdkdv.csv. - Inspect integration logs such as
poc_kl/mi300/fmha_bwd_asm/scripts/poc.log. - Current
gfx942note:D128 32mx1is skipped byauto_integration.py; live D128 runtime kernels come from the16mx1_48nx4family.
- Inspect
Reproduce a single failing case and compare layouts.
- Run one failing
SBHDcase and matchingBSHDorBHSDcontrols. - Prefer
-kname=1 -v=1.
- Run one failing
Stage-split the failure.
- Add
-v3_dump_args=1 -v3_check_d=1. - If
Dpasses and gradients fail, the bug is in the maindqdkdvshader path.
- Add
Audit the shader math.
- Search for comments like:
LseD always in bhs layoutdQ always in bhsd layoutwhen dQ in 32bits, its offset is Hdim*s_LseD_base
- Look for any batch term derived from
s_BAsfor side buffers.
- Search for comments like:
Pick the fix style.
- Use the ABI-based fix when SGPR headroom exists.
- Use the SBHD heuristic when SGPRs are tight.
- If
dQderives fromHdim * s_LseD_base, fixings_LseD_baseis enough. - If
dQhas its owns_BAs-based batch term, patch that too.
Rebuild from the real source path.
- Use
auto_integration.pyonly for supportedGen/Genlflows. - For direct kernels, rebuild from the actual direct source file, not from a similarly named but skipped pipeline.
- Use
Reverify.
- Run the focused regression script.
- Run the broad runtime-mapped verifier.
- Re-check one
SBHDcase against aBSHDcontrol before concluding.
Fix Decision Table
Pattern A: ABI-Based Lse/D Base Fix
Use when the kernel has spare SGPRs and the packed ABI exposes the needed fields.
Target formula:
LseD_base = batch_id * nhead_q * Hs_lsed + head_id * Hs_lsed
This is the cleanest fix because it is layout-independent.
Pattern B: SGPR-Safe Heuristic
Use when there is no SGPR room for new nhead_q / Hs_lsed aliases.
Detect SBHD with:
s_BAs < s_Seqs
Then rebuild the BHS batch term from the batch stride instead of using the non-SBHD shortcut.
Pattern C: dQ Already Uses Hdim * s_LseD_base
If the shader comment or code shows that 32-bit dQ offset is derived from Hdim * s_LseD_base, only fix s_LseD_base.
Pattern D: dQ Has Its Own s_BAs Batch Term
If the shader separately computes s_dQ_base from s_BAs, apply the same SBHD branching logic there.
High-Risk Gotchas
- Do not patch
mi300_sp3_to_asmunless that path is the real build source. - Do not assume
32mx1is active just because it looks closer to the loaded kernel name. auto_integration.pymay replace many.cofiles in one run.SWA,group, andcausal_brmay map to different source families than plain / causal direct kernels.- A one-off
SWAfailure can be flaky; rerun the exact case before patching. - For
gfx950, repeat the source-family mapping step instead of copying thegfx942assumptions.
Files to Inspect First
aiter/csrc/cpp_itfs/mha_bwd.cuaiter/hsa/gfx942/fmha_v3_bwd/fmha_bwd_dqdkdv.csvpoc_kl/mi300/fmha_bwd_asm/scripts/auto_integration.pypoc_kl/mi300/fmha_bwd_asm/scripts/poc.logaiter/op_tests/cpp/mha/verify_a16_sbhd_kernels.shaiter/op_tests/cpp/mha/verify_sbhd_source_kernels.sh
Additional Resources
- For root cause patterns, formulas, rebuild commands, and
gfx950porting notes, read reference.md.