okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b2de2641f
main
1#include "models.h" 2 3void llama_model_mellum::load_arch_hparams (llama_model_loader & ml ) { 4ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps ); 5ml .get_key_or_arr (LLM_KV_EXPERT_FEED_FORWARD_LENGTH ,hparams .n_ff_exp_arr ,hparams .n_layer_all ); 6ml .get_key (LLM_KV_ATTENTION_SLIDING_WINDOW ,hparams .n_swa , false); 7 8if (hparams .n_swa > 0 ) { 9hparams .swa_type = LLAMA_SWA_TYPE_STANDARD ; 10 11load_swa_pattern (ml ,4 ); 12 13hparams .rope_freq_base_train_swa = hparams .rope_freq_base_train ; 14hparams .rope_freq_scale_train_swa = hparams .rope_freq_scale_train ; 15 16ml .get_key (LLM_KV_ROPE_FREQ_BASE_SWA ,hparams .rope_freq_base_train_swa , false); 17 }else { 18hparams .swa_type = LLAMA_SWA_TYPE_NONE ; 19 } 20 21switch (hparams .n_layer ()) { 22case 28 :type = LLM_TYPE_12B_A2_5B ;break ; 23default :type = LLM_TYPE_UNKNOWN ; 24 } 25} 26 27void llama_model_mellum::load_arch_tensors (llama_model_loader & ) { 28LLAMA_LOAD_LOCALS ; 29 30tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 31 32// output 33output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 34output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },0 ); 35 36for (int i = 0 ;i < n_layer ;++ i ) { 37auto & layer = layers [i ]; 38 39layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 40 41create_tensor_qkv (layer ,i ,n_embd ,n_embd_head_k * n_head ,n_embd_gqa ,n_embd_gqa ,0 ); 42layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd_head_k * n_head ,n_embd },0 ); 43 44layer .attn_k_norm = create_tensor (tn (LLM_TENSOR_ATTN_K_NORM ,"weight" ,i ), {n_embd_head_k },0 ); 45layer .attn_q_norm = create_tensor (tn (LLM_TENSOR_ATTN_Q_NORM ,"weight" ,i ), {n_embd_head_k },0 ); 46 47layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 48 49layer .ffn_gate_inp = create_tensor (tn (LLM_TENSOR_FFN_GATE_INP ,"weight" ,i ), {n_embd ,n_expert },0 ); 50 51if (n_expert == 0 ) { 52throw std::runtime_error ("n_expert must be > 0 for Mellum" ); 53 } 54if (n_expert_used == 0 ) { 55throw std::runtime_error ("n_expert_used must be > 0 for Mellum" ); 56 } 57 58const int64_t n_ff_exp = hparams .n_ff_exp () ?hparams .n_ff_exp () :n_ff /n_expert_used ; 59 60layer .ffn_gate_exps = create_tensor (tn (LLM_TENSOR_FFN_GATE_EXPS ,"weight" ,i ), {n_embd ,n_ff_exp ,n_expert },0 ); 61layer .ffn_down_exps = create_tensor (tn (LLM_TENSOR_FFN_DOWN_EXPS ,"weight" ,i ), {n_ff_exp ,n_embd ,n_expert },0 ); 62layer .ffn_up_exps = create_tensor (tn (LLM_TENSOR_FFN_UP_EXPS ,"weight" ,i ), {n_embd ,n_ff_exp ,n_expert },0 ); 63 } 64} 65 66std::unique_ptr < llm_graph_context > llama_model_mellum::build_arch_graph (const llm_graph_params & params )const { 67if (hparams .swa_type == LLAMA_SWA_TYPE_STANDARD ) { 68return std::make_unique < graph < true>> (* this ,params ); 69 } 70return std::make_unique < graph < false>> (* this ,params ); 71} 72 73template < bool iswa > 74llama_model_mellum::graph < iswa > ::graph (const llama_model & model ,const llm_graph_params & params ) :llm_graph_context (params ) { 75const int64_t n_embd_head = hparams .n_embd_head_v (); 76 77GGML_ASSERT (n_embd_head == hparams .n_embd_head_k ()); 78GGML_ASSERT (n_embd_head == n_rot ); 79 80ggml_tensor * cur ; 81ggml_tensor * inpL ; 82 83inpL = build_inp_embd (model .tok_embd ); 84 85// inp_pos - contains the positions 86ggml_tensor * inp_pos = build_inp_pos (); 87 88using inp_attn_type = std::conditional_t < iswa ,llm_graph_input_attn_kv_iswa ,llm_graph_input_attn_kv > ; 89inp_attn_type * inp_attn = nullptr ; 90 91if constexpr (iswa ) { 92inp_attn = build_attn_inp_kv_iswa (); 93 }else { 94inp_attn = build_attn_inp_kv (); 95 } 96 97ggml_tensor * inp_out_ids = build_inp_out_ids (); 98 99for (int il = 0 ;il < n_layer ;++ il ) { 100ggml_tensor * inpSA = inpL ; 101 102// norm 103cur = build_norm (inpL , 104model .layers [il ].attn_norm ,nullptr , 105LLM_NORM_RMS ,il ); 106cb (cur ,"attn_norm" ,il ); 107 108// self_attention 109 { 110// compute Q and K and RoPE them 111auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 112n_embd_head ,n_head ,n_head_kv ,il ); 113 114Qcur = build_norm (Qcur ,model .layers [il ].attn_q_norm ,nullptr ,LLM_NORM_RMS ,il ); 115cb (Qcur ,"Qcur_normed" ,il ); 116 117Kcur = build_norm (Kcur ,model .layers [il ].attn_k_norm ,nullptr ,LLM_NORM_RMS ,il ); 118cb (Kcur ,"Kcur_normed" ,il ); 119 120const bool is_swa = hparams .is_swa (il ); 121 122if (is_swa ) { 123// For sliding window layers, use regular rope with no yarn rope scaling. 124// This is achieved here by setting freq_scale and attn_factor to 1. 125// We also set ext_factor to 0 to avoid a few unnecessary computations. 126Qcur = ggml_rope_ext ( 127ctx0 ,Qcur ,inp_pos ,nullptr , 128n_rot ,rope_type ,n_ctx_orig ,freq_base ,1.0 , 1290.0 ,1.0 ,beta_fast ,beta_slow 130 ); 131 132Kcur = ggml_rope_ext ( 133ctx0 ,Kcur ,inp_pos ,nullptr , 134n_rot ,rope_type ,n_ctx_orig ,freq_base ,1.0 , 1350.0 ,1.0 ,beta_fast ,beta_slow 136 ); 137 }else { 138Qcur = ggml_rope_ext ( 139ctx0 ,Qcur ,inp_pos ,nullptr , 140n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 141ext_factor ,attn_factor ,beta_fast ,beta_slow 142 ); 143 144Kcur = ggml_rope_ext ( 145ctx0 ,Kcur ,inp_pos ,nullptr , 146n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 147ext_factor ,attn_factor ,beta_fast ,beta_slow 148 ); 149 } 150 151cb (Qcur ,"Qcur" ,il ); 152cb (Kcur ,"Kcur" ,il ); 153cb (Vcur ,"Vcur" ,il ); 154 155cur = build_attn (inp_attn , 156model .layers [il ].wo ,model .layers [il ].wo_b ,model .layers [il ].wo_s , 157Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,1.0f /sqrtf (float (n_embd_head )),il ); 158 } 159if (il == n_layer - 1 && inp_out_ids ) { 160cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 161inpSA = ggml_get_rows (ctx0 ,inpSA ,inp_out_ids ); 162 } 163ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 164cb (ffn_inp ,"ffn_inp" ,il ); 165 166// MoE 167cur = build_norm (ffn_inp , 168model .layers [il ].ffn_norm ,nullptr , 169LLM_NORM_RMS ,il ); 170cb (cur ,"ffn_norm" ,il ); 171 172ggml_tensor * moe_out = 173build_moe_ffn (cur , 174model .layers [il ].ffn_gate_inp , 175model .layers [il ].ffn_up_exps , 176model .layers [il ].ffn_gate_exps , 177model .layers [il ].ffn_down_exps , 178nullptr , 179n_expert ,n_expert_used , 180LLM_FFN_SILU , true, 181hparams .expert_weights_scale , 182LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX , 183il , 184nullptr ,nullptr , 185model .layers [il ].ffn_up_exps_s , 186model .layers [il ].ffn_gate_exps_s , 187model .layers [il ].ffn_down_exps_s ); 188cb (moe_out ,"ffn_moe_out" ,il ); 189cur = moe_out ; 190 191cur = ggml_add (ctx0 ,cur ,ffn_inp ); 192cb (cur ,"ffn_out" ,il ); 193 194cur = build_cvec (cur ,il ); 195cb (cur ,"l_out" ,il ); 196 197// input for next layer 198inpL = cur ; 199 } 200cur = inpL ; 201 202cur = build_norm (cur , 203model .output_norm ,nullptr , 204LLM_NORM_RMS ,-1 ); 205 206cb (cur ,"result_norm" ,-1 ); 207res -> t_embd = cur ; 208 209// lm_head 210cur = build_lora_mm (model .output ,cur ,model .output_s ); 211 212cb (cur ,"result_output" ,-1 ); 213res -> t_logits = cur ; 214 215ggml_build_forward_expand (gf ,cur ); 216} 217 218template struct llama_model_mellum::graph < false> ; 219template struct llama_model_mellum:: graph < true > ;