-
Notifications
You must be signed in to change notification settings - Fork 143
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
add support for Qwen #129
Open
cjw-d
wants to merge
17
commits into
Tencent:main
Choose a base branch
from
cjw-d:qwen
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
add support for Qwen #129
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
a08a34e
add logN-scaling
cjw-d 7f22312
add dynamic ntk
cjw-d 7240c2d
add qwen config
cjw-d da9d7b4
add qwen conversion script
cjw-d 24e00f0
modify conversion script
cjw-d af5f1ce
remove old conversion script
cjw-d e261047
add QwenTokenizer
cjw-d 97cdb6a
modify logN-scaling
cjw-d ede088f
fix bug
cjw-d 98508ac
modify logN-scaling
cjw-d a80884e
modify QwenTokenizer
cjw-d 121eb3a
Merge branch 'main' of https://github.com/Tencent/TencentPretrain
cjw-d 6b4eaff
add qwen_special_tokens_map
cjw-d 68c4ad5
add qwen config
cjw-d c2527db
refactor
cjw-d d999642
refact rope
cjw-d 7258fa2
fix file format
cjw-d 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"emb_size": 2048, | ||
"feedforward_size": 5504, | ||
"hidden_size": 2048, | ||
"hidden_act": "silu", | ||
"heads_num": 16, | ||
"layers_num": 24, | ||
"dropout": 0.0, | ||
"data_processor": "lm", | ||
"max_seq_length": 8192, | ||
"embedding": ["word"], | ||
"remove_transformer_bias": true, | ||
"remove_attention_bias": false, | ||
"remove_embedding_layernorm": true, | ||
"rotary_position_embedding": true, | ||
"encoder": "transformer", | ||
"feed_forward": "gated", | ||
"mask": "causal", | ||
"layernorm_positioning": "pre", | ||
"layernorm": "rms", | ||
"target": ["lm"], | ||
"use_logn_attn": true, | ||
"use_dynamic_ntk": true, | ||
"use_rotate_half": true | ||
} |
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"emb_size": 4096, | ||
"feedforward_size": 11008, | ||
"hidden_size": 4096, | ||
"hidden_act": "silu", | ||
"heads_num": 32, | ||
"layers_num": 32, | ||
"dropout": 0.0, | ||
"data_processor": "lm", | ||
"max_seq_length": 8192, | ||
"embedding": ["word"], | ||
"remove_transformer_bias": true, | ||
"remove_attention_bias": false, | ||
"remove_embedding_layernorm": true, | ||
"rotary_position_embedding": true, | ||
"encoder": "transformer", | ||
"feed_forward": "gated", | ||
"mask": "causal", | ||
"layernorm_positioning": "pre", | ||
"layernorm": "rms", | ||
"target": ["lm"], | ||
"use_logn_attn": true, | ||
"use_dynamic_ntk": true, | ||
"use_rotate_half": true | ||
} |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"pad_token": "<|endoftext|>", | ||
"cls_token": "<|im_start|>", | ||
"sep_token": "<|im_end|>", | ||
"sentinel_token": "<|extra_0|>" | ||
} |
54 changes: 54 additions & 0 deletions
54
scripts/convert_qwen_from_huggingface_to_tencentpretrain.py
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import argparse | ||
import os | ||
import collections | ||
from safetensors.torch import load_file | ||
import torch | ||
|
||
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) | ||
parser.add_argument("--input_model_path", type=str, default="models/input_model.bin", | ||
help=".") | ||
parser.add_argument("--output_model_path", type=str, default="models/output_model.bin", | ||
help=".") | ||
parser.add_argument("--layers_num", type=int, default=12) | ||
|
||
args = parser.parse_args() | ||
|
||
input_model = {} | ||
for file_name in os.listdir(args.input_model_path): | ||
if os.path.splitext(file_name)[-1][1:] == "safetensors": | ||
dict = load_file(filename=os.path.join(args.input_model_path, file_name)) | ||
input_model.update(dict) | ||
|
||
output_model = collections.OrderedDict() | ||
emb_size = input_model["transformer.h." + str(0) + ".attn.c_attn.weight"].shape[1] | ||
|
||
output_model["embedding.word.embedding.weight"] = input_model["transformer.wte.weight"] | ||
|
||
|
||
for i in range(args.layers_num): | ||
for j in range(3): | ||
output_model["encoder.transformer." + str(i) + ".self_attn.linear_layers." + str(j) + ".weight"] = \ | ||
input_model["transformer.h." + str(i) + ".attn.c_attn.weight"][j*emb_size:(j+1)*emb_size, :] | ||
output_model["encoder.transformer." + str(i) + ".self_attn.linear_layers." + str(j) + ".bias"] = \ | ||
input_model["transformer.h." + str(i) + ".attn.c_attn.bias"][j*emb_size:(j+1)*emb_size] | ||
|
||
output_model["encoder.transformer." + str(i) + ".self_attn.final_linear.weight"] = \ | ||
input_model["transformer.h." + str(i) + ".attn.c_proj.weight"] | ||
|
||
output_model["encoder.transformer." + str(i) + ".layer_norm_1.weight"] = \ | ||
input_model["transformer.h." + str(i) + ".ln_1.weight"] | ||
|
||
output_model["encoder.transformer." + str(i) + ".feed_forward.linear_gate.weight"] = \ | ||
input_model["transformer.h." + str(i) + ".mlp.w2.weight"] | ||
output_model["encoder.transformer." + str(i) + ".feed_forward.linear_1.weight"] = \ | ||
input_model["transformer.h." + str(i) + ".mlp.w1.weight"] | ||
output_model["encoder.transformer." + str(i) + ".feed_forward.linear_2.weight"] = \ | ||
input_model["transformer.h." + str(i) + ".mlp.c_proj.weight"] | ||
|
||
output_model["encoder.transformer." + str(i) + ".layer_norm_2.weight"] = \ | ||
input_model["transformer.h." + str(i) + ".ln_2.weight"] | ||
|
||
output_model["encoder.layer_norm.weight"] = input_model["transformer.ln_f.weight"] | ||
output_model["target.lm.output_layer.weight"] = input_model["lm_head.weight"] | ||
|
||
torch.save(output_model, args.output_model_path) |
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
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import torch | ||
|
||
def apply_logn_scaling(key_size: int, | ||
query_size: int, | ||
logn_tensor: torch.Tensor, | ||
xq: torch.Tensor | ||
) -> torch.tensor: | ||
seq_start = key_size - query_size | ||
seq_end = key_size | ||
logn_tensor = logn_tensor[:, :, seq_start:seq_end, :].type_as(xq) | ||
return xq * logn_tensor.expand_as(xq) |
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.
训练不需要ntk,只有推理需要,如果只考虑训练的话这里是否有可能简化?