char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
98a01afd9
char/aliceai-t5-moe
1#include "models.h" 2 3#include <type_traits> 4 5void llama_model_aliceai_t5_moe::load_arch_hparams (llama_model_loader & ml ) { 6ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps ); 7ml .get_key (LLM_KV_DECODER_BLOCK_COUNT ,hparams .dec_n_layer ); 8uint32_t decoder_start_token_id ; 9ml .get_key (LLM_KV_DECODER_START_TOKEN_ID ,decoder_start_token_id ); 10hparams .dec_start_token_id = decoder_start_token_id ; 11ml .get_key (LLM_KV_ENCODER_ATTENTION_HEAD_COUNT_KV ,n_head_kv_enc ); 12ml .get_key_or_arr (LLM_KV_EXPERT_FEED_FORWARD_LENGTH ,hparams .n_ff_exp_arr ,hparams .n_layer_all ); 13ml .get_key (LLM_KV_EXPERT_WEIGHTS_SCALE ,hparams .expert_weights_scale ); 14ml .get_key (LLM_KV_EXPERT_WEIGHTS_NORM ,hparams .expert_weights_norm ); 15ml .get_key (LLM_KV_EXPERT_GATING_FUNC ,hparams .expert_gating_func ); 16 17if (hparams .dec_n_layer == 0 || hparams .dec_n_layer > hparams .n_layer ()) { 18throw std::runtime_error ("AliceAI requires 0 < decoder_block_count <= block_count" ); 19 } 20if (hparams .n_head ()== 0 || n_head_kv_enc == 0 || hparams .n_head () %n_head_kv_enc != 0 || 21hparams .n_head_kv ()== 0 || hparams .n_head () %hparams .n_head_kv ()!= 0 ) { 22throw std::runtime_error ("AliceAI KV head counts must divide the query head count" ); 23 } 24if (hparams .n_expert == 0 || hparams .n_ff_exp ()== 0 || 25hparams .expert_gating_func != LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID ) { 26throw std::runtime_error ("AliceAI requires sigmoid routing and nonempty experts" ); 27 } 28 29if (hparams .rope_scaling_type_train == LLAMA_ROPE_SCALING_TYPE_YARN ) { 30ml .get_key (LLM_KV_ROPE_SCALING_YARN_ATTN_FACTOR ,hparams .rope_attn_factor ); 31// GGML applies the default YaRN magnitude inside RoPE; metadata gives the total magnitude. 32hparams .rope_attn_factor /=1.0f + 0.1f * logf (1.0f /hparams .rope_freq_scale_train ); 33 } 34 35// The published causal flag describes the encoder. llama_encode disables causality separately. 36hparams .causal_attn = true; 37type = LLM_TYPE_UNKNOWN ; 38} 39 40void llama_model_aliceai_t5_moe::load_arch_tensors (llama_model_loader & ) { 41LLAMA_LOAD_LOCALS ; 42 43if (hparams .dec_start_token_id < 0 || hparams .dec_start_token_id >=n_vocab ) { 44throw std::runtime_error ("AliceAI decoder start token is outside the vocabulary" ); 45 } 46if (n_embd_head_k != n_embd_head_v || n_embd_head_k == 0 ) { 47throw std::runtime_error ("AliceAI requires equal, nonzero key and value head dimensions" ); 48 } 49 50const int64_t n_ff_exp = hparams .n_ff_exp (); 51const int64_t n_embd_q = n_embd_head_k * n_head ; 52const int64_t n_embd_k_enc = n_embd_head_k * n_head_kv_enc ; 53const int64_t n_embd_v_enc = n_embd_head_v * n_head_kv_enc ; 54 55tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 56output_norm_enc = create_tensor (tn (LLM_TENSOR_ENC_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 57output_norm = create_tensor (tn (LLM_TENSOR_DEC_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 58output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED ); 59if (output == nullptr ) { 60output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED ); 61 } 62 63layers_enc .resize (n_layer ); 64for (int i = 0 ;i < n_layer ;++ i ) { 65auto & layer = layers_enc [i ]; 66 67layer .attn_norm = create_tensor (tn (LLM_TENSOR_ENC_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 68layer .wq = create_tensor (tn (LLM_TENSOR_ENC_ATTN_Q ,"weight" ,i ), {n_embd ,n_embd_q },0 ); 69layer .wk = create_tensor (tn (LLM_TENSOR_ENC_ATTN_K ,"weight" ,i ), {n_embd ,n_embd_k_enc },0 ); 70layer .wv = create_tensor (tn (LLM_TENSOR_ENC_ATTN_V ,"weight" ,i ), {n_embd ,n_embd_v_enc },0 ); 71layer .wo = create_tensor (tn (LLM_TENSOR_ENC_ATTN_OUT ,"weight" ,i ), {n_embd_head_v * n_head ,n_embd },0 ); 72 73layer .ffn_norm = create_tensor (tn (LLM_TENSOR_ENC_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 74layer .ffn_gate_inp = create_tensor (tn (LLM_TENSOR_ENC_FFN_GATE_INP ,"weight" ,i ), {n_embd ,n_expert },0 ); 75layer .ffn_gate_exps = create_tensor (tn (LLM_TENSOR_ENC_FFN_GATE_EXPS ,"weight" ,i ), {n_embd ,n_ff_exp ,n_expert },0 ); 76layer .ffn_up_exps = create_tensor (tn (LLM_TENSOR_ENC_FFN_UP_EXPS ,"weight" ,i ), {n_embd ,n_ff_exp ,n_expert },0 ); 77layer .ffn_down_exps = create_tensor (tn (LLM_TENSOR_ENC_FFN_DOWN_EXPS ,"weight" ,i ), {n_ff_exp ,n_embd ,n_expert },0 ); 78layer .ffn_exp_probs_b = create_tensor (tn (LLM_TENSOR_ENC_EXP_PROBS_B ,i ), {n_expert },0 ); 79layer .ffn_down_b = create_tensor (tn (LLM_TENSOR_ENC_FFN_OUTPUT_B ,i ), {n_embd },0 ); 80 } 81 82for (uint32_t i = 0 ;i < hparams .dec_n_layer ;++ i ) { 83auto & layer = layers [i ]; 84 85layer .attn_norm = create_tensor (tn (LLM_TENSOR_DEC_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 86layer .wq = create_tensor (tn (LLM_TENSOR_DEC_ATTN_Q ,"weight" ,i ), {n_embd ,n_embd_q },0 ); 87layer .wk = create_tensor (tn (LLM_TENSOR_DEC_ATTN_K ,"weight" ,i ), {n_embd ,n_embd_k_gqa },0 ); 88layer .wv = create_tensor (tn (LLM_TENSOR_DEC_ATTN_V ,"weight" ,i ), {n_embd ,n_embd_v_gqa },0 ); 89layer .wo = create_tensor (tn (LLM_TENSOR_DEC_ATTN_OUT ,"weight" ,i ), {n_embd_head_v * n_head ,n_embd },0 ); 90 91layer .attn_norm_cross = create_tensor (tn (LLM_TENSOR_DEC_CROSS_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 92layer .wq_cross = create_tensor (tn (LLM_TENSOR_DEC_CROSS_ATTN_Q ,"weight" ,i ), {n_embd ,n_embd_q },0 ); 93layer .wk_cross = create_tensor (tn (LLM_TENSOR_DEC_CROSS_ATTN_K ,"weight" ,i ), {n_embd ,n_embd_k_gqa },0 ); 94layer .wv_cross = create_tensor (tn (LLM_TENSOR_DEC_CROSS_ATTN_V ,"weight" ,i ), {n_embd ,n_embd_v_gqa },0 ); 95layer .wo_cross = create_tensor (tn (LLM_TENSOR_DEC_CROSS_ATTN_OUT ,"weight" ,i ), {n_embd_head_v * n_head ,n_embd },0 ); 96 97layer .ffn_norm = create_tensor (tn (LLM_TENSOR_DEC_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 98layer .ffn_gate_inp = create_tensor (tn (LLM_TENSOR_DEC_FFN_GATE_INP ,"weight" ,i ), {n_embd ,n_expert },0 ); 99layer .ffn_gate_exps = create_tensor (tn (LLM_TENSOR_DEC_FFN_GATE_EXPS ,"weight" ,i ), {n_embd ,n_ff_exp ,n_expert },0 ); 100layer .ffn_up_exps = create_tensor (tn (LLM_TENSOR_DEC_FFN_UP_EXPS ,"weight" ,i ), {n_embd ,n_ff_exp ,n_expert },0 ); 101layer .ffn_down_exps = create_tensor (tn (LLM_TENSOR_DEC_FFN_DOWN_EXPS ,"weight" ,i ), {n_ff_exp ,n_embd ,n_expert },0 ); 102layer .ffn_exp_probs_b = create_tensor (tn (LLM_TENSOR_DEC_EXP_PROBS_B ,i ), {n_expert },0 ); 103layer .ffn_down_b = create_tensor (tn (LLM_TENSOR_DEC_FFN_OUTPUT_B ,i ), {n_embd },0 ); 104 } 105} 106 107std::unique_ptr < llm_graph_context > llama_model_aliceai_t5_moe::build_arch_graph (const llm_graph_params & params )const { 108switch (params .gtype ) { 109case LLM_GRAPH_TYPE_ENCODER : 110return std::make_unique < graph < true>> (* this ,params ); 111case LLM_GRAPH_TYPE_DEFAULT : 112case LLM_GRAPH_TYPE_DECODER : 113return std::make_unique < graph < false>> (* this ,params ); 114default : 115GGML_ABORT ("invalid graph type" ); 116 } 117} 118 119template < bool is_enc > 120llama_model_aliceai_t5_moe::graph < is_enc > ::graph (const llama_model_aliceai_t5_moe & model ,const llm_graph_params & params ) :llm_graph_context (params ) { 121const int64_t n_embd_head = hparams .n_embd_head_k (); 122const int64_t n_head_kv_self = is_enc ?model .n_head_kv_enc :n_head_kv ; 123const int n_layers = is_enc ?n_layer :hparams .dec_n_layer ; 124const auto & layers = is_enc ?model .layers_enc :model .layers ; 125const float kq_scale = 1.0f /sqrtf (float (n_embd_head )); 126 127ggml_tensor * inpL = build_inp_embd (model .tok_embd ); 128ggml_tensor * inp_pos = build_inp_pos (); 129ggml_tensor * inp_out_ids = is_enc ?nullptr :build_inp_out_ids (); 130ggml_tensor * embd_enc = nullptr ; 131llm_graph_input_attn_cross * inp_cross = nullptr ; 132 133using inp_attn_type = std::conditional_t < is_enc ,llm_graph_input_attn_no_cache ,llm_graph_input_attn_kv > ; 134inp_attn_type * inp_attn ; 135if constexpr (is_enc ) { 136inp_attn = build_attn_inp_no_cache (); 137 }else { 138inp_attn = build_attn_inp_kv (); 139embd_enc = build_inp_cross_embd (); 140inp_cross = build_attn_inp_cross (); 141 } 142 143for (int il = 0 ;il < n_layers ;++ il ) { 144const auto & layer = layers [il ]; 145ggml_tensor * residual = inpL ; 146ggml_tensor * cur = build_norm (inpL ,layer .attn_norm ,nullptr ,LLM_NORM_RMS ,il ); 147cb (cur ,"attn_norm" ,il ); 148 149auto [Qcur ,Kcur ,Vcur ]= build_qkv (layer ,cur ,n_embd_head ,n_head ,n_head_kv_self ,il ); 150Qcur = ggml_rope_ext (ctx0 ,Qcur ,inp_pos ,nullptr ,n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 151ext_factor ,attn_factor ,beta_fast ,beta_slow ); 152Kcur = ggml_rope_ext (ctx0 ,Kcur ,inp_pos ,nullptr ,n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 153ext_factor ,attn_factor ,beta_fast ,beta_slow ); 154cb (Qcur ,"Qcur" ,il ); 155cb (Kcur ,"Kcur" ,il ); 156 157cur = build_attn (inp_attn ,layer .wo ,nullptr ,nullptr , 158Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,kq_scale ,il ); 159cur = ggml_add (ctx0 ,cur ,residual ); 160cb (cur ,"attn_out" ,il ); 161 162if constexpr (!is_enc ) { 163residual = cur ; 164cur = build_norm (cur ,layer .attn_norm_cross ,nullptr ,LLM_NORM_RMS ,il ); 165cb (cur ,"attn_norm_cross" ,il ); 166 167Qcur = build_lora_mm (layer .wq_cross ,cur ); 168Kcur = build_lora_mm (layer .wk_cross ,embd_enc ); 169Vcur = build_lora_mm (layer .wv_cross ,embd_enc ); 170Qcur = ggml_reshape_3d (ctx0 ,Qcur ,n_embd_head ,n_head ,n_tokens ); 171Kcur = ggml_reshape_3d (ctx0 ,Kcur ,n_embd_head ,n_head_kv ,embd_enc -> ne [1 ]); 172Vcur = ggml_reshape_3d (ctx0 ,Vcur ,n_embd_head ,n_head_kv ,embd_enc -> ne [1 ]); 173cur = build_attn (inp_cross ,layer .wo_cross ,nullptr ,nullptr , 174Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,kq_scale ,il ); 175cur = ggml_add (ctx0 ,cur ,residual ); 176cb (cur ,"cross_attn_out" ,il ); 177 } 178 179if (il == n_layers - 1 && inp_out_ids ) { 180cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 181 } 182residual = cur ; 183cur = build_norm (cur ,layer .ffn_norm ,nullptr ,LLM_NORM_RMS ,il ); 184cb (cur ,"ffn_norm" ,il ); 185cur = build_moe_ffn (cur ,layer .ffn_gate_inp ,layer .ffn_up_exps ,layer .ffn_gate_exps ,layer .ffn_down_exps , 186layer .ffn_exp_probs_b ,n_expert ,hparams .n_expert_used (il ),LLM_FFN_SILU , 187hparams .expert_weights_norm ,hparams .expert_weights_scale ,LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID ,il ); 188cur = ggml_add (ctx0 ,cur ,layer .ffn_down_b ); 189cb (cur ,"ffn_out" ,il ); 190cur = ggml_add (ctx0 ,cur ,residual ); 191inpL = build_cvec (cur ,il ); 192cb (inpL ,"l_out" ,il ); 193 } 194 195ggml_tensor * cur = build_norm (inpL ,is_enc ?model .output_norm_enc :model .output_norm ,nullptr ,LLM_NORM_RMS ,-1 ); 196cb (cur ,"result_norm" ,-1 ); 197res -> t_embd = cur ; 198 199if constexpr (!is_enc ) { 200cur = build_lora_mm (model .output ,cur ); 201cb (cur ,"result_output" ,-1 ); 202res -> t_logits = cur ; 203 } 204ggml_build_forward_expand (gf ,cur ); 205} 206 207template struct llama_model_aliceai_t5_moe::graph < true> ; 208template struct llama_model_aliceai_t5_moe:: graph < false > ;