Skip to content

Validate CUDA FMHA seqstart/seqlen tensor values - #31641

Open
apsonawane wants to merge 3 commits into
mainfrom
msrc/fmha-seqstart-bounds-fix
Open

Validate CUDA FMHA seqstart/seqlen tensor values#31641
apsonawane wants to merge 3 commits into
mainfrom
msrc/fmha-seqstart-bounds-fix

Conversation

@apsonawane

Copy link
Copy Markdown
Contributor

This pull request adds validation logic for attention mask and sequence length inputs in CUDA attention kernels, improving input robustness and error reporting. It introduces host-side checks to catch invalid sequence length and start values before launching GPU kernels, and expands test coverage to ensure these checks work as intended.

Input Validation Enhancements:

Test Coverage:

These changes improve error detection for invalid input data, making debugging easier and preventing undefined behavior in CUDA attention operations.

- validate MASK_1D_KEY_SEQ_LEN_START contents before memory-efficient attention launch

- validate GQA seqlens_k bounds before right-padding FMHA path

- add CUDA MultiHeadAttention regression for invalid seqstart values
@apsonawane
apsonawane requested review from tianleiwu and a lite review from Copilot August 4, 2026 21:54

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 adds host-side validation for CUDA attention inputs (mask seqstart/seqlen and GQA seqlens) to fail fast with clearer errors before launching fused/MEM-efficient attention kernels, and adds a regression test to ensure invalid seqstart values are rejected.

Changes:

  • Added host-side validation for MASK_1D_KEY_SEQ_LEN_START mask contents in CUDA memory-efficient attention (MultiHeadAttention path).
  • Added host-side validation for GQA seqlens_k values prior to launching CUDA memory-efficient attention.
  • Added a CUDA-only unit test that asserts invalid seqstart_q values are rejected with an error.

Reviewed changes

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

File Description
onnxruntime/contrib_ops/cuda/bert/attention_impl.cu Adds host-side validation for 1D mask (seqlen + cumulative start arrays) before MEA kernel launch.
onnxruntime/contrib_ops/cuda/bert/group_query_attention_impl.cu Adds host-side validation for GQA seqlens_k values and wires it into the MEA path.
onnxruntime/test/contrib_ops/multihead_attention_op_test.cc Adds a CUDA test that expects failure for invalid seqstart_q in the 1D mask encoding.

Comment thread onnxruntime/contrib_ops/cuda/bert/attention_impl.cu
Comment thread onnxruntime/contrib_ops/cuda/bert/group_query_attention_impl.cu
@apsonawane
apsonawane enabled auto-merge (squash) August 5, 2026 17:49
apsonawane and others added 2 commits August 5, 2026 10:49
Gate the new host-side seqstart/seqlen validation behind a shared
ORT_CUDA_ATTENTION_VALIDATE_SEQ_LENS environment variable so the release path
avoids per-call device-to-host copies and stream synchronization in the hot
attention execution path.

Also switch the validation copies to explicit cudaMemcpyDeviceToHost and update
the CUDA tests to enable the validation env var when they expect failures.

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.

2 participants