okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b2de2641f
main
1#include "models.h" 2 3void llama_model_gemma2::load_arch_hparams (llama_model_loader & ml ) { 4hparams .swa_type = LLAMA_SWA_TYPE_STANDARD ; 5hparams .n_swa = 4096 ;// default value of gemma 2 6load_swa_pattern (ml ,2 ); 7hparams .attn_soft_cap = true; 8hparams .rope_freq_base_train_swa = hparams .rope_freq_base_train ; 9hparams .rope_freq_scale_train_swa = hparams .rope_freq_scale_train ; 10 11ml .get_key (LLM_KV_ROPE_FREQ_BASE_SWA ,hparams .rope_freq_base_train_swa , false); 12ml .get_key (LLM_KV_ATTENTION_SLIDING_WINDOW ,hparams .n_swa , false); 13ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps ); 14ml .get_key (LLM_KV_ATTN_LOGIT_SOFTCAPPING ,hparams .f_attn_logit_softcapping , false); 15ml .get_key (LLM_KV_FINAL_LOGIT_SOFTCAPPING ,hparams .f_final_logit_softcapping , false); 16 17switch (hparams .n_layer ()) { 18case 26 :type = LLM_TYPE_2B ;break ; 19case 42 :type = LLM_TYPE_9B ;break ; 20case 46 :type = LLM_TYPE_27B ;break ; 21default :type = LLM_TYPE_UNKNOWN ; 22 } 23 24// ref: https://github.com/google/gemma_pytorch/blob/014acb7ac4563a5f77c76d7ff98f31b568c16508/gemma/config.py#L173 25hparams .f_attention_scale = type == LLM_TYPE_27B 26 ?1.0f / std::sqrt (float (hparams .n_embd /hparams .n_head (0 ))) 27 :1.0f / std::sqrt (float (hparams .n_embd_head_k ())); 28} 29 30void llama_model_gemma2::load_arch_tensors (llama_model_loader & ) { 31LLAMA_LOAD_LOCALS ; 32 33tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 34 35// output 36output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 37output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED );// same as tok_embd, duplicated to allow offloading 38 39for (int i = 0 ;i < n_layer ;++ i ) { 40auto & layer = layers [i ]; 41 42layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 43 44create_tensor_qkv (layer ,i ,n_embd ,n_embd_head_k * n_head ,n_embd_k_gqa ,n_embd_v_gqa ,0 ); 45layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd_head_k * n_head ,n_embd },0 ); 46layer .attn_post_norm = create_tensor (tn (LLM_TENSOR_ATTN_POST_NORM ,"weight" ,i ), {n_embd },0 ); 47 48layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 49layer .ffn_gate = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"weight" ,i ), {n_embd ,n_ff },0 ); 50layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },0 ); 51layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },0 ); 52layer .ffn_post_norm = create_tensor (tn (LLM_TENSOR_FFN_POST_NORM ,"weight" ,i ), {n_embd },0 ); 53 } 54} 55 56std::unique_ptr < llm_graph_context > llama_model_gemma2::build_arch_graph (const llm_graph_params & params )const { 57return std::make_unique < graph > (* this ,params ); 58} 59 60llama_model_gemma2::graph::graph (const llama_model & model ,const llm_graph_params & params ) :llm_graph_context (params ) { 61const int64_t n_embd_head = hparams .n_embd_head_k (); 62 63ggml_tensor * cur ; 64ggml_tensor * inpL ; 65 66inpL = build_inp_embd (model .tok_embd ); 67 68inpL = ggml_scale (ctx0 ,inpL ,sqrtf (n_embd )); 69cb (inpL ,"inp_scaled" ,-1 ); 70 71// inp_pos - contains the positions 72ggml_tensor * inp_pos = build_inp_pos (); 73 74auto * inp_attn = build_attn_inp_kv_iswa (); 75 76ggml_tensor * inp_out_ids = build_inp_out_ids (); 77 78for (int il = 0 ;il < n_layer ;++ il ) { 79const float freq_base_l = model .get_rope_freq_base (cparams ,il ); 80const float freq_scale_l = model .get_rope_freq_scale (cparams ,il ); 81 82// norm 83cur = build_norm (inpL , 84model .layers [il ].attn_norm ,NULL , 85LLM_NORM_RMS ,il ); 86cb (cur ,"attn_norm" ,il ); 87 88// self-attention 89 { 90// compute Q and K and RoPE them 91auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 92n_embd_head ,n_head ,n_head_kv ,il ); 93 94Qcur = ggml_rope_ext ( 95ctx0 ,Qcur ,inp_pos ,nullptr , 96n_rot ,rope_type ,n_ctx_orig ,freq_base_l ,freq_scale_l , 97ext_factor ,attn_factor ,beta_fast ,beta_slow ); 98 99Kcur = ggml_rope_ext ( 100ctx0 ,Kcur ,inp_pos ,nullptr , 101n_rot ,rope_type ,n_ctx_orig ,freq_base_l ,freq_scale_l , 102ext_factor ,attn_factor ,beta_fast ,beta_slow ); 103 104cb (Qcur ,"Qcur" ,il ); 105cb (Kcur ,"Kcur" ,il ); 106cb (Vcur ,"Vcur" ,il ); 107 108Qcur = ggml_scale (ctx0 ,Qcur ,hparams .f_attention_scale ); 109 110cur = build_attn (inp_attn , 111model .layers [il ].wo ,NULL ,model .layers [il ].wo_s , 112Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,1.0f ,il ); 113 } 114if (il == n_layer - 1 && inp_out_ids ) { 115cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 116inpL = ggml_get_rows (ctx0 ,inpL ,inp_out_ids ); 117 } 118cur = build_norm (cur , 119model .layers [il ].attn_post_norm ,NULL , 120LLM_NORM_RMS ,il ); 121cb (cur ,"attn_post_norm" ,il ); 122 123ggml_tensor * sa_out = ggml_add (ctx0 ,cur ,inpL ); 124cb (sa_out ,"sa_out" ,il ); 125 126cur = build_norm (sa_out , 127model .layers [il ].ffn_norm ,NULL , 128LLM_NORM_RMS ,il ); 129cb (cur ,"ffn_norm" ,il ); 130 131// feed-forward network 132 { 133cur = build_ffn (cur , 134model .layers [il ].ffn_up ,NULL ,NULL , 135model .layers [il ].ffn_gate ,NULL ,NULL , 136model .layers [il ].ffn_down ,NULL ,NULL , 137NULL , 138LLM_FFN_GELU ,LLM_FFN_PAR ,il ); 139cb (cur ,"ffn_out" ,il ); 140 } 141cur = build_norm (cur , 142model .layers [il ].ffn_post_norm ,NULL , 143LLM_NORM_RMS ,-1 ); 144cb (cur ,"ffn_post_norm" ,-1 ); 145 146cur = ggml_add (ctx0 ,cur ,sa_out ); 147 148cur = build_cvec (cur ,il ); 149cb (cur ,"l_out" ,il ); 150 151// input for next layer 152inpL = cur ; 153 } 154cur = inpL ; 155 156cur = build_norm (cur , 157model .output_norm ,NULL , 158LLM_NORM_RMS ,-1 ); 159 160cb (cur ,"result_norm" ,-1 ); 161res -> t_embd = cur ; 162 163// lm_head 164cur = build_lora_mm (model .output ,cur ,model .output_s ); 165 166// final logit soft-capping 167cur = ggml_scale (ctx0 ,cur ,1.0f /hparams .f_final_logit_softcapping ); 168cur = ggml_tanh (ctx0 ,cur ); 169cur = ggml_scale (ctx0 ,cur ,hparams .f_final_logit_softcapping ); 170 171cb (cur ,"result_output" ,-1 ); 172res -> t_logits = cur ; 173 174ggml_build_forward_expand (gf ,cur ); 175}