Skip to content

Commit

Permalink
支持直接加载Qwen(一代)的HF模型
Browse files Browse the repository at this point in the history
  • Loading branch information
cgli committed Dec 1, 2024
1 parent e6d4e56 commit 8c7eb27
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
8 changes: 4 additions & 4 deletions docs/models.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@

| 模型 | 加载后转换 | 离线转换 | 直接读取 |
|-------------------: |------------|------------|------------|
| Qwen/Qwen-7B-Chat | [](#其它模型) | [](#qwen模型导出) | |
| Qwen/Qwen-14B-Chat | [](#其它模型) | [](#qwen模型导出) | |
| Qwen/Qwen-72B-Chat | [](#其它模型) | [](#qwen模型导出) | |
| Qwen/Qwen-1_8B-Chat | [](#其它模型) | [](#qwen模型导出) | |
| Qwen/Qwen-7B-Chat | [](#其它模型) | [](#qwen模型导出) | |
| Qwen/Qwen-14B-Chat | [](#其它模型) | [](#qwen模型导出) | |
| Qwen/Qwen-72B-Chat | [](#其它模型) | [](#qwen模型导出) | |
| Qwen/Qwen-1_8B-Chat | [](#其它模型) | [](#qwen模型导出) | |
| Qwen/Qwen1.5-0.5B-Chat | [](#其它模型) | [](#qwen模型导出) | ✔<sup>3</sup> |
| Qwen/Qwen1.5-1.8B-Chat | [](#其它模型) | [](#qwen模型导出) | ✔<sup>3</sup> |
| Qwen/Qwen1.5-4B-Chat | [](#其它模型) | [](#qwen模型导出) | ✔<sup>3</sup> |
Expand Down
14 changes: 13 additions & 1 deletion src/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,18 @@ namespace fastllm {
model->history_sep = "";
model->weight.tokenizer.type = Tokenizer::TokenizerType::QWEN;
model->weight.tokenizer.chatTemplate = "";
} else if (tokenizerClass == "QWenTokenizer") {
// Qwen用的分词
std::vector <std::string> lines, line;
SplitString(ReadAllFile(path + "qwen.tiktoken"), {'\n'}, lines);
for (int i = 0; i < lines.size(); i++) {
SplitString(lines[i], {' '}, line);
model->weight.AddTokenizerWord(Base64Decode(line[0]), atoi(line[1].c_str()), 1.0f);
}
model->weight.tokenizer.type = Tokenizer::TokenizerType::QWEN;
model->weight.tokenizer.chatTemplate = "";
model->weight.dicts["im_end_id"] = std::to_string(lines.size() + 1);
model->weight.dicts["im_start_id"] = std::to_string(lines.size() + 2);
} else {
ErrorInFastLLM("Unsupport tokenizer_class: " + tokenizerClass);
}
Expand Down Expand Up @@ -637,7 +649,7 @@ namespace fastllm {
for (auto &it : generation_config.object_items()) {
if ("eos_token_id" == it.first && it.second.type() == json11::Json::ARRAY)
continue;
model->weight.AddDict(it.first, it.second.dump().c_str());
model->weight.AddDict(it.first, it.second.is_string() ? it.second.string_value() : it.second.dump());
}
// 更新eos_token_id
if (generation_config["eos_token_id"].is_array()) {
Expand Down
5 changes: 5 additions & 0 deletions src/models/qwen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ namespace fastllm {
}

weight.embeddingNames.insert("transformer.wte.weight");
weight.linearNames = {
"lm_head.weight", "transformer.h.*.ln_1.weight", "transformer.h.*.attn.c_attn.weight",
"transformer.h.*.attn.c_proj.weight", "transformer.h.*.ln_2.weight",
"transformer.h.*.mlp.w1.weight", "transformer.h.*.mlp.w2.weight", "transformer.h.*.mlp.c_proj.weight"
};
}

int QWenModel::Forward(const Data &inputIds,
Expand Down

0 comments on commit 8c7eb27

Please sign in to comment.