-
Notifications
You must be signed in to change notification settings - Fork 13
[model] Support bailing v2 5 #85
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
3
commits into
modelscope:main
Choose a base branch
from
Jintao-Huang:support_bailing_v2_5
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
3 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
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 bailing_moe, glm4, hunyuan, llm, minimax_m2, olmoe, qwen3_emb, qwen3_next | ||
| from . import bailing_hybrid, bailing_moe, glm4, hunyuan, llm, minimax_m2, olmoe, qwen3_emb, qwen3_next |
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,68 @@ | ||||||||||||||||||||||||||||||||||
| # Copyright (c) ModelScope Contributors. All rights reserved. | ||||||||||||||||||||||||||||||||||
| from megatron.core.inference.contexts import BaseInferenceContext | ||||||||||||||||||||||||||||||||||
| from megatron.core.packed_seq_params import PackedSeqParams | ||||||||||||||||||||||||||||||||||
| from torch import Tensor | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| from mcore_bridge.bridge import GPTBridge | ||||||||||||||||||||||||||||||||||
| from ..constant import ModelType | ||||||||||||||||||||||||||||||||||
| from ..register import ModelLoader, ModelMeta, register_model | ||||||||||||||||||||||||||||||||||
| from typing import Optional, Union, Tuple | ||||||||||||||||||||||||||||||||||
| from megatron.core.transformer.attention import SelfAttention | ||||||||||||||||||||||||||||||||||
| from megatron.core.transformer.attention import SelfAttentionSubmodules | ||||||||||||||||||||||||||||||||||
| from megatron.core.transformer.transformer_config import TransformerConfig | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| class BailingHybridBridge(GPTBridge): | ||||||||||||||||||||||||||||||||||
| pass | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| class LinearAttention(SelfAttention): | ||||||||||||||||||||||||||||||||||
| def __init__( | ||||||||||||||||||||||||||||||||||
| self, | ||||||||||||||||||||||||||||||||||
| config: TransformerConfig, | ||||||||||||||||||||||||||||||||||
| *args, **kwargs, | ||||||||||||||||||||||||||||||||||
| ): | ||||||||||||||||||||||||||||||||||
| super().__init__(config, *args, **kwargs) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| def forward( | ||||||||||||||||||||||||||||||||||
| self, | ||||||||||||||||||||||||||||||||||
| hidden_states: Tensor, | ||||||||||||||||||||||||||||||||||
| attention_mask: Tensor, | ||||||||||||||||||||||||||||||||||
| **kwargs, | ||||||||||||||||||||||||||||||||||
| ) -> Tuple[Tensor, Tensor]: | ||||||||||||||||||||||||||||||||||
| return super().forward(hidden_states, attention_mask, **kwargs) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| class BailingHybridLoader(ModelLoader): | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| def get_transformer_layer_spec(self, vp_stage: Optional[int] = None): | ||||||||||||||||||||||||||||||||||
| hf_config = self.config.hf_config | ||||||||||||||||||||||||||||||||||
| num_layers = hf_config.num_hidden_layers | ||||||||||||||||||||||||||||||||||
| group_size = hf_config.layer_group_size | ||||||||||||||||||||||||||||||||||
| tail_start = num_layers // group_size * group_size | ||||||||||||||||||||||||||||||||||
| hf_config.attention_layer_type = [ | ||||||||||||||||||||||||||||||||||
| "attention" | ||||||||||||||||||||||||||||||||||
| if (layer_idx + 1) % group_size == 0 or layer_idx >= tail_start | ||||||||||||||||||||||||||||||||||
| else "linear_attention" | ||||||||||||||||||||||||||||||||||
| for layer_idx in range(num_layers) | ||||||||||||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||||||||||||
| layer_specs = super().get_transformer_layer_spec(vp_stage=vp_stage) | ||||||||||||||||||||||||||||||||||
| multi_latent_attention = self.config.multi_latent_attention | ||||||||||||||||||||||||||||||||||
| self.config.multi_latent_attention = False | ||||||||||||||||||||||||||||||||||
| linear_layer_specs = super().get_transformer_layer_spec(vp_stage=vp_stage) | ||||||||||||||||||||||||||||||||||
| self.config.multi_latent_attention = multi_latent_attention | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+49
to
+53
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 current implementation for getting
Consider refactoring this logic to be safer and more efficient.
Suggested change
|
||||||||||||||||||||||||||||||||||
| for i, layer_spec in enumerate(layer_specs.layer_specs): | ||||||||||||||||||||||||||||||||||
| if hf_config.attention_layer_type[i] == 'linear_attention': | ||||||||||||||||||||||||||||||||||
| linear_spec = linear_layer_specs.layer_specs[i].submodules.self_attention | ||||||||||||||||||||||||||||||||||
| linear_spec.module = LinearAttention | ||||||||||||||||||||||||||||||||||
| layer_spec.submodules.self_attention = linear_spec | ||||||||||||||||||||||||||||||||||
| return layer_specs | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| register_model( | ||||||||||||||||||||||||||||||||||
| ModelMeta( | ||||||||||||||||||||||||||||||||||
| ModelType.bailing_hybrid, | ||||||||||||||||||||||||||||||||||
| ['bailing_hybrid'], | ||||||||||||||||||||||||||||||||||
| bridge_cls=BailingHybridBridge, | ||||||||||||||||||||||||||||||||||
| loader=BailingHybridLoader, | ||||||||||||||||||||||||||||||||||
| )) | ||||||||||||||||||||||||||||||||||
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
__init__andforwardmethods in theLinearAttentionclass are redundant as they just call the superclass methods with the same arguments. You can remove them for cleaner and more concise code.After this change,
TransformerConfigandTuplewill become unused imports and should also be removed, along with other unused imports in this file (BaseInferenceContext,PackedSeqParams,Union, andSelfAttentionSubmodules).