Skip to content

Fix CUDA MHA shared-cache scratch lifetime - #31968

Merged
Akshay Sonawane (apsonawane) merged 3 commits into
mainfrom
fix/cuda-mha-seqlens-buffer-lifetime
Aug 13, 2026
Merged

Fix CUDA MHA shared-cache scratch lifetime#31968
Akshay Sonawane (apsonawane) merged 3 commits into
mainfrom
fix/cuda-mha-seqlens-buffer-lifetime

Conversation

@apsonawane

Copy link
Copy Markdown
Contributor

Keep the sequence-length scratch allocation alive through asynchronous attention launches, populate it with correctly typed total lengths, and seed non-aliased shared-cache outputs before in-place append.

Keep the sequence-length scratch allocation alive through asynchronous attention launches, populate it with correctly typed total lengths, and seed non-aliased shared-cache outputs before in-place append.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

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 fixes CUDA MultiHeadAttention behavior when past_present_share_buffer is enabled but outputs are not actually aliased (e.g., in tests), ensuring in-place KV append paths have valid inputs and that scratch buffers remain alive across asynchronous CUDA launches.

Changes:

  • Seed present_{key,value} with past_{key,value} when buffer-sharing kernels append in place but runtime outputs are not aliased.
  • Keep the seqlens_k_total scratch allocation alive until after QkvToContext is invoked, and populate it with correctly typed total sequence lengths.
  • Add a regression test that exercises the non-cache_indirection in-place KV concat path under past/present buffer sharing.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
onnxruntime/contrib_ops/cuda/bert/multihead_attention.cc Fixes scratch-buffer lifetime for seqlens_k_total, corrects its element type/value, and ensures non-aliased present outputs are initialized before in-place append.
onnxruntime/test/contrib_ops/multihead_attention_op_test.cc Adds a regression test covering past/present buffer sharing with in-place KV concat (no cache_indirection).

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread onnxruntime/contrib_ops/cuda/bert/multihead_attention.cc Outdated
Comment thread onnxruntime/contrib_ops/cuda/bert/multihead_attention.cc
Replace the synchronous host-to-device copy of the past-present sequence
lengths with a stream-ordered device fill so the buffer is populated in
order with the attention launches and remains valid during CUDA graph
capture. Also guard the past-to-present cache seeding against missing
tensors and expand the comment explaining why it is needed.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.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.

4 participants