Repository navigation
[HIP] [JIT] gfx1250 mha: fix the CK JIT builds and route the Python backward to asm - #7487
Conversation
* CK's CMakeLists defines USE_NEW_UNIFIED_FRAMEWORK=0 for any build that targets gfx1250, which the unified framework does not support yet. The JIT bypasses that CMake, so ck_tile defaulted the macro to 1: the host pass of every gfx1250 fmha kernel picked MMA types for the HOST target and failed with "'exec' following the 'template' keyword does not refer to a template" and "B distribution is wrong!". * build_module adds -DUSE_NEW_UNIFIED_FRAMEWORK=0 whenever a targeted arch is gfx1250 or gfx1250-strict. This unblocks the mha_varlen_fwd/mha_fwd JIT builds that flash_attn_varlen_func/flash_attn_func fall back to on gfx1250 (e.g. head dim 64 without a sink). Signed-off-by: Jim,Guo <[email protected]> Co-authored-by: Cursor <[email protected]>
* cmdGenFunc_mha_fwd, cmdGenFunc_mha_bwd and cmdGenFunc_mha_varlen_bwd append _ck_targets_flag(), as the varlen forward recipes already do. generate.py defaults to --targets gfx9,gfx950, so on gfx1250 these modules had no gfx125 dispatch branch and failed at runtime with "fmha_bwd: no kernel found for given traits" / "invalid argument for fmha_varlen_bwd", and "invalid argument for fmha_fwd" in batch mode. * No change on gfx9 hosts, where _ck_targets_flag() is empty. Signed-off-by: Jim,Guo <[email protected]> Co-authored-by: Cursor <[email protected]>
…nels * _flash_attn_varlen_backward and _flash_attn_backward send gfx1250 B0+ to fmha_v3_varlen_bwd / fmha_v3_bwd for the shipped kernel set: bf16, hdim 64, 128 and 192x128, no mask or bottom-right causal, no dropout, alibi or sink. The asm modules have no CK fallback, so the gate matches the csv exactly and everything else stays on CK. gfx1250 only ships a32, so atomic32 is forced. * Batch mode no longer prefers CK for seqlen_q <= 16 on gfx1250, which builds no such CK tiles. * asm_mha_bwd.cu passed deterministic=false whatever the caller asked, so a batch-mode deterministic backward ran the non-deterministic kernels. Signed-off-by: Jim,Guo <[email protected]> Co-authored-by: Cursor <[email protected]>
* flash_attn_func and flash_attn_varlen_func return the FlyDSL forward result
directly when FlyDSL handles the config. That tensor carries no autograd
graph, so with flydsl installed a gfx1250 training call could not run
backward ("element 0 of tensors does not require grad").
* Calls that need gradients now skip that path and go through the autograd
functions; no_grad and inference calls are unchanged.
Signed-off-by: Jim,Guo <[email protected]>
Co-authored-by: Cursor <[email protected]>
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: |
NOT READY FOR HUMAN REVIEWPR Self-ReviewRevision Author self-review accepted. Readiness report: Author answers
Show the author's self-review answersBehavior and intentValidation and unverified scopePR-specific questionChanged op testsPR state
Copilot findings
Code and op-test hygiene
Required CI
Shadow mode reports what would block but keeps the status successful. Set |
There was a problem hiding this comment.
🟡 Changes recommended
The gfx1250 gates omit native dQ workspace limits, causing oversized requests to fail instead of using the CK fallback, and use build-target rather than runtime GPU detection.
2 open issues
What changed in this PR
This PR restores gfx1250 flash-attention training by fixing CK JIT compilation and connecting Python backward dispatch to the existing assembly kernels.
Changes:
- Disables CK’s unsupported unified framework for gfx1250 builds and adds missing code-generation target flags.
- Routes supported gfx1250 backward requests to assembly kernels and forwards deterministic mode.
- Bypasses the FlyDSL forward fast path when gradients are needed and updates architecture-related tests.
| File | Description |
|---|---|
op_tests/test_jit_cache_transaction.py |
Stubs the new target helper in isolated build tests. |
op_tests/test_build_targets_architecture.py |
Tests gfx1250 detection across build-target lists. |
csrc/py_itfs_cu/asm_mha_bwd.cu |
Forwards deterministic mode to native backward execution. |
aiter/ops/mha.py |
Updates CK code generation, gfx1250 backward dispatch, and gradient-aware forward routing. |
aiter/jit/core.py |
Adds shared gfx1250 target detection and the CK compatibility build flag. |
Review effort: Balanced


Summary
On gfx1250,
aiter.flash_attn_varlen_functraining could not run (bf16, THD, hdim 64, non-causal,sink=None,cu_seqlens_q == cu_seqlens_k). On current main the CK varlen forward fails to JIT-build, and once it builds, the backward finds no kernel. This PR fixes the CK JIT builds for gfx1250, routes the Python backward to the gfx1250 asm kernels (#6454, #6996, #7160), and fixes two issues found while validating it.Motivation
Reproducer:
flash_attn_varlen_funcwith q/k/v(20000, 2, 64)or(20000, 4, 64)split into two 10000-token sequences, and(20000, 2, 64)split into 10000 ragged sequences (max 5057), then.backward().fmha_bwd: no kernel found for given traits, skipping run/invalid argument for fmha_varlen_bwd.mha_varlen_fwd_bf16_nlogits_nbias_nmask_lse_ndropout_nskip_nqscale:'exec' following the 'template' keyword does not refer to a template,static assertion failed: B distribution is wrong!. hdim 64 withsink=Nonehas no asm forward (the D64 binaries areENABLE_SINK=1and always loadsink[head]), so it takes CK.Causes:
USE_NEW_UNIFIED_FRAMEWORK=0for any build that targets gfx1250, which the unified ck_tile framework does not support yet. The JIT does not go through that CMake, sock_tile/core/arch/arch.hppdefaults the macro to 1, the host pass picks MMA types for the HOST target, and every gfx1250 CK fmha kernel fails to compile. With-DUSE_NEW_UNIFIED_FRAMEWORK=0the failing units compile; with only-DCK_CMAKE_GPU_TARGET_IDS=0x1250they still fail.generate.pydefaults to--targets gfx9,gfx950. The varlen forward recipes append_ck_targets_flag()(ew kernels support on gfx12. #3330), butcmdGenFunc_mha_fwd,cmdGenFunc_mha_bwdandcmdGenFunc_mha_varlen_bwddo not, so their generated API has no gfx125 branch._flash_attn_varlen_backwardand_flash_attn_backwardonly send gfx942/gfx950 tofmha_v3_*_bwd, so the gfx1250 asm backward was reachable only from the C++ API.Changes
aiter/jit/core.py:build_moduleadds-DUSE_NEW_UNIFIED_FRAMEWORK=0when any targeted arch is gfx1250 or gfx1250-strict, as CK's CMakeLists does. It is a whole-TU define so the host and device passes agree; only CK headers read it. The arch test moves into_targets_gfx1250, which_cluster_launch_targetnow calls.aiter/ops/mha.py:cmdGenFunc_mha_fwd,cmdGenFunc_mha_bwdandcmdGenFunc_mha_varlen_bwdappend_ck_targets_flag(), which is empty on gfx9.fmha_v3_varlen_bwd/fmha_v3_bwdfor the shipped kernel set: bf16, hdim 64/64, 128/128 and 192/128, no mask or bottom-right causal, no dropout, alibi or sink. Those modules are built with-DONLY_FAV3=1and have no CK fallback, so the gate matcheshsa/gfx1250/fmha_v3_bwd/*.csvexactly and everything else stays on CK. gfx1250 only ships a32, sois_v3_atomic_fp32is forced to True. Batch mode skips theseqlen_q <= 16CK preference on gfx1250, which builds no such CK tiles.flash_attn_func/flash_attn_varlen_funcno longer take the FlyDSL forward fast path when gradients are needed. That path returns a tensor with no autograd graph; with flydsl installed the reproducer failed withelement 0 of tensors does not require grad and does not have a grad_fn.no_gradcalls are unchanged.csrc/py_itfs_cu/asm_mha_bwd.cu: passdeterministicthrough instead of a hard-codedfalse. On gfx1250 a batch-mode deterministic backward ran the non-deterministic kernels. gfx942/gfx950 are unaffected: Python never sends a deterministic request to their asm path, andfmha_v3_bwdrejects one off gfx1250.op_tests/test_build_targets_architecture.py,op_tests/test_jit_cache_transaction.py: cover_targets_gfx1250and stub it wherebuild_moduleruns in an isolated namespace.Performance (if applicable)
Before this PR the gfx1250 backward did not run. Rough backward times on the reproducer shapes (hdim 64, bf16, non-causal; CUDA events, mean of 20 after 3 warmups, launch overhead included): the asm route this PR selects vs the CK kernels this PR makes build.
Testing
Tested on gfx1250 only; see "Validation and unverified scope". Performance above is rough timing, not a benchmark run.
Documentation
Dispatch and build-flag fixes only; the gfx1250 backward feature list in
op_tests/cpp/mha/README.md(#7160) already describes the kernels the Python path now reaches.Dependencies
Breaking Changes
None.
PR Self-Review
Reviewed revision
1cf7bf18c0f6Behavior and intent
On gfx1250,
flash_attn_varlen_func/flash_attn_functraining could not run: the CK forward JIT build failed (unified ck_tile framework left on), the CK backward module had no gfx125 kernels (no--targets), and the Python backward never reached the gfx1250 asm kernels. After this PR the CK fmha modules build for gfx1250, bf16 backward with hdim 64/128/192x128 (no mask or bottom-right causal, no dropout/alibi/sink) runs on the asm kernels, deterministic included, and the rest falls back to CK with gfx125 kernels. Two more fixes: the batch asm entry passesdeterministicthrough, and the FlyDSL forward fast path is skipped when gradients are needed because its output cannot be differentiated.Validation and unverified scope
Hardware: gfx1250 (MI450 engineering sample), docker
rocm/fw-bringup:gfx1250-atom-20260923(torch 2.11.0+rocm10.1.0, HIP 7.16),ENABLE_CK=1,GPU_ARCHS=gfx1250.--case all --check-accuracy: the three shapes pass forward and backward. WithAITER_LOG_MORE=2, one process per shape, each callsmha_varlen_fwd(CK) andfmha_v3_varlen_bwd, loadingfmha_bwd_hd64_odo_bf16_group,fmha_bwd_hd64_bf16_a32_pssk_groupandfmha_bwd_hd64_dq_convert_bf16_group;mha_varlen_bwdis not called.dout(pass:max|err| / max|ref| < 2e-2for out/dq/dk/dv; observed at most 6.1e-3):_detkernels loaded), asm causal (causal_brkernels), and CK forced by hiding the asm gate. The ragged shape has 2470 zero-length sequences and 9999 of 10000 at most 3 tokens long.flash_attn_func: hdim 64/128 x causal x deterministic at b=2 s=1000 h=4 h_k=2; b=3 s=257 h=4 h_k=1 deterministic; CK forced for hdim 64/128. Deterministic runs are bit-identical.core.pychange, 40 gfx125 units of themha_varlen_fwdmodule failed with the reported errors; after it, the bf16mha_varlen_fwd,mha_fwd,mha_varlen_bwdandmha_bwdmodules built here all build, and their APIs dispatch ongfx125.op_tests/test_build_targets_architecture.py,op_tests/test_jit_cache_transaction.py,op_tests/test_mha_optional_flydsl.py: 92 passed. Black and Ruff clean.Unverified:
deterministic=Trueto their asm path. The FlyDSL change does apply wherever FlyDSL takes a request (gfx950 fp8, gfx1250): grad-requiring calls now go through the autograd functions. That was run only on gfx1250.gfx1250-strict): the asm gates are off there viais_gfx1250_asm_supported(); the CK path was not run on A0.blob_gen_cmdof themodule_mha_*/libmha_*entries inoptCompilerConfig.json(used by prebuilds, not by these Python paths), and the batch_prefill and paged-KV forward codegen, still pass no--targets.PR-specific question
The asm backward is selected only on
get_gfx() == "gfx1250"withis_gfx1250_asm_supported()(B0+; there are nohsa/gfx1250-strictbwd objects). The request must be bf16 with (hdim_q, hdim_v) in {(64, 64), (128, 128), (192, 128)} andnhead_q % nhead_k == 0, with no alibi (batch mode: no bias or dbias either),dropout_p == 0and no sink/d_sink. The mask must be none or bottom-right causal:window_size_left == -1after clamping to the k length, andwindow_size_right == -1unless causal. Any seqlen_q/seqlen_k is accepted, since the kernels are pssk; that includes zero-length sequences in group mode and padded cu_seqlens. Batch mode on gfx1250 also skips theseqlen_q <= 16CK preference.These are exactly the
hsa/gfx1250/fmha_v3_bwd/fmha_bwd_dqdkdv.csvrows (bf16, atomic32=1, pddv=0, mask 0 or 2, batch/group, stock/deterministic), each with matching odo and dq_convert rows.is_v3_atomic_fp32is forced to True on gfx1250, so the atomic32=1 rows always match.The invariant:
module_fmha_v3_bwdandmodule_fmha_v3_varlen_bwdare built with-DONLY_FAV3=1, so a miss inget_heuristic_kernelraises instead of falling back. The Python gate therefore stays a subset of the csv. Head dims that would need padding (pddv=1), fp16, top-left causal in group mode, window masks (mask type 3, includingwindow_size_left == 0), dropout, alibi and sink never reach it; they stay on CK, which now has gfx125 kernels. Insidefmha_v3_bwd, the arch/stepping check and the config lookup still return -1 before any kernel is loaded.On gfx942/gfx950 the new gates are False,
_ck_targets_flag()is empty, and the batch asm entry still receivesdeterministic=Falsefrom Python (fmha_v3_bwdrejects deterministic requests off gfx1250).-DUSE_NEW_UNIFIED_FRAMEWORK=0is added only when a targeted arch is gfx1250 or gfx1250-strict. The FlyDSL change removes the fast path only whentorch.is_grad_enabled()is True and q, k or v requires grad.Changed op tests
op_tests/test_build_targets_architecture.py:test_gfx1250_target_follows_any_targeted_archpins_targets_gfx1250, which now also gates-DUSE_NEW_UNIFIED_FRAMEWORK=0, including multi-archGPU_ARCHS. It overlapstest_cluster_launch_follows_any_targeted_arch, since_cluster_launch_targetcalls the same helper, but states the helper's own contract.op_tests/test_jit_cache_transaction.py:build_moduleruns in a hand-built namespace there, and now calls_targets_gfx1250. The new stub returns False, as for the simulated gfx942 target, next to the existing_cluster_launch_targetstub; without it the test fails withNameError. What the test covers is unchanged.