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