okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b2de2641f
main
1#include "models.h" 2 3void llama_model_olmo2::load_arch_hparams (llama_model_loader & ml ) { 4ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps ); 5 6const bool found_swa = ml .get_key (LLM_KV_ATTENTION_SLIDING_WINDOW ,hparams .n_swa , false); 7if (found_swa && hparams .n_swa > 0 ) { 8hparams .swa_type = LLAMA_SWA_TYPE_STANDARD ; 9load_swa_pattern (ml ,4 ); 10 11hparams .rope_freq_base_train_swa = hparams .rope_freq_base_train ; 12hparams .rope_freq_scale_train_swa = 1.0 ;// See olmo2.cpp 13ml .get_key (LLM_KV_ROPE_FREQ_BASE_SWA ,hparams .rope_freq_base_train_swa , false); 14 }else { 15hparams .swa_type = LLAMA_SWA_TYPE_NONE ; 16 } 17 18switch (hparams .n_layer ()) { 19case 16 :type = LLM_TYPE_1B ;break ; 20case 32 :type = LLM_TYPE_7B ;break ; 21case 40 :type = LLM_TYPE_13B ;break ; 22case 64 :type = LLM_TYPE_32B ;break ; 23default :type = LLM_TYPE_UNKNOWN ; 24 } 25} 26 27void llama_model_olmo2::load_arch_tensors (llama_model_loader & ) { 28LLAMA_LOAD_LOCALS ; 29 30const int64_t n_embd_head = n_embd /n_head ; 31 32tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 33 34// output 35output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 36output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },0 ); 37 38for (int i = 0 ;i < n_layer ;++ i ) { 39auto & layer = layers [i ]; 40 41create_tensor_qkv (layer ,i ,n_embd ,n_embd ,n_embd_gqa ,n_embd_gqa ,0 ); 42layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd ,n_embd },0 ); 43layer .attn_q_norm = create_tensor (tn (LLM_TENSOR_ATTN_Q_NORM ,"weight" ,i ), {n_embd },0 ); 44layer .attn_k_norm = create_tensor (tn (LLM_TENSOR_ATTN_K_NORM ,"weight" ,i ), {n_head_kv * n_embd_head },0 ); 45layer .attn_post_norm = create_tensor (tn (LLM_TENSOR_ATTN_POST_NORM ,"weight" ,i ), {n_embd },0 ); 46 47layer .ffn_gate = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"weight" ,i ), {n_embd ,n_ff },0 ); 48layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },0 ); 49layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },0 ); 50layer .ffn_post_norm = create_tensor (tn (LLM_TENSOR_FFN_POST_NORM ,"weight" ,i ), {n_embd },0 ); 51 } 52} 53 54std::unique_ptr < llm_graph_context > llama_model_olmo2::build_arch_graph (const llm_graph_params & params )const { 55if (hparams .swa_type == LLAMA_SWA_TYPE_STANDARD ) { 56return std::make_unique < graph < true>> (* this ,params ); 57 }else { 58return std::make_unique < graph < false>> (* this ,params ); 59 } 60} 61 62template < bool iswa > 63llama_model_olmo2::graph < iswa > ::graph (const llama_model & model ,const llm_graph_params & params ) :llm_graph_context (params ) { 64const int64_t n_embd_head = hparams .n_embd_head_v (); 65 66GGML_ASSERT (n_embd_head == hparams .n_embd_head_k ()); 67GGML_ASSERT (n_embd_head == n_rot ); 68 69ggml_tensor * cur ; 70ggml_tensor * inpL ; 71 72inpL = build_inp_embd (model .tok_embd ); 73 74// inp_pos - contains the positions 75ggml_tensor * inp_pos = build_inp_pos (); 76 77using inp_attn_type = std::conditional_t < iswa ,llm_graph_input_attn_kv_iswa ,llm_graph_input_attn_kv > ; 78inp_attn_type * inp_attn = nullptr ; 79 80if constexpr (iswa ) { 81inp_attn = build_attn_inp_kv_iswa (); 82 }else { 83inp_attn = build_attn_inp_kv (); 84 } 85ggml_tensor * inp_out_ids = build_inp_out_ids (); 86 87for (int il = 0 ;il < n_layer ;++ il ) { 88ggml_tensor * inpSA = inpL ; 89 90cur = inpL ; 91 92// self_attention 93 { 94auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 95n_embd_head ,n_head , 96n_embd_head ,n_head_kv , 97n_embd_head ,n_head_kv , 98il , false); 99cb (Qcur ,"Qcur" ,il ); 100cb (Kcur ,"Kcur" ,il ); 101cb (Vcur ,"Vcur" ,il ); 102 103Qcur = build_norm (Qcur ,model .layers [il ].attn_q_norm ,NULL , 104LLM_NORM_RMS ,il ); 105cb (Qcur ,"Qcur_normed" ,il ); 106 107Kcur = build_norm (Kcur ,model .layers [il ].attn_k_norm ,NULL , 108LLM_NORM_RMS ,il ); 109cb (Kcur ,"Kcur_normed" ,il ); 110 111Qcur = ggml_reshape_3d (ctx0 ,Qcur ,n_embd_head ,n_head ,n_tokens ); 112Kcur = ggml_reshape_3d (ctx0 ,Kcur ,n_embd_head ,n_head_kv ,n_tokens ); 113Vcur = ggml_reshape_3d (ctx0 ,Vcur ,n_embd_head ,n_head_kv ,n_tokens ); 114 115const bool is_swa = hparams .is_swa (il ); 116 117if (is_swa ) { 118// For sliding window layers, Olmo3 use regular rope with no yarn rope scaling. 119// This is achieved here by setting freq_scale and attn_factor to 1. 120// We also set ext_factor to 0 to avoid a few unnecessary computations. 121Qcur = ggml_rope_ext ( 122ctx0 ,Qcur ,inp_pos ,nullptr , 123n_rot ,rope_type ,n_ctx_orig ,freq_base ,1.0 , 1240.0 ,1.0 ,beta_fast ,beta_slow 125 ); 126 127Kcur = ggml_rope_ext ( 128ctx0 ,Kcur ,inp_pos ,nullptr , 129n_rot ,rope_type ,n_ctx_orig ,freq_base ,1.0 , 1300.0 ,1.0 ,beta_fast ,beta_slow 131 ); 132 }else { 133Qcur = ggml_rope_ext ( 134ctx0 ,Qcur ,inp_pos ,nullptr , 135n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 136ext_factor ,attn_factor ,beta_fast ,beta_slow 137 ); 138 139Kcur = ggml_rope_ext ( 140ctx0 ,Kcur ,inp_pos ,nullptr , 141n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 142ext_factor ,attn_factor ,beta_fast ,beta_slow 143 ); 144 } 145cb (Qcur ,"Qcur" ,il ); 146cb (Kcur ,"Kcur" ,il ); 147cb (Vcur ,"Vcur" ,il ); 148 149cur = build_attn (inp_attn , 150model .layers [il ].wo ,NULL ,model .layers [il ].wo_s , 151Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,1.0f /sqrtf (float (n_embd_head )),il ); 152 } 153if (il == n_layer - 1 && inp_out_ids ) { 154cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 155inpSA = ggml_get_rows (ctx0 ,inpSA ,inp_out_ids ); 156 } 157cur = build_norm (cur , 158model .layers [il ].attn_post_norm ,NULL , 159LLM_NORM_RMS ,il ); 160cb (cur ,"attn_post_norm" ,il ); 161 162ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 163cb (ffn_inp ,"ffn_inp" ,il ); 164 165// feed-forward network 166cur = build_ffn (ffn_inp , 167model .layers [il ].ffn_up ,NULL ,NULL , 168model .layers [il ].ffn_gate ,NULL ,NULL , 169model .layers [il ].ffn_down ,NULL ,NULL , 170NULL , 171LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 172cb (cur ,"ffn_out" ,il ); 173 174cur = build_norm (cur , 175model .layers [il ].ffn_post_norm ,NULL , 176LLM_NORM_RMS ,-1 ); 177cb (cur ,"ffn_post_norm" ,-1 ); 178 179cur = ggml_add (ctx0 ,cur ,ffn_inp ); 180cb (cur ,"ffn_out" ,il ); 181 182cur = build_cvec (cur ,il ); 183cb (cur ,"l_out" ,il ); 184 185// input for next layer 186inpL = cur ; 187 } 188cur = inpL ; 189 190cur = build_norm (cur , 191model .output_norm ,NULL , 192LLM_NORM_RMS ,-1 ); 193 194cb (cur ,"result_norm" ,-1 ); 195res -> t_embd = cur ; 196 197// lm_head 198cur = build_lora_mm (model .output ,cur ,model .output_s ); 199 200cb (cur ,"result_output" ,-1 ); 201res -> t_logits = cur ; 202 203ggml_build_forward_expand (gf ,cur ); 204} 205 206// Explicit template instantiations 207template struct llama_model_olmo2::graph < false> ; 208template struct llama_model_olmo2:: graph < true > ;