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_openai_moe::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 ); 7 8hparams .swa_type = LLAMA_SWA_TYPE_STANDARD ; 9uint32_t swa_period = 2 ; 10ml .get_key_or_arr (LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN ,swa_period , false); 11hparams .set_swa_pattern (swa_period ); 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 17switch (hparams .n_layer ()) { 18case 24 :type = LLM_TYPE_20B ;break ; 19case 36 :type = LLM_TYPE_120B ;break ; 20default :type = LLM_TYPE_UNKNOWN ; 21 } 22} 23 24void llama_model_openai_moe::load_arch_tensors (llama_model_loader & ) { 25LLAMA_LOAD_LOCALS ; 26 27const int64_t n_ff_exp = hparams .n_ff_exp (); 28 29tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 30 31// output 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 35for (int i = 0 ;i < n_layer ;++ i ) { 36auto & layer = layers [i ]; 37 38layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 39layer .attn_post_norm = create_tensor (tn (LLM_TENSOR_ATTN_POST_NORM ,"weight" ,i ), {n_embd },0 ); 40 41create_tensor_qkv (layer ,i ,n_embd ,n_head * n_rot ,n_head_kv * n_rot ,n_head_kv * n_rot ,0 ); 42layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_head * n_rot ,n_embd },0 ); 43 44layer .attn_sinks = create_tensor (tn (LLM_TENSOR_ATTN_SINKS ,"weight" ,i ), {n_head },0 ); 45 46layer .ffn_gate_inp = create_tensor (tn (LLM_TENSOR_FFN_GATE_INP ,"weight" ,i ), {n_embd ,n_expert },0 ); 47layer .ffn_gate_exps = create_tensor (tn (LLM_TENSOR_FFN_GATE_EXPS ,"weight" ,i ), {n_embd ,n_ff_exp ,n_expert },0 ); 48layer .ffn_down_exps = create_tensor (tn (LLM_TENSOR_FFN_DOWN_EXPS ,"weight" ,i ), {n_ff_exp ,n_embd ,n_expert },0 ); 49layer .ffn_up_exps = create_tensor (tn (LLM_TENSOR_FFN_UP_EXPS ,"weight" ,i ), {n_embd ,n_ff_exp ,n_expert },0 ); 50 51layer .wo_b = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"bias" ,i ), {n_embd },0 ); 52 53layer .ffn_gate_inp_b = create_tensor (tn (LLM_TENSOR_FFN_GATE_INP ,"bias" ,i ), {n_expert },0 ); 54layer .ffn_gate_exps_b = create_tensor (tn (LLM_TENSOR_FFN_GATE_EXPS ,"bias" ,i ), {n_ff_exp ,n_expert },0 ); 55layer .ffn_down_exps_b = create_tensor (tn (LLM_TENSOR_FFN_DOWN_EXPS ,"bias" ,i ), {n_embd ,n_expert },0 ); 56layer .ffn_up_exps_b = create_tensor (tn (LLM_TENSOR_FFN_UP_EXPS ,"bias" ,i ), {n_ff_exp ,n_expert },0 ); 57 } 58} 59 60std::unique_ptr < llm_graph_context > llama_model_openai_moe::build_arch_graph (const llm_graph_params & params )const { 61return std::make_unique < graph > (* this ,params ); 62} 63 64llama_model_openai_moe::graph::graph (const llama_model & model ,const llm_graph_params & params ) :llm_graph_context (params ) { 65ggml_tensor * cur ; 66ggml_tensor * inpL ; 67 68inpL = build_inp_embd (model .tok_embd ); 69 70// inp_pos - contains the positions 71ggml_tensor * inp_pos = build_inp_pos (); 72 73auto * inp_attn = build_attn_inp_kv_iswa (); 74 75ggml_tensor * inp_out_ids = build_inp_out_ids (); 76 77for (int il = 0 ;il < n_layer ;++ il ) { 78res -> t_layer_inp [il ]= inpL ; 79 80const float freq_base_l = model .get_rope_freq_base (cparams ,il ); 81const float freq_scale_l = model .get_rope_freq_scale (cparams ,il ); 82 83ggml_tensor * inpSA = inpL ; 84 85// norm 86cur = build_norm (inpL , 87model .layers [il ].attn_norm ,nullptr , 88LLM_NORM_RMS ,il ); 89cb (cur ,"attn_norm" ,il ); 90 91// self-attention 92 { 93// compute Q and K and RoPE them 94auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 95n_rot ,n_head ,n_head_kv ,il ); 96 97Qcur = ggml_rope_ext ( 98ctx0 ,Qcur ,inp_pos ,nullptr , 99n_rot ,rope_type ,n_ctx_orig ,freq_base_l ,freq_scale_l , 100ext_factor ,attn_factor ,beta_fast ,beta_slow 101 ); 102 103Kcur = ggml_rope_ext ( 104ctx0 ,Kcur ,inp_pos ,nullptr , 105n_rot ,rope_type ,n_ctx_orig ,freq_base_l ,freq_scale_l , 106ext_factor ,attn_factor ,beta_fast ,beta_slow 107 ); 108 109cb (Qcur ,"Qcur" ,il ); 110cb (Kcur ,"Kcur" ,il ); 111cb (Vcur ,"Vcur" ,il ); 112 113cur = build_attn (inp_attn , 114model .layers [il ].wo ,model .layers [il ].wo_b ,model .layers [il ].wo_s , 115Qcur ,Kcur ,Vcur ,nullptr ,model .layers [il ].attn_sinks ,nullptr ,1.0f /sqrtf (float (n_rot )),il ); 116 117cb (cur ,"attn_out" ,il ); 118 } 119if (il == n_layer - 1 && inp_out_ids && cparams .embeddings_nextn_masked ) { 120// skip computing output for unused tokens 121cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 122inpSA = ggml_get_rows (ctx0 ,inpSA ,inp_out_ids ); 123 } 124ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 125cb (ffn_inp ,"ffn_inp" ,il ); 126 127cur = ffn_inp ; 128cur = build_norm (cur , 129model .layers [il ].attn_post_norm ,nullptr , 130LLM_NORM_RMS ,il ); 131cb (cur ,"attn_post_norm" ,il ); 132 133// MoE branch 134cur = build_moe_ffn (cur , 135model .layers [il ].ffn_gate_inp ,model .layers [il ].ffn_gate_inp_b , 136model .layers [il ].ffn_up_exps ,model .layers [il ].ffn_up_exps_b , 137model .layers [il ].ffn_gate_exps ,model .layers [il ].ffn_gate_exps_b , 138model .layers [il ].ffn_down_exps ,model .layers [il ].ffn_down_exps_b , 139nullptr , 140n_expert ,n_expert_used , 141LLM_FFN_SWIGLU_OAI_MOE , false, 142hparams .expert_weights_scale , 143LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX_WEIGHT , 144il ); 145cb (cur ,"ffn_moe_out" ,il ); 146 147cur = ggml_add (ctx0 ,cur ,ffn_inp ); 148 149cur = build_cvec (cur ,il ); 150cb (cur ,"l_out" ,il ); 151 152// input for next layer 153inpL = cur ; 154 } 155cur = inpL ; 156 157res -> t_h_nextn = cur ; 158 159if (!cparams .embeddings_nextn_masked && inp_out_ids ) { 160cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 161 } 162 163cur = build_norm (cur , 164model .output_norm ,NULL , 165LLM_NORM_RMS ,-1 ); 166 167cb (cur ,"result_norm" ,-1 ); 168res -> t_embd = cur ; 169 170// lm_head 171cur = build_lora_mm (model .output ,cur ,model .output_s ); 172 173cb (cur ,"result_output" ,-1 ); 174res -> t_logits = cur ; 175 176ggml_build_forward_expand (gf ,cur ); 177}