-
Notifications
You must be signed in to change notification settings - Fork 453
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
Support Qwen2-MoE models #2723
Merged
Merged
Support Qwen2-MoE models #2723
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e0b221e
add qwen2-moe
lzhangzz cea0524
eliminate `inter_size_` from ffn layer
lzhangzz b7d050a
clean up
lzhangzz 8fade57
fix lint
lzhangzz 4c0f902
clean up
lzhangzz 7447b73
Update config.yaml
zhulinJulia24 fd17b0f
Merge branch 'main' into moe2
zhulinJulia24 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 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 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 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 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 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 |
---|---|---|
|
@@ -120,3 +120,64 @@ def model_info(self): | |
cfg = super().model_info() | ||
cfg['attn_bias'] = 1 | ||
return cfg | ||
|
||
|
||
class Qwen2MoeReader(LlamaReader): | ||
|
||
ffn_pattern = r'shared_expert\.' | ||
|
||
def moe_ffn_expert(self, e=None, i=None, kind=None): | ||
if not kind: | ||
return self.filter(r'experts') | ||
result = [] | ||
for key in ['gate', 'down', 'up']: | ||
name = f'model.layers.{i}.mlp.experts.{e}.{key}_proj.{kind}' | ||
tensor = self.params.get(name) | ||
tensor = self.transform(tensor, kind) | ||
result.append(tensor) | ||
return (*result, ) | ||
|
||
def moe_ffn_gate(self, i): | ||
return self.params.get(f'model.layers.{i}.mlp.gate.weight') | ||
|
||
def _ffn(self, i: int, kind: str): | ||
"""Get ffn kind for layer i.""" | ||
if not kind: | ||
return self.filter(self.ffn_pattern) | ||
result = [] | ||
for key in ['gate', 'down', 'up']: | ||
tensor = self.params[ | ||
f'model.layers.{i}.mlp.shared_expert.{key}_proj.{kind}'] | ||
tensor = self.transform(tensor, kind) | ||
result.append(tensor) | ||
return (*result, ) | ||
|
||
def moe_ffn_shared_gate(self, i): | ||
return self.params.get( | ||
f'model.layers.{i}.mlp.shared_expert_gate.weight') | ||
|
||
|
||
@INPUT_MODELS.register_module(name='qwen2-moe') | ||
class Qwen2MoeModel(LlamaModel): | ||
|
||
Reader = Qwen2MoeReader | ||
|
||
def tokenizer_info(self): | ||
"""https://huggingface.co/Qwen/Qwen1.5-7B-Chat/blob/main/generation_con | ||
fig.json.""" # noqa: E501 | ||
n_words = 152064 | ||
bos_id = 151643 | ||
eos_id = 151645 | ||
return n_words, bos_id, eos_id | ||
|
||
def model_info(self): | ||
cfg = self.model_config | ||
info = super().model_info() | ||
info['expert_num'] = cfg['num_experts'] | ||
info['expert_inter_size'] = cfg['moe_intermediate_size'] | ||
info['experts_per_token'] = cfg['num_experts_per_tok'] | ||
info['inter_size'] = cfg['shared_expert_intermediate_size'] | ||
info['moe_shared_gate'] = True | ||
info['moe_norm_topk_prob'] = cfg['norm_topk_prob'] | ||
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. moe_norm_topk_prob is not defined in |
||
info['attn_bias'] = 1 | ||
return info |
This file contains 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.
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.
int -> bool