Skip to content

[CK_TILE] FMHA BWD: stream-async workspace prepare - #183

Merged
DDEle merged 3 commits into
ck_improve_mainfrom
yiding12/fmha-bwd-async-prepare
May 19, 2026
Merged

[CK_TILE] FMHA BWD: stream-async workspace prepare#183
DDEle merged 3 commits into
ck_improve_mainfrom
yiding12/fmha-bwd-async-prepare

Conversation

@DDEle

@DDEle DDEle commented May 13, 2026

Copy link
Copy Markdown
Collaborator

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() D2H
sync that PR #182 had to perform on every group-mode bwd call.

Changes

  • csrc/composable_kernel: bump submodule to mono-split/users/yiding12/fmha-bwd-async-prepare HEAD.
  • csrc/flash_attn_ck/mha_bwd.cpp / mha_varlen_bwd.cpp:
    • 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.
    • 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 (group mode): 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.

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.py on gfx950 with
    rocm/pytorch:rocm7.1.1_ubuntu24.04_py3.12_pytorch_release_2.9.1:
    260680 passed, 152076 skipped, 0 failed (22:43)

@DDEle

DDEle commented May 13, 2026

Copy link
Copy Markdown
Collaborator Author

Base automatically changed from yiding12/fmha-bwd-workspace to ck_improve_main May 14, 2026 01:58
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
DDEle force-pushed the yiding12/fmha-bwd-async-prepare branch from af2e667 to e38b717 Compare May 14, 2026 02:38
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).
@DDEle
DDEle requested a review from rocking5566 May 19, 2026 02:29
@DDEle
DDEle marked this pull request as ready for review May 19, 2026 02:33
@rocking5566
rocking5566 requested a review from Copilot May 19, 2026 08:19

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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() to launcher.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 device cu_seqlens pointers 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_async uses stream obtained earlier via getCurrent*Stream() before the CUDAGuard is set to q.device(). If the caller’s current device differs from q.device(), this can enqueue the async workspace prep on a stream belonging to the wrong device. Consider moving at::cuda::CUDAGuard device_guard{q.device()}; before retrieving the current stream (and then fetch stream after the guard) so the prep and subsequent launcher.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_async is passed stream that is captured before at::cuda::CUDAGuard device_guard{q.device()}; is constructed. If the current device at entry differs from q.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.

Comment thread csrc/flash_attn_ck/mha_varlen_bwd.cpp Outdated
Comment thread csrc/flash_attn_ck/mha_bwd.cpp Outdated

@rocking5566 rocking5566 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@DDEle
DDEle merged commit de0071c into ck_improve_main May 19, 2026
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants