Skip to content

ONNX Attention MEA errors out with cudaErrorInvalidValue for KV-shared layers with borrowed K/V #28376

Description

Problem

ONNX Attention op's CUTLASS MEA (Memory Efficient Attention) aligned kernel crashes with cudaErrorInvalidValue at specific sequence lengths (multiples of 8) for KV-shared layers where K/V tensors are borrowed from another layer's present_key/present_value output.

Root cause

The crash is in the aligned CUTLASS FMHA kernel (selected when total_kv % kAlignmentQ == 0, i.e. total_kv % 8 == 0 for fp16). The unaligned kernel (selected when total_kv % 8 != 0) works correctly for the same inputs.

This affects KV-shared Attention layers where:

  • past_key == nullptr (K/V is the full cache from another layer, not incrementally concatenated)
  • An additive attention bias (mask) is present
  • total_kv_length is a multiple of 8
seq_len total_kv % 8 Kernel path Result
1 1 unaligned
4 4 unaligned
7 7 unaligned
8 0 aligned ❌ crash
9 1 unaligned
16 0 aligned ❌ crash
27 3 unaligned
32 0 aligned ❌ crash

Verified workaround

Adding force_unaligned flag to MemoryEfficientAttentionParams and setting it when past_key == nullptr && attn_bias != nullptr prevents the crash at all sequence lengths. However, the unaligned kernel produces slightly different numerics (cos_sim ~0.965 vs CPU), suggesting the MEA kernel may not fully handle the "full cache as K without past" pattern correctly.

Current plan

Default EP (ONNX Attention with unfused fallback) at 157.8 tok/s is the shipping configuration. The ORT team is investigating GQA support for new_kv_length=0 (KV-shared pattern), which would be the proper fix. When GQA supports KV-shared layers natively, the model can switch to CUDA EP with GQA for potentially better performance.

Model architecture (Gemma4 E2B-it)

Gemma4 has 35 decoder layers:

  • Layers 0-14: Non-shared (own KV cache) — work fine with both GQA and ONNX Attention
  • Layers 15-34: KV-shared — borrow K/V from source layers via Transpose+Reshape of present.N.key. No past_key/past_value inputs.

Performance data

Configuration tok/s VRAM Status
F16 INT4 default EP 176.7 9 GB ✅ Best speed
F16 default EP 157.8 12 GB Shipping config
F16 CUDA EP (GQA + Attention) 151.4 14 GB ⚠️ Needs head_dim guard
F32 default EP 143.9 22 GB

Environment

  • GPU: NVIDIA H200 (SM 9.0)
  • ORT: 1.27.0 from main
  • CUDA: 13.0, cuDNN: 9
  • Model: google/gemma-4-e2b-it (2.6B)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions