Repository navigation
GQA: read a total_sequence_length produced by a device node - #33945
Open
Dante (KafKafrnZ) wants to merge 1 commit into
Open
Dante (KafKafrnZ) wants to merge 1 commit into
Dante (KafKafrnZ) wants to merge 1 commit into
Conversation
A value copied by MemcpyToHost is in host-accessible memory, so its device type is GPU and the helper treated it as unreadable and used 0. That rejected a valid attention_bias, skipped validation and first-prompt detection, and with cuDNN SDPA and a static cache returned wrong output with no error. Use UsesCpuMemory(), as Reshape does. Fixes microsoft#33867 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 change to recognize host-accessible sequence-length tensors.
No open issues 🎉
What changed in this PR
Fixes #33867 by allowing GroupQueryAttention to read total_sequence_length computed by a CUDA node and copied into host-accessible memory, rather than incorrectly treating it as zero.
Changes:
- Uses
UsesCpuMemory()to recognize readable host memory while preserving validation skipping for device-only values. - Adds four CUDA regression tests covering bias presence and static-cache usage, checking output values and present-cache shapes.
| File | Description |
|---|---|
onnxruntime/test/contrib_ops/group_query_attention_op_test.cc |
Adds regression coverage for CUDA-computed sequence lengths. |
onnxruntime/contrib_ops/cpu/bert/group_query_attention_helper.h |
Recognizes host-accessible sequence-length tensors using UsesCpuMemory(). |
Review effort: Balanced
Tianlei Wu (tianleiwu)
approved these changes
Nov 6, 2026
This branch has not been deployed
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.
Problem
On the CUDA EP, GroupQueryAttention reads
total_sequence_lengthas 0 when a CUDA node computes it. Fixes #33867, which reports the first row below. The other two rows are the same bug.attention_biasattention_bias dimension 3 must be equal to total_sequence_length, got 8Flash total sequence length must be positive and fit int32For the last row, on an RTX 5070 Ti, where cuDNN SDPA is auto-selected: with Q and K zero, a first prompt returned all zeros where the running mean of V is expected (
got 0, expected 0.125). Over a prefill and 3 decode steps with random inputs, the output differed from a fed total at every step (by up to 2.65), and the present cache differed from the first decode step on (by up to 3.54).This isn't a recent regression. On 1.26.0, before bias support, a computed total ran with no error and returned a present of length 0,
(2, 2, 0, 16).Cause
Input 6 is declared
OrtMemTypeCPUInput, so a value from a CUDA node arrives throughMemcpyToHost, whose output is in host-accessible memory with device type GPU:CheckInputsonly acceptsdevice.Type() == OrtDevice::CPU, so it treats that readable value as unreadable and uses 0. The check came from #26384, for WebGPU values that really are on the device.Fix
Use
device.UsesCpuMemory(), ascuda/tensor/reshape.ccdoes.allocation_planner.cc, theMemCpyToHostcase). With the length alternating between runs, 2000 runs gave 0 errors and 0 wrong outputs. As a control, with that planner condition removed, 1950 of 2000 runs failed on a stale length.seqlens_kis left alone. It has the same check, but it isn't a CPU input on the CUDA kernel, so a computed value really is on the device.Behaviour change
A computed total now gets the same validation and first-prompt detection as a fed one. For example, a later multi-token chunk with batch 2 used to run unvalidated and now gets
batch_size must be 1 when sequence_length > 1 and past context is given., the same as a fed total.Testing
RTX 5070 Ti (sm_120), Windows, CUDA 13.4, cuDNN 9.20, on main
ee5b21dd99.CudaTotalSequenceLengthFromCudaNode*tests (with and without a bias, each with and without a static cache): all fail without the fix and pass with it.GroupQueryAttention*:PagedAttention*183 passed / 37 skipped,MultiHeadAttentionTest*33,AttentionTest*89,CUDA_EP_Unittest.All613 / 8 skipped.Not traced: which step on the cuDNN path produces the zeros. The kernel's debug line shows
EffectiveKvLengthBound=0instead of 8, and the new tokens are written to the cache correctly either way.🤖 Generated with Claude Code