[CK_TILE] FMHA BWD: stream-async workspace prepare for group mode - #3150
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates AITER’s CK_TILE FMHA backward (mha_bwd) “group mode” path to avoid host-blocking, stream-synchronizing D2H copies by switching workspace preparation to a fully stream-ordered async pipeline, enabled by newer Composable Kernel launcher APIs.
Changes:
- Extends
aiter::mha_bwd_argswith apinned_host_alloccallback to provide pinned host staging buffers for the async workspace pipeline. - Switches CK workspace preparation in
aiter::mha_bwdtoprepare_workspace_async(...), removing synchronous seqstart D2H copies in group mode. - Updates PyTorch CK wrappers and the C++ benchmark to provide pinned host allocators and to accommodate launcher/workspace handling changes.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
csrc/include/mha_bwd.h |
Adds pinned_host_alloc API and documents intended fallback behavior. |
csrc/cpp_itfs/mha_bwd.cu |
Uses CK launcher async workspace preparation and removes sync seqstart host staging. |
csrc/py_itfs_ck/mha_bwd_kernels.cu |
Supplies a pinned host allocator via PyTorch pinned-memory tensors. |
csrc/py_itfs_ck/mha_varlen_bwd_kernels.cu |
Same pinned host allocator approach for varlen backward wrapper. |
op_tests/cpp/mha/benchmark_mha_bwd.cpp |
Adds pinned host allocation/free strategy and updates workspace allocation approach. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
DDEle
force-pushed
the
ck-fmha-bwd-async-prepare
branch
from
May 13, 2026 07:30
126b44b to
eb02c9b
Compare
DDEle
added a commit
that referenced
this pull request
May 13, 2026
- mha_bwd.h: pinned_host_alloc doc no longer claims a synchronous D2H fallback (CK launcher now throws when missing). Doc says it's required in group mode and unused in batch mode. - mha_bwd.cu: explicitly check pinned_host_alloc before entering the group-mode async path, matching the existing seqstart_*_ptr precondition check, so callers see a clean AITER_LOG_ERROR rather than a launcher exception. - benchmark_mha_bwd.cpp: remove the dead fmha_bwd_traits local that was left behind when launcher construction moved into aiter::mha_bwd. Triggered -Wunused-variable; the workspace_alloc design comment is kept.
2 tasks
- mha_bwd.h: pinned_host_alloc doc no longer claims a synchronous D2H fallback (CK launcher now throws when missing). Doc says it's required in group mode and unused in batch mode. - mha_bwd.cu: explicitly check pinned_host_alloc before entering the group-mode async path, matching the existing seqstart_*_ptr precondition check, so callers see a clean AITER_LOG_ERROR rather than a launcher exception. - benchmark_mha_bwd.cpp: remove the dead fmha_bwd_traits local that was left behind when launcher construction moved into aiter::mha_bwd. Triggered -Wunused-variable; the workspace_alloc design comment is kept.
DDEle
force-pushed
the
ck-fmha-bwd-async-prepare
branch
from
May 14, 2026 02:24
421ee22 to
b76f334
Compare
DDEle
force-pushed
the
ck-fmha-bwd-async-prepare
branch
from
May 15, 2026 01:37
affd548 to
270fa1e
Compare
valarLip
approved these changes
May 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Adapts AITER to ROCm/rocm-libraries#7331, which exposes a fully stream-async workspace preparation API on
fmha_bwd_launcher.PR #2948 left two synchronous
hipMemcpyD2H copies inaiter::mha_bwd's group-mode path (readingseqstart_q/k_ptrfor launcher construction). These sync copies block the host (~10–30 µs each) and implicitly synchronize the device by draining the user's stream, breaking CPU/GPU overlap on hot training paths.This PR drops the two sync copies and stages the seqstart-dependent workspace metadata via a fully async D2H → host-pack → H2D pipeline on the user's stream.
Technical Details
csrc/include/mha_bwd.h: adds apinned_host_alloccallback tomha_bwd_argsthat returns astd::shared_ptr<void>over a pinned host buffer. The deleter is invoked once the launcher releases the buffer on a stream-tailhipLaunchHostFunc.csrc/cpp_itfs/mha_bwd.cu: drops the two synchipMemcpyD2H copies in the group-mode path. Callslauncher.prepare_workspace_async(...)on the user's stream, which scheduleshipMemsetAsync(zero dq_acc) →hipMemcpyAsyncD2H →hipLaunchHostFunc(host-pack) →hipMemcpyAsyncH2D, all on the same stream.PyTorch wrappers (
csrc/py_itfs_ck/mha_bwd_kernels.cu,mha_varlen_bwd_kernels.cu):pinned_host_alloclambda backed byat::empty(..., pin_memory=true)(CachingHostAllocator). The returnedshared_ptrkeepalive is held until the launcher's stream-tail callback fires, so the buffer cannot be recycled while still in flight.op_tests/cpp/mha/benchmark_mha_bwd.cpp:pinned_host_allocbacked by barehipHostMalloc; release routed throughck_tile::pinned_host_releaserto deferhipHostFreeoff the HIP driver callback thread (which holds runtime locks and would deadlock against concurrent main-threadhipFree).Bumps
composable_kernelsubmodule to1f4cc34e6(PR #7331 tip).Dependencies
ck-fmha-bwd-workspace).Test Plan
On gfx950 inside
rocm/pytorch:latest:python op_tests/test_mha.py -b 2 -n 16 -q 512 -k 512 -d bf16 -d_qk_v 128,128 -gr 1python op_tests/test_mha_varlen.py -b 4 -nh 16 -s 512,512 -dt bf16 -d_qk_v 128,128 -gr 1op_tests/cpp/mha/bwd.exe -prec=bf16 -b=2 -h=4 -h_k=2 -d=128 -s=512 -mode=1 -v=1Test Result
test_mha.py: pass; padding diff 0.0020 < pytorch tol 0.0078test_mha_varlen.py: pass; 4 Group Mode Test variants (mixed, q_only, k_only, no_padding) all within tolerance; dQ/dK/dV CK max diff ≤ pytorch refbwd.exegroup mode:valid:ySubmission Checklist