-
Notifications
You must be signed in to change notification settings - Fork 2.6k
[None][feat] Add Gemma 4 12B Unified (encoder-free multimodal) support #15768
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8120447
[None][feat] Add Gemma 4 12B Unified (gemma4_unified) support
Hudayday f7de1d5
[None][test] Fix Gemma4Unified unit test (mapper-registry lookup + __…
Hudayday 6e6cc9e
[None][test] Skip gemma4_unified test when transformers<5.10
Hudayday 3317304
Merge branch 'main' of https://github.com/NVIDIA/TensorRT-LLM into fe…
Hudayday 4452b4f
[None][chore] Apply pre-commit (isort + ruff-format) to gemma4_unified
Hudayday ccb3070
[None][fix] Gemma4: keep audio causal in mm bidirectional mask (match…
Hudayday 3b16ff6
[None][feat] gemma4-12b-unified: address PR review feedback
Hudayday 1c0067e
[None][feat] gemma4-12b-unified: decouple from the transformers pin
Hudayday b7a94a0
Merge branch 'main' of https://github.com/NVIDIA/TensorRT-LLM into fe…
Hudayday 55a406a
[None][chore] Suppress bandit false positives on multimodal delimiter…
Hudayday 7bc24d2
[None][fix] gemma4-12b-unified: review feedback and shim fixes
Hudayday cb8ab4b
[None][fix] gemma4-12b-unified: skip processor fallback without a tok…
Hudayday de5812f
[None][fix] gemma4-12b-unified: back the inherited mm_token_ids property
Hudayday File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| """Config classes for Gemma 4 12B Unified (encoder-free multimodal). | ||
|
|
||
| Registered with the transformers CONFIG_MAPPING (see `_torch/configs/__init__.py`) | ||
| so `AutoConfig.from_pretrained` can parse a Gemma 4 12B checkpoint whenever the | ||
| installed transformers does not ship the `gemma4_unified` model_types natively. | ||
|
|
||
| All fields are read directly from the checkpoint's config.json, matching the | ||
| attribute names used by `Gemma4UnifiedForConditionalGeneration`. The text | ||
| backbone of the 12B is a standard dense Gemma 4 text model, so its sub-config | ||
| reuses the native `Gemma4TextConfig`. | ||
| """ | ||
|
|
||
| from transformers import Gemma4TextConfig | ||
| from transformers.configuration_utils import PretrainedConfig | ||
|
|
||
|
|
||
| class Gemma4UnifiedTextConfig(Gemma4TextConfig): | ||
| """Text sub-config for Gemma 4 12B Unified. | ||
|
|
||
| The 12B text backbone is a standard dense Gemma 4 text model; only the | ||
| model_type string differs, so this is a pure alias of the native | ||
| `Gemma4TextConfig`. | ||
| """ | ||
|
|
||
| model_type = "gemma4_unified_text" | ||
|
|
||
|
|
||
| class Gemma4UnifiedVisionConfig(PretrainedConfig): | ||
| """Sub-config for the encoder-free vision projector.""" | ||
|
|
||
| model_type = "gemma4_unified_vision" | ||
|
|
||
| def __init__( | ||
| self, | ||
| mm_embed_dim: int = 3840, | ||
| mm_posemb_size: int = 1120, | ||
| output_proj_dims: int = 3840, | ||
| patch_size: int = 16, | ||
| pooling_kernel_size: int = 3, | ||
| rms_norm_eps: float = 1e-6, | ||
| **kwargs, | ||
| ): | ||
| super().__init__(**kwargs) | ||
| self.mm_embed_dim = mm_embed_dim | ||
| self.mm_posemb_size = mm_posemb_size | ||
| self.output_proj_dims = output_proj_dims | ||
| self.patch_size = patch_size | ||
| self.pooling_kernel_size = pooling_kernel_size | ||
| self.rms_norm_eps = rms_norm_eps | ||
|
|
||
|
|
||
| class Gemma4UnifiedAudioConfig(PretrainedConfig): | ||
| """Sub-config for the encoder-free audio projector. | ||
|
|
||
| `output_proj_dims` and `hidden_size` alias `audio_embed_dim` (the raw audio | ||
| frame width) when not given, matching the HF implementation; they are plain | ||
| attributes here so a checkpoint config.json that spells them out loads as-is. | ||
| """ | ||
|
|
||
| model_type = "gemma4_unified_audio" | ||
|
|
||
| def __init__( | ||
| self, | ||
| audio_embed_dim: int = 640, | ||
| rms_norm_eps: float = 1e-6, | ||
| output_proj_dims: int | None = None, | ||
| hidden_size: int | None = None, | ||
| **kwargs, | ||
| ): | ||
| super().__init__(**kwargs) | ||
| self.audio_embed_dim = audio_embed_dim | ||
| self.rms_norm_eps = rms_norm_eps | ||
| self.output_proj_dims = ( | ||
| output_proj_dims if output_proj_dims is not None else audio_embed_dim | ||
| ) | ||
| self.hidden_size = hidden_size if hidden_size is not None else audio_embed_dim | ||
|
|
||
|
|
||
| class Gemma4UnifiedConfig(PretrainedConfig): | ||
| """Top-level config for Gemma 4 12B Unified (encoder-free multimodal). | ||
|
|
||
| Parses `config.json` fields required by | ||
| `Gemma4UnifiedForConditionalGeneration` without depending on any | ||
| natively shipped transformers class. The `text_config`, `vision_config`, and | ||
| `audio_config` sub-configs are reconstructed from nested dicts using the | ||
| shim classes above. | ||
| """ | ||
|
|
||
| model_type = "gemma4_unified" | ||
|
|
||
| def __init__( | ||
| self, | ||
| text_config=None, | ||
| vision_config=None, | ||
| audio_config=None, | ||
| image_token_id: int = 258880, | ||
| audio_token_id: int = 258881, | ||
| video_token_id: int = 258884, | ||
| tie_word_embeddings: bool = True, | ||
| **kwargs, | ||
| ): | ||
| super().__init__(tie_word_embeddings=tie_word_embeddings, **kwargs) | ||
| self.image_token_id = image_token_id | ||
| self.audio_token_id = audio_token_id | ||
| self.video_token_id = video_token_id | ||
|
|
||
| # Sub-configs arrive as dicts from AutoConfig.from_pretrained; rebuild | ||
| # them with the classes above. | ||
| if text_config is not None: | ||
| if isinstance(text_config, dict): | ||
| self.text_config = Gemma4UnifiedTextConfig(**text_config) | ||
| else: | ||
| self.text_config = text_config | ||
| else: | ||
| self.text_config = None | ||
|
|
||
| if vision_config is not None: | ||
| if isinstance(vision_config, dict): | ||
| self.vision_config = Gemma4UnifiedVisionConfig(**vision_config) | ||
| else: | ||
| self.vision_config = vision_config | ||
| else: | ||
| self.vision_config = None | ||
|
|
||
| if audio_config is not None: | ||
| if isinstance(audio_config, dict): | ||
| self.audio_config = Gemma4UnifiedAudioConfig(**audio_config) | ||
| else: | ||
| self.audio_config = audio_config | ||
| else: | ||
| self.audio_config = None |
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.