-
Notifications
You must be signed in to change notification settings - Fork 65
issue/349 - add GLM4 causal LM model support #352
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
JoeZhang-0x000
wants to merge
1
commit into
InfiniTensor:main
Choose a base branch
from
JoeZhang-0x000:issue/349
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| #include "glm4_for_causal_lm.hpp" | ||
| #include "../llama/llama_for_causal_lm.hpp" | ||
| #include "../models_registry.hpp" | ||
|
|
||
| namespace infinilm::models::glm4 { | ||
|
|
||
| std::shared_ptr<infinilm::config::ModelConfig> create_glm4_model_config( | ||
| std::shared_ptr<infinilm::config::ModelConfig> model_config) { | ||
| const std::string &model_type = model_config->get<std::string>("model_type"); | ||
| if ("glm4" != model_type) { | ||
| throw std::runtime_error( | ||
| "infinilm::models::glm4::create_glm4_model_config: model_type is not glm4"); | ||
| } | ||
|
|
||
| nlohmann::json &config_json = model_config->get_config_json(); | ||
|
|
||
| if (!config_json.contains("head_dim")) { | ||
| config_json["head_dim"] = model_config->get<size_t>("hidden_size") | ||
| / model_config->get<size_t>("num_attention_heads"); | ||
| } | ||
|
|
||
| if (!config_json.contains("attention_bias")) { | ||
| config_json["attention_bias"] = true; | ||
| } | ||
|
|
||
| return model_config; | ||
| } | ||
|
|
||
| } // namespace infinilm::models::glm4 | ||
|
|
||
| namespace { | ||
|
|
||
| #ifndef USE_CLASSIC_LLAMA | ||
|
|
||
| INFINILM_REGISTER_CAUSAL_LM_MODEL( | ||
| glm4, | ||
| infinilm::models::llama::LlamaForCausalLM, | ||
| infinilm::models::glm4::create_glm4_model_config); | ||
|
|
||
| #endif | ||
|
|
||
| } // namespace |
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,11 @@ | ||
| #pragma once | ||
|
|
||
| #include "../../layers/common_modules.hpp" | ||
| #include <memory> | ||
|
|
||
| namespace infinilm::models::glm4 { | ||
|
|
||
| std::shared_ptr<infinilm::config::ModelConfig> create_glm4_model_config( | ||
| std::shared_ptr<infinilm::config::ModelConfig> model_config); | ||
|
|
||
| } // namespace infinilm::models::glm4 |
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,6 +1,7 @@ | ||
| #include "llama_attention.hpp" | ||
|
|
||
| #include "../../utils.hpp" | ||
| #include "llama_utils.hpp" | ||
| #include "infinicore/nn/linear.hpp" | ||
| #include "infinicore/nn/rope.hpp" | ||
| #include "infinicore/ops.hpp" | ||
|
|
@@ -42,6 +43,7 @@ LlamaAttention::LlamaAttention(const LlamaConfig &config, | |
| num_attention_heads_(config.num_attention_heads), | ||
| num_key_value_heads_(config.num_key_value_heads), | ||
| head_dim_(config.head_dim), | ||
| rotary_dim_(config.head_dim), | ||
| kv_dim_(config.kv_dim()), | ||
| use_bias_(config.attention_bias), | ||
| use_output_bias_(config.attention_output_bias), | ||
|
|
@@ -90,6 +92,7 @@ LlamaAttention::LlamaAttention(std::shared_ptr<infinilm::config::ModelConfig> mo | |
| num_attention_heads_(model_config->get<size_t>("num_attention_heads")), | ||
| num_key_value_heads_(model_config->get<size_t>("num_key_value_heads")), | ||
| head_dim_(model_config->get_head_dim()), | ||
| rotary_dim_(get_rotary_dim(model_config->get_head_dim(), model_config->get_or<double>("partial_rotary_factor", 1.0))), | ||
| kv_dim_(model_config->get_kv_dim()), | ||
| use_bias_(model_config->get_or<bool>("attention_bias", true)), | ||
| use_output_bias_(model_config->get_or<bool>("attention_output_bias", false)), | ||
|
|
@@ -204,8 +207,21 @@ infinicore::Tensor LlamaAttention::forward_(const infinicore::Tensor &hidden_sta | |
|
|
||
| // 4. Apply RoPE to Q and K | ||
| auto q_rope = infinicore::Tensor::empty({batch_size, num_attention_heads_, seq_len, head_dim_}, q_reshaped->dtype(), q_reshaped->device())->permute({0, 2, 1, 3}); | ||
| rotary_emb_->forward(q_rope, q_reshaped, pos_ids_for_rope); // [bs, seq_len, n_q_head, head_dim] | ||
| rotary_emb_->forward(k_reshaped, pos_ids_for_rope, true); // [bs, seq_len, n_kv_head, head_dim] | ||
| q_rope->copy_from(q_reshaped); | ||
|
|
||
| auto k_rope = infinicore::Tensor::empty({batch_size, seq_len, num_key_value_heads_, head_dim_}, k_reshaped->dtype(), k_reshaped->device()); | ||
| k_rope->copy_from(k_reshaped); | ||
|
|
||
| if (rotary_dim_ == head_dim_) { | ||
| rotary_emb_->forward(q_rope, q_reshaped, pos_ids_for_rope); // [bs, seq_len, n_q_head, head_dim] | ||
| rotary_emb_->forward(k_rope, pos_ids_for_rope, true); // [bs, seq_len, n_kv_head, head_dim] | ||
| } else { | ||
| rotary_emb_->forward( | ||
| q_rope->narrow({{3, 0, rotary_dim_}}), | ||
| q_reshaped->narrow({{3, 0, rotary_dim_}}), | ||
| pos_ids_for_rope); | ||
| rotary_emb_->forward(k_rope->narrow({{3, 0, rotary_dim_}}), pos_ids_for_rope, true); | ||
| } | ||
|
|
||
| infinilm::KVQuantUtils::quantize( | ||
| k_reshaped, v_reshaped, | ||
|
|
@@ -217,7 +233,7 @@ infinicore::Tensor LlamaAttention::forward_(const infinicore::Tensor &hidden_sta | |
| // Convert to [batch, n_head, seq_len, head_dim] for cache | ||
| // Ensure contiguous after permute for F16 compatibility with cache operations | ||
| q_reshaped = q_rope->permute({0, 2, 1, 3}); // [bs, n_q_head, seq_len, head_dim] | ||
| auto k_permuted = k_reshaped->permute({0, 2, 1, 3}); // [bs, n_kv_head, seq_len, head_dim] | ||
| auto k_permuted = k_rope->permute({0, 2, 1, 3}); // [bs, n_kv_head, seq_len, head_dim] | ||
| auto v_permuted = v_reshaped->permute({0, 2, 1, 3}); // [bs, n_kv_head, seq_len, head_dim] | ||
| infinicore::Tensor k_total; // [bs, n_kv_head, max_seq_len, head_dim] | ||
| infinicore::Tensor v_total; // [bs, n_kv_head, max_seq_len, head_dim] | ||
|
|
@@ -330,8 +346,19 @@ infinicore::Tensor LlamaAttention::forward_paged_(const infinicore::Tensor &hidd | |
| } | ||
|
|
||
| // 4. Apply RoPE to Q and K | ||
| rotary_emb_->forward(q_reshaped, pos_ids_for_rope, true); // [bs, seq_len, n_q_head, head_dim] | ||
| rotary_emb_->forward(k_reshaped, pos_ids_for_rope, true); // [bs, seq_len, n_kv_head, head_dim] | ||
| if (rotary_dim_ == head_dim_) { | ||
| rotary_emb_->forward(q_reshaped, pos_ids_for_rope, true); // [seq_len, n_q_head, head_dim] | ||
| rotary_emb_->forward(k_reshaped, pos_ids_for_rope, true); // [seq_len, n_kv_head, head_dim] | ||
| } else { | ||
| auto q_rope = infinicore::Tensor::empty({seq_len, num_attention_heads_, head_dim_}, q_reshaped->dtype(), q_reshaped->device()); | ||
| q_rope->copy_from(q_reshaped); | ||
| auto k_rope = infinicore::Tensor::empty({seq_len, num_key_value_heads_, head_dim_}, k_reshaped->dtype(), k_reshaped->device()); | ||
| k_rope->copy_from(k_reshaped); | ||
| rotary_emb_->forward(q_rope->narrow({{2, 0, rotary_dim_}}), pos_ids_for_rope, true); | ||
| rotary_emb_->forward(k_rope->narrow({{2, 0, rotary_dim_}}), pos_ids_for_rope, true); | ||
| q_reshaped = q_rope; | ||
| k_reshaped = k_rope; | ||
|
Collaborator
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. 这个地方新建了两个变量q_rope 和k_rope ,他们是q_reshaped和k_reshaped的赋值。 请问rotary_emb_->forward可以直接操作q_reshaped和k_reshaped么,不新建变量可以么 |
||
| } | ||
|
|
||
| // 5. Prepare KV caches | ||
| // Ensure contiguous after permute for F16 compatibility with cache operations | ||
|
|
||
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 |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| #pragma once | ||
|
|
||
| #include <algorithm> | ||
| #include <cmath> | ||
| #include <cstddef> | ||
|
|
||
| namespace infinilm::models::llama_legacy { | ||
|
|
||
| inline size_t get_rotary_dim(size_t head_dim, double partial_rotary_factor) { | ||
| if (partial_rotary_factor <= 0.0 || partial_rotary_factor >= 1.0) { | ||
| return head_dim; | ||
| } | ||
|
|
||
| size_t rotary_dim = static_cast<size_t>(std::llround( | ||
| static_cast<double>(head_dim) * partial_rotary_factor)); | ||
| rotary_dim = std::clamp(rotary_dim, static_cast<size_t>(2), head_dim); | ||
| if (rotary_dim % 2 != 0) { | ||
| rotary_dim -= 1; | ||
| } | ||
| return std::max(rotary_dim, static_cast<size_t>(2)); | ||
| } | ||
|
|
||
| } // namespace infinilm::models::llama_legacy |
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.
原则上讲尽量不要修改和使用llama legacy,那段旧版代码不确定什么时候就给删了。
能摆脱对llama legacy的依赖的话,应该就不需要在config factory, rank worker和auto config中做修改了