-
Notifications
You must be signed in to change notification settings - Fork 74
feat: support qwen3.5 llm #447
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
PanZezhong1725
wants to merge
4
commits into
main
Choose a base branch
from
issue/446
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
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| #include "mamba_cache.hpp" | ||
|
|
||
| #include "../global_state/global_state.hpp" | ||
| #include "infinicore/context/context.hpp" | ||
|
|
||
| namespace infinilm::cache { | ||
|
|
||
| infinicore::Tensor MambaCache::create_layer_conv_state( | ||
| infinicore::Size k_dim, | ||
| infinicore::Size v_dim, | ||
| infinicore::Size num_k_heads, | ||
| infinicore::Size num_v_heads, | ||
| infinicore::Size conv_kernel_dim, | ||
| infinicore::DataType dtype, | ||
| size_t pool_size) { | ||
| const engine::distributed::RankInfo &rank_info = infinilm::global_state::get_tensor_model_parallel_rank_info(); | ||
| const auto [num_rank_k_heads, num_rank_v_heads] = get_rank_head_counts(num_k_heads, num_v_heads, rank_info.tp_size); | ||
| const size_t conv_state_len = conv_kernel_dim > 0 ? conv_kernel_dim - 1 : 0; | ||
| const size_t conv_dim = 2 * num_rank_k_heads * k_dim + num_rank_v_heads * v_dim; | ||
|
|
||
| auto conv_state = infinicore::Tensor::zeros( | ||
| {pool_size, conv_dim, conv_state_len}, | ||
| dtype, | ||
| rank_info.device); | ||
| infinicore::context::syncStream(); | ||
| return conv_state; | ||
| } | ||
|
|
||
| infinicore::Tensor MambaCache::create_layer_ssm_state( | ||
| infinicore::Size k_dim, | ||
| infinicore::Size v_dim, | ||
| infinicore::Size num_k_heads, | ||
| infinicore::Size num_v_heads, | ||
| infinicore::DataType dtype, | ||
| size_t pool_size) { | ||
| const engine::distributed::RankInfo &rank_info = infinilm::global_state::get_tensor_model_parallel_rank_info(); | ||
| const auto rank_head_counts = get_rank_head_counts(num_k_heads, num_v_heads, rank_info.tp_size); | ||
| const size_t num_rank_v_heads = rank_head_counts.second; | ||
|
|
||
| auto ssm_state = infinicore::Tensor::zeros( | ||
| {pool_size, num_rank_v_heads, v_dim, k_dim}, | ||
| dtype, | ||
| rank_info.device); | ||
|
|
||
| infinicore::context::syncStream(); | ||
| return ssm_state; | ||
| } | ||
|
|
||
| std::pair<size_t, size_t> MambaCache::get_rank_head_counts( | ||
| infinicore::Size num_k_heads, | ||
| infinicore::Size num_v_heads, | ||
| size_t tp_size) { | ||
| bool is_kv_replica = (num_k_heads < tp_size && num_v_heads < tp_size && num_k_heads == num_v_heads && tp_size % num_k_heads == 0); | ||
| size_t num_rank_k_heads = is_kv_replica ? 1 : (num_k_heads / tp_size); | ||
| size_t num_rank_v_heads = is_kv_replica ? 1 : (num_v_heads / tp_size); | ||
| return {num_rank_k_heads, num_rank_v_heads}; | ||
| } | ||
|
|
||
| } // namespace infinilm::cache |
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,39 @@ | ||
| #pragma once | ||
|
|
||
| #include "base_cache.hpp" | ||
|
|
||
| #include "infinicore/device.hpp" | ||
| #include "infinicore/tensor.hpp" | ||
| #include <cstddef> | ||
| #include <infinicore/dtype.hpp> | ||
| #include <utility> | ||
|
|
||
| namespace infinilm::cache { | ||
|
|
||
| class MambaCache { | ||
| public: | ||
| static infinicore::Tensor create_layer_conv_state( | ||
| infinicore::Size k_dim, | ||
| infinicore::Size v_dim, | ||
| infinicore::Size num_k_heads, | ||
| infinicore::Size num_v_heads, | ||
| infinicore::Size conv_kernel_dim, | ||
| infinicore::DataType dtype, | ||
| size_t pool_size); | ||
|
|
||
| static infinicore::Tensor create_layer_ssm_state( | ||
| infinicore::Size k_dim, | ||
| infinicore::Size v_dim, | ||
| infinicore::Size num_k_heads, | ||
| infinicore::Size num_v_heads, | ||
| infinicore::DataType dtype, | ||
| size_t pool_size); | ||
|
|
||
| private: | ||
| static std::pair<size_t, size_t> get_rank_head_counts( | ||
| infinicore::Size num_k_heads, | ||
| infinicore::Size num_v_heads, | ||
| size_t tp_size); | ||
| }; | ||
|
|
||
| } // namespace infinilm::cache |
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
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,140 @@ | ||
| #include "qwen3_5_attention.hpp" | ||
| #include "../../global_state/global_state.hpp" | ||
| #include "../../layers/attention/attention.hpp" | ||
| #include "../../utils.hpp" | ||
| #include <infinicore/ops/mul.hpp> | ||
| #include <infinicore/ops/sigmoid.hpp> | ||
|
|
||
| namespace infinilm::models::qwen3_5 { | ||
|
|
||
| Qwen35Attention::Qwen35Attention(std::shared_ptr<infinilm::config::ModelConfig> model_config, | ||
| size_t layer_idx, | ||
| const infinicore::Device &device) { | ||
| layer_idx_ = layer_idx; | ||
| hidden_size_ = model_config->get<size_t>("hidden_size"); | ||
| head_dim_ = model_config->get<size_t>("head_dim"); | ||
| rotary_dim_ = model_config->get_rotary_dim(); | ||
|
|
||
| const auto &dtype{model_config->get_dtype()}; | ||
| size_t total_num_heads = model_config->get<size_t>("num_attention_heads"); | ||
| size_t total_num_kv_heads = model_config->get<size_t>("num_key_value_heads"); | ||
| bool use_bias = model_config->get_or<bool>("attention_bias", true); | ||
| bool use_output_bias = model_config->get_or<bool>("attention_output_bias", false); | ||
| double rms_norm_eps = model_config->get<double>("rms_norm_eps"); | ||
|
|
||
| attention_backend_ = infinilm::global_state::get_infinilm_config().attention_backend; | ||
| const engine::distributed::RankInfo &rank_info = infinilm::global_state::get_tensor_model_parallel_rank_info(); | ||
| int tp_rank = infinilm::global_state::get_tensor_model_parallel_rank(); | ||
| int tp_size = infinilm::global_state::get_tensor_model_parallel_world_size(); | ||
| if ((total_num_kv_heads < tp_size) || (0 != (total_num_kv_heads % tp_size))) { | ||
| throw std::runtime_error("infinilm::models::qwen3_5::Qwen35Attention: num_key_value_heads must be divisible by tp_size"); | ||
| } | ||
|
|
||
| num_attention_heads_ = total_num_heads / tp_size; | ||
| num_key_value_heads_ = total_num_kv_heads / tp_size; | ||
|
|
||
| auto quantization_method = model_config->get_quantization_method(); | ||
| auto register_fn = [this](const std::string &n, infinicore::nn::Parameter p) { this->register_parameter(n, std::move(p)); }; | ||
| qkv_proj_ = std::make_shared<Qwen35FusedQKVLinear>( | ||
| hidden_size_, head_dim_, total_num_heads, total_num_kv_heads, | ||
| "q_proj", "k_proj", "v_proj", register_fn, | ||
| quantization_method, use_bias, dtype, device, rank_info); | ||
| o_proj_ = this->register_module<layers::linear::RowParallelLinear>( | ||
| "o_proj", total_num_heads * head_dim_, hidden_size_, quantization_method, | ||
| use_output_bias, dtype, device, tp_rank, tp_size, rank_info.comm); | ||
|
|
||
| rotary_emb_ = infinilm::layers::rotary_embedding::get_rope(model_config, device); | ||
|
|
||
| float scaling = 1.0f / std::sqrt(static_cast<float>(head_dim_)); | ||
| attn_ = std::make_shared<infinilm::layers::attention::AttentionLayer>(num_attention_heads_, head_dim_, scaling, num_key_value_heads_, layer_idx_, | ||
| kv_cache_k_scale_, kv_cache_v_scale_, attention_backend_); | ||
|
|
||
| INFINICORE_NN_MODULE_INIT(q_norm, head_dim_, rms_norm_eps, dtype, device); | ||
| INFINICORE_NN_MODULE_INIT(k_norm, head_dim_, rms_norm_eps, dtype, device); | ||
|
|
||
| infinilm::layers::attention::init_kv_cache_quant_params(register_fn, device, kv_cache_k_scale_, kv_cache_v_scale_); | ||
| } | ||
|
|
||
| infinicore::Tensor Qwen35Attention::forward(const infinicore::Tensor &positions, | ||
| const infinicore::Tensor &hidden_states) const { | ||
| if (::infinilm::backends::AttentionBackend::STATIC_ATTN == attention_backend_) { | ||
| return forward_static_(positions, hidden_states); | ||
| } | ||
| return forward_paged_(positions, hidden_states); | ||
| } | ||
|
|
||
| infinicore::Tensor Qwen35Attention::forward_static_(const infinicore::Tensor &position_ids, | ||
| const infinicore::Tensor &hidden_states) const { | ||
| auto hidden_states_mutable = hidden_states; | ||
| auto shape = hidden_states->shape(); | ||
| size_t batch_size = shape[0]; | ||
| size_t seq_len = shape[1]; | ||
|
|
||
| auto [q, gate, k, v] = qkv_proj_->forward_split(hidden_states_mutable); | ||
|
|
||
| q = q_norm_->forward(q->view({batch_size * seq_len, num_attention_heads_, head_dim_})); | ||
| k = k_norm_->forward(k->view({batch_size * seq_len, num_key_value_heads_, head_dim_})); | ||
|
|
||
| auto q_reshaped = q->view({batch_size, seq_len, num_attention_heads_, head_dim_}); | ||
| auto k_reshaped = k->view({batch_size, seq_len, num_key_value_heads_, head_dim_}); | ||
| auto v_reshaped = v->view({batch_size, seq_len, num_key_value_heads_, head_dim_}); | ||
|
|
||
| auto pos_shape = position_ids->shape(); | ||
| infinicore::Tensor pos_ids_for_rope = position_ids; | ||
| if (pos_shape.size() == 2) { | ||
| auto pos_narrowed = position_ids->narrow({{0, 0, 1}}); | ||
| pos_ids_for_rope = pos_narrowed->view({pos_shape[1]}); | ||
| } else if (pos_shape.size() == 1) { | ||
| pos_ids_for_rope = position_ids; | ||
| } else { | ||
| throw std::runtime_error("infinilm::models::qwen3_5::Qwen35Attention: Unexpected position_ids shape"); | ||
| } | ||
|
|
||
| auto q_rotary = q_reshaped->narrow({{3, 0, rotary_dim_}}); | ||
| auto k_rotary = k_reshaped->narrow({{3, 0, rotary_dim_}}); | ||
| rotary_emb_->forward(q_rotary, pos_ids_for_rope, true); | ||
| rotary_emb_->forward(k_rotary, pos_ids_for_rope, true); | ||
|
|
||
| auto attn_output = attn_->forward(q_reshaped, k_reshaped, v_reshaped); | ||
| attn_output = infinicore::op::mul(attn_output, infinicore::op::sigmoid(gate)); | ||
| return o_proj_->forward(attn_output); | ||
| } | ||
|
|
||
| infinicore::Tensor Qwen35Attention::forward_paged_(const infinicore::Tensor &position_ids, | ||
| const infinicore::Tensor &hidden_states) const { | ||
| auto hidden_states_mutable = hidden_states; | ||
| auto shape = hidden_states->shape(); | ||
| size_t batch_size = shape[0]; | ||
| size_t seq_len = shape[1]; | ||
|
|
||
| ASSERT_EQ(batch_size, 1); | ||
|
|
||
| auto [q, gate, k, v] = qkv_proj_->forward_split(hidden_states_mutable); | ||
|
|
||
| auto q_reshaped = q->view({seq_len, num_attention_heads_, head_dim_}); | ||
| auto k_reshaped = k->view({seq_len, num_key_value_heads_, head_dim_}); | ||
| auto v_reshaped = v->view({seq_len, num_key_value_heads_, head_dim_}); | ||
| q_reshaped = q_norm_->forward(q_reshaped); | ||
| k_reshaped = k_norm_->forward(k_reshaped); | ||
|
|
||
| auto pos_shape = position_ids->shape(); | ||
| infinicore::Tensor pos_ids_for_rope = position_ids; | ||
| if (pos_shape.size() == 2) { | ||
| auto pos_narrowed = position_ids->narrow({{0, 0, 1}}); | ||
| pos_ids_for_rope = pos_narrowed->view({pos_shape[1]}); | ||
| } else if (pos_shape.size() == 1) { | ||
| pos_ids_for_rope = position_ids; | ||
| } else { | ||
| throw std::runtime_error("Unexpected position_ids shape"); | ||
| } | ||
|
|
||
| auto q_rotary = q_reshaped->narrow({{2, 0, rotary_dim_}}); | ||
| auto k_rotary = k_reshaped->narrow({{2, 0, rotary_dim_}}); | ||
| rotary_emb_->forward(q_rotary, pos_ids_for_rope, true); | ||
| rotary_emb_->forward(k_rotary, pos_ids_for_rope, true); | ||
|
|
||
| auto attn_output = attn_->forward(q_reshaped, k_reshaped, v_reshaped); | ||
| attn_output = infinicore::op::mul(attn_output, infinicore::op::sigmoid(gate)->view(attn_output->shape())); | ||
| return o_proj_->forward(attn_output); | ||
| } | ||
| } // namespace infinilm::models::qwen3_5 |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
init_done_ = true; 是不是因为这个位置是本应该加,但是每加。
该pr中顺手加了么
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.
是