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