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_bailingmoe2::load_arch_hparams (llama_model_loader & ml ) { 4ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps ); 5ml .get_key (LLM_KV_LEADING_DENSE_BLOCK_COUNT ,hparams .n_layer_dense_lead , false); 6ml .get_key_or_arr (LLM_KV_EXPERT_FEED_FORWARD_LENGTH ,hparams .n_ff_exp_arr ,hparams .n_layer_all ); 7ml .get_key (LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH ,hparams .n_ff_shexp , false); 8ml .get_key (LLM_KV_EXPERT_SHARED_COUNT ,hparams .n_expert_shared ); 9ml .get_key (LLM_KV_EXPERT_WEIGHTS_SCALE ,hparams .expert_weights_scale , false); 10ml .get_key (LLM_KV_EXPERT_WEIGHTS_NORM ,hparams .expert_weights_norm , false); 11ml .get_key (LLM_KV_EXPERT_GATING_FUNC ,hparams .expert_gating_func ); 12 13switch (hparams .n_layer ()) { 14case 20 :type = LLM_TYPE_16B_A1B ;break ; 15case 32 :type = LLM_TYPE_100B_A6B ;break ; 16default :type = LLM_TYPE_UNKNOWN ; 17 } 18} 19 20void llama_model_bailingmoe2::load_arch_tensors (llama_model_loader & ) { 21LLAMA_LOAD_LOCALS ; 22const int64_t n_expert_shared = hparams .n_expert_shared ; 23 24const int64_t n_ff_exp = hparams .n_ff_exp (); 25 26tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 27 28// output 29output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 30output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },0 ); 31 32GGML_ASSERT (n_expert > 0 && "n_expert must be > 0 for bailingmoe2" ); 33GGML_ASSERT (n_expert_used > 0 && "n_expert_used must be > 0 for bailingmoe2" ); 34 35for (int i = 0 ;i < n_layer_all ;++ i ) { 36int flags = 0 ; 37if (i >=n_layer ) { 38// skip all tensors in the NextN layers 39flags |=TENSOR_SKIP ; 40 } 41 42auto & layer = layers [i ]; 43 44layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },flags ); 45 46layer .wqkv = create_tensor (tn (LLM_TENSOR_ATTN_QKV ,"weight" ,i ), {n_embd ,n_embd + 2 * n_embd_gqa },flags ); 47layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd_head_k * n_head ,n_embd },flags ); 48 49layer .attn_q_norm = create_tensor (tn (LLM_TENSOR_ATTN_Q_NORM ,"weight" ,i ), {n_embd_head_k },flags ); 50layer .attn_k_norm = create_tensor (tn (LLM_TENSOR_ATTN_K_NORM ,"weight" ,i ), {n_embd_head_k },flags ); 51 52layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },flags ); 53 54if (static_cast < uint32_t > (i ) >=hparams .n_layer_dense_lead ) {// MoE layers 55const int64_t n_ff_shexp = (hparams .n_ff_shexp ?hparams .n_ff_shexp :n_ff_exp )* n_expert_shared ; 56 57layer .ffn_gate_inp = create_tensor (tn (LLM_TENSOR_FFN_GATE_INP ,"weight" ,i ), {n_embd ,n_expert },flags ); 58layer .ffn_exp_probs_b = create_tensor (tn (LLM_TENSOR_FFN_EXP_PROBS_B ,"bias" ,i ), {n_expert },TENSOR_NOT_REQUIRED |flags ); 59 60layer .ffn_gate_exps = create_tensor (tn (LLM_TENSOR_FFN_GATE_EXPS ,"weight" ,i ), {n_embd ,n_ff_exp ,n_expert },flags ); 61layer .ffn_down_exps = create_tensor (tn (LLM_TENSOR_FFN_DOWN_EXPS ,"weight" ,i ), {n_ff_exp ,n_embd ,n_expert },flags ); 62layer .ffn_up_exps = create_tensor (tn (LLM_TENSOR_FFN_UP_EXPS ,"weight" ,i ), {n_embd ,n_ff_exp ,n_expert },flags ); 63 64layer .ffn_gate_shexp = create_tensor (tn (LLM_TENSOR_FFN_GATE_SHEXP ,"weight" ,i ), {n_embd ,n_ff_shexp },flags ); 65layer .ffn_down_shexp = create_tensor (tn (LLM_TENSOR_FFN_DOWN_SHEXP ,"weight" ,i ), {n_ff_shexp ,n_embd },flags ); 66layer .ffn_up_shexp = create_tensor (tn (LLM_TENSOR_FFN_UP_SHEXP ,"weight" ,i ), {n_embd ,n_ff_shexp },flags ); 67 }else {// Dense layers 68layer .ffn_gate = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"weight" ,i ), {n_embd ,n_ff },flags ); 69layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },flags ); 70layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },flags ); 71 } 72 73// NextN/MTP tensors (preserved but unused) - conditionally load for last nextn_predict_layers 74if (i >=n_layer ) { 75layer .nextn .eh_proj = create_tensor (tn (LLM_TENSOR_NEXTN_EH_PROJ ,"weight" ,i ), {2 * n_embd ,n_embd },flags ); 76layer .nextn .embed_tokens = create_tensor (tn (LLM_TENSOR_NEXTN_EMBED_TOKENS ,"weight" ,i ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED |flags ); 77layer .nextn .enorm = create_tensor (tn (LLM_TENSOR_NEXTN_ENORM ,"weight" ,i ), {n_embd },flags ); 78layer .nextn .hnorm = create_tensor (tn (LLM_TENSOR_NEXTN_HNORM ,"weight" ,i ), {n_embd },flags ); 79layer .nextn .shared_head_head = create_tensor (tn (LLM_TENSOR_NEXTN_SHARED_HEAD_HEAD ,"weight" ,i ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED |flags ); 80layer .nextn .shared_head_norm = create_tensor (tn (LLM_TENSOR_NEXTN_SHARED_HEAD_NORM ,"weight" ,i ), {n_embd },TENSOR_NOT_REQUIRED |flags ); 81layer .layer_out_norm = create_tensor (tn (LLM_TENSOR_LAYER_OUT_NORM ,"weight" ,i ), {n_embd },flags ); 82 } 83 } 84} 85 86std::unique_ptr < llm_graph_context > llama_model_bailingmoe2::build_arch_graph (const llm_graph_params & params )const { 87return std::make_unique < graph > (* this ,params ); 88} 89 90llama_model_bailingmoe2::graph::graph (const llama_model & model ,const llm_graph_params & params ) : 91llm_graph_context (params ) { 92const int64_t n_embd_head = hparams .n_embd_head_v (); 93 94GGML_ASSERT (n_embd_head == hparams .n_embd_head_k ()); 95 96ggml_tensor * cur ; 97ggml_tensor * inpL ; 98 99inpL = build_inp_embd (model .tok_embd ); 100 101// inp_pos - contains the positions 102ggml_tensor * inp_pos = build_inp_pos (); 103 104auto * inp_attn = build_attn_inp_kv (); 105 106ggml_tensor * inp_out_ids = build_inp_out_ids (); 107 108for (int il = 0 ;il < n_layer ;++ il ) { 109ggml_tensor * inpSA = inpL ; 110 111// norm 112cur = build_norm (inpL ,model .layers [il ].attn_norm ,NULL ,LLM_NORM_RMS ,il ); 113cb (cur ,"attn_norm" ,il ); 114 115// self_attention 116 { 117auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 118n_embd_head ,n_head ,n_head_kv ,il ); 119 120Qcur = build_norm (Qcur ,model .layers [il ].attn_q_norm ,NULL ,LLM_NORM_RMS ,il ); 121cb (Qcur ,"Qcur_normed" ,il ); 122 123Qcur = ggml_rope_ext (ctx0 ,Qcur ,inp_pos ,nullptr ,n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 124ext_factor ,attn_factor ,beta_fast ,beta_slow ); 125 126Kcur = build_norm (Kcur ,model .layers [il ].attn_k_norm ,NULL ,LLM_NORM_RMS ,il ); 127cb (Kcur ,"Kcur_normed" ,il ); 128 129Kcur = ggml_rope_ext (ctx0 ,Kcur ,inp_pos ,nullptr ,n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 130ext_factor ,attn_factor ,beta_fast ,beta_slow ); 131 132cb (Qcur ,"Qcur" ,il ); 133cb (Kcur ,"Kcur" ,il ); 134cb (Vcur ,"Vcur" ,il ); 135 136cur = build_attn (inp_attn , 137model .layers [il ].wo ,model .layers [il ].wo_b ,model .layers [il ].wo_s , 138Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,1.0f /sqrtf (float (n_embd_head )),il ); 139 } 140 141if (il == n_layer - 1 && inp_out_ids ) { 142cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 143inpSA = ggml_get_rows (ctx0 ,inpSA ,inp_out_ids ); 144 } 145 146ggml_tensor * sa_out = ggml_add (ctx0 ,cur ,inpSA ); 147cb (sa_out ,"sa_out" ,il ); 148 149// MoE branch 150cur = build_norm (sa_out ,model .layers [il ].ffn_norm ,NULL ,LLM_NORM_RMS ,il ); 151cb (cur ,"ffn_norm" ,il ); 152 153if (static_cast < uint32_t > (il )< hparams .n_layer_dense_lead ) { 154cur = build_ffn (cur , 155model .layers [il ].ffn_up ,NULL ,NULL , 156model .layers [il ].ffn_gate ,NULL ,NULL , 157model .layers [il ].ffn_down ,NULL ,NULL , 158NULL ,LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 159cb (cur ,"ffn_out" ,il ); 160 }else { 161ggml_tensor * moe_out = build_moe_ffn (cur , 162model .layers [il ].ffn_gate_inp , 163model .layers [il ].ffn_up_exps , 164model .layers [il ].ffn_gate_exps , 165model .layers [il ].ffn_down_exps , 166model .layers [il ].ffn_exp_probs_b , 167n_expert ,n_expert_used , 168LLM_FFN_SILU ,hparams .expert_weights_norm , 169hparams .expert_weights_scale , 170 (llama_expert_gating_func_type )hparams .expert_gating_func , 171il ); 172cb (moe_out ,"ffn_moe_out" ,il ); 173 174 { 175ggml_tensor * ffn_shexp = 176build_ffn (cur , 177model .layers [il ].ffn_up_shexp ,NULL ,NULL , 178model .layers [il ].ffn_gate_shexp ,NULL ,NULL , 179model .layers [il ].ffn_down_shexp ,NULL ,NULL , 180NULL ,LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 181cb (ffn_shexp ,"ffn_shexp" ,il ); 182 183cur = ggml_add (ctx0 ,moe_out ,ffn_shexp ); 184cb (cur ,"ffn_out" ,il ); 185 } 186 } 187 188cur = ggml_add (ctx0 ,cur ,sa_out ); 189 190cur = build_cvec (cur ,il ); 191cb (cur ,"l_out" ,il ); 192 193// input for next layer 194inpL = cur ; 195 } 196 197cur = inpL ; 198 199cur = build_norm (cur ,model .output_norm ,NULL ,LLM_NORM_RMS ,-1 ); 200 201cb (cur ,"result_norm" ,-1 ); 202res -> t_embd = cur ; 203 204// lm_head 205cur = build_lora_mm (model .output ,cur ,model .output_s ); 206 207cb (cur ,"result_output" ,-1 ); 208res -> t_logits = cur ; 209 210ggml_build_forward_expand (gf ,cur ); 211}