okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b2de2641f
main
1#include "models.h" 2 3void llama_model_afmoe::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_GATING_FUNC ,hparams .expert_gating_func , false); 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_ATTENTION_SLIDING_WINDOW ,hparams .n_swa , false); 12 13// Set up interleaved sliding window attention (ISWA) 14// Pattern: 3 sliding - 1 full (global_attn_every_n_layers = 4) 15if (hparams .n_swa > 0 ) { 16hparams .swa_type = LLAMA_SWA_TYPE_STANDARD ; 17load_swa_pattern (ml ,4 ); 18 19hparams .rope_freq_base_train_swa = hparams .rope_freq_base_train ; 20hparams .rope_freq_scale_train_swa = hparams .rope_freq_scale_train ; 21ml .get_key (LLM_KV_ROPE_FREQ_BASE_SWA ,hparams .rope_freq_base_train_swa , false); 22 }else { 23hparams .swa_type = LLAMA_SWA_TYPE_NONE ; 24 } 25 26// Default to sigmoid if not set 27if (hparams .expert_gating_func == LLAMA_EXPERT_GATING_FUNC_TYPE_NONE ) { 28hparams .expert_gating_func = LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID ; 29 } 30 31switch (hparams .n_layer ()) { 32case 56 :type = LLM_TYPE_6B ;break ; 33case 32 :type = LLM_TYPE_26B ;break ; 34default :type = LLM_TYPE_UNKNOWN ; 35 } 36} 37 38void llama_model_afmoe::load_arch_tensors (llama_model_loader & ) { 39LLAMA_LOAD_LOCALS ; 40const int64_t n_expert_shared = hparams .n_expert_shared ; 41 42tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 43 44// output 45output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 46output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED ); 47 48// if output is NULL, init from the input tok embed 49if (output == NULL ) { 50output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED ); 51 } 52 53const int64_t n_ff_exp = hparams .n_ff_exp (); 54 55for (int i = 0 ;i < n_layer ;++ i ) { 56auto & layer = layers [i ]; 57 58// dual attention normalization 59layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 60layer .attn_post_norm = create_tensor (tn (LLM_TENSOR_ATTN_POST_NORM ,"weight" ,i ), {n_embd },0 ); 61 62// attention projections 63create_tensor_qkv (layer ,i ,n_embd ,n_embd_head_k * n_head ,n_embd_k_gqa ,n_embd_v_gqa ,0 ); 64layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd_head_k * n_head ,n_embd },0 ); 65 66// Q/K normalization 67layer .attn_q_norm = create_tensor (tn (LLM_TENSOR_ATTN_Q_NORM ,"weight" ,i ), {n_embd_head_k },0 ); 68layer .attn_k_norm = create_tensor (tn (LLM_TENSOR_ATTN_K_NORM ,"weight" ,i ), {n_embd_head_k },0 ); 69 70// attention gating 71layer .wqkv_gate = create_tensor (tn (LLM_TENSOR_ATTN_GATE ,"weight" ,i ), {n_embd ,n_embd_head_k * n_head },0 ); 72 73// dual ffn normalization 74layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 75layer .ffn_post_norm = create_tensor (tn (LLM_TENSOR_FFN_POST_NORM ,"weight" ,i ), {n_embd },0 ); 76 77if (static_cast < uint32_t > (i ) >=hparams .n_layer_dense_lead ) { 78// MoE layers 79layer .ffn_gate_inp = create_tensor (tn (LLM_TENSOR_FFN_GATE_INP ,"weight" ,i ), {n_embd ,n_expert },0 ); 80layer .ffn_exp_probs_b = create_tensor (tn (LLM_TENSOR_FFN_EXP_PROBS_B ,"bias" ,i ), {n_expert },0 ); 81 82// grouped expert weights 83layer .ffn_gate_exps = create_tensor (tn (LLM_TENSOR_FFN_GATE_EXPS ,"weight" ,i ), {n_embd ,n_ff_exp ,n_expert },0 ); 84layer .ffn_down_exps = create_tensor (tn (LLM_TENSOR_FFN_DOWN_EXPS ,"weight" ,i ), {n_ff_exp ,n_embd ,n_expert },0 ); 85layer .ffn_up_exps = create_tensor (tn (LLM_TENSOR_FFN_UP_EXPS ,"weight" ,i ), {n_embd ,n_ff_exp ,n_expert },0 ); 86 87// shared expert 88if (n_expert_shared > 0 ) { 89const int64_t n_ff_shexp = n_ff_exp * n_expert_shared ; 90layer .ffn_gate_shexp = create_tensor (tn (LLM_TENSOR_FFN_GATE_SHEXP ,"weight" ,i ), {n_embd ,n_ff_shexp },0 ); 91layer .ffn_down_shexp = create_tensor (tn (LLM_TENSOR_FFN_DOWN_SHEXP ,"weight" ,i ), {n_ff_shexp ,n_embd },0 ); 92layer .ffn_up_shexp = create_tensor (tn (LLM_TENSOR_FFN_UP_SHEXP ,"weight" ,i ), {n_embd ,n_ff_shexp },0 ); 93 } 94 }else { 95// Dense layers 96layer .ffn_gate = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"weight" ,i ), {n_embd ,n_ff },0 ); 97layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },0 ); 98layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },0 ); 99 } 100 } 101} 102 103std::unique_ptr < llm_graph_context > llama_model_afmoe::build_arch_graph (const llm_graph_params & params )const { 104return std::make_unique < graph > (* this ,params ); 105} 106 107llama_model_afmoe::graph::graph (const llama_model & model ,const llm_graph_params & params ) :llm_graph_context (params ) { 108const int64_t n_embd_head = hparams .n_embd_head_v (); 109GGML_ASSERT (n_embd_head == hparams .n_embd_head_k ()); 110 111ggml_tensor * cur ; 112ggml_tensor * inpL ; 113 114inpL = build_inp_embd (model .tok_embd ); 115 116// MuP scaling: embeddings * sqrt(hidden_size) 117// mup_enabled = true, hidden_size = 1024, scale = 32.0 118inpL = ggml_scale (ctx0 ,inpL ,sqrtf (float (n_embd ))); 119cb (inpL ,"inp_embd_scaled" ,-1 ); 120 121// inp_pos - contains the positions 122ggml_tensor * inp_pos = build_inp_pos (); 123auto * inp_attn = build_attn_inp_kv_iswa (); 124ggml_tensor * inp_out_ids = build_inp_out_ids (); 125 126const float kq_scale = 1.0f /sqrtf (float (n_embd_head )); 127 128for (int il = 0 ;il < n_layer ;++ il ) { 129const float freq_base_l = model .get_rope_freq_base (cparams ,il ); 130const float freq_scale_l = model .get_rope_freq_scale (cparams ,il ); 131 132ggml_tensor * inpSA = inpL ; 133 134// This overlaps with SWA layers in current models, so get_rope_freq_base/scale may be superfluous 135const bool use_rope = hparams .n_no_rope_layer_step > 0 && 136 (il + 1 ) %hparams .n_no_rope_layer_step != 0 ; 137 138// dual attention normalization (pre) 139cur = build_norm (inpL , 140model .layers [il ].attn_norm ,NULL , 141LLM_NORM_RMS ,il ); 142cb (cur ,"attn_norm" ,il ); 143 144// self-attention 145 { 146ggml_tensor * attn_inp = cur ;// save input for gate computation 147 148auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 149n_embd_head ,n_head ,n_head_kv ,il ); 150 151// compute gate from input 152ggml_tensor * gate = build_lora_mm (model .layers [il ].wqkv_gate ,attn_inp ); 153cb (gate ,"attn_gate_proj" ,il ); 154 155// Q/K normalization 156Qcur = build_norm (Qcur ,model .layers [il ].attn_q_norm ,NULL ,LLM_NORM_RMS ,il ); 157Kcur = build_norm (Kcur ,model .layers [il ].attn_k_norm ,NULL ,LLM_NORM_RMS ,il ); 158cb (Qcur ,"Qcur_normed" ,il ); 159cb (Kcur ,"Kcur_normed" ,il ); 160 161if (use_rope ) { 162Qcur = ggml_rope_ext ( 163ctx0 ,Qcur ,inp_pos ,nullptr , 164n_rot ,rope_type ,n_ctx_orig ,freq_base_l ,freq_scale_l , 165ext_factor ,attn_factor ,beta_fast ,beta_slow ); 166cb (Qcur ,"Qcur_rope" ,il ); 167 168Kcur = ggml_rope_ext ( 169ctx0 ,Kcur ,inp_pos ,nullptr , 170n_rot ,rope_type ,n_ctx_orig ,freq_base_l ,freq_scale_l , 171ext_factor ,attn_factor ,beta_fast ,beta_slow ); 172cb (Kcur ,"Kcur_rope" ,il ); 173 } 174 175cur = build_attn (inp_attn , 176NULL ,NULL ,NULL ,// wo will be applied after gating 177Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,kq_scale ,il ); 178cb (cur ,"attn_out" ,il ); 179 180// attention gating: attn_out * sigmoid(gate) BEFORE o_proj 181gate = ggml_sigmoid (ctx0 ,gate ); 182cb (gate ,"attn_gate_sig" ,il ); 183cur = ggml_mul (ctx0 ,cur ,gate ); 184cb (cur ,"attn_gated" ,il ); 185 186// now apply output projection 187cur = build_lora_mm (model .layers [il ].wo ,cur ,model .layers [il ].wo_s ); 188cb (cur ,"attn_o_proj" ,il ); 189 } 190 191// dual attention normalization (post) 192cur = build_norm (cur , 193model .layers [il ].attn_post_norm ,NULL , 194LLM_NORM_RMS ,il ); 195cb (cur ,"attn_post_norm" ,il ); 196 197if (il == n_layer - 1 && inp_out_ids ) { 198cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 199inpSA = ggml_get_rows (ctx0 ,inpSA ,inp_out_ids ); 200 } 201 202ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 203cb (ffn_inp ,"ffn_inp" ,il ); 204 205// dual ffn normalization (pre) 206cur = build_norm (ffn_inp , 207model .layers [il ].ffn_norm ,NULL , 208LLM_NORM_RMS ,il ); 209cb (cur ,"ffn_norm" ,il ); 210 211// MoE or dense FFN 212if ((uint32_t )il >=hparams .n_layer_dense_lead ) { 213// MoE layer with sigmoid routing, normalization, and scaling 214ggml_tensor * moe_out = build_moe_ffn (cur , 215model .layers [il ].ffn_gate_inp , 216model .layers [il ].ffn_up_exps , 217model .layers [il ].ffn_gate_exps , 218model .layers [il ].ffn_down_exps , 219model .layers [il ].ffn_exp_probs_b , 220n_expert ,n_expert_used , 221LLM_FFN_SILU , 222hparams .expert_weights_norm ,// norm_w (route_norm=True) 223hparams .expert_weights_scale ,// w_scale (route_scale=2.826) 224 (llama_expert_gating_func_type )hparams .expert_gating_func , 225il ); 226cb (moe_out ,"ffn_moe_out" ,il ); 227 228// shared expert 229if (hparams .n_expert_shared > 0 ) { 230ggml_tensor * ffn_shexp = build_ffn (cur , 231model .layers [il ].ffn_up_shexp ,NULL ,NULL , 232model .layers [il ].ffn_gate_shexp ,NULL ,NULL , 233model .layers [il ].ffn_down_shexp ,NULL ,NULL , 234NULL , 235LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 236cb (ffn_shexp ,"ffn_shexp" ,il ); 237 238cur = ggml_add (ctx0 ,moe_out ,ffn_shexp ); 239cb (cur ,"ffn_out" ,il ); 240 }else { 241cur = moe_out ; 242 } 243 }else { 244// dense layer 245cur = build_ffn (cur , 246model .layers [il ].ffn_up ,NULL ,NULL , 247model .layers [il ].ffn_gate ,NULL ,NULL , 248model .layers [il ].ffn_down ,NULL ,NULL , 249NULL , 250LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 251cb (cur ,"ffn_out" ,il ); 252 } 253 254// dual ffn normalization (post) 255cur = build_norm (cur , 256model .layers [il ].ffn_post_norm ,NULL , 257LLM_NORM_RMS ,il ); 258cb (cur ,"ffn_post_norm" ,il ); 259 260cur = ggml_add (ctx0 ,cur ,ffn_inp ); 261cur = build_cvec (cur ,il ); 262cb (cur ,"l_out" ,il ); 263 264// input for next layer 265inpL = cur ; 266 } 267 268cur = inpL ; 269 270cur = build_norm (cur , 271model .output_norm ,NULL , 272LLM_NORM_RMS ,-1 ); 273cb (cur ,"result_norm" ,-1 ); 274 275res -> t_embd = cur ; 276 277// lm_head 278cur = build_lora_mm (model .output ,cur ,model .output_s ); 279cb (cur ,"result_output" ,-1 ); 280res -> t_logits = cur ; 281 282ggml_build_forward_expand (gf ,cur ); 283}