-
Notifications
You must be signed in to change notification settings - Fork 11
[model] Support gemma4 #56
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
Open
Jintao-Huang
wants to merge
6
commits into
modelscope:main
Choose a base branch
from
Jintao-Huang:support_gemma4
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 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
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 |
|---|---|---|
| @@ -1,2 +1,2 @@ | ||
| # Copyright (c) ModelScope Contributors. All rights reserved. | ||
| from . import glm, internvl, kimi_vl, llama4, llava, qwen, qwen3_5, qwen3_5_gdn, qwen3_omni, qwen3_vl | ||
| from . import gemma4, glm, internvl, kimi_vl, llama4, llava, qwen, qwen3_5, qwen3_5_gdn, qwen3_omni, qwen3_vl |
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,109 @@ | ||
| # Copyright (c) ModelScope Contributors. All rights reserved. | ||
| import copy | ||
| from megatron.core.models.gpt.gpt_layer_specs import get_gpt_decoder_block_spec | ||
| from megatron.core.transformer.attention import SelfAttention, SelfAttentionSubmodules | ||
| from transformers import AutoModel, PretrainedConfig | ||
| from typing import Optional | ||
|
|
||
| from mcore_bridge.bridge import MultimodalGPTBridge | ||
| from mcore_bridge.config import ModelConfig | ||
|
|
||
| from ..constant import ModelType | ||
| from ..gpt_model import GPTModel | ||
| from ..mm_gpt_model import MultimodalGPTModel | ||
| from ..register import ModelLoader, ModelMeta, register_model | ||
| from ..rope import get_rope_inv_freq | ||
| from .utils import HuggingFaceVit | ||
|
|
||
|
|
||
| class Gemma4Vit(HuggingFaceVit): | ||
| module_mapping = { | ||
| 'model.vision_tower': 'vision_tower', | ||
| 'model.embed_vision': 'embed_vision', | ||
| 'model.audio_tower': 'audio_tower', | ||
| 'model.embed_audio': 'embed_audio', | ||
| } | ||
| _vision_tower = ['vision_tower', 'audio_tower'] | ||
| _aligner = ['embed_vision', 'embed_audio'] | ||
| support_multimodal = False | ||
|
|
||
| def prepare_model(self, hf_config: PretrainedConfig): | ||
| from transformers.models.gemma4.modeling_gemma4 import Gemma4MultimodalEmbedder | ||
| self.vision_tower = AutoModel.from_config(hf_config.vision_config) | ||
| self.audio_tower = AutoModel.from_config(hf_config.audio_config) if hf_config.audio_config is not None else None | ||
| self.embed_vision = Gemma4MultimodalEmbedder(hf_config.vision_config, hf_config.text_config) | ||
| self.embed_audio = ( | ||
| Gemma4MultimodalEmbedder(hf_config.audio_config, hf_config.text_config) | ||
| if hf_config.audio_config is not None else None) | ||
|
|
||
| def get_inputs_embeds(self, inputs_embeds, **kwargs): | ||
| return inputs_embeds | ||
|
|
||
|
|
||
| class Gemma4SelfAttention(SelfAttention): | ||
|
|
||
| def __init__( | ||
| self, | ||
| config: ModelConfig, | ||
| submodules: SelfAttentionSubmodules, | ||
| layer_number: int, | ||
| *args, | ||
| **kwargs, | ||
| ): | ||
| text_config = config.hf_config.text_config | ||
| super().__init__(config, submodules, layer_number, *args, **kwargs) | ||
|
|
||
|
|
||
| class Gemma4Bridge(MultimodalGPTBridge): | ||
| pass | ||
|
|
||
|
|
||
| class Gemma4TextGPTModel(GPTModel): | ||
|
|
||
| def __init__(self, *args, **kwargs): | ||
| super().__init__(*args, **kwargs) | ||
| print() | ||
|
|
||
| def _set_inv_freq(self): | ||
| rope_scaling = self.config.rope_scaling | ||
| self.config.rope_scaling = rope_scaling['sliding_attention'] | ||
| new_inv_freq, attention_scaling = get_rope_inv_freq(self.config) | ||
| assert attention_scaling == 1, 'not support' | ||
| self.rotary_pos_emb.inv_freq = new_inv_freq.to(self.rotary_pos_emb.inv_freq.device) | ||
| # full | ||
| self.full_rotary_pos_emb = copy.copy(self.rotary_pos_emb) | ||
| self.config.rope_scaling = rope_scaling['full_attention'] | ||
| kwargs = {} | ||
| if self.config.rope_scaling['rope_type'] == 'proportional': | ||
| kwargs['head_dim_key'] = 'global_head_dim' | ||
| new_inv_freq, attention_scaling = get_rope_inv_freq(self.config, **kwargs) | ||
| assert attention_scaling == 1, 'not support' | ||
| self.full_rotary_pos_emb.inv_freq = new_inv_freq | ||
| self.attention_scaling = attention_scaling | ||
|
|
||
| self.config.rope_scaling = rope_scaling | ||
|
Comment on lines
+67
to
+84
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. The implementation of
|
||
|
|
||
|
|
||
| class Gemma4GPTModel(MultimodalGPTModel): | ||
| language_model_cls = Gemma4TextGPTModel | ||
|
|
||
|
|
||
| class Gemma4Loader(ModelLoader): | ||
| model_cls = Gemma4GPTModel | ||
|
|
||
| def get_transformer_layer_spec(self, vp_stage: Optional[int] = None): | ||
| layer_specs = get_gpt_decoder_block_spec( | ||
| self.config, use_transformer_engine=True, normalization=self.config.normalization, vp_stage=vp_stage) | ||
| for layer_spec in layer_specs.layer_specs: | ||
| layer_spec.submodules.self_attention.module = Gemma4SelfAttention | ||
| return layer_specs | ||
|
|
||
|
|
||
| register_model( | ||
| ModelMeta( | ||
| ModelType.gemma4, | ||
| ['gemma4'], | ||
| bridge_cls=Gemma4Bridge, | ||
| visual_cls=Gemma4Vit, | ||
| loader=Gemma4Loader, | ||
| )) | ||
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
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.
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.
The
inference_contextis not passed to the_get_rotary_pos_embmethod. This will cause the method to skip critical inference-specific logic, such as utilizing the RoPE cache or correctly calculating the rotary sequence length for flash decoding, which can lead to performance degradation or incorrect results during inference.