-
Notifications
You must be signed in to change notification settings - Fork 14
[model] support bailing #55
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
8 commits
Select commit
Hold shift + click to select a range
6168a17
support bailing
Jintao-Huang 796efc5
fix
Jintao-Huang ea099f0
update
Jintao-Huang d10ced2
update
Jintao-Huang d107b02
fix
Jintao-Huang c609fbc
Merge branch 'main' into support_bailing
Jintao-Huang 6355d85
support bailing_moe
Jintao-Huang a7038c5
fix
Jintao-Huang 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| /.vscode | ||
| *.egg-info/ | ||
| /.idea/ | ||
| /.qoder/ | ||
| build/ | ||
| dist/ | ||
| __pycache__/ | ||
|
|
||
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 glm4, hunyuan, llm, minimax_m2, olmoe, qwen3_emb, qwen3_next | ||
| from . import 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,86 @@ | ||
| # Copyright (c) ModelScope Contributors. All rights reserved. | ||
| import torch | ||
| from megatron.core.transformer.attention import SelfAttention | ||
| from torch import Tensor | ||
| from typing import Optional | ||
|
|
||
| from mcore_bridge.bridge import GPTBridge | ||
|
|
||
| from ..constant import ModelType | ||
| from ..register import ModelLoader, ModelMeta, register_model | ||
|
|
||
|
|
||
| class BailingMoeSelfAttention(SelfAttention): | ||
|
|
||
| def get_query_key_value_tensors( | ||
| self, | ||
| hidden_states: Tensor, | ||
| key_value_states: Optional[Tensor] = None, | ||
| *args, | ||
| **kwargs, | ||
| ): | ||
| """Override to handle BailingMoE's non-interleaved QKV weight layout. | ||
|
|
||
| BailingMoE stores weights as [Q_all | K_all | V_all] (split by head count), | ||
| not Megatron's interleaved [q1 q2 k1 v1 | q3 q4 k2 v2 | ...]. | ||
| """ | ||
| # [sq, b, h] --> [sq, b, (num_heads + 2 * num_kv_heads) * head_dim] | ||
| mixed_qkv, _ = self.linear_qkv(hidden_states) | ||
|
Jintao-Huang marked this conversation as resolved.
|
||
|
|
||
| # [sq, b, (num_heads + 2 * num_kv_heads) * head_dim] | ||
| # --> [sq, b, num_heads + 2 * num_kv_heads, head_dim] | ||
| new_tensor_shape = mixed_qkv.size()[:-1] + ( | ||
| self.num_attention_heads_per_partition + 2 * self.num_query_groups_per_partition, | ||
| self.hidden_size_per_attention_head, | ||
| ) | ||
| mixed_qkv = mixed_qkv.view(*new_tensor_shape) | ||
|
|
||
| # Split by head count: [sq, b, num_heads, hn], [sq, b, num_kv_heads, hn], [sq, b, num_kv_heads, hn] | ||
| query, key, value = torch.split( | ||
| mixed_qkv, | ||
| [ | ||
| self.num_attention_heads_per_partition, self.num_query_groups_per_partition, | ||
| self.num_query_groups_per_partition | ||
| ], | ||
| dim=2, | ||
| ) | ||
|
|
||
| if self.q_layernorm is not None: | ||
| query = self.q_layernorm(query) | ||
|
|
||
| if self.k_layernorm is not None: | ||
| key = self.k_layernorm(key) | ||
|
|
||
| return query, key, value | ||
|
|
||
|
|
||
| class BailingMoeLoader(ModelLoader): | ||
|
|
||
| def get_transformer_layer_spec(self, vp_stage: Optional[int] = None): | ||
| transformer_layer_spec = super().get_transformer_layer_spec(vp_stage) | ||
| for layer_spec in transformer_layer_spec.layer_specs: | ||
| layer_spec.submodules.self_attention.module = BailingMoeSelfAttention | ||
| return transformer_layer_spec | ||
|
|
||
|
|
||
| class BailingMoeBridge(GPTBridge): | ||
| hf_embed_key = 'model.word_embeddings.weight' | ||
| hf_attn_prefix = 'attention' | ||
| hf_q_norm_key = 'query_layernorm.weight' | ||
| hf_k_norm_key = 'key_layernorm.weight' | ||
| hf_expert_bias_key = 'gate.expert_bias' | ||
| hf_o_proj_key = 'dense' | ||
|
|
||
| def _set_qkv(self, mg_attn, hf_state_dict, to_mcore: bool): | ||
| self._set_state_dict(mg_attn, 'linear_qkv.weight', hf_state_dict, 'query_key_value.weight', to_mcore) | ||
|
Jintao-Huang marked this conversation as resolved.
|
||
| assert not self.config.add_bias_linear | ||
| return hf_state_dict | ||
|
|
||
|
|
||
| register_model( | ||
| ModelMeta( | ||
| ModelType.bailing_moe, | ||
| ['bailing_moe'], | ||
| bridge_cls=BailingMoeBridge, | ||
| loader=BailingMoeLoader, | ||
| )) | ||
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.
While adding
score_functionto theconfig_mappingis correct, thebailing_moemodel type should also be explicitly handled in thehf_to_mcore_configfunction (around line 120 and 164) to ensureqk_layernormis enabled and the router score function is set tosigmoid. The bridge definition inbailing_moe.pyincludes QK normalization keys and expert bias, which strongly suggests these configurations are required for the model to function correctly in Megatron-Core.