|
| 1 | +#include "ernie4_5_attention.hpp" |
| 2 | + |
| 3 | +#include "../../global_state/global_state.hpp" |
| 4 | +#include "../../layers/attention/attention.hpp" |
| 5 | +#include "../../utils.hpp" |
| 6 | +#include "infinicore/ops.hpp" |
| 7 | + |
| 8 | +#include <cmath> |
| 9 | + |
| 10 | +namespace infinilm::models::ernie4_5_moe_vl { |
| 11 | + |
| 12 | +Ernie4_5Attention::Ernie4_5Attention(std::shared_ptr<infinilm::config::ModelConfig> model_config, |
| 13 | + size_t layer_idx, |
| 14 | + const infinicore::Device &device) { |
| 15 | + layer_idx_ = layer_idx; |
| 16 | + hidden_size_ = model_config->get<size_t>("hidden_size"); |
| 17 | + head_dim_ = model_config->get<size_t>("head_dim"); |
| 18 | + |
| 19 | + const auto &dtype = model_config->get_dtype(); |
| 20 | + const size_t total_num_heads = model_config->get<size_t>("num_attention_heads"); |
| 21 | + const size_t total_num_kv_heads = model_config->get<size_t>("num_key_value_heads"); |
| 22 | + const bool use_bias = model_config->get_or<bool>("attention_bias", false); |
| 23 | + const bool use_output_bias = model_config->get_or<bool>("attention_output_bias", false); |
| 24 | + const auto &config_json = model_config->get_config_json(); |
| 25 | + |
| 26 | + attention_backend_ = infinilm::global_state::get_infinilm_config().attention_backend; |
| 27 | + const engine::distributed::RankInfo &rank_info = infinilm::global_state::get_tensor_model_parallel_rank_info(); |
| 28 | + const int tp_rank = infinilm::global_state::get_tensor_model_parallel_rank(); |
| 29 | + const int tp_size = infinilm::global_state::get_tensor_model_parallel_world_size(); |
| 30 | + |
| 31 | + num_attention_heads_ = total_num_heads / tp_size; |
| 32 | + num_key_value_heads_ = total_num_kv_heads < static_cast<size_t>(tp_size) |
| 33 | + ? 1 |
| 34 | + : total_num_kv_heads / tp_size; |
| 35 | + |
| 36 | + auto quantization_method = model_config->get_quantization_method(); |
| 37 | + auto register_fn = [this](const std::string &name, infinicore::nn::Parameter parameter) { |
| 38 | + this->register_parameter(name, std::move(parameter)); |
| 39 | + }; |
| 40 | + qkv_proj_ = std::make_shared<layers::linear::QKVParallelLinear>( |
| 41 | + hidden_size_, head_dim_, total_num_heads, total_num_kv_heads, |
| 42 | + "q_proj", "k_proj", "v_proj", register_fn, |
| 43 | + quantization_method, use_bias, dtype, device, rank_info); |
| 44 | + o_proj_ = this->register_module<layers::linear::RowParallelLinear>( |
| 45 | + "o_proj", total_num_heads * head_dim_, hidden_size_, quantization_method, |
| 46 | + use_output_bias, dtype, device, tp_rank, tp_size, rank_info.comm); |
| 47 | + |
| 48 | + rotary_emb_ = infinilm::layers::rotary_embedding::get_rope(model_config, device); |
| 49 | + rope_theta_ = model_config->get_or<double>("rope_theta", 500000.0); |
| 50 | + if (config_json.contains("rope_scaling") && config_json["rope_scaling"].contains("mrope_section")) { |
| 51 | + const auto §ions = config_json["rope_scaling"]["mrope_section"]; |
| 52 | + if (sections.is_array() && sections.size() == 3) { |
| 53 | + mrope_section_h_ = sections.at(0).get<size_t>(); |
| 54 | + mrope_section_w_ = sections.at(1).get<size_t>(); |
| 55 | + mrope_section_t_ = sections.at(2).get<size_t>(); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + const float scaling = 1.0f / std::sqrt(static_cast<float>(head_dim_)); |
| 60 | + attn_ = std::make_shared<infinilm::layers::attention::AttentionLayer>( |
| 61 | + num_attention_heads_, head_dim_, scaling, num_key_value_heads_, layer_idx_, |
| 62 | + kv_cache_k_scale_, kv_cache_v_scale_, attention_backend_); |
| 63 | + |
| 64 | + infinilm::layers::attention::init_kv_cache_quant_params(register_fn, device, kv_cache_k_scale_, kv_cache_v_scale_); |
| 65 | +} |
| 66 | + |
| 67 | +infinicore::Tensor Ernie4_5Attention::forward(const infinicore::Tensor &positions, |
| 68 | + const infinicore::Tensor &hidden_states) const { |
| 69 | + if (::infinilm::backends::AttentionBackend::STATIC_ATTN == attention_backend_) { |
| 70 | + return forward_static_(positions, hidden_states); |
| 71 | + } |
| 72 | + return forward_paged_(positions, hidden_states); |
| 73 | +} |
| 74 | + |
| 75 | +infinicore::Tensor Ernie4_5Attention::forward_static_(const infinicore::Tensor &position_ids, |
| 76 | + const infinicore::Tensor &hidden_states) const { |
| 77 | + auto hidden_states_mutable = hidden_states; |
| 78 | + const auto shape = hidden_states->shape(); |
| 79 | + const size_t batch_size = shape[0]; |
| 80 | + const size_t seq_len = shape[1]; |
| 81 | + |
| 82 | + auto [q, k, v] = qkv_proj_->forward_split(hidden_states_mutable); |
| 83 | + auto q_reshaped = q->view({batch_size, seq_len, num_attention_heads_, head_dim_}); |
| 84 | + auto k_reshaped = k->view({batch_size, seq_len, num_key_value_heads_, head_dim_}); |
| 85 | + auto v_reshaped = v->view({batch_size, seq_len, num_key_value_heads_, head_dim_}); |
| 86 | + |
| 87 | + auto pos_shape = position_ids->shape(); |
| 88 | + if ((pos_shape.size() == 3 && pos_shape[0] == 1 && pos_shape[1] == seq_len && pos_shape[2] == 3) || (pos_shape.size() == 2 && pos_shape[0] == seq_len && pos_shape[1] == 3)) { |
| 89 | + ASSERT_EQ(batch_size, 1); |
| 90 | + auto q_mrope = q_reshaped->squeeze(0); |
| 91 | + auto k_mrope = k_reshaped->squeeze(0); |
| 92 | + infinicore::Tensor pos_ids_for_mrope = position_ids; |
| 93 | + if (pos_shape.size() == 3) { |
| 94 | + pos_ids_for_mrope = position_ids->squeeze(0); |
| 95 | + } |
| 96 | + infinicore::op::ernie45_mrope_( |
| 97 | + q_mrope, k_mrope, pos_ids_for_mrope, |
| 98 | + rope_theta_, mrope_section_h_, mrope_section_w_, mrope_section_t_); |
| 99 | + auto attn_output = attn_->forward(q_reshaped, k_reshaped, v_reshaped); |
| 100 | + return o_proj_->forward(attn_output); |
| 101 | + } |
| 102 | + |
| 103 | + infinicore::Tensor pos_ids_for_rope = position_ids; |
| 104 | + if (pos_shape.size() == 2) { |
| 105 | + auto pos_narrowed = position_ids->narrow({{0, 0, 1}}); |
| 106 | + pos_ids_for_rope = pos_narrowed->contiguous()->view({pos_shape[1]}); |
| 107 | + } else if (pos_shape.size() == 1) { |
| 108 | + pos_ids_for_rope = position_ids->contiguous(); |
| 109 | + } else { |
| 110 | + throw std::runtime_error("Ernie4_5Attention: Unexpected position_ids shape"); |
| 111 | + } |
| 112 | + |
| 113 | + 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}); |
| 114 | + rotary_emb_->forward(q_rope, q_reshaped, pos_ids_for_rope); |
| 115 | + rotary_emb_->forward(k_reshaped, pos_ids_for_rope, true); |
| 116 | + |
| 117 | + auto attn_output = attn_->forward(q_rope, k_reshaped, v_reshaped); |
| 118 | + return o_proj_->forward(attn_output); |
| 119 | +} |
| 120 | + |
| 121 | +infinicore::Tensor Ernie4_5Attention::forward_paged_(const infinicore::Tensor &position_ids, |
| 122 | + const infinicore::Tensor &hidden_states) const { |
| 123 | + auto hidden_states_mutable = hidden_states; |
| 124 | + const auto shape = hidden_states->shape(); |
| 125 | + const size_t batch_size = shape[0]; |
| 126 | + const size_t seq_len = shape[1]; |
| 127 | + |
| 128 | + ASSERT_EQ(batch_size, 1); |
| 129 | + |
| 130 | + auto [q, k, v] = qkv_proj_->forward_split(hidden_states_mutable); |
| 131 | + auto q_reshaped = q->view({seq_len, num_attention_heads_, head_dim_}); |
| 132 | + auto k_reshaped = k->view({seq_len, num_key_value_heads_, head_dim_}); |
| 133 | + auto v_reshaped = v->view({seq_len, num_key_value_heads_, head_dim_}); |
| 134 | + |
| 135 | + auto pos_shape = position_ids->shape(); |
| 136 | + if ((pos_shape.size() == 3 && pos_shape[0] == 1 && pos_shape[1] == seq_len && pos_shape[2] == 3) || (pos_shape.size() == 2 && pos_shape[0] == seq_len && pos_shape[1] == 3)) { |
| 137 | + infinicore::Tensor pos_ids_for_mrope = position_ids; |
| 138 | + if (pos_shape.size() == 3) { |
| 139 | + pos_ids_for_mrope = position_ids->squeeze(0); |
| 140 | + } |
| 141 | + infinicore::op::ernie45_mrope_( |
| 142 | + q_reshaped, k_reshaped, pos_ids_for_mrope, |
| 143 | + rope_theta_, mrope_section_h_, mrope_section_w_, mrope_section_t_); |
| 144 | + auto attn_output = attn_->forward(q_reshaped, k_reshaped, v_reshaped); |
| 145 | + return o_proj_->forward(attn_output); |
| 146 | + } |
| 147 | + |
| 148 | + infinicore::Tensor pos_ids_for_rope = position_ids; |
| 149 | + if (pos_shape.size() == 2) { |
| 150 | + auto pos_narrowed = position_ids->narrow({{0, 0, 1}}); |
| 151 | + pos_ids_for_rope = pos_narrowed->view({pos_shape[1]}); |
| 152 | + } else if (pos_shape.size() != 1) { |
| 153 | + throw std::runtime_error("Ernie4_5Attention: Unexpected position_ids shape"); |
| 154 | + } |
| 155 | + |
| 156 | + rotary_emb_->forward(q_reshaped, pos_ids_for_rope, true); |
| 157 | + rotary_emb_->forward(k_reshaped, pos_ids_for_rope, true); |
| 158 | + |
| 159 | + auto attn_output = attn_->forward(q_reshaped, k_reshaped, v_reshaped); |
| 160 | + return o_proj_->forward(attn_output); |
| 161 | +} |
| 162 | + |
| 163 | +} // namespace infinilm::models::ernie4_5_moe_vl |
0 commit comments