char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
3d10bcd19
char/aliceai-t5-moe
1#include "models.h" 2 3void llama_model_maple::load_arch_hparams (llama_model_loader & ml ) { 4hparams .swa_type = LLAMA_SWA_TYPE_STANDARD ; 5 6ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps ); 7ml .get_key (LLM_KV_ATTENTION_SLIDING_WINDOW ,hparams .n_swa ); 8ml .get_key_or_arr (LLM_KV_EXPERT_FEED_FORWARD_LENGTH ,hparams .n_ff_exp_arr ,hparams .n_layer_all ); 9 10ml .get_arr (LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN ,hparams .is_swa_impl ); 11 12hparams .rope_freq_base_train_swa = hparams .rope_freq_base_train ; 13hparams .rope_freq_scale_train_swa = hparams .rope_freq_scale_train ; 14ml .get_key (LLM_KV_ROPE_FREQ_BASE_SWA ,hparams .rope_freq_base_train_swa , false); 15 16ml .get_key_or_arr (LLM_KV_SWIGLU_CLAMP_EXP ,hparams .swiglu_clamp_exp ,hparams .n_layer_all ); 17 18switch (hparams .n_layer ()) { 19case 24 :type = LLM_TYPE_20B ;break ; 20default :type = LLM_TYPE_UNKNOWN ; 21 } 22} 23 24void llama_model_maple::load_arch_tensors (llama_model_loader & ) { 25LLAMA_LOAD_LOCALS ; 26 27const int64_t n_ff_exp = hparams .n_ff_exp (); 28const int64_t head_dim = hparams .n_embd_head_k (); 29 30tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 31 32output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 33output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },0 ); 34 35if (n_expert == 0 ) { 36throw std::runtime_error ("n_expert must be > 0 for Maple" ); 37 } 38if (n_expert_used == 0 ) { 39throw std::runtime_error ("n_expert_used must be > 0 for Maple" ); 40 } 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_head * head_dim ,n_head_kv * head_dim ,n_head_kv * head_dim ,0 ); 48layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_head * head_dim ,n_embd },0 ); 49 50layer .attn_q_norm = create_tensor (tn (LLM_TENSOR_ATTN_Q_NORM ,"weight" ,i ), {head_dim },0 ); 51layer .attn_k_norm = create_tensor (tn (LLM_TENSOR_ATTN_K_NORM ,"weight" ,i ), {head_dim },0 ); 52layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 53 54layer .ffn_gate_inp = create_tensor (tn (LLM_TENSOR_FFN_GATE_INP ,"weight" ,i ), {n_embd ,n_expert },0 ); 55layer .ffn_gate_exps = create_tensor (tn (LLM_TENSOR_FFN_GATE_EXPS ,"weight" ,i ), {n_embd ,n_ff_exp ,n_expert },0 ); 56layer .ffn_down_exps = create_tensor (tn (LLM_TENSOR_FFN_DOWN_EXPS ,"weight" ,i ), {n_ff_exp ,n_embd ,n_expert },0 ); 57layer .ffn_up_exps = create_tensor (tn (LLM_TENSOR_FFN_UP_EXPS ,"weight" ,i ), {n_embd ,n_ff_exp ,n_expert },0 ); 58 } 59} 60 61std::unique_ptr < llm_graph_context > llama_model_maple::build_arch_graph (const llm_graph_params & params )const { 62return std::make_unique < graph > (* this ,params ); 63} 64 65llama_model_maple::graph::graph (const llama_model & model ,const llm_graph_params & params ) : 66llm_graph_context (params ) { 67const int64_t n_embd_head = hparams .n_embd_head_k (); 68 69GGML_ASSERT (n_embd_head == hparams .n_embd_head_v ()); 70 71ggml_tensor * inpL = build_inp_embd (model .tok_embd ); 72ggml_tensor * inp_pos = build_inp_pos (); 73auto * inp_attn = build_attn_inp_kv_iswa (); 74ggml_tensor * inp_out_ids = build_inp_out_ids (); 75 76for (int il = 0 ;il < n_layer ;++ il ) { 77ggml_tensor * inpSA = inpL ; 78 79ggml_tensor * cur = build_norm (inpL ,model .layers [il ].attn_norm ,nullptr ,LLM_NORM_RMS ,il ); 80cb (cur ,"attn_norm" ,il ); 81 82 { 83auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur ,n_embd_head ,n_head ,n_head_kv ,il ); 84 85Qcur = build_norm (Qcur ,model .layers [il ].attn_q_norm ,nullptr ,LLM_NORM_RMS ,il ); 86Kcur = build_norm (Kcur ,model .layers [il ].attn_k_norm ,nullptr ,LLM_NORM_RMS ,il ); 87cb (Qcur ,"Qcur_normed" ,il ); 88cb (Kcur ,"Kcur_normed" ,il ); 89 90if (hparams .is_swa (il )) { 91const int64_t n_rot_l = hparams .n_rot (il ); 92const float freq_base_l = model .get_rope_freq_base (cparams ,il ); 93const float freq_scale_l = model .get_rope_freq_scale (cparams ,il ); 94 95Qcur = ggml_rope_ext (ctx0 ,Qcur ,inp_pos ,nullptr ,n_rot_l ,rope_type ,n_ctx_orig ,freq_base_l , 96freq_scale_l ,ext_factor ,attn_factor ,beta_fast ,beta_slow ); 97Kcur = ggml_rope_ext (ctx0 ,Kcur ,inp_pos ,nullptr ,n_rot_l ,rope_type ,n_ctx_orig ,freq_base_l , 98freq_scale_l ,ext_factor ,attn_factor ,beta_fast ,beta_slow ); 99 } 100cb (Qcur ,"Qcur" ,il ); 101cb (Kcur ,"Kcur" ,il ); 102cb (Vcur ,"Vcur" ,il ); 103 104cur = build_attn (inp_attn , 105model .layers [il ].wo ,nullptr ,model .layers [il ].wo_s , 106Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,1.0f /sqrtf (float (n_embd_head )),il ); 107cb (cur ,"attn_out" ,il ); 108 } 109 110if (il == n_layer - 1 && inp_out_ids ) { 111cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 112inpSA = ggml_get_rows (ctx0 ,inpSA ,inp_out_ids ); 113 } 114 115ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 116cb (ffn_inp ,"ffn_inp" ,il ); 117 118cur = build_norm (ffn_inp ,model .layers [il ].ffn_norm ,nullptr ,LLM_NORM_RMS ,il ); 119cb (cur ,"ffn_norm" ,il ); 120 121cur = build_moe_ffn (cur , 122model .layers [il ].ffn_gate_inp , 123model .layers [il ].ffn_up_exps , 124model .layers [il ].ffn_gate_exps , 125model .layers [il ].ffn_down_exps , 126nullptr , 127n_expert ,n_expert_used , 128LLM_FFN_SILU , true, 1291.0f , 130LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX , 131il ); 132cb (cur ,"ffn_moe_out" ,il ); 133 134cur = ggml_add (ctx0 ,cur ,ffn_inp ); 135cur = build_cvec (cur ,il ); 136cb (cur ,"l_out" ,il ); 137 138inpL = cur ; 139 } 140 141ggml_tensor * cur = build_norm (inpL ,model .output_norm ,nullptr ,LLM_NORM_RMS ,-1 ); 142cb (cur ,"result_norm" ,-1 ); 143res -> t_embd = cur ; 144 145cur = build_lora_mm (model .output ,cur ,model .output_s ); 146cb (cur ,"result_output" ,-1 ); 147res -> t_logits = cur ; 148 149ggml_build_forward_expand (gf ,cur ); 150}