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_dots1::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_COUNT ,hparams .n_expert_shared ); 8ml .get_key (LLM_KV_EXPERT_WEIGHTS_SCALE ,hparams .expert_weights_scale , false); 9ml .get_key (LLM_KV_EXPERT_WEIGHTS_NORM ,hparams .expert_weights_norm , false); 10ml .get_key (LLM_KV_EXPERT_GATING_FUNC ,hparams .expert_gating_func , false); 11 12switch (hparams .n_layer ()) { 13case 62 :type = LLM_TYPE_142B ;break ; 14default :type = LLM_TYPE_UNKNOWN ; 15 } 16} 17 18void llama_model_dots1::load_arch_tensors (llama_model_loader & ) { 19LLAMA_LOAD_LOCALS ; 20const int64_t n_expert_shared = hparams .n_expert_shared ; 21 22const int64_t n_ff_exp = hparams .n_ff_exp (); 23 24tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 25 26output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 27output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },0 ); 28 29for (int i = 0 ;i < n_layer ;++ i ) { 30auto & layer = layers [i ]; 31 32layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 33 34create_tensor_qkv (layer ,i ,n_embd ,n_embd_head_k * n_head ,n_embd_head_k * n_head ,n_embd_head_k * n_head ,0 ); 35layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd_head_k * n_head ,n_embd },0 ); 36 37layer .attn_k_norm = create_tensor (tn (LLM_TENSOR_ATTN_K_NORM ,"weight" ,i ), {n_embd_head_k },0 ); 38layer .attn_q_norm = create_tensor (tn (LLM_TENSOR_ATTN_Q_NORM ,"weight" ,i ), {n_embd_head_k },0 ); 39 40layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 41 42if (i < (int )hparams .n_layer_dense_lead ) { 43layer .ffn_gate = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"weight" ,i ), {n_embd ,n_ff },0 ); 44layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },0 ); 45layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },0 ); 46 }else { 47layer .ffn_gate_inp = create_tensor (tn (LLM_TENSOR_FFN_GATE_INP ,"weight" ,i ), {n_embd ,n_expert },0 ); 48layer .ffn_exp_probs_b = create_tensor (tn (LLM_TENSOR_FFN_EXP_PROBS_B ,"bias" ,i ), {n_expert },TENSOR_NOT_REQUIRED ); 49 50if (n_expert == 0 ) { 51throw std::runtime_error ("n_expert must be > 0" ); 52 } 53if (n_expert_used == 0 ) { 54throw std::runtime_error ("n_expert_used must be > 0" ); 55 } 56 57// MoE branch 58layer .ffn_gate_exps = create_tensor (tn (LLM_TENSOR_FFN_GATE_EXPS ,"weight" ,i ), {n_embd ,n_ff_exp ,n_expert },0 ); 59layer .ffn_down_exps = create_tensor (tn (LLM_TENSOR_FFN_DOWN_EXPS ,"weight" ,i ), {n_ff_exp ,n_embd ,n_expert },0 ); 60layer .ffn_up_exps = create_tensor (tn (LLM_TENSOR_FFN_UP_EXPS ,"weight" ,i ), {n_embd ,n_ff_exp ,n_expert },0 ); 61 62// Shared expert branch 63layer .ffn_gate_shexp = create_tensor (tn (LLM_TENSOR_FFN_GATE_SHEXP ,"weight" ,i ), {n_embd ,n_ff_exp * n_expert_shared },0 ); 64layer .ffn_down_shexp = create_tensor (tn (LLM_TENSOR_FFN_DOWN_SHEXP ,"weight" ,i ), {n_ff_exp * n_expert_shared ,n_embd },0 ); 65layer .ffn_up_shexp = create_tensor (tn (LLM_TENSOR_FFN_UP_SHEXP ,"weight" ,i ), {n_embd ,n_ff_exp * n_expert_shared },0 ); 66 } 67 } 68} 69 70std::unique_ptr < llm_graph_context > llama_model_dots1::build_arch_graph (const llm_graph_params & params )const { 71return std::make_unique < graph > (* this ,params ); 72} 73 74llama_model_dots1::graph::graph (const llama_model & model ,const llm_graph_params & params ) : 75llm_graph_context (params ) { 76const int64_t n_embd_head = hparams .n_embd_head_v (); 77 78GGML_ASSERT (n_embd_head == hparams .n_embd_head_k ()); 79GGML_ASSERT (n_embd_head == n_rot ); 80 81ggml_tensor * cur ; 82ggml_tensor * inpL ; 83 84inpL = build_inp_embd (model .tok_embd ); 85 86// inp_pos - contains the positions 87ggml_tensor * inp_pos = build_inp_pos (); 88 89auto * inp_attn = build_attn_inp_kv (); 90 91ggml_tensor * inp_out_ids = build_inp_out_ids (); 92 93for (int il = 0 ;il < n_layer ;++ il ) { 94ggml_tensor * inpSA = inpL ; 95 96// norm 97cur = build_norm (inpL ,model .layers [il ].attn_norm ,NULL ,LLM_NORM_RMS ,il ); 98cb (cur ,"attn_norm" ,il ); 99 100// self_attention 101 { 102// compute Q and K and RoPE them 103auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 104n_embd_head ,n_head ,n_head_kv ,il ); 105 106Qcur = build_norm (Qcur ,model .layers [il ].attn_q_norm ,NULL ,LLM_NORM_RMS ,il ); 107cb (Qcur ,"Qcur_normed" ,il ); 108 109Qcur = ggml_rope_ext (ctx0 ,Qcur ,inp_pos ,nullptr ,n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 110ext_factor ,attn_factor ,beta_fast ,beta_slow ); 111 112Kcur = build_norm (Kcur ,model .layers [il ].attn_k_norm ,NULL ,LLM_NORM_RMS ,il ); 113cb (Kcur ,"Kcur_normed" ,il ); 114 115Kcur = ggml_rope_ext (ctx0 ,Kcur ,inp_pos ,nullptr ,n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 116ext_factor ,attn_factor ,beta_fast ,beta_slow ); 117 118cb (Qcur ,"Qcur" ,il ); 119cb (Kcur ,"Kcur" ,il ); 120cb (Vcur ,"Vcur" ,il ); 121 122cur = build_attn (inp_attn , 123model .layers [il ].wo ,model .layers [il ].wo_b ,model .layers [il ].wo_s , 124Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,1.0f /sqrtf (float (n_embd_head )),il ); 125 } 126if (il == n_layer - 1 && inp_out_ids ) { 127cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 128inpSA = ggml_get_rows (ctx0 ,inpSA ,inp_out_ids ); 129 } 130ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 131cb (ffn_inp ,"ffn_inp" ,il ); 132 133// MoE branch 134cur = build_norm (ffn_inp ,model .layers [il ].ffn_norm ,NULL ,LLM_NORM_RMS ,il ); 135cb (cur ,"ffn_norm" ,il ); 136 137if ((uint32_t )il < hparams .n_layer_dense_lead ) { 138cur = build_ffn (cur , 139model .layers [il ].ffn_up ,NULL ,NULL , 140model .layers [il ].ffn_gate ,NULL ,NULL , 141model .layers [il ].ffn_down ,NULL ,NULL , 142NULL ,LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 143cb (cur ,"ffn_out" ,il ); 144 }else { 145ggml_tensor * moe_out = build_moe_ffn (cur , 146model .layers [il ].ffn_gate_inp , 147model .layers [il ].ffn_up_exps , 148model .layers [il ].ffn_gate_exps , 149model .layers [il ].ffn_down_exps , 150model .layers [il ].ffn_exp_probs_b , 151n_expert ,n_expert_used , 152LLM_FFN_SILU ,hparams .expert_weights_norm , 153hparams .expert_weights_scale , 154 (llama_expert_gating_func_type )hparams .expert_gating_func , 155il ); 156cb (moe_out ,"ffn_moe_out" ,il ); 157 158 { 159ggml_tensor * ffn_shexp = 160build_ffn (cur , 161model .layers [il ].ffn_up_shexp ,NULL ,NULL , 162model .layers [il ].ffn_gate_shexp ,NULL ,NULL , 163model .layers [il ].ffn_down_shexp ,NULL ,NULL , 164NULL ,LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 165cb (ffn_shexp ,"ffn_shexp" ,il ); 166 167cur = ggml_add (ctx0 ,moe_out ,ffn_shexp ); 168cb (cur ,"ffn_out" ,il ); 169 } 170 } 171cur = ggml_add (ctx0 ,cur ,ffn_inp ); 172 173cur = build_cvec (cur ,il ); 174cb (cur ,"l_out" ,il ); 175 176// input for next layer 177inpL = cur ; 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}