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