CK mha bwd: add sink attention score gradient support#2321
Draft
LJ-underdog wants to merge 6 commits intomainfrom
Draft
CK mha bwd: add sink attention score gradient support#2321LJ-underdog wants to merge 6 commits intomainfrom
LJ-underdog wants to merge 6 commits intomainfrom
Conversation
Contributor
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
|
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR extends the CK-backed MHA backward paths (mha_bwd / mha_varlen_bwd) to accept sink attention log-scores and optionally accumulate a sink gradient (d_sink), and adds Python tests to validate d_sink correctness.
Changes:
- Plumbs
sink/d_sinkthrough the Torch C++ interfaces, pybind args, and CK kernel argument structs. - Updates CK kernel launch argument packing to pass sink pointers into backward kernels (batch + varlen).
- Adds new GPU tests for
mha_bwdandmha_varlen_bwdd_sinkaccumulation vs a PyTorch reference.
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
op_tests/test_mha_sink_bwd.py |
New tests validating d_sink accumulation for batch and varlen backward kernels. |
aiter/ops/mha.py |
Updates Python-exposed mha_bwd / mha_varlen_bwd signatures to accept sink / d_sink. |
csrc/include/torch/mha_bwd.h |
Extends the Torch C++ API for mha_bwd to accept sink / d_sink. |
csrc/include/torch/mha_varlen_bwd.h |
Extends the Torch C++ API for mha_varlen_bwd to accept sink / d_sink. |
csrc/include/rocm_ops.hpp |
Adds sink / d_sink parameters to the pybind signatures for backward ops. |
csrc/include/mha_bwd.h |
Extends mha_bwd_args with sink pointer fields. |
csrc/cpp_itfs/mha_bwd.cu |
Passes sink pointers into the CK fmha_bwd_args used by the non-asm path. |
csrc/py_itfs_ck/mha_bwd_kernels.cu |
Adds optional sink/d_sink plumbing to CK batch-mode backward wrapper. |
csrc/py_itfs_ck/mha_varlen_bwd_kernels.cu |
Adds optional sink/d_sink plumbing to CK varlen backward wrapper. |
csrc/py_itfs_cu/fmha_bwd_pre_post_kernel_generate.py |
Updates codegen template to include LSEDataType in pipeline problem typing. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
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
This PR extends the CK-backed MHA backward paths (mha_bwd / mha_varlen_bwd) to accept sink attention log-scores and optionally accumulate a sink gradient (d_sink), and adds Python tests to validate d_sink correctness.
Technical Details
Plumbs sink / d_sink through the Torch C++ interfaces, pybind args, and CK kernel argument structs.
Updates CK kernel launch argument packing to pass sink pointers into backward kernels (batch + varlen).
Adds new GPU tests for mha_bwd and mha_varlen_bwd d_sink accumulation vs a PyTorch reference.
Test Plan
Add test in test_mha_bwd&varlen_bwd.py
Test Result
Local test passed
Submission Checklist