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