Repository navigation
Conversation
transformers 5.15 (huggingface/transformers#47384) moves global_head_dim and num_global_key_value_heads into per_layer_config, and reading head_dim or num_key_value_heads on the global Gemma 4 text config raises AmbiguousGlobalPerLayerAttributeError. Read them per layer type instead, skip per-layer attributes in the generic config mapping, and pick the right head_dim_key for the full-attention proportional RoPE.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
transformers 5.15 (huggingface/transformers#47384) turned the Gemma 4 text config into a heterogeneous per-layer config:
global_head_dimandnum_global_key_value_headsare no longer attributes ofGemma4TextConfig; they are folded intoper_layer_configfor thefull_attentionlayers (ashead_dim/num_key_value_heads).head_dimornum_key_value_headson the global config raisesAmbiguousGlobalPerLayerAttributeError(aRuntimeError, sohasattrdoes not catch it).requirements.txtallows these versions (the bound went to<5.17.0in #185 and was removed in #232), and with them loading any Gemma 4 checkpoint fails inhf_to_mcore_config:There is a second, silent issue behind it.
Gemma4TextGPTModel._set_inv_freqpasseshead_dim_key='global_head_dim'for the proportional RoPE of the full-attention layers. In transformers>=5.15 the RoPE init function resolvesper_layer_config['full_attention']first, which has noglobal_head_dim, so it falls back tohidden_size // num_attention_heads(168 instead of 512 for the 31B model, i.e. 84 frequencies instead of 256).Change
config/parser.pyget_gemma4_attention_dims(text_config, layer_type)returns(head_dim, num_key_value_heads)for a layer type. With per-layer configs it readstext_config.per_layer_config[layer_type]; otherwise it keeps the existingglobal_head_dim/num_global_key_value_headslogic.get_gemma4_rope_head_dim_key(text_config)returns'head_dim'with per-layer configs and'global_head_dim'otherwise._convert_configskips attributes listed inconfig.per_layer_attributes, so the generic mapping no longer touches values that have no single global meaning. For Gemma 4,kv_channels/num_query_groupsare then set explicitly from the sliding layers, which is what the global values meant before.model/mm_gpts/gemma4.py:Gemma4SelfAttention,Gemma4Bridge._set_qkv,_set_inv_freqandunpack_pp_inputuse the helpers instead of readingglobal_head_dim/num_global_key_value_headsdirectly.This does not set
allow_global_per_layer_attribute_access; values are read from the layer they belong to. Behavior with transformers<5.15 is unchanged: the helpers take the old branch, andper_layer_attributesis empty there.Verification
Tested on
mainat 8b567b3.tests/test_gemma4_per_layer_config.pybuilds Gemma 4 31B and E2B text configs (real attention fields, 6 layers; 31B hasattention_k_eq_v=True, E2B does not). Most cases load the parser functions from source liketest_dsv4_mla_rope.py, so they run on CPU without Transformer Engine. They check:get_gemma4_attention_dimsreturns (256, 16) / (512, 4) for 31B sliding / full layers and (256, 1) / (512, 1) for E2B;_convert_configdoes not raise and does not report per-layer values;inv_freqcomputed withget_gemma4_rope_head_dim_keyequals HF's ownGemma4TextRotaryEmbedding(...).full_attention_inv_freq(256 entries for head_dim 512);hf_to_mcore_configreturns the sliding-layerkv_channels/num_query_groups(skipped without Transformer Engine).On
mainthe same configs give: 5.14 works; 5.15 and 5.16 raiseAmbiguousGlobalPerLayerAttributeErrorin_convert_config, and the full-attention RoPE withhead_dim_key='global_head_dim'has 84 frequencies instead of 256.End to end on 8x H200 (Gemma 4 31B, ms-swift Megatron CPT, TP4/PP2, packing):
(These runs also enabled the head_dim 512 attention kernel from a follow-up PR; the config path tested here is the same either way.)
flake8,isortandyapf(versions from.pre-commit-config.yaml) are clean on the changed files.