Skip to content

Fix CUTLASS FMHA BiasLoader alignment for unaligned kernel path - #28369

Merged
Tianlei Wu (tianleiwu) merged 4 commits into
mainfrom
fix-cutlass-biasloader-alignment
May 27, 2026
Merged

Fix CUTLASS FMHA BiasLoader alignment for unaligned kernel path#28369
Tianlei Wu (tianleiwu) merged 4 commits into
mainfrom
fix-cutlass-biasloader-alignment

Conversation

@justinchuby

Copy link
Copy Markdown
Contributor

BiasLoader hardcoded 128-bit vectorized loads (ElementsPerAccess = 128/sizeof_bits = 8 for fp16) regardless of the isAligned template flag. When bias stride was not a multiple of 8, the unaligned kernel was selected but BiasLoader still used 128-bit loads → cudaErrorMisalignedAddress.

Fix: Use kAlignmentA (4 for unaligned, 8 for aligned) instead of hardcoded 8.

Tested with Gemma4 Attention + mask at all seq lengths 1–32.

BiasLoader hardcoded 128-bit (8 fp16 element) vectorized loads via
ElementsPerAccess = 128 / sizeof_bits<scalar_t> regardless of the
isAligned template parameter. When attention bias stride
(total_sequence_length) was not a multiple of 8, the unaligned kernel
was selected but BiasLoader still used 128-bit loads on the bias,
causing cudaErrorMisalignedAddress.

Fix: Use kAlignmentA (kMinimumAlignment=4 for unaligned path,
kAlignmentA=8 for aligned path) as BiasLoader's ElementsPerAccess.
This allows the unaligned kernel to use 64-bit loads for the bias.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Justin Chu <justinchu@microsoft.com>
Test the BiasLoader alignment fix with total_kv_length values that are
not divisible by 8 (the fp16 vectorized load width). Before the fix,
these would cause wrong results or crashes in the MEA kernel path.

16 test cases across 3 categories:
- MHA decode: 8 lengths (5, 7, 9, 13, 27 unaligned + 8, 16, 32 aligned)
- MHA prompt: 5 lengths (5, 7, 13 unaligned + 8, 16 aligned)
- GQA decode: 3 lengths (5, 9 unaligned + 16 aligned)

All use float16 with 4D additive attention mask on CUDA EP, comparing
against PyTorch reference.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Justin Chu <justinchu@microsoft.com>

@tianleiwu Tianlei Wu (tianleiwu) 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.

Summary

Clean, minimal fix for a real crash caused by the CUTLASS BiasLoader hardcoding 128-bit vectorized loads regardless of the kernel's alignment path. The one-line kernel change is correct and consistent with how kAlignmentA is already used for Q/K iterators. The test coverage is thorough in terms of length combinations but has one high-priority gap: it does not disable Flash Attention, so on SM80+ GPUs the CUTLASS MEA path may never be exercised.

Kernel Fix

Positive: The fix is minimal and exactly right — replacing 128 / cutlass::sizeof_bits<scalar_t>::value (always 8 for fp16) with kAlignmentA, which already adapts via kIsAligned ? DefaultConfig::kAlignmentA : GemmType::kMinimumAlignment (8 aligned, 4 unaligned). This makes BiasLoader consistent with Q/K iterators and with the existing check_supported() validation and dispatch-side alignment check in fmha_launch_template.h.

Tests

Positive: Good test matrix covering decode, prompt, and GQA paths with both aligned and unaligned total KV lengths.

See inline comment for one concern.

…mha.py

Co-authored-by: Tianlei Wu <tlwu@microsoft.com>
@justinchuby

Copy link
Copy Markdown
Contributor Author

Copilot fix lint

Copilot AI commented May 26, 2026

Copy link
Copy Markdown
Contributor

Copilot fix lint

Fixed in b8bee38. I removed the accidental duplicate class definition in test_mha.py; py_compile and lintrunner now pass locally.

@tianleiwu
Tianlei Wu (tianleiwu) merged commit 1f92c9d into main May 27, 2026
98 of 107 checks passed
@tianleiwu
Tianlei Wu (tianleiwu) deleted the fix-cutlass-biasloader-alignment branch May 27, 2026 01:00
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 MEA errors out with cudaErrorInvalidValue for KV-shared layers with borrowed K/V

3 participants