Skip to content

[CK_TILE] Use Unified Workspace for FMHA BWD - #2948

Merged
valarLip merged 7 commits into
mainfrom
ck-fmha-bwd-workspace
May 14, 2026
Merged

[CK_TILE] Use Unified Workspace for FMHA BWD#2948
valarLip merged 7 commits into
mainfrom
ck-fmha-bwd-workspace

Conversation

@DDEle

@DDEle DDEle commented Apr 29, 2026

Copy link
Copy Markdown
Contributor

Motivation

Adapts AITER to ROCm/rocm-libraries#6152 ("CK_TILE Use Unified Workspace for FMHA BWD"), which replaces the per-arg dq_acc_ptr + 4 stride fields in fmha_bwd_args with a single opaque workspace_ptr and exposes fmha_bwd_launcher.workspace_size / prepare_workspace(void*).

Technical Details

Cleans up aiter::mha_bwd_args ABI: drops dq_acc_ptr and the 4 dq_acc strides, replacing them with one callback that serves both dispatch paths:

std::function<void*(size_t bytes, bool zero_init)> workspace_alloc;
  • CK (in aiter::mha_bwd): constructs fmha_bwd_launcher, queries workspace_size, calls workspace_alloc(size, false), forwards the pointer to prepare_workspace + launcher.run. Torch entries no longer construct the launcher themselves; py_itfs_ck/* shrinks (traits + launcher + workspace blocks deleted, varlen also drops its private cu_seqlens D2H copy).
  • ASM v3 (in fmha_v3_bwd): determines dq_accum shape / byte count internally (based on is_group_mode, v3_atomic_fp32, hdim, batch, nhead, seqlen) and calls workspace_alloc(bytes, true). Torch entries no longer allocate or zero dq_accum themselves; py_itfs_cu/asm_mha_*.cu shrinks to a small lambda.

The zero_init flag lets each backing storage pick its efficient zero path (torch::zeros, DeviceMem::SetZero, ...) instead of forcing the dispatch path to know HIP memset semantics. ASM kernels need zero-init because they atomically accumulate into dq_accum; the CK launcher fills its own workspace via prepare_workspace.

Test Plan

On gfx950 inside rocm/pytorch:rocm7.2_ubuntu24.04_py3.12_pytorch_release_2.9.1:

  • python op_tests/test_mha.py
  • python op_tests/test_mha_varlen.py

In addition, we have CK side downstream passed: jenkins / rocm-libraries-folder/Composable Kernel / PR-6152 / #14

Test Result

All cases pass. dQ / dK / dV max diff ≤ 0.008 (fp16 numerical tolerance ~0.01); no AssertionError / Traceback / hipError.

Submission Checklist

DDEle added 3 commits April 27, 2026 21:44
PR #2321 inlined launcher.dq_acc_splits / needs_zero_dq_acc as a
hardcoded `nsplits = ceil(seqlen_k/16)` and unconditional zero, because
the CK branch it bumped to had temporarily removed fmha_bwd_launcher.
The pinned CK now has the launcher back, so restore the #2216 pattern:
construct fmha_bwd_traits + fmha_bwd_launcher and read nsplits and
needs_zero_dq_acc from it.

Functional behavior is unchanged with the current pinned CK; this is a
prep commit to keep the diff for the upcoming #6152 (unified workspace)
adaptation small.

Touched files:
- csrc/py_itfs_ck/mha_bwd_kernels.cu (batch CK entry)
- csrc/py_itfs_ck/mha_varlen_bwd_kernels.cu (group CK entry)
- op_tests/cpp/mha/benchmark_mha_bwd.cpp (benchmark host)
CK PR #6152 replaces fmha_bwd_args.dq_acc_ptr + the four dq_acc_*
strides with a single opaque workspace_ptr, and exposes
fmha_bwd_launcher.workspace_size + prepare_workspace(void*) instead of
dq_acc_splits + needs_zero_dq_acc.

aiter::mha_bwd_args is unchanged (no new fields). The existing
dq_acc_ptr field doubles as the CK workspace pointer for the CK path;
the four dq_acc_* stride fields are kept for the ASM v3 path that
still consumes them. The torch entries (mha_bwd_kernels.cu /
mha_varlen_bwd_kernels.cu / benchmark_mha_bwd.cpp) construct the
launcher, allocate an at::Tensor workspace of launcher.workspace_size
bytes, call launcher.prepare_workspace, and pass workspace.data_ptr()
through dq_acc_ptr.

For group mode, the launcher requires host-side seqstart arrays. The
torch varlen entry copies cu_seqlens_q_padded (or cu_seqlens_q if no
padded variant was provided) via .to(at::kCPU) — using the same
"physical seqstart" convention that the kernel itself indexes dq_acc
with. The aiter::mha_bwd dispatcher does its own hipMemcpy from
a.seqstart_q_ptr / a.seqstart_k_ptr for the CK fallback path. Neither
host buffer crosses the call boundary.

Submodule bumped to 8a59f8afa58 (subtree-split of monorepo
users/yiding12/fmha-bwd-workspace tip, includes the per-nhead
dq_acc stride fix for group mode).
Removes the dual-purpose dq_acc_ptr field and the four dq_acc_* stride
fields from aiter::mha_bwd_args, replacing them with one callback that
serves both dispatch paths:

  std::function<void*(size_t bytes, bool zero_init)> workspace_alloc;

- CK fallback (in aiter::mha_bwd):
  Constructs fmha_bwd_launcher, queries workspace_size, calls
  workspace_alloc(size, zero_init=false), then forwards the pointer to
  launcher.prepare_workspace + launcher.run. The torch entries no
  longer construct fmha_bwd_launcher themselves; py_itfs_ck/* shrinks
  considerably (traits + launcher + workspace blocks deleted, varlen
  also drops its private cu_seqlens D2H copy).

- ASM v3 (in fmha_v3_bwd):
  Determines dq_accum shape and byte count internally (based on
  is_group_mode, v3_atomic_fp32, hdim, batch, nhead, seqlen) and calls
  workspace_alloc(bytes, zero_init=true). The torch entries no longer
  allocate or zero dq_accum themselves; py_itfs_cu/asm_mha_*.cu shrinks
  to a small lambda.

The zero_init flag lets each backing storage pick its efficient zero
path (torch::zeros, DeviceMem::SetZero, ...) instead of forcing the
dispatch path to know HIP memset semantics. ASM kernels need it
because they atomically accumulate into dq_accum; the CK launcher
fills its own workspace and does not need a pre-zero.
@DDEle
DDEle requested review from a team and Copilot April 29, 2026 02:52
@github-actions

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

  • ✅ Pre-checks (submodule verification, code formatting)
  • ✅ Aiter op tests (gfx942 + gfx950)
  • ✅ Triton tests on MI35X (only when aiter/ops/triton/** or related paths are changed)

Extended tests (opt-in via labels):

Label Tests
ci:triton-300x Run an additional Triton test job on MI300X in PRs; main branch always runs both MI35X and MI300X
ci:sglang SGLang integration tests
ci:atom ATOM benchmark (DeepSeek-R1 + GPT-OSS)
ci:vllm vLLM benchmark
ci:all All of the above

Add labels via the sidebar or gh pr edit 2948 --add-label <label>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 refactors FMHA backward (bwd) call sites to use a unified per-call workspace allocator instead of passing an explicit dq_accum buffer and its layout/strides, aligning CK and ASM paths behind a single “workspace” mechanism.

Changes:

  • Add workspace_alloc(bytes, zero_init) callback to mha_bwd_args and thread it through Python interfaces and the C++ dispatcher.
  • Update CK + ASM Python wrappers to allocate a byte workspace tensor on demand (optionally zero-initialized).
  • Update the C++ benchmark harness to pre-allocate a single device buffer sized to satisfy both ASM dq_accum and CK workspace needs.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
csrc/include/mha_bwd.h Adds workspace_alloc callback to mha_bwd_args and removes explicit dq_acc fields/strides.
csrc/cpp_itfs/mha_bwd.cu Uses workspace_alloc for CK launcher workspace and ASM dq_accum allocation; adds host-side seqstart handling for group mode.
csrc/py_itfs_ck/mha_bwd_kernels.cu Switches from dedicated dq_accum tensor to workspace_alloc for CK bwd wrapper.
csrc/py_itfs_ck/mha_varlen_bwd_kernels.cu Same as above for varlen CK bwd wrapper.
csrc/py_itfs_cu/asm_mha_bwd.cu Switches from dedicated dq_accum tensor to workspace_alloc for ASM bwd wrapper.
csrc/py_itfs_cu/asm_mha_varlen_bwd.cu Same as above for varlen ASM bwd wrapper.
op_tests/cpp/mha/benchmark_mha_bwd.cpp Pre-allocates a single device buffer and serves it via workspace_alloc to satisfy both backends.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread csrc/cpp_itfs/mha_bwd.cu Outdated
Comment thread csrc/cpp_itfs/mha_bwd.cu
Comment thread csrc/cpp_itfs/mha_bwd.cu
Comment thread csrc/include/mha_bwd.h
Comment thread op_tests/cpp/mha/benchmark_mha_bwd.cpp Outdated
- mha_bwd CK fallback: explicitly reject group mode with missing seqstart
  pointers (LOG_ERROR + return -1) instead of silently passing nullptr to
  the launcher. Matches the existing AITER_LOG_WARNING pattern in
  fmha_v3_bwd for unsupported configurations.

- benchmark workspace_alloc: replace silent return-nullptr-on-oversize
  with AITER_CHECK so the failure is loud at the actual call site rather
  than as an opaque kernel crash later. Also tighten the zero-init path
  to hipMemset only `bytes` rather than the full pre-allocated buffer.
@DDEle DDEle added the ci:atom label Apr 29, 2026
slippedJim
slippedJim previously approved these changes May 8, 2026
@valarLip
valarLip merged commit 99855d5 into main May 14, 2026
55 of 57 checks passed
@valarLip
valarLip deleted the ck-fmha-bwd-workspace branch May 14, 2026 01:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants