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