Skip to content

Require shared KV buffer for GQA cuDNN SDPA auto-selection - #34111

Open
bmehta001 wants to merge 2 commits into
mainfrom
bmehta001/gqa-cudnn-sdpa-auto-shared-kv
Open

bmehta001 wants to merge 2 commits into
mainfrom
bmehta001/gqa-cudnn-sdpa-auto-shared-kv

Conversation

@bmehta001

Copy link
Copy Markdown
Contributor

Description

On SM>=90, GroupQueryAttention auto-selects cuDNN SDPA. cuDNN receives seqlen_present_kv_cache (the present KV capacity) as its KV sequence length. When past_present_share_buffer is false, that capacity grows by one on every decode step, so the cuDNN graph/plan is rebuilt for every generated token.

This change only auto-selects cuDNN when the past/present buffer is shared (fixed capacity). Explicit cuDNN requests via sdpa_kernel / ORT_SDPA_KERNEL are still honored. The GQA workspace estimator still treats cuDNN as reachable, which remains a conservative upper bound.

Motivation and Context

Ministral-3-3B (fp16 GQA, past_present_share_buffer=false) decoded at ~3 tok/s on H100. Session profiling showed GQA at ~315 ms of a ~320 ms decode step, constant regardless of KV length; ORT_ENABLE_ATTENTION_KERNEL_DEBUG_INFO showed SdpaKernel=CUDNN_FLASH_ATTENTION.

Validation (H100, CUDA 12.8, cuDNN 9.8, CMAKE_CUDA_ARCHITECTURES=90)

Single GQA node (32 Q heads, 8 KV heads, head size 128, fp16, past 512, unshared KV), median over 60 decode steps:

Selection ms/step
Default with this change (Flash) 0.60
Forced cuDNN (sdpa_kernel=8, old auto behavior) 319.5
Forced Flash (sdpa_kernel=1) 0.83

Max abs diff, default vs cuDNN outputs: 2.4e-4.

End-to-end Ministral-3-3B on H100 with sdpa_kernel=1 (equivalent to this change): ~3 -> ~250 tok/s decode; warm multi-turn image TTFT ~490 ms -> 30-68 ms; identical greedy token counts.

On SM>=90, GroupQueryAttention auto-prefers cuDNN SDPA. cuDNN is given the
present KV capacity as its sequence length. When past and present are not
shared, that capacity grows by one every decode step, so cuDNN builds a new
execution plan for every generated token. With Ministral-3-3B (fp16 GQA,
past_present_share_buffer=false) on H100 this made each GQA call ~12 ms and
decode ~3 tok/s; Flash attention gives ~250 tok/s on the same model.

Only auto-select cuDNN when the KV buffer is shared (fixed capacity). An
explicit cuDNN request through sdpa_kernel / ORT_SDPA_KERNEL is still
honored. The workspace estimator keeps treating cuDNN as reachable, which
remains a conservative upper bound.

Files changed:
- onnxruntime/contrib_ops/cuda/bert/group_query_attention.cc

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 458edacb-429b-4169-99fb-c2241f5113d4
Copilot AI balanced review requested due to automatic review settings November 11, 2026 17:44

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.

🟡 Changes recommended

Missing regression tests for SM90+ cuDNN selection with shared buffers, unshared buffers, and explicit selection leave guard removal and recurring per-token plan building undetected.

1 open issue
What changed in this PR

This PR limits automatic cuDNN SDPA selection for CUDA GroupQueryAttention to shared KV buffers, avoiding per-token execution-plan rebuilding during decode with growing, unshared caches.

Changes:

  • Requires shared past/present KV buffers for automatic cuDNN selection on SM90+.
  • Preserves explicit cuDNN requests through sdpa_kernel or ORT_SDPA_KERNEL.
  • Documents the performance rationale for the new selection guard.
File Description
onnxruntime/​contrib_ops/​cuda/​bert/​group_query_attention.cc Adds the shared-buffer requirement to automatic cuDNN selection while preserving explicit requests.

Review effort: Balanced

@bmehta001 bmehta001 self-assigned this Nov 11, 2026
Copilot comment #4541338598: cover SM90 shared/unshared auto-selection and
explicit selection with unshared buffers. The production call uses
IsGQACudnnSdpaEnabled at group_query_attention.cc:803; its definition is in
group_query_attention_eligibility.h:127.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: f390d3f0-4e55-45ca-a858-bdc339efc3e3

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.

🟢 Approval recommended

No active review issues block the change limiting automatic cuDNN selection to shared KV buffers.

1 open issue

Review effort: Balanced

This branch has not been deployed

No deployments
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.

2 participants