Skip to content

Use new per_layer_config for Gemma 4 so that heterogeneous attention config is explicit - #47384

Merged
hmellor merged 17 commits into
huggingface:mainfrom
hmellor:gemma4-heterogeneous-config
Jul 23, 2026
Merged

hmellor merged 17 commits into
huggingface:mainfrom
hmellor:gemma4-heterogeneous-config

Conversation

@hmellor

@hmellor hmellor commented Jul 17, 2026 •

Copy link
Copy Markdown
Member

CI

Gemma 4 contains a different head_dim and num_key_value_heads for the full attention and sliding attention layers. Currently, which layers use which is implicit during model construction.

This PR:

This will enable downstream libraries like vLLM to load Gemma 4 using the Transformers modelling backend more easily as this heterogeneity can be discovered via the config.

@hmellor
hmellor requested review from ArthurZucker, SunMarc and vasqu and removed request for SunMarc July 17, 2026 14:45

@ArthurZucker ArthurZucker left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, can we remove 1 if else?

Comment thread src/transformers/configuration_utils.py Resolved
@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

hmellor added 2 commits July 20, 2026 11:14
Signed-off-by: Harry Mellor <[email protected]>
Signed-off-by: Harry Mellor <[email protected]>

@vasqu vasqu left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I see the issue now that you guys meant. Left my 2 cents

Comment thread src/transformers/configuration_utils.py Resolved
Comment thread src/transformers/modeling_rope_utils.py Resolved
Comment thread tests/generation/test_utils.py Resolved
hmellor added 7 commits July 20, 2026 14:31
Signed-off-by: Harry Mellor <[email protected]>
Signed-off-by: Harry Mellor <[email protected]>
Signed-off-by: Harry Mellor <[email protected]>
Signed-off-by: Harry Mellor <[email protected]>
Signed-off-by: Harry Mellor <[email protected]>

@vasqu vasqu left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 🫡

Signed-off-by: Harry Mellor <[email protected]>
@vasqu

vasqu commented Jul 20, 2026

Copy link
Copy Markdown
Collaborator

run-slow: diffusion_gemma, gemma4, gemma4_unified, llama

@github-actions

Copy link
Copy Markdown
Contributor

Workflow Run ⚙️

This comment contains run-slow, running the specified jobs:

models: ["models/diffusion_gemma", "models/gemma4", "models/gemma4_unified", "models/llama"]
quantizations: []

@hmellor
hmellor added this pull request to the merge queue Jul 20, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jul 20, 2026
@github-actions

Copy link
Copy Markdown
Contributor

CI Results

Workflow Run ⚙️

Commit Info

Context Commit Description
RUN 5df29b9f workflow commit (merge commit)
PR f7c76ab4 branch commit (from PR)
main 71554973 base commit (on main)

⚠️ Model CI failed to report results

The test failure analysis could not be completed. Please check the workflow run for details.

2 similar comments
@github-actions

Copy link
Copy Markdown
Contributor

CI Results

Workflow Run ⚙️

Commit Info

Context Commit Description
RUN 5df29b9f workflow commit (merge commit)
PR f7c76ab4 branch commit (from PR)
main 71554973 base commit (on main)

⚠️ Model CI failed to report results

The test failure analysis could not be completed. Please check the workflow run for details.

@github-actions

Copy link
Copy Markdown
Contributor

CI Results

Workflow Run ⚙️

Commit Info

Context Commit Description
RUN 5df29b9f workflow commit (merge commit)
PR f7c76ab4 branch commit (from PR)
main 71554973 base commit (on main)

⚠️ Model CI failed to report results

The test failure analysis could not be completed. Please check the workflow run for details.

@vasqu

vasqu commented Jul 20, 2026

Copy link
Copy Markdown
Collaborator

Seems like it introduced new failures on gemma4 unified, not sure if this is a diff between nightly and slow ci 👀

@github-actions

Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: diffusion_gemma, gemma4, gemma4_unified

@hmellor
hmellor force-pushed the gemma4-heterogeneous-config branch from 54a303e to bdfc956 Compare July 23, 2026 08:54
@github-actions

Copy link
Copy Markdown
Contributor

CI recap

Dashboard: View test results in Grafana
Latest run: 29992777032:1
Result: success | Jobs: 15 | Tests: 166,127 | Failures: 0 | Duration: 12h 32m

@hmellor

hmellor commented Jul 23, 2026

Copy link
Copy Markdown
Member Author

run-slow: diffusion_gemma, gemma4, gemma4_unified

@github-actions

Copy link
Copy Markdown
Contributor

Workflow Run ⚙️

This comment contains run-slow, running the specified jobs:

models: ["models/diffusion_gemma", "models/gemma4", "models/gemma4_unified"]
quantizations: []

@github-actions

Copy link
Copy Markdown
Contributor

CI Results

Workflow Run ⚙️

Commit Info

Context Commit Description
RUN ea028b59 workflow commit (merge commit)
PR f53d5ee6 branch commit (from PR)
main c0bd9e62 base commit (on main)

✅ No failing test specific to this PR 🎉 👏 !

@hmellor
hmellor added this pull request to the merge queue Jul 23, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jul 23, 2026
@hmellor
hmellor added this pull request to the merge queue Jul 23, 2026
Merged via the queue into huggingface:main with commit 7d5bc4f Jul 23, 2026
104 checks passed
@hmellor
hmellor deleted the gemma4-heterogeneous-config branch July 23, 2026 11:27
melcheikh added a commit to melcheikh/vllm that referenced this pull request Jul 24, 2026
… impls

Gemma 4 does not load through its native implementations against
Transformers main. Two separate causes:

`_get_transformers_backend_cls` compares the root and text configs with
`!=`, which walks every field and raises on a heterogeneous config. Compare
identity instead, here and in the Transformers backend's `is_multimodal`:
`get_hf_text_config` already treats identity as the nesting test, and a deep
comparison of two configs is expensive and undefined for per-layer
attributes.

The native impls then read the `global_head_dim` and
`num_global_key_value_heads` keys that huggingface/transformers#47384
deleted. gemma4.py, gemma4_mtp.py and gemma4_dspark.py now resolve per-layer
geometry through one shared helper covering both config layouts.

Also fixes two problems in the existing layout handling:

- Detect the layout by whether `global_head_dim` is present, not by whether
  the heterogeneity mixin is. The mixin is on every config from Transformers
  5.15.0 on, so keying off it sends old-layout configs -- which is what every
  published Gemma 4 checkpoint still uses -- down the per-layer path, where
  `per_layer_config` carries no overrides and silently returns the sliding
  attention head_dim (256) for full attention layers that need 512.

- Iterate `per_layer_config` by index rather than by layer type. Indexing by
  type copies and compares every layer config of that type, and `layer_types`
  has one entry per layer rather than one per type, so `get_head_size()` took
  47ms; per-layer resolution in `create_attention_instances` added 8.3s to
  startup for the 48-layer 12B. Now 0.3ms and 350ms.

Verified on google/gemma-4-12B-it-qat-w4a16-ct against Transformers main:
generation is correct both with and without the Transformers modelling
backend.

Co-authored-by: Claude <[email protected]>
Signed-off-by: martin el cheikh <[email protected]>
stevhliu pushed a commit to stevhliu/transformers that referenced this pull request Jul 30, 2026
…n config is explicit (huggingface#47384)

* Use new `per_layer_config` for Gemma4 so that heterogeneous attention config is explicit

Signed-off-by: Harry Mellor <[email protected]>

* Remove the now legacy fields

Signed-off-by: Harry Mellor <[email protected]>

* Fix docstrings

Signed-off-by: Harry Mellor <[email protected]>

* Fix test harness

Signed-off-by: Harry Mellor <[email protected]>

* Magic default

Signed-off-by: Harry Mellor <[email protected]>

* Reinstate unused rope kwarg

Signed-off-by: Harry Mellor <[email protected]>

* Make `per_layer_config` more flexible/useful

Signed-off-by: Harry Mellor <[email protected]>

* Propagate with modular

Signed-off-by: Harry Mellor <[email protected]>

* Less ugly solution to heterogeneous values in `validate_architecture`

Signed-off-by: Harry Mellor <[email protected]>

* ruff

Signed-off-by: Harry Mellor <[email protected]>

* update test

Signed-off-by: Harry Mellor <[email protected]>

* Update test again

Signed-off-by: Harry Mellor <[email protected]>

* Fix tests

Signed-off-by: Harry Mellor <[email protected]>

* make fix-repo

Signed-off-by: Harry Mellor <[email protected]>

* update other tests

Signed-off-by: Harry Mellor <[email protected]>

---------

Signed-off-by: Harry Mellor <[email protected]>
Sainava pushed a commit to Sainava/Sai-transformers that referenced this pull request Aug 3, 2026
…n config is explicit (huggingface#47384)

* Use new `per_layer_config` for Gemma4 so that heterogeneous attention config is explicit

Signed-off-by: Harry Mellor <[email protected]>

* Remove the now legacy fields

Signed-off-by: Harry Mellor <[email protected]>

* Fix docstrings

Signed-off-by: Harry Mellor <[email protected]>

* Fix test harness

Signed-off-by: Harry Mellor <[email protected]>

* Magic default

Signed-off-by: Harry Mellor <[email protected]>

* Reinstate unused rope kwarg

Signed-off-by: Harry Mellor <[email protected]>

* Make `per_layer_config` more flexible/useful

Signed-off-by: Harry Mellor <[email protected]>

* Propagate with modular

Signed-off-by: Harry Mellor <[email protected]>

* Less ugly solution to heterogeneous values in `validate_architecture`

Signed-off-by: Harry Mellor <[email protected]>

* ruff

Signed-off-by: Harry Mellor <[email protected]>

* update test

Signed-off-by: Harry Mellor <[email protected]>

* Update test again

Signed-off-by: Harry Mellor <[email protected]>

* Fix tests

Signed-off-by: Harry Mellor <[email protected]>

* make fix-repo

Signed-off-by: Harry Mellor <[email protected]>

* update other tests

Signed-off-by: Harry Mellor <[email protected]>

---------

Signed-off-by: Harry Mellor <[email protected]>
@vasqu vasqu mentioned this pull request Sep 8, 2026
hjh0119 pushed a commit to modelscope/mcore-bridge that referenced this pull request Oct 27, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants