Repository navigation
fix: CodeGen attention on transformers >= 5.24 - #2052
XiaoyuanWang111 wants to merge 2 commits into
Conversation
transformers 5.24 moved CodeGen's rotary embedding to CodeGenModel.rotary_emb, which passes (sin, cos) as position_embeddings, and replaced the attention's embed_positions and scale_attn with scaling = head_dim ** -0.5. The bridge read the removed attributes and raised. Use position_embeddings and scaling when present, and the old attributes otherwise.
jlarson4
left a comment
There was a problem hiding this comment.
Thanks for putting this together @XiaoyuanWang111! Just a couple comments, one on testing and the other on a unique edge case. Let me know if you have any questions.
| sin: Optional[torch.Tensor] = None | ||
| cos: Optional[torch.Tensor] = None | ||
| if position_embeddings is not None: | ||
| sin, cos = position_embeddings |
There was a problem hiding this comment.
On 5.24, existing tests only confirm the forward runs. The tests pass with or without this swap. Could you add a unit test that puts the stand-in in test_codegen_attention_bridge.py into the new layout and checks it against the old one?
There was a problem hiding this comment.
Added in 102a752. test_position_embeddings_match_embed_positions runs the stand-in in the old layout (embed_positions and scale_attn, called with position_ids), then moves it to the 5.24 layout (no table, scaling = head_dim ** -0.5, called with position_embeddings taken from the same table) and requires the output and the pattern to be identical. It covers rotary_dim 8 (partial) and 16 (the whole head). Swapping sin and cos, or using scaling without inverting it, makes it fail.
| cos: Optional[torch.Tensor] = None | ||
| if position_embeddings is not None: | ||
| sin, cos = position_embeddings | ||
| elif position_ids is not None: |
There was a problem hiding this comment.
On 5.24, a call that only has position_ids lands here and raises on embed_positions. get_random_inputs will hit that issue, so the component benchmark will fail every CodeGen attention block. Can you please have it supply position_embeddings when the module has no table of its own?
There was a problem hiding this comment.
Fixed in 102a752. When the wrapped module has no embed_positions, get_random_inputs now also returns position_embeddings, built from position_ids with the same sinusoidal table that CodeGenRotaryEmbedding uses (the new _create_sinusoidal_positions returns exactly what HF's create_sinusoidal_positions does). I ran benchmark_all_components on a tiny random CodeGen. With transformers 5.24.1, both attention blocks failed before (2 of 13 components, the embed_positions error) and all 13 pass now; 5.23.0 passes before and after. test_random_inputs_supply_position_embeddings covers it. test_codegen_attention_bridge.py, test_codegen_adapter.py and test_parallel_residual_identities.py give 135 passed on 5.24.1 and on 5.13.0, merged onto current dev.
On transformers >= 5.24 CodeGenAttention has no embed_positions, so the component benchmark's position_ids-only call failed on every CodeGen attention block. get_random_inputs now builds (sin, cos) from position_ids when the wrapped module has no table of its own. Add unit tests that move the stand-in to the 5.24 layout (position_embeddings, scaling) and check output and pattern against the earlier layout (embed_positions, scale_attn), and that check the position_embeddings get_random_inputs builds.
Description
transformers 5.24.0 (released 2026-11-21) moved CodeGen's rotary embedding from the attention module to the model:
CodeGenModel.rotary_embcomputes(sin, cos)once and passes it to each layer asposition_embeddings, andCodeGenAttentionlostembed_positionsandscale_attn(it now hasscaling = head_dim ** -0.5).CodeGenAttentionBridge._reconstruct_attentionreads both removed attributes, so every forward of a bridged CodeGen model raises:pyproject.tomlasks fortransformers>=5.9.0with no upper bound, so a fresh install gets 5.24.1, which behaves the same. CI runs the lock's 5.13.0, which is why it does not show there; the CodeGen cases oftests/integration/model_bridge/test_parallel_residual_identities.pyfail ondevunder 5.24.0 and 5.24.1.The change takes
(sin, cos)fromposition_embeddingswhen the layer passes them and slicesembed_positionsbyposition_idsotherwise, and takes the scale fromscale_attnwhen the module has it and from1 / scalingotherwise. Nothing changes on transformers < 5.24.To reproduce on
devwith transformers 5.24.1 (downloads a tiny test checkpoint):On
devwith 5.24.1 the bridge forward raises theAttributeErrorabove. With this change it prints1.6e-07(1.5e-07ondevwith 5.23.0).Tests: the existing CodeGen tests cover it once they run on 5.24 (
test_codegen_attention_bridge.py,test_codegen_adapter.py,test_parallel_residual_identities.py): ondevwith 5.24.1, 6 failed and 126 passed (the six CodeGen cases of the parallel-residual identities); with this change all 132 pass under 5.24.1 and under 5.13.0. With #2047 merged, cached generation works too: on 5.24.1,generate(..., use_past_kv_cache=True)returns the same tokens as without the cache.Type of change
Checklist: