okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b2de2641f
main
1#include "models.h" 2 3void llama_model_gemma3::load_arch_hparams (llama_model_loader & ml ) { 4const bool found_swa = ml .get_key (LLM_KV_ATTENTION_SLIDING_WINDOW ,hparams .n_swa , false); 5if (found_swa && hparams .n_swa > 0 ) { 6hparams .swa_type = LLAMA_SWA_TYPE_STANDARD ; 7load_swa_pattern (ml ,6 ); 8 9ml .get_key (LLM_KV_ROPE_FREQ_BASE_SWA ,hparams .rope_freq_base_train_swa , false); 10 }else { 11hparams .swa_type = LLAMA_SWA_TYPE_NONE ; 12 } 13 14hparams .f_final_logit_softcapping = 0.0f ; 15ml .get_key (LLM_KV_FINAL_LOGIT_SOFTCAPPING ,hparams .f_final_logit_softcapping , false); 16ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps ); 17 18switch (hparams .n_layer ()) { 19case 18 :type = LLM_TYPE_270M ;break ; 20case 26 :type = LLM_TYPE_1B ;break ; 21case 32 :type = LLM_TYPE_8B ;break ;// Rnj-1 22case 34 :type = LLM_TYPE_4B ;break ; 23case 48 :type = LLM_TYPE_12B ;break ; 24case 62 :type = LLM_TYPE_27B ;break ; 25default :type = LLM_TYPE_UNKNOWN ; 26 } 27 28// ref: https://github.com/google/gemma_pytorch/blob/014acb7ac4563a5f77c76d7ff98f31b568c16508/gemma/config.py#L289 29hparams .f_attention_scale = type == LLM_TYPE_27B 30 ?1.0f / std::sqrt (float (hparams .n_embd /hparams .n_head (0 ))) 31 :1.0f / std::sqrt (float (hparams .n_embd_head_k ())); 32} 33 34void llama_model_gemma3::load_arch_tensors (llama_model_loader & ) { 35LLAMA_LOAD_LOCALS ; 36 37tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 38 39// output 40output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 41output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED ); 42 43// if output is NULL, init from the input tok embed 44if (output == NULL ) { 45output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED ); 46 } 47 48// Dense linear weights 49dense_2_out_layers = create_tensor (tn (LLM_TENSOR_DENSE_2_OUT ,"weight" ), {n_embd ,hparams .dense_2_feat_out },TENSOR_NOT_REQUIRED ); 50dense_3_out_layers = create_tensor (tn (LLM_TENSOR_DENSE_3_OUT ,"weight" ), {hparams .dense_3_feat_in ,n_embd },TENSOR_NOT_REQUIRED ); 51 52 53for (int i = 0 ;i < n_layer ;++ i ) { 54auto & layer = layers [i ]; 55 56layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 57 58create_tensor_qkv (layer ,i ,n_embd ,n_embd_head_k * n_head ,n_embd_k_gqa ,n_embd_v_gqa ,0 ); 59layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd_head_k * n_head ,n_embd },0 ); 60 61layer .attn_post_norm = create_tensor (tn (LLM_TENSOR_ATTN_POST_NORM ,"weight" ,i ), {n_embd },0 ); 62layer .attn_k_norm = create_tensor (tn (LLM_TENSOR_ATTN_K_NORM ,"weight" ,i ), {n_embd_head_k },0 ); 63layer .attn_q_norm = create_tensor (tn (LLM_TENSOR_ATTN_Q_NORM ,"weight" ,i ), {n_embd_head_k },0 ); 64 65layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 66layer .ffn_gate = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"weight" ,i ), {n_embd ,n_ff },0 ); 67layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },0 ); 68layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },0 ); 69layer .ffn_post_norm = create_tensor (tn (LLM_TENSOR_FFN_POST_NORM ,"weight" ,i ), {n_embd },0 ); 70 } 71} 72 73std::unique_ptr < llm_graph_context > llama_model_gemma3::build_arch_graph (const llm_graph_params & params )const { 74if (hparams .swa_type == LLAMA_SWA_TYPE_STANDARD ) { 75return std::make_unique < graph < true>> (* this ,params ); 76 }else { 77return std::make_unique < graph < false>> (* this ,params ); 78 } 79} 80 81template < bool iswa > 82llama_model_gemma3::graph < iswa > ::graph (const llama_model & model ,const llm_graph_params & params ) :llm_graph_context (params ) { 83const int64_t n_embd_head = hparams .n_embd_head_k (); 84 85ggml_tensor * cur ; 86ggml_tensor * inpL ; 87 88inpL = build_inp_embd (model .tok_embd ); 89 90// important: do not normalize weights for raw embeddings input (i.e. encoded image embeddings) 91inpL = ggml_scale (ctx0 ,inpL ,ubatch .token ?sqrtf (n_embd ) :1.0f ); 92cb (inpL ,"inp_scaled" ,-1 ); 93 94// inp_pos - contains the positions 95ggml_tensor * inp_pos = build_inp_pos (); 96 97// TODO: is causal == true correct? might need some changes 98using inp_attn_type = std::conditional_t < iswa ,llm_graph_input_attn_kv_iswa ,llm_graph_input_attn_kv > ; 99inp_attn_type * inp_attn = nullptr ; 100 101if constexpr (iswa ) { 102inp_attn = build_attn_inp_kv_iswa (); 103 }else { 104inp_attn = build_attn_inp_kv (); 105 } 106 107ggml_tensor * inp_out_ids = build_inp_out_ids (); 108 109for (int il = 0 ;il < n_layer ;++ il ) { 110float freq_base_l = 0.0f ; 111float freq_scale_l = 0.0f ; 112 113if constexpr (iswa ) { 114freq_base_l = model .get_rope_freq_base (cparams ,il ); 115freq_scale_l = model .get_rope_freq_scale (cparams ,il ); 116 }else { 117freq_base_l = freq_base ; 118freq_scale_l = freq_scale ; 119 } 120 121// norm 122cur = build_norm (inpL ,model .layers [il ].attn_norm ,NULL ,LLM_NORM_RMS ,il ); 123cb (cur ,"attn_norm" ,il ); 124 125// self-attention 126 { 127// compute Q and K and RoPE them 128auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 129n_embd_head ,n_head ,n_head_kv ,il ); 130 131Qcur = build_norm (Qcur ,model .layers [il ].attn_q_norm ,NULL ,LLM_NORM_RMS ,il ); 132cb (Qcur ,"Qcur_normed" ,il ); 133 134Qcur = ggml_rope_ext ( 135ctx0 ,Qcur ,inp_pos ,nullptr , 136n_rot ,rope_type ,n_ctx_orig ,freq_base_l ,freq_scale_l , 137ext_factor ,attn_factor ,beta_fast ,beta_slow ); 138 139Kcur = build_norm (Kcur ,model .layers [il ].attn_k_norm ,NULL ,LLM_NORM_RMS ,il ); 140cb (Kcur ,"Kcur_normed" ,il ); 141 142Kcur = ggml_rope_ext ( 143ctx0 ,Kcur ,inp_pos ,nullptr , 144n_rot ,rope_type ,n_ctx_orig ,freq_base_l ,freq_scale_l , 145ext_factor ,attn_factor ,beta_fast ,beta_slow ); 146 147cb (Qcur ,"Qcur" ,il ); 148cb (Kcur ,"Kcur" ,il ); 149cb (Vcur ,"Vcur" ,il ); 150 151// ref: https://github.com/google/gemma_pytorch/blob/014acb7ac4563a5f77c76d7ff98f31b568c16508/gemma/model.py#L315 152Qcur = ggml_scale (ctx0 ,Qcur ,hparams .f_attention_scale ); 153 154cur = build_attn (inp_attn , 155model .layers [il ].wo ,NULL ,model .layers [il ].wo_s , 156Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,1.0f ,il ); 157 } 158if (il == n_layer - 1 && inp_out_ids ) { 159cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 160inpL = ggml_get_rows (ctx0 ,inpL ,inp_out_ids ); 161 } 162cur = build_norm (cur , 163model .layers [il ].attn_post_norm ,NULL , 164LLM_NORM_RMS ,il ); 165cb (cur ,"attn_post_norm" ,il ); 166 167ggml_tensor * sa_out = ggml_add (ctx0 ,cur ,inpL ); 168cb (sa_out ,"sa_out" ,il ); 169 170cur = build_norm (sa_out , 171model .layers [il ].ffn_norm ,NULL , 172LLM_NORM_RMS ,il ); 173cb (cur ,"ffn_norm" ,il ); 174 175// feed-forward network 176 { 177cur = build_ffn (cur , 178model .layers [il ].ffn_up ,NULL ,NULL , 179model .layers [il ].ffn_gate ,NULL ,NULL , 180model .layers [il ].ffn_down ,NULL ,NULL , 181NULL , 182LLM_FFN_GELU ,LLM_FFN_PAR ,il ); 183cb (cur ,"ffn_out" ,il ); 184 } 185cur = build_norm (cur , 186model .layers [il ].ffn_post_norm ,NULL , 187LLM_NORM_RMS ,-1 ); 188cb (cur ,"ffn_post_norm" ,il ); 189 190cur = ggml_add (ctx0 ,cur ,sa_out ); 191 192cur = build_cvec (cur ,il ); 193cb (cur ,"l_out" ,il ); 194 195// input for next layer 196inpL = cur ; 197 } 198cur = inpL ; 199 200cur = build_norm (cur , 201model .output_norm ,NULL , 202LLM_NORM_RMS ,-1 ); 203 204cb (cur ,"result_norm" ,-1 ); 205res -> t_embd = cur ; 206 207// lm_head 208cur = build_lora_mm (model .output ,cur ,model .output_s ); 209 210if (hparams .f_final_logit_softcapping ) { 211cur = ggml_scale (ctx0 ,cur ,1.0f /hparams .f_final_logit_softcapping ); 212cur = ggml_tanh (ctx0 ,cur ); 213cur = ggml_scale (ctx0 ,cur ,hparams .f_final_logit_softcapping ); 214 } 215 216cb (cur ,"result_output" ,-1 ); 217res -> t_logits = cur ; 218 219ggml_build_forward_expand (gf ,cur ); 220} 221 222template struct llama_model_gemma3::graph < false> ; 223template struct llama_model_gemma3:: graph < true > ;