okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b2de2641f
main
1#include "models.h" 2 3void llama_model_smallthinker::load_arch_hparams (llama_model_loader & ml ) { 4const bool found_swa = ml .get_key (LLM_KV_ATTENTION_SLIDING_WINDOW ,hparams .n_swa , false); 5 6if (found_swa && hparams .n_swa > 0 ) { 7hparams .swa_type = LLAMA_SWA_TYPE_STANDARD ; 8hparams .n_swa = 4096 ; 9load_swa_pattern (ml ,4 , true); 10 11hparams .rope_freq_base_train_swa = hparams .rope_freq_base_train ; 12hparams .rope_freq_scale_train_swa = hparams .rope_freq_scale_train ; 13ml .get_key (LLM_KV_ROPE_FREQ_BASE_SWA ,hparams .rope_freq_base_train_swa , false); 14 }else { 15hparams .swa_type = LLAMA_SWA_TYPE_NONE ; 16hparams .n_no_rope_layer_step = hparams .n_layer (); 17 } 18 19ml .get_key_or_arr (LLM_KV_EXPERT_FEED_FORWARD_LENGTH ,hparams .n_ff_exp_arr ,hparams .n_layer_all , false); 20ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps ); 21ml .get_key (LLM_KV_EXPERT_GATING_FUNC ,hparams .expert_gating_func , false); 22 23switch (hparams .n_layer ()) { 24case 32 :type = LLM_TYPE_4B ;break ; 25case 52 :type = LLM_TYPE_20B ;break ; 26default :type = LLM_TYPE_UNKNOWN ; 27 } 28} 29 30void llama_model_smallthinker::load_arch_tensors (llama_model_loader & ) { 31LLAMA_LOAD_LOCALS ; 32 33tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 34 35// output 36output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 37output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED ); 38 39// if output is NULL, init from the input tok embed 40if (output == NULL ) { 41output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED ); 42 } 43 44for (int i = 0 ;i < n_layer ;++ i ) { 45auto & layer = layers [i ]; 46 47layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 48 49create_tensor_qkv (layer ,i ,n_embd ,n_embd_head_k * n_head ,n_embd_gqa ,n_embd_gqa ,0 ); 50layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd_head_k * n_head ,n_embd },0 ); 51 52layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 53 54GGML_ASSERT (n_expert > 0 && "n_expert must be > 0 for SMALLTHINKER" ); 55GGML_ASSERT (n_expert_used > 0 && "n_expert_used must be > 0 for SMALLTHINKER" ); 56 57// MoE branch 58const int64_t n_ff_exp = hparams .n_ff_exp (); 59layer .ffn_gate_inp = create_tensor (tn (LLM_TENSOR_FFN_GATE_INP ,"weight" ,i ), {n_embd ,n_expert },0 ); 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_smallthinker::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 }else { 70return std::make_unique < graph < false>> (* this ,params ); 71 } 72} 73 74template < bool iswa > 75llama_model_smallthinker::graph < iswa > ::graph (const llama_model & model ,const llm_graph_params & params ) :llm_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 89using inp_attn_type = std::conditional_t < iswa ,llm_graph_input_attn_kv_iswa ,llm_graph_input_attn_kv > ; 90inp_attn_type * inp_attn = nullptr ; 91 92if constexpr (iswa ) { 93inp_attn = build_attn_inp_kv_iswa (); 94 }else { 95inp_attn = build_attn_inp_kv (); 96 } 97ggml_tensor * inp_out_ids = build_inp_out_ids (); 98 99for (int il = 0 ;il < n_layer ;++ il ) { 100const float freq_base_l = model .get_rope_freq_base (cparams ,il ); 101const float freq_scale_l = model .get_rope_freq_scale (cparams ,il ); 102 103ggml_tensor * inpSA = inpL ; 104 105// This overlaps with SWA layers in current models, so get_rope_freq_base/scale may be superfluous 106const bool use_rope = hparams .n_no_rope_layer_step == n_layer || 107il %hparams .n_no_rope_layer_step != 0 ; 108 109ggml_tensor * probs = build_lora_mm (model .layers [il ].ffn_gate_inp ,inpL );// [n_expert, n_tokens] 110cb (probs ,"ffn_moe_logits" ,il ); 111 112// norm 113cur = build_norm (inpL ,model .layers [il ].attn_norm ,NULL ,LLM_NORM_RMS ,il ); 114cb (cur ,"attn_norm" ,il ); 115 116// self_attention 117 { 118// compute Q and K and RoPE them 119auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 120n_embd_head ,n_head ,n_head_kv ,il ); 121 122if (use_rope ) { 123Qcur = ggml_rope_ext (ctx0 ,Qcur ,inp_pos ,nullptr ,n_rot ,rope_type ,n_ctx_orig ,freq_base_l ,freq_scale_l , 124ext_factor ,attn_factor ,beta_fast ,beta_slow ); 125 126Kcur = ggml_rope_ext (ctx0 ,Kcur ,inp_pos ,nullptr ,n_rot ,rope_type ,n_ctx_orig ,freq_base_l ,freq_scale_l , 127ext_factor ,attn_factor ,beta_fast ,beta_slow ); 128 } 129cb (Qcur ,"Qcur" ,il ); 130cb (Kcur ,"Kcur" ,il ); 131 132cur = build_attn (inp_attn , 133model .layers [il ].wo ,model .layers [il ].wo_b ,model .layers [il ].wo_s , 134Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,1.0f /sqrtf (float (n_embd_head )),il ); 135 } 136if (il == n_layer - 1 && inp_out_ids ) { 137cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 138inpSA = ggml_get_rows (ctx0 ,inpSA ,inp_out_ids ); 139probs = ggml_get_rows (ctx0 ,probs ,inp_out_ids ); 140 } 141ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 142cb (ffn_inp ,"ffn_inp" ,il ); 143 144// MoE branch 145cur = build_norm (ffn_inp ,model .layers [il ].ffn_norm ,NULL ,LLM_NORM_RMS ,il ); 146cb (cur ,"ffn_norm" ,il ); 147 148ggml_tensor * ffn_out = 149build_moe_ffn (cur , 150nullptr , 151model .layers [il ].ffn_up_exps , 152model .layers [il ].ffn_gate_exps , 153model .layers [il ].ffn_down_exps , 154nullptr , 155n_expert ,n_expert_used , 156LLM_FFN_RELU , true, 157hparams .expert_weights_scale , 158static_cast < llama_expert_gating_func_type > (hparams .expert_gating_func ), 159il ,probs ); 160 161cb (ffn_out ,"ffn_out" ,il ); 162cur = ffn_out ; 163 164cur = ggml_add (ctx0 ,cur ,ffn_inp ); 165 166cur = build_cvec (cur ,il ); 167cb (cur ,"l_out" ,il ); 168 169// input for next layer 170inpL = cur ; 171 } 172cur = inpL ; 173 174cur = build_norm (cur ,model .output_norm ,NULL ,LLM_NORM_RMS ,-1 ); 175cb (cur ,"result_norm" ,-1 ); 176res -> t_embd = cur ; 177 178// lm_head 179cur = build_lora_mm (model .output ,cur ,model .output_s ); 180cb (cur ,"result_output" ,-1 ); 181res -> t_logits = cur ; 182 183ggml_build_forward_expand (gf ,cur ); 184} 185 186// Explicit template instantiations 187template struct llama_model_smallthinker::graph < false> ; 188template struct llama_model_smallthinker:: graph < true > ;