Repository navigation
[CUDA] Zero the cuDNN SDPA graph cache key before hashing it - #34081
Merged
Tianlei Wu (tianleiwu) merged 1 commit intoNov 10, 2026
Merged
Tianlei Wu (tianleiwu) merged 1 commit into
Tianlei Wu (tianleiwu) merged 1 commit into
Conversation
run() fills GraphParams field by field, but BytesHash hashes all sizeof(GraphParams) bytes. The struct has 9 padding bytes (34-35, 53-55, 60-63 on x64 MSVC and GCC), so two calls with equal fields could hash differently, miss mha_graph_cache and build the same cuDNN graph again. FillPagedGraphParams already zeroes its key for the same reason. Refs microsoft#33995 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues remain for the cache-key initialization change intended to prevent padding-related duplicate graph builds.
No open issues 🎉
What changed in this PR
This PR prevents duplicate cuDNN SDPA graph builds caused by uninitialized padding in ONNX Runtime’s graph-cache key. It does not remove the initial build required for each new sequence length.
Changes:
- Zero-initialize
GraphParamsbefore assigning its fields so byte-wise hashing uses consistent padding. - Add a comment explaining why initialization is necessary.
No actionable findings.
| File | Description |
|---|---|
| onnxruntime/contrib_ops/cuda/bert/cudnn_fmha/cudnn_flash_attention.cc | Zeroes the SDPA cache key and documents the padding-related cache misses. |
Review effort: Balanced
Tianlei Wu (tianleiwu)
approved these changes
Nov 10, 2026
Tianlei Wu (tianleiwu)
enabled auto-merge (squash)
November 10, 2026 16:03
Tianlei Wu (tianleiwu)
merged commit Nov 10, 2026
dbbdc8a
into
microsoft:main
98 of 99 checks passed
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.
Description
cudnn_sdpa::run()fills themha_graph_cachekey (GraphParams) one field at a time, butBytesHashhashes allsizeof(GraphParams)bytes. The padding bytes are never written, so they hold whatever was on the stack. Two calls with equal fields can then hash differently, miss the cache and build the same cuDNN graph again. This change zeroes the struct first, asFillPagedGraphParamsalready does for the paged cache.On x64,
GraphParamsis 64 bytes, and 9 of them are padding: bytes 34-35, 53-55 and 60-63 (printed withoffsetofunder MSVC 19.51 and GCC 13.3). Everycudnn_sdpa::runcaller shares this cache: contrib MHA, contrib GQA and the ONNXAttentionop.Refs #33995. This only removes the duplicate build. It doesn't change the one build per new length that #33995 asks about.
Motivation and Context
john-rocky found this on 1.31.1 (comment on #33424). The Qwen3-0.6B fp16 decoder built the cuDNN graph twice for most new prompt lengths, and the two keys differed only in bytes 34-35. Adding this memset to a v1.31.1 source build brought every length down to one build.
I reproduced the duplicate on the stock 1.31.0 wheel, with a different GPU and cuDNN version, and checked main. Setup: RTX 5070 Ti Laptop GPU (SM 12.0), Windows 11, driver 621.01. The model is Qwen3-0.6B exported with the onnxruntime-genai 0.19.0 builder (
-p fp16 -e cuda), run with the defaultsdpa_kernel, prefill only with no past. It ran twice at 600 tokens, then three times at each other length. Builds are counted asCUDNN_BACKEND_OPERATIONGRAPH_DESCRIPTORfinalizes in the cuDNN API log (CUDNN_LOGLEVEL_DBG=3):The 1.31.0 row matches john-rocky's PyPI 1.31.1 row. My main build doesn't show the duplicate with this model, and a temporary stderr line per cache lookup, added to that build without this change, shows why. All 728 lookups (28 layers × 26 runs) had the same padding:
00 00at 34-35,00 00 00at 53-55 and0b 00 00 00at 60-63. Byte 60 is left over from the stack. In that build it happens to be the same on every call, so the lookups still hit. Whether they hit depends on what earlier calls leave at that address, and that differs between the 1.31.x builds and this one.cudnn_flash_attention.ccin that build is identical to current main.Testing
On the main build above, with this change:
onnxruntime_provider_test:GroupQueryAttention*:PagedAttention*183 passed / 37 skipped,MultiHeadAttentionTest*33 passed,AttentionTest.*89 passed. WithORT_ENABLE_ATTENTION_KERNEL_DEBUG_INFO=1, cuDNN SDPA was picked 21, 31 and 3 times in those runs.I didn't add a unit test. The cache is local to this file, and whether the padding differs depends on the stack, so a test can't reliably trigger the miss through the public API. The paged key's memset has no dedicated test either. I can add one through the CUDA test bridge if you'd like.
#33424 moves this fill into a
FillGraphParamshelper that also zeroes the struct. Whichever of the two merges second will need a one-hunk rebase.About the question on #33995 whether 93e7d33 (#33123) solves the issue: that commit caches Conv execution plans in
nn/conv.ccand doesn't touchcudnn_flash_attention.cc, so it doesn't change this cache.🤖 Generated with Claude Code