Skip to content

Fix CUDA ONNX Attention: min_bias_align crash on SM<80 and MEA NaN for fully-masked batches#27831

Merged
titaiwangms merged 1 commit into
mainfrom
titaiwang/fix_cuda
Apr 4, 2026
Merged

Fix CUDA ONNX Attention: min_bias_align crash on SM<80 and MEA NaN for fully-masked batches#27831
titaiwangms merged 1 commit into
mainfrom
titaiwang/fix_cuda

Conversation

@titaiwangms

@titaiwangms titaiwangms commented Mar 24, 2026

Copy link
Copy Markdown
Contributor

Description:

Summary

Fixes three issues in the CUDA ONNX Attention operator and improves spec compliance:

  1. min_bias_align crash on SM<80: The alignment check for Memory Efficient Attention (MEA) bias used 4*sizeof(T) (bytes), but the check is against element counts. Fixed to 4 elements, matching CUTLASS kMinimumAlignment. This prevented valid MEA dispatch on SM<80.

  2. MEA NaN for fully-masked batches: When nonpad_kv_seqlen=0, CUTLASS MEA computes 1/s_prime where s_prime=0, producing NaN. Added ZeroOutputForFullyMaskedBatches kernel (MEA path only) to zero output for these batches. Uses int64_t for element count to prevent overflow at large context lengths.

  3. Flash rejects attn_mask for spec compliance: Flash Attention's paged KV cache produces spec-divergent present_key/present_value layout when used with attn_mask + past_key. Flash now requires attn_mask == nullptr — cases with bool mask + past_key fall to the unfused runner which handles them spec-correctly. Removed ~137 lines of dead code (ConvertMaskToSeqlensKernel, LaunchConvertMaskToFlashSeqlensK) no longer needed after this change.

Known limitation

Related

@yuslepukhin yuslepukhin requested review from Copilot and tianleiwu and removed request for Copilot March 24, 2026 19:34
Comment thread onnxruntime/core/providers/cuda/shared_inc/fpgeneric.h Outdated

@github-actions github-actions Bot 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.

You can commit the suggested changes from lintrunner.

Comment thread onnxruntime/core/providers/cuda/shared_inc/fpgeneric.h Outdated
titaiwangms added a commit that referenced this pull request Mar 24, 2026
…igned dims

PR #27831 fell back to CUBLAS_DEFAULT_MATH which still uses TF32 on Ampere+ GPUs
(SM>=80) since cuBLAS 11.0. Changed to CUBLAS_PEDANTIC_MATH when dimensions are
not 4-aligned to guarantee no tensor core usage, preventing CUDA error 716
(misaligned address) on CUDA 12.9+.

Three-way logic in all three float cuBLAS GEMM helpers:
- TF32 requested + dimensions aligned: CUBLAS_TF32_TENSOR_OP_MATH
- TF32 requested + dimensions NOT aligned: CUBLAS_PEDANTIC_MATH
- TF32 not requested: CUBLAS_DEFAULT_MATH (unchanged)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@titaiwangms titaiwangms force-pushed the titaiwang/fix_cuda branch 2 times, most recently from 38fef79 to 0796037 Compare March 25, 2026 20:22
@titaiwangms titaiwangms changed the title Fix TF32 misaligned address error in cuBLAS GEMM functions Fix misaligned BiasLoader access in CUTLASS FMHA attention dispatch Mar 25, 2026
@titaiwangms

titaiwangms commented Mar 25, 2026

Copy link
Copy Markdown
Contributor Author

@titaiwangms titaiwangms force-pushed the titaiwang/fix_cuda branch 2 times, most recently from 9a1bf88 to 2cccef2 Compare March 26, 2026 23:44

@github-actions github-actions Bot 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.

You can commit the suggested changes from lintrunner.

Comment thread onnxruntime/core/providers/cuda/shared_inc/fpgeneric.h Outdated
Comment thread onnxruntime/core/providers/cuda/shared_inc/fpgeneric.h
Comment thread onnxruntime/test/providers/cpu/llm/attention_op_test.cc Outdated
Comment thread onnxruntime/test/providers/cpu/llm/attention_op_test.cc Outdated
Comment thread onnxruntime/test/providers/cpu/llm/attention_op_test.cc Outdated
@titaiwangms titaiwangms changed the title Fix misaligned BiasLoader access in CUTLASS FMHA attention dispatch Fix ONNX Attention CUDA: bias alignment, unfused decode concat, and MEA NaN Mar 27, 2026
@titaiwangms titaiwangms requested a review from Copilot March 27, 2026 05:48

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 correctness and stability issues in the CUDA EP implementation of the ONNX Attention op, focusing on decode-time bool masking with past KV, and NaNs in CUTLASS Memory Efficient Attention (MEA) for fully-masked batches.

Changes:

  • Aligns MEA bias-stride eligibility logic in attention.cc and adds post-MEA output zeroing for fully-masked batches.
  • Fixes unfused decode behavior for bool masks with past KV by using variable-length KV concat consistent with Flash layout semantics (and zero-inits present buffers).
  • Adds C++ and Python tests covering decode bool-mask edge cases (partial masks, divergent per-batch seqlens).

Reviewed changes

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

Show a summary per file
File Description
onnxruntime/test/python/transformers/test_onnx_attention/test_mha.py Adds CUDA-only graph-level tests for bool-mask decode with past KV (partial mask + divergent seqlens).
onnxruntime/test/providers/cpu/llm/attention_op_test.cc Adds CUDA execution tests forcing unfused path and verifying variable-length concat correctness in decode.
onnxruntime/core/providers/cuda/llm/attention_mask_impl.h Declares a CUDA helper to zero outputs for fully-masked batches (seqlens_k==0).
onnxruntime/core/providers/cuda/llm/attention_mask_impl.cu Implements and instantiates the ZeroOutputForFullyMaskedBatches kernel and launcher.
onnxruntime/core/providers/cuda/llm/attention.cc Applies fully-masked output zeroing for MEA and unfused nonpad paths; fixes unfused bool-mask decode concat semantics; updates MEA eligibility alignment check.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread onnxruntime/core/providers/cuda/llm/attention.cc Outdated
Comment thread onnxruntime/core/providers/cuda/llm/attention.cc Outdated

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

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


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread onnxruntime/core/providers/cuda/llm/attention.cc Outdated
Comment thread onnxruntime/core/providers/cuda/llm/attention.cc Outdated
Comment thread onnxruntime/core/providers/cuda/llm/attention.cc
@titaiwangms titaiwangms marked this pull request as ready for review March 27, 2026 17:13
@titaiwangms titaiwangms reopened this Mar 27, 2026
@titaiwangms

Copy link
Copy Markdown
Contributor Author

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.

ONNX Attention: Flash runner interprets bool attn_mask as padding mask, diverges from ONNX spec

4 participants