[CK_TILE] FMHA BWD: stream-async workspace prepare - #183
Merged
Conversation
Collaborator
Author
Bump composable_kernel submodule to mono-split/users/yiding12/fmha-bwd- async-prepare HEAD and adapt the FMHA BWD host wrappers to the new async workspace prepare API (CK PR #7331): - Replace launcher.prepare_workspace() with prepare_workspace_async(), which enqueues the full workspace setup (dq_acc zero, group-mode D2H of seqstart, host-side metadata pack via hipLaunchHostFunc, H2D back to device) on the caller's stream. No host-blocking sync remains in the BWD launch path. - Pass a pinned_host_alloc lambda backed by PyTorch's CachingHostAllocator (torch::empty(..., pin_memory=true)). The launcher keeps the returned shared_ptr alive via a stream-tail hipLaunchHostFunc keepalive so the pinned buffer is not recycled while async copies are still in flight. - mha_varlen_bwd: drop the cu_seqlens_q.cpu() / cu_seqlens_k.cpu() host copies; the launcher now reads device seqstart directly via async D2H. get_ck_fmha_varlen_bwd_traits no longer takes seqstart_qs/ks.
DDEle
force-pushed
the
yiding12/fmha-bwd-async-prepare
branch
from
May 14, 2026 02:38
af2e667 to
e38b717
Compare
ROCm/rocm-libraries#7331 (async workspace prepare for FMHA BWD launcher) landed on develop. Move csrc/composable_kernel from the pre-merge fork tip ce838e19e5 to ROCm/composable_kernel develop tip 83566edb0f, which is the split commit for #7331 (rocm-libraries 5692db0).
There was a problem hiding this comment.
Pull request overview
This PR updates the CK_TILE FlashAttention backward (FMHA BWD) host wrappers to use the new stream-asynchronous workspace preparation API, removing the per-call host-blocking D2H sync previously needed in varlen/group-mode backward.
Changes:
- Switch from
launcher.prepare_workspace()tolauncher.prepare_workspace_async()and enqueue workspace preparation work on the caller’s stream. - Add a pinned-host allocator backed by PyTorch pinned memory and pass it into async workspace preparation.
- In varlen/group mode, remove
cu_seqlens_*.cpu()host copies and pass devicecu_seqlenspointers into async workspace preparation.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| csrc/flash_attn_ck/mha_varlen_bwd.cpp | Removes host cu_seqlens copies and uses stream-async workspace prep with a pinned-host allocator. |
| csrc/flash_attn_ck/mha_bwd.cpp | Uses stream-async workspace prep with a pinned-host allocator for non-varlen backward. |
Comments suppressed due to low confidence (2)
csrc/flash_attn_ck/mha_varlen_bwd.cpp:385
prepare_workspace_asyncusesstreamobtained earlier viagetCurrent*Stream()before theCUDAGuardis set toq.device(). If the caller’s current device differs fromq.device(), this can enqueue the async workspace prep on a stream belonging to the wrong device. Consider movingat::cuda::CUDAGuard device_guard{q.device()};before retrieving the current stream (and then fetchstreamafter the guard) so the prep and subsequentlauncher.run(...)use the correct per-device stream.
ck_tile::stream_config prep_cfg{stream};
launcher.prepare_workspace_async(
workspace_ptr,
reinterpret_cast<const int*>(cu_seqlens_q.data_ptr()),
reinterpret_cast<const int*>(cu_seqlens_k.data_ptr()),
prep_cfg,
pinned_host_alloc);
csrc/flash_attn_ck/mha_bwd.cpp:368
prepare_workspace_asyncis passedstreamthat is captured beforeat::cuda::CUDAGuard device_guard{q.device()};is constructed. If the current device at entry differs fromq.device(), the workspace prep can be enqueued on the wrong device’s stream. Move the device guard earlier and retrieve the current stream after the guard so async preparation and the kernel launch run on the intended device/stream.
ck_tile::stream_config prep_cfg{stream};
launcher.prepare_workspace_async(workspace_ptr,
/*seqstart_q_dev=*/nullptr,
/*seqstart_k_dev=*/nullptr,
prep_cfg,
pinned_host_alloc);
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
rocking5566
added a commit
that referenced
this pull request
Jul 16, 2026
…ILab#2675) * Add sink_ptr/d_sink_ptr to fmha_bwd_args to match updated CK submodule Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * update submodule * [CK_TILE] Use Unified Workspace for FMHA BWD (#182) * [CK_TILE] Use Unified Workspace for FMHA BWD Bump composable_kernel submodule to mono-split/users/yiding12/fmha-bwd-workspace HEAD and adapt the FMHA BWD host wrappers to the new unified workspace API: - Replace dq_acc tensor argument with workspace_ptr in get_ck_fmha_bwd_args / get_ck_fmha_varlen_bwd_args - Drop dq_acc strides that have been removed from fmha_bwd_args - In mha_bwd / mha_varlen_bwd, allocate the device workspace based on fmha_bwd_launcher::workspace_size and call launcher.prepare_workspace() - Invoke launcher.run(args, stream_config) instead of fmha_bwd(...) * Update CK pin as ROCm/rocm-libraries#6152 merged * [CK_TILE] FMHA BWD: stream-async workspace prepare (#183) * [CK_TILE] FMHA BWD: stream-async workspace prepare Bump composable_kernel submodule to mono-split/users/yiding12/fmha-bwd- async-prepare HEAD and adapt the FMHA BWD host wrappers to the new async workspace prepare API (CK PR #7331): - Replace launcher.prepare_workspace() with prepare_workspace_async(), which enqueues the full workspace setup (dq_acc zero, group-mode D2H of seqstart, host-side metadata pack via hipLaunchHostFunc, H2D back to device) on the caller's stream. No host-blocking sync remains in the BWD launch path. - Pass a pinned_host_alloc lambda backed by PyTorch's CachingHostAllocator (torch::empty(..., pin_memory=true)). The launcher keeps the returned shared_ptr alive via a stream-tail hipLaunchHostFunc keepalive so the pinned buffer is not recycled while async copies are still in flight. - mha_varlen_bwd: drop the cu_seqlens_q.cpu() / cu_seqlens_k.cpu() host copies; the launcher now reads device seqstart directly via async D2H. get_ck_fmha_varlen_bwd_traits no longer takes seqstart_qs/ks. * [CK_TILE] FMHA BWD: bump CK submodule to develop tip (#7331 merged) ROCm/rocm-libraries#7331 (async workspace prepare for FMHA BWD launcher) landed on develop. Move csrc/composable_kernel from the pre-merge fork tip ce838e19e5 to ROCm/composable_kernel develop tip 83566edb0f, which is the split commit for #7331 (rocm-libraries 5692db0). * [CK_TILE] FMHA BWD: explicit at::kCPU on pinned host TensorOptions * Update CK and enable RDNA backward --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: Yi DING <yi.ding@amd.com> Co-authored-by: Hosang Yoon <hosang.yoon@amd.com>
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.
Adapts the FMHA BWD host wrappers to the new stream-async workspace prepare
API in CK PR #7331, eliminating the host-blocking
cu_seqlens.cpu()D2Hsync that PR #182 had to perform on every group-mode bwd call.
Changes
mono-split/users/yiding12/fmha-bwd-async-prepareHEAD.launcher.prepare_workspace()withprepare_workspace_async(),which enqueues the full workspace setup (dq_acc zero, group-mode D2H of
seqstart, host-side metadata pack via
hipLaunchHostFunc, H2D back todevice) on the caller's stream.
pinned_host_alloclambda backed by PyTorch'sCachingHostAllocator(torch::empty(..., pin_memory=true)). Thelauncher keeps the returned
shared_ptralive via a stream-tailhipLaunchHostFunckeepalive so the pinned buffer is not recycledwhile async copies are still in flight.
cu_seqlens_q.cpu()/cu_seqlens_k.cpu()host copies; the launcher now reads device seqstartdirectly via async D2H.
get_ck_fmha_varlen_bwd_traitsno longer takesseqstart_qs/ks.Equivalence with aiter
This PR is the FA-side parallel of aiter PR ROCm/aiter#3150, just as #182 is
the FA-side parallel of aiter PR ROCm/aiter#2948.
Validation
pytest tests/test_flash_attn_ck.pyon gfx950 withrocm/pytorch:rocm7.1.1_ubuntu24.04_py3.12_pytorch_release_2.9.1:260680 passed, 152076 skipped, 0 failed (22:43)