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_grovemoe::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 ); 5ml .get_key (LLM_KV_EXPERT_CHUNK_FEED_FORWARD_LENGTH ,hparams .n_ff_chexp , false); 6ml .get_key (LLM_KV_EXPERT_GROUP_SCALE ,hparams .expert_group_scale ); 7ml .get_key (LLM_KV_EXPERTS_PER_GROUP ,hparams .n_group_experts ); 8ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps ); 9 10switch (hparams .n_layer ()) { 11case 48 :type = LLM_TYPE_30B_A3B ;break ; 12default :type = LLM_TYPE_UNKNOWN ; 13 } 14} 15 16void llama_model_grovemoe::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 },TENSOR_NOT_REQUIRED ); 24// if output is NULL, init from the input tok embed 25if (output == NULL ) { 26output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED ); 27 } 28 29GGML_ASSERT (n_expert > 0 && "n_expert must be > 0 for GROVEMOE" ); 30GGML_ASSERT (n_expert_used > 0 && "n_expert_used must be > 0 for GROVEMOE" ); 31GGML_ASSERT (hparams .n_group_experts > 0 && "n_group_experts must be > 0 for GROVEMOE" ); 32 33for (int i = 0 ;i < n_layer ;++ i ) { 34auto & layer = layers [i ]; 35 36layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 37 38create_tensor_qkv (layer ,i ,n_embd ,n_embd_head_k * n_head ,n_embd_gqa ,n_embd_gqa ,0 ); 39layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd_head_k * n_head ,n_embd },0 ); 40 41layer .attn_k_norm = create_tensor (tn (LLM_TENSOR_ATTN_K_NORM ,"weight" ,i ), {n_embd_head_k },0 ); 42layer .attn_q_norm = create_tensor (tn (LLM_TENSOR_ATTN_Q_NORM ,"weight" ,i ), {n_embd_head_k },0 ); 43 44layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 45 46layer .ffn_gate_inp = create_tensor (tn (LLM_TENSOR_FFN_GATE_INP ,"weight" ,i ), {n_embd ,n_expert },0 ); 47 48// MoE branch 49const int64_t n_ff_exp = hparams .n_ff_exp () ?hparams .n_ff_exp () :n_ff /n_expert_used ; 50const int64_t n_ff_chexp = hparams .n_ff_chexp ?hparams .n_ff_chexp :n_embd_head_k ; 51const int64_t n_chunk_expert = n_expert /hparams .n_group_experts ; 52 53layer .ffn_gate_exps = create_tensor (tn (LLM_TENSOR_FFN_GATE_EXPS ,"weight" ,i ), {n_embd ,n_ff_exp ,n_expert },0 ); 54layer .ffn_down_exps = create_tensor (tn (LLM_TENSOR_FFN_DOWN_EXPS ,"weight" ,i ), {n_ff_exp ,n_embd ,n_expert },0 ); 55layer .ffn_up_exps = create_tensor (tn (LLM_TENSOR_FFN_UP_EXPS ,"weight" ,i ), {n_embd ,n_ff_exp ,n_expert },0 ); 56 57layer .ffn_gate_chexps = create_tensor (tn (LLM_TENSOR_FFN_GATE_CHEXPS ,"weight" ,i ), {n_embd ,n_ff_chexp ,n_chunk_expert },0 ); 58layer .ffn_down_chexps = create_tensor (tn (LLM_TENSOR_FFN_DOWN_CHEXPS ,"weight" ,i ), {n_ff_chexp ,n_embd ,n_chunk_expert },0 ); 59layer .ffn_up_chexps = create_tensor (tn (LLM_TENSOR_FFN_UP_CHEXPS ,"weight" ,i ), {n_embd ,n_ff_chexp ,n_chunk_expert },0 ); 60 } 61} 62 63std::unique_ptr < llm_graph_context > llama_model_grovemoe::build_arch_graph (const llm_graph_params & params )const { 64return std::make_unique < graph > (* this ,params ); 65} 66 67llama_model_grovemoe::graph::graph (const llama_model & model ,const llm_graph_params & params ) : 68llm_graph_context (params ) { 69const int64_t n_embd_head = hparams .n_embd_head_v (); 70const int64_t n_chunk_expert = n_expert /hparams .n_group_experts ; 71 72GGML_ASSERT (n_embd_head == hparams .n_embd_head_k ()); 73GGML_ASSERT (n_embd_head == n_rot ); 74 75ggml_tensor * cur ; 76ggml_tensor * inpL ; 77 78inpL = build_inp_embd (model .tok_embd ); 79 80// inp_pos - contains the positions 81ggml_tensor * inp_pos = build_inp_pos (); 82 83auto * inp_attn = build_attn_inp_kv (); 84 85ggml_tensor * inp_out_ids = build_inp_out_ids (); 86 87for (int il = 0 ;il < n_layer ;++ il ) { 88ggml_tensor * inpSA = inpL ; 89 90// norm 91cur = build_norm (inpL ,model .layers [il ].attn_norm ,NULL ,LLM_NORM_RMS ,il ); 92cb (cur ,"attn_norm" ,il ); 93 94// self_attention 95 { 96// compute Q and K and RoPE them 97auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 98n_embd_head ,n_head ,n_head_kv ,il ); 99 100Qcur = build_norm (Qcur ,model .layers [il ].attn_q_norm ,NULL ,LLM_NORM_RMS ,il ); 101cb (Qcur ,"Qcur_normed" ,il ); 102 103Qcur = ggml_rope_ext (ctx0 ,Qcur ,inp_pos ,nullptr ,n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 104ext_factor ,attn_factor ,beta_fast ,beta_slow ); 105 106Kcur = build_norm (Kcur ,model .layers [il ].attn_k_norm ,NULL ,LLM_NORM_RMS ,il ); 107cb (Kcur ,"Kcur_normed" ,il ); 108 109Kcur = ggml_rope_ext (ctx0 ,Kcur ,inp_pos ,nullptr ,n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 110ext_factor ,attn_factor ,beta_fast ,beta_slow ); 111 112cb (Qcur ,"Qcur" ,il ); 113cb (Kcur ,"Kcur" ,il ); 114cb (Vcur ,"Vcur" ,il ); 115 116cur = build_attn (inp_attn , 117model .layers [il ].wo ,model .layers [il ].wo_b ,model .layers [il ].wo_s , 118Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,1.0f /sqrtf (float (n_embd_head )),il ); 119 } 120 121if (il == n_layer - 1 && inp_out_ids ) { 122cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 123inpSA = ggml_get_rows (ctx0 ,inpSA ,inp_out_ids ); 124 } 125 126ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 127cb (ffn_inp ,"ffn_inp" ,il ); 128 129// MoE branch 130cur = build_norm (ffn_inp ,model .layers [il ].ffn_norm ,NULL ,LLM_NORM_RMS ,il ); 131cb (cur ,"ffn_norm" ,il ); 132 133ggml_tensor * probs = build_lora_mm (model .layers [il ].ffn_gate_inp ,cur );// [n_expert, n_tokens] 134cb (probs ,"ffn_moe_logits" ,il ); 135 136ggml_tensor * moe_out = 137build_moe_ffn (cur , 138nullptr , 139model .layers [il ].ffn_up_exps , 140model .layers [il ].ffn_gate_exps , 141model .layers [il ].ffn_down_exps , 142nullptr , 143n_expert ,n_expert_used , 144LLM_FFN_SILU , true, 145hparams .expert_weights_scale , 146LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX , 147il , 148probs ); 149cb (moe_out ,"ffn_moe_out" ,il ); 150cur = moe_out ; 151 152// TODO: Only do the expert selection and weights once 153moe_out = build_moe_ffn (cur , 154nullptr , 155model .layers [il ].ffn_up_chexps , 156model .layers [il ].ffn_gate_chexps , 157model .layers [il ].ffn_down_chexps , 158nullptr , 159n_chunk_expert ,n_expert_used > n_chunk_expert ?n_chunk_expert :n_expert_used , 160LLM_FFN_SILU , true, 161hparams .expert_weights_scale , 162LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX , 163il , 164probs ); 165cb (moe_out ,"ffn_adj_moe_out" ,il ); 166 167cur = ggml_add (ctx0 ,cur ,ggml_scale (ctx0 ,moe_out ,hparams .expert_group_scale )); 168cb (cur ,"ffn_final_moe_out" ,il ); 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 } 178 179cur = inpL ; 180 181cur = build_norm (cur ,model .output_norm ,NULL ,LLM_NORM_RMS ,-1 ); 182 183cb (cur ,"result_norm" ,-1 ); 184res -> t_embd = cur ; 185 186// lm_head 187cur = build_lora_mm (model .output ,cur ,model .output_s ); 188 189cb (cur ,"result_output" ,-1 ); 190res -> t_logits = cur ; 191 192ggml_build_forward_expand (gf ,cur ); 193}