You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 41d2641
Browse filesBrowse the repository at this point in the historyBrowse files
Enable GQA for KV-shared layers and fix dtype issues in vision and audio (#279)
## [Gemma4] Enable GQA for KV-shared layers, fix vision encoder dtype,
fix audio SkipNorm fusion
### Summary
Four changes to enable optimized Gemma4 multimodal inference:
1. **GQA for KV-shared layers** — use `GroupQueryAttention` instead of
standard `Attention` for the 20 KV-shared decoder layers
2. **Vision encoder float32 input** — accept float32 `pixel_values` and
cast inside the model to prevent NaN
3. **Audio encoder SkipNorm fix** — prevent incorrect fusion of
`Add(MatMul, 1D_bias) + RMSNorm`
4. **Enable `past_present_share_buffer`** — avoid per-step KV cache
allocation
---
### 1. GQA for KV-shared layers (`gemma4.py`)
KV-shared layers borrow K/V from a source layer's
`present_key`/`present_value` (already in BNSH format). Instead of
Transpose+Reshape into BSNH for the standard `Attention` op, we now
pass:
- Empty K/V tensors `[B, 0, kv_hidden]` (`kv_sequence_length=0`)
- Source layer's present K/V directly as `past_key`/`past_value`
This eliminates Transpose+Reshape ops from the graph for all 20 shared
layers and uses the optimized GQA flash attention kernel. Requires
corresponding ORT changes to support `kv_sequence_length=0` (separate
ORT PR).
**Decoder loop change:** All layers (shared and non-shared) now receive
GQA context when the EP supports it. The previous `is_shared` exclusion
and fallback attention bias/position embedding generation for shared
layers are removed.
### 2. Vision encoder float32 input (`_gemma4.py`)
**Problem:** The image processor in ORT-GenAI outputs float32
`pixel_values`, but the vision encoder declared its input as
`config.dtype` (float16). The ORT-GenAI trimming path did a raw `memcpy`
from float32 source into an fp16 tensor using fp16 strides —
reinterpreting float32 bit patterns as fp16, producing NaN.
**Fix:** Declare `pixel_values` as `ir.DataType.FLOAT` and add
`op.Cast(pixel_values, to=config.dtype)` inside the ONNX model when
needed. This keeps the host-side pipeline in float32 and lets the model
handle the precision conversion safely.
### 3. Audio encoder SkipNorm fix (`_skip_norm.py`)
**Problem:** The `AddRMSNormToSkipNorm` rewrite rule fused
`Add(MatMul_result, 1D_bias) + RMSNorm` into
`SkipSimplifiedLayerNormalization`. The 1D bias `[hidden]` became the
`skip` input, but the op requires both inputs to have the same shape
(`[B, S, hidden]`). This caused:
- CPU: `skip is expected to have 3 or 2 dimensions, got 1`
- CUDA: `input and skip shall have same shape when
enable_skip_layer_norm_strict_mode is True`
**Fix:** Add a rank equality check in the `check()` method. Fusion is
skipped when the two Add inputs have different ranks (e.g., 3D tensor +
1D bias via broadcasting).
### 4. Enable `past_present_share_buffer` (`genai_config.py`)
Set `past_present_share_buffer` to `true` by default for all EPs. This
pre-allocates KV cache buffers and aliases past/present pointers,
avoiding per-step allocation and past→present copies.
---
### Testing
| Scenario | Status |
|----------|--------|
| Text-only generation (CPU) | ✅ |
| Text-only generation (CUDA) | ✅ |
| Multimodal image+text (CPU) | ✅ Clean output, no repetition |
| Multimodal image+text (CUDA) | ✅ Clean output, no crashes |
| Model graph | ✅ 35 GQA nodes (15 source + 20 shared), 0 Attention
nodes |
---------
Co-authored-by: Copilot <[email protected]>
0 commit comments