okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b2de2641f
main
1#include "models.h" 2 3void llama_model_cohere2::load_arch_hparams (llama_model_loader & ml ) { 4hparams .swa_type = LLAMA_SWA_TYPE_STANDARD ; 5load_swa_pattern (ml ,4 ); 6 7hparams .rope_freq_base_train_swa = hparams .rope_freq_base_train ; 8hparams .rope_freq_scale_train_swa = hparams .rope_freq_scale_train ; 9 10ml .get_key (LLM_KV_ROPE_FREQ_BASE_SWA ,hparams .rope_freq_base_train_swa , false); 11ml .get_key (LLM_KV_ATTENTION_SLIDING_WINDOW ,hparams .n_swa ); 12ml .get_key (LLM_KV_LOGIT_SCALE ,hparams .f_logit_scale ); 13ml .get_key (LLM_KV_ATTENTION_LAYERNORM_EPS ,hparams .f_norm_eps ); 14 15switch (hparams .n_layer ()) { 16case 32 :type = LLM_TYPE_8B ;break ; 17default :type = LLM_TYPE_UNKNOWN ; 18 } 19} 20 21void llama_model_cohere2::load_arch_tensors (llama_model_loader & ) { 22LLAMA_LOAD_LOCALS ; 23 24tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 25 26// output 27output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 28// init output from the input tok embed 29output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab }, 30TENSOR_DUPLICATED ); 31 32for (int i = 0 ;i < n_layer ;++ i ) { 33auto & layer = layers [i ]; 34 35layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 36 37create_tensor_qkv (layer ,i ,n_embd ,n_embd ,n_embd_gqa ,n_embd_gqa ,0 ); 38layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd ,n_embd },0 ); 39 40layer .ffn_gate = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"weight" ,i ), {n_embd ,n_ff },0 ); 41layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },0 ); 42layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },0 ); 43 } 44} 45 46std::unique_ptr < llm_graph_context > llama_model_cohere2::build_arch_graph (const llm_graph_params & params )const { 47return std::make_unique < graph > (* this ,params ); 48} 49 50llama_model_cohere2::graph::graph (const llama_model & model ,const llm_graph_params & params ) :llm_graph_context (params ) { 51const int64_t n_embd_head = hparams .n_embd_head_v (); 52 53GGML_ASSERT (n_embd_head == hparams .n_embd_head_k ()); 54 55const float f_logit_scale = hparams .f_logit_scale ; 56 57ggml_tensor * cur ; 58ggml_tensor * inpL ; 59 60inpL = build_inp_embd (model .tok_embd ); 61 62// inp_pos - contains the positions 63ggml_tensor * inp_pos = build_inp_pos (); 64 65auto * inp_attn = build_attn_inp_kv_iswa (); 66 67ggml_tensor * inp_out_ids = build_inp_out_ids (); 68 69for (int il = 0 ;il < n_layer ;++ il ) { 70const bool is_swa = hparams .is_swa (il ); 71// UNUSED: 72// const float freq_base_l = model.get_rope_freq_base (cparams, il); 73// const float freq_scale_l = model.get_rope_freq_scale(cparams, il); 74 75// norm 76cur = build_norm (inpL ,model .layers [il ].attn_norm ,NULL ,LLM_NORM ,il ); 77cb (cur ,"attn_norm" ,il ); 78ggml_tensor * ffn_inp = cur ; 79 80// self-attention 81 { 82// rope freq factors for 128k context 83ggml_tensor * rope_factors = model .get_rope_factors (cparams ,il ); 84 85// compute Q and K and RoPE them 86auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 87n_embd_head ,n_head ,n_head_kv ,il ); 88 89if (is_swa ) { 90Qcur = ggml_rope_ext ( 91ctx0 ,Qcur ,inp_pos ,rope_factors , 92n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 93ext_factor ,attn_factor ,beta_fast ,beta_slow 94 ); 95 96Kcur = ggml_rope_ext ( 97ctx0 ,Kcur ,inp_pos ,rope_factors , 98n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 99ext_factor ,attn_factor ,beta_fast ,beta_slow 100 ); 101 } 102 103cb (Qcur ,"Qcur" ,il ); 104cb (Kcur ,"Kcur" ,il ); 105cb (Vcur ,"Vcur" ,il ); 106 107cur = build_attn (inp_attn , 108model .layers [il ].wo ,model .layers [il ].wo_b ,model .layers [il ].wo_s , 109Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,1.0f /sqrtf (float (n_embd_head )),il ); 110 } 111 112if (il == n_layer - 1 && inp_out_ids ) { 113cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 114inpL = ggml_get_rows (ctx0 ,inpL ,inp_out_ids ); 115ffn_inp = ggml_get_rows (ctx0 ,ffn_inp ,inp_out_ids ); 116 } 117 118ggml_tensor * attn_out = cur ; 119 120// feed-forward network 121 { 122cur = build_ffn (ffn_inp , 123model .layers [il ].ffn_up ,NULL ,model .layers [il ].ffn_up_s , 124model .layers [il ].ffn_gate ,NULL ,model .layers [il ].ffn_gate_s , 125model .layers [il ].ffn_down ,NULL ,model .layers [il ].ffn_down_s , 126NULL ,LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 127cb (cur ,"ffn_out" ,il ); 128 } 129 130// add together residual + FFN + self-attention 131cur = ggml_add (ctx0 ,cur ,inpL ); 132cur = ggml_add (ctx0 ,cur ,attn_out ); 133 134cur = build_cvec (cur ,il ); 135cb (cur ,"l_out" ,il ); 136 137// input for next layer 138inpL = cur ; 139 } 140 141cur = inpL ; 142 143cur = build_norm (cur ,model .output_norm ,NULL ,LLM_NORM ,-1 ); 144 145cb (cur ,"result_norm" ,-1 ); 146res -> t_embd = cur ; 147 148// lm_head 149cur = build_lora_mm (model .output ,cur ,model .output_s ); 150 151if (f_logit_scale ) { 152cur = ggml_scale (ctx0 ,cur ,f_logit_scale ); 153 } 154 155cb (cur ,"result_output" ,-1 ); 156res -> t_logits = cur ; 157 158ggml_build_forward_expand (gf ,cur ); 159}