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