Fix DuoAttention QK normalization and Gemma3 RoPE 🤖🤖🤖
Bug
duo_attention_on_the_fly reconstructs query and key states from decoder-layer hidden states, but checks module against Qwen3Attention and Gemma3Attention. module is a decoder layer, so those checks are always false and the Q/K normalization used by Qwen3 and Gemma3 attention is skipped.
The same path also needs to use Gemma3's per-layer rotary embedding. In supported Transformers releases, sliding and full Gemma3 layers use distinct rotary embeddings or select their layer type explicitly. Applying the global embedding to every layer produces scores that disagree with the model's native attention states; on newer releases the old call can also fail because the rotary implementation requires the layer type.
To Reproduce
- Construct a small
Qwen3ForCausalLMorGemma3ForCausalLMmodel. - Run
duo_attention_on_the_flyand compare its reconstructed Q/K states and scores with the model's native attention states. - On the affected implementation, Q/K norms are absent from the reconstruction. For Gemma3 with both sliding and full layers, the scorer uses the wrong RoPE for at least one layer; recent Transformers releases can raise while selecting RoPE.
Validation
The proposed fix binds attention = module.self_attn, applies Q/K norms for Qwen3/Gemma3, and reuses Gemma3's native per-layer rotary selection. Its focused, offline test suite builds real tiny Qwen3, Llama, and Gemma3 models and compares the scorer with independently captured native attention states. It passed locally with Transformers 4.56.0 and 5.2.0 on CPU:
.venv/bin/python -m pytest tests/presses/test_duo_attention_on_the_fly.py -q
# 6 passed
make style
# flake8, mypy, and SPDX checks passed
No GPU validation was performed.
Repository version
71640b4f9061054a7630c5049bb9ee659a01523c (main at validation time)
🤖🤖🤖
I have prepared a focused fix and offline regression tests for this issue. I will open the PR against main now. 🤖🤖🤖