char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
c61b98b87
char/aliceai-t5-moe
1#include "models.h" 2 3void llama_model_qwen2moe::load_arch_hparams (llama_model_loader & ml ) { 4ml .get_key_or_arr (LLM_KV_EXPERT_FEED_FORWARD_LENGTH ,hparams .n_ff_exp_arr ,hparams .n_layer_all , false); 5ml .get_key (LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH ,hparams .n_ff_shexp , false); 6 7ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps ); 8 9switch (hparams .n_layer ()) { 10case 24 :type = LLM_TYPE_A2_7B ;break ; 11case 28 :type = LLM_TYPE_57B_A14B ;break ; 12default :type = LLM_TYPE_UNKNOWN ; 13 } 14} 15 16void llama_model_qwen2moe::load_arch_tensors (llama_model_loader & ) { 17LLAMA_LOAD_LOCALS ; 18 19tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 20 21// output 22output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 23output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },0 ); 24 25for (int i = 0 ;i < n_layer ;++ i ) { 26auto & layer = layers [i ]; 27 28layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 29 30create_tensor_qkv (layer ,i ,n_embd ,n_embd ,n_embd_gqa ,n_embd_gqa ,0 ); 31layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd ,n_embd },0 ); 32 33layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 34 35layer .ffn_gate_inp = create_tensor (tn (LLM_TENSOR_FFN_GATE_INP ,"weight" ,i ), {n_embd ,n_expert },0 ); 36 37if (n_expert == 0 ) { 38throw std::runtime_error ("n_expert must be > 0 for QWEN2MOE" ); 39 } 40if (n_expert_used == 0 ) { 41throw std::runtime_error ("n_expert_used must be > 0 for QWEN2MOE" ); 42 } 43 44// MoE branch 45const int64_t n_ff_exp = hparams .n_ff_exp () ?hparams .n_ff_exp () :n_ff /n_expert_used ; 46 47layer .ffn_gate_exps = create_tensor (tn (LLM_TENSOR_FFN_GATE_EXPS ,"weight" ,i ), {n_embd ,n_ff_exp ,n_expert },0 ); 48layer .ffn_down_exps = create_tensor (tn (LLM_TENSOR_FFN_DOWN_EXPS ,"weight" ,i ), {n_ff_exp ,n_embd ,n_expert },0 ); 49layer .ffn_up_exps = create_tensor (tn (LLM_TENSOR_FFN_UP_EXPS ,"weight" ,i ), {n_embd ,n_ff_exp ,n_expert },0 ); 50 51// Shared expert branch 52const int64_t n_ff_shexp = hparams .n_ff_shexp ?hparams .n_ff_shexp :n_ff ; 53 54layer .ffn_gate_inp_shexp = create_tensor (tn (LLM_TENSOR_FFN_GATE_INP_SHEXP ,"weight" ,i ), {n_embd },0 ); 55layer .ffn_gate_shexp = create_tensor (tn (LLM_TENSOR_FFN_GATE_SHEXP ,"weight" ,i ), {n_embd ,n_ff_shexp },0 ); 56layer .ffn_down_shexp = create_tensor (tn (LLM_TENSOR_FFN_DOWN_SHEXP ,"weight" ,i ), {n_ff_shexp ,n_embd },0 ); 57layer .ffn_up_shexp = create_tensor (tn (LLM_TENSOR_FFN_UP_SHEXP ,"weight" ,i ), {n_embd ,n_ff_shexp },0 ); 58 } 59} 60 61std::unique_ptr < llm_graph_context > llama_model_qwen2moe::build_arch_graph (const llm_graph_params & params )const { 62return std::make_unique < graph > (* this ,params ); 63} 64 65llama_model_qwen2moe::graph::graph (const llama_model & model ,const llm_graph_params & params ) :llm_graph_context (params ) { 66const int64_t n_embd_head = hparams .n_embd_head_v (); 67 68GGML_ASSERT (n_embd_head == hparams .n_embd_head_k ()); 69GGML_ASSERT (n_embd_head == n_rot ); 70 71ggml_tensor * cur ; 72ggml_tensor * inpL ; 73 74inpL = build_inp_embd (model .tok_embd ); 75 76// inp_pos - contains the positions 77ggml_tensor * inp_pos = build_inp_pos (); 78 79auto * inp_attn = build_attn_inp_kv (); 80 81ggml_tensor * inp_out_ids = build_inp_out_ids (); 82 83for (int il = 0 ;il < n_layer ;++ il ) { 84ggml_tensor * inpSA = inpL ; 85 86// norm 87cur = build_norm (inpL , 88model .layers [il ].attn_norm ,NULL , 89LLM_NORM_RMS ,il ); 90cb (cur ,"attn_norm" ,il ); 91 92// self_attention 93 { 94// compute Q and K and RoPE them 95auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 96n_embd_head ,n_head ,n_head_kv ,il ); 97 98Qcur = ggml_rope_ext ( 99ctx0 ,Qcur ,inp_pos ,nullptr , 100n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 101ext_factor ,attn_factor ,beta_fast ,beta_slow 102 ); 103 104Kcur = ggml_rope_ext ( 105ctx0 ,Kcur ,inp_pos ,nullptr , 106n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 107ext_factor ,attn_factor ,beta_fast ,beta_slow 108 ); 109 110cb (Qcur ,"Qcur" ,il ); 111cb (Kcur ,"Kcur" ,il ); 112cb (Vcur ,"Vcur" ,il ); 113 114cur = build_attn (inp_attn , 115model .layers [il ].wo ,model .layers [il ].wo_b ,model .layers [il ].wo_s , 116Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,1.0f /sqrtf (float (n_embd_head )),il ); 117 } 118if (il == n_layer - 1 && inp_out_ids ) { 119cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 120inpSA = ggml_get_rows (ctx0 ,inpSA ,inp_out_ids ); 121 } 122ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 123cb (ffn_inp ,"ffn_inp" ,il ); 124 125// MoE branch 126cur = build_norm (ffn_inp , 127model .layers [il ].ffn_norm ,NULL , 128LLM_NORM_RMS ,il ); 129cb (cur ,"ffn_norm" ,il ); 130 131ggml_tensor * moe_out = 132build_moe_ffn (cur , 133model .layers [il ].ffn_gate_inp , 134model .layers [il ].ffn_up_exps , 135model .layers [il ].ffn_gate_exps , 136model .layers [il ].ffn_down_exps , 137nullptr , 138n_expert ,n_expert_used , 139LLM_FFN_SILU , false, 140hparams .expert_weights_scale , 141LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX , 142il ); 143cb (moe_out ,"ffn_moe_out" ,il ); 144 145// FFN shared expert 146 { 147ggml_tensor * cur_gate_inp = build_lora_mm (model .layers [il ].ffn_gate_inp_shexp ,cur ); 148cb (cur_gate_inp ,"ffn_shexp_gate_inp" ,il ); 149 150// sigmoid 151ggml_tensor * cur_gate = ggml_div (ctx0 ,ggml_silu (ctx0 ,cur_gate_inp ),cur_gate_inp ); 152cb (cur_gate ,"ffn_shexp_gate" ,il ); 153 154ggml_tensor * cur_ffn = build_ffn (cur , 155model .layers [il ].ffn_up_shexp ,NULL ,NULL , 156model .layers [il ].ffn_gate_shexp ,NULL ,NULL , 157model .layers [il ].ffn_down_shexp ,NULL ,NULL , 158NULL , 159LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 160cb (cur_ffn ,"ffn_shexp" ,il ); 161 162ggml_tensor * ffn_shexp_out = ggml_mul (ctx0 ,cur_ffn ,cur_gate ); 163cb (ffn_shexp_out ,"ffn_shexp_out" ,il ); 164 165moe_out = ggml_add (ctx0 ,moe_out ,ffn_shexp_out ); 166cb (moe_out ,"ffn_out" ,il ); 167 168cur = moe_out ; 169 } 170cur = ggml_add (ctx0 ,cur ,ffn_inp ); 171 172cur = build_cvec (cur ,il ); 173cb (cur ,"l_out" ,il ); 174 175// input for next layer 176inpL = cur ; 177 } 178cur = inpL ; 179 180cur = build_norm (cur , 181model .output_norm ,NULL , 182LLM_NORM_RMS ,-1 ); 183 184cb (cur ,"result_norm" ,-1 ); 185res -> t_embd = cur ; 186 187// lm_head 188cur = build_lora_mm (model .output ,cur ,model .output_s ); 189 190cb (cur ,"result_output" ,-1 ); 191res -> t_logits = cur ; 192 193ggml_build_forward_expand (gf ,cur ); 194}