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