-
Notifications
You must be signed in to change notification settings - Fork 608
[Gemma3] Add supported tasks and normalized config #2345
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
Closed
simondanielsson
wants to merge
2
commits into
huggingface:main
from
simondanielsson:feature/add-gemma3-export
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,6 +62,14 @@ def __getattr__(self, attr_name): | |
|
|
||
| attr = getattr(config, leaf_attr_name, None) | ||
|
|
||
| if attr is None: | ||
| # Fallback to checking if the leaf attribute name exists as mapped | ||
| try: | ||
| mapped_attr_name = super().__getattribute__(leaf_attr_name.upper()) | ||
| attr = getattr(config, mapped_attr_name, None) | ||
| except AttributeError: | ||
| pass | ||
|
|
||
| # If the attribute was not specified manually, try to fallback on the attribute_map. | ||
| if attr is None: | ||
| attribute_map = getattr(self.config, "attribute_map", {}) | ||
|
|
@@ -134,9 +142,40 @@ def __getattr__(self, attr_name): | |
| return super().__getattr__(attr_name) | ||
|
|
||
|
|
||
| Pix2StructNormalizedTextConfig = NormalizedTextAndVisionConfig.with_args( | ||
| text_config="text_config", vision_config="vision_config" | ||
| ) | ||
| def create_normalized_text_and_vision_config( | ||
| text_config_cls: type[NormalizedTextConfig] = NormalizedTextConfig, | ||
| vision_config_cls: type[NormalizedVisionConfig] = NormalizedVisionConfig, | ||
| ) -> type[NormalizedConfig]: | ||
| """ | ||
| Create a normalized config for a model with both a text and vision config. | ||
|
|
||
| This allows for custom renaming of parameters within the nested configs. | ||
|
|
||
| Example usage: | ||
| >>> MyNormalizedTextAndVisionConfigWithGQA = create_normalized_text_and_vision_config( | ||
| text_config_cls=NormalizedTextConfigWithGQA | ||
| ).with_args(text_config="text_config", vision_config="vision_config") | ||
|
|
||
| Attributes: | ||
| text_config_cls ([`type[NormalizedTextConfig]`]): | ||
| Normalized configuration class to use for the text config. | ||
|
|
||
| vision_config_cls ([`type[NormalizedVisionConfig]`]): | ||
| Normalized configuration class to use for the vision config. | ||
| """ | ||
|
|
||
| class CustomNormalizedTextAndVisionConfig(text_config_cls, vision_config_cls): | ||
| TEXT_CONFIG = None | ||
| VISION_CONFIG = None | ||
|
|
||
| def __getattr__(self, attr_name): | ||
| if self.TEXT_CONFIG is not None and attr_name.upper() in dir(text_config_cls): | ||
| attr_name = f"{self.TEXT_CONFIG}.{attr_name}" | ||
| elif self.VISION_CONFIG is not None and attr_name.upper() in dir(vision_config_cls): | ||
| attr_name = f"{self.VISION_CONFIG}.{attr_name}" | ||
| return super().__getattr__(attr_name) | ||
|
|
||
| return CustomNormalizedTextAndVisionConfig | ||
|
|
||
|
|
||
| class NormalizedEncoderDecoderConfig(NormalizedConfig): | ||
|
|
@@ -161,7 +200,6 @@ def __getattr__(self, attr_name): | |
| num_attention_heads="encoder_attention_heads", | ||
| hidden_size="d_model", | ||
| ) | ||
|
|
||
| GPT2LikeNormalizedTextConfig = NormalizedTextConfig.with_args(num_attention_heads="n_head", hidden_size="n_embd") | ||
| T5LikeNormalizedTextConfig = NormalizedTextConfig.with_args( | ||
| num_attention_heads="num_heads", | ||
|
|
@@ -173,23 +211,32 @@ def __getattr__(self, attr_name): | |
| GPTBigCodeNormalizedTextConfig = NormalizedTextConfig.with_args( | ||
| num_attention_heads="n_head", hidden_size="n_embd", num_layers="n_layer" | ||
| ) | ||
|
|
||
| WhisperLikeNormalizedTextConfig = NormalizedTextConfig.with_args( | ||
| hidden_size="d_model", | ||
| ) | ||
|
|
||
| TrOCRLikeNormalizedTextConfig = NormalizedTextConfig.with_args( | ||
| num_layers="decoder_layers", | ||
| num_attention_heads="decoder_attention_heads", | ||
| hidden_size="hidden_size", | ||
| ) | ||
|
|
||
| SpeechToTextLikeNormalizedTextConfig = NormalizedSeq2SeqConfig.with_args( | ||
| decoder_num_layers="decoder_layers", | ||
| num_layers="decoder_layers", | ||
| input_features_per_channel="input_feat_per_channel", | ||
| allow_new=True, | ||
| ) | ||
| Pix2StructNormalizedTextConfig = NormalizedTextAndVisionConfig.with_args( | ||
| text_config="text_config", vision_config="vision_config" | ||
| ) | ||
|
|
||
|
|
||
| class Gemma3NormalizedTextConfigWithGQA(NormalizedTextConfigWithGQA): | ||
| HEAD_DIM = "text_config.head_dim" | ||
|
|
||
|
|
||
| Gemma3NormalizedTextAndVisionConfig = create_normalized_text_and_vision_config( | ||
| text_config_cls=Gemma3NormalizedTextConfigWithGQA | ||
| ).with_args(text_config="text_config", vision_config="vision_config") | ||
|
|
||
|
|
||
| class NormalizedConfigManager: | ||
|
|
@@ -253,11 +300,13 @@ class NormalizedConfigManager: | |
| "electra": NormalizedTextConfig, | ||
| "encoder-decoder": NormalizedEncoderDecoderConfig, | ||
| "gemma": NormalizedTextConfigWithGQA, | ||
| "gemma3": Gemma3NormalizedTextAndVisionConfig, | ||
| "gpt2": GPT2LikeNormalizedTextConfig, | ||
| "gpt_bigcode": GPTBigCodeNormalizedTextConfig, | ||
| "gpt_neo": NormalizedTextConfig.with_args(num_attention_heads="num_heads"), | ||
| "gpt_neox": NormalizedTextConfig, | ||
| "gptj": GPT2LikeNormalizedTextConfig, | ||
| "granite": NormalizedTextConfigWithGQA, | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Explanation: this one was out of place |
||
| "imagegpt": GPT2LikeNormalizedTextConfig, | ||
| "internlm2": NormalizedTextConfigWithGQA, | ||
| "llama": NormalizedTextConfigWithGQA, | ||
|
|
@@ -298,7 +347,6 @@ class NormalizedConfigManager: | |
| "qwen3": NormalizedTextConfig, | ||
| "qwen3_moe": NormalizedTextConfig, | ||
| "smollm3": NormalizedTextConfig, | ||
| "granite": NormalizedTextConfigWithGQA, | ||
| } | ||
|
|
||
| @classmethod | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: Happy to hear if you have a cleaner way of doing this :)