okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b2de2641f
main
1#include "models.h" 2 3void llama_model_exaone4::load_arch_hparams (llama_model_loader & ml ) { 4if (hparams .n_layer ()== 64 ) {// 32B 5hparams .swa_type = LLAMA_SWA_TYPE_STANDARD ; 6hparams .n_swa = 4096 ; 7load_swa_pattern (ml ,4 ); 8 9hparams .rope_freq_base_train_swa = hparams .rope_freq_base_train ; 10hparams .rope_freq_scale_train_swa = hparams .rope_freq_scale_train ; 11ml .get_key (LLM_KV_ROPE_FREQ_BASE_SWA ,hparams .rope_freq_base_train_swa , false); 12 } 13 14ml .get_key (LLM_KV_ATTENTION_SLIDING_WINDOW ,hparams .n_swa , false); 15ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps ); 16 17switch (hparams .n_layer ()) { 18case 30 :type = LLM_TYPE_1_2B ;break ; 19case 64 :type = LLM_TYPE_32B ;break ; 20default :type = LLM_TYPE_UNKNOWN ; 21 } 22} 23 24void llama_model_exaone4::load_arch_tensors (llama_model_loader & ) { 25LLAMA_LOAD_LOCALS ; 26 27tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 28 29// output 30output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 31output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED ); 32 33// if output is NULL, init from the input tok embed 34if (output == NULL ) { 35output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED ); 36 } 37 38for (int i = 0 ;i < n_layer_all ;++ i ) { 39const bool is_nextn = i >=n_layer ; 40int flags = 0 ; 41if (is_nextn ) { 42// NextN/MTP layers are preserved in GGUF but are not executed yet. 43flags |=TENSOR_SKIP ; 44 } 45 46auto & layer = layers [i ]; 47 48create_tensor_qkv (layer ,i ,n_embd ,n_embd_head_k * n_head ,n_embd_k_gqa ,n_embd_v_gqa ,flags ); 49layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd ,n_embd },flags ); 50 51if (!is_nextn ) { 52layer .rope_freqs = create_tensor (tn (LLM_TENSOR_ROPE_FREQS ,"weight" ,i ), {n_rot /2 },TENSOR_NOT_REQUIRED | (i != 0 ?TENSOR_DUPLICATED :0 )); 53 } 54 55layer .attn_post_norm = create_tensor (tn (LLM_TENSOR_ATTN_POST_NORM ,"weight" ,i ), {n_embd },flags ); 56layer .attn_q_norm = create_tensor (tn (LLM_TENSOR_ATTN_Q_NORM ,"weight" ,i ), {n_embd_head_k },flags ); 57layer .attn_k_norm = create_tensor (tn (LLM_TENSOR_ATTN_K_NORM ,"weight" ,i ), {n_embd_head_k },flags ); 58 59layer .ffn_gate = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"weight" ,i ), {n_embd ,n_ff },flags ); 60layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },flags ); 61layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },flags ); 62layer .ffn_post_norm = create_tensor (tn (LLM_TENSOR_FFN_POST_NORM ,"weight" ,i ), {n_embd },flags ); 63 64if (is_nextn ) { 65layer .nextn .eh_proj = create_tensor (tn (LLM_TENSOR_NEXTN_EH_PROJ ,"weight" ,i ), {2 * n_embd ,n_embd },flags ); 66layer .nextn .enorm = create_tensor (tn (LLM_TENSOR_NEXTN_ENORM ,"weight" ,i ), {n_embd },flags ); 67layer .nextn .hnorm = create_tensor (tn (LLM_TENSOR_NEXTN_HNORM ,"weight" ,i ), {n_embd },flags ); 68layer .nextn .shared_head_norm = create_tensor (tn (LLM_TENSOR_NEXTN_SHARED_HEAD_NORM ,"weight" ,i ), {n_embd },flags |TENSOR_NOT_REQUIRED ); 69 } 70 } 71} 72 73std::unique_ptr < llm_graph_context > llama_model_exaone4::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_exaone4::graph < iswa > ::graph (const llama_model & model ,const llm_graph_params & params ) : 83llm_graph_context (params ) { 84const int64_t n_embd_head = hparams .n_embd_head_k (); 85 86GGML_ASSERT (n_embd_head == hparams .n_embd_head_v ()); 87GGML_ASSERT (n_embd_head == n_rot ); 88 89ggml_tensor * cur ; 90ggml_tensor * inpL ; 91 92inpL = build_inp_embd (model .tok_embd ); 93 94// inp_pos - contains the positions 95ggml_tensor * inp_pos = build_inp_pos (); 96 97using inp_attn_type = std::conditional_t < iswa ,llm_graph_input_attn_kv_iswa ,llm_graph_input_attn_kv > ; 98inp_attn_type * inp_attn = nullptr ; 99 100if constexpr (iswa ) { 101inp_attn = build_attn_inp_kv_iswa (); 102 }else { 103inp_attn = build_attn_inp_kv (); 104 } 105ggml_tensor * inp_out_ids = build_inp_out_ids (); 106 107for (int il = 0 ;il < n_layer ;++ il ) { 108ggml_tensor * inpSA = inpL ; 109 110// use RoPE for SWA layers or non-SWA models 111const bool use_rope = hparams .is_swa (il )|| hparams .swa_type == LLAMA_SWA_TYPE_NONE ; 112 113cur = inpL ; 114 115// self-attention 116 { 117ggml_tensor * rope_factors = model .get_rope_factors (cparams ,il ); 118 119auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 120n_embd_head ,n_head ,n_head_kv ,il ); 121 122Qcur = build_norm (Qcur ,model .layers [il ].attn_q_norm ,NULL ,LLM_NORM_RMS ,il ); 123Kcur = build_norm (Kcur ,model .layers [il ].attn_k_norm ,NULL ,LLM_NORM_RMS ,il ); 124cb (Qcur ,"Qcur_normed" ,il ); 125cb (Kcur ,"Kcur_normed" ,il ); 126 127if (use_rope ) { 128Qcur = ggml_rope_ext (ctx0 ,Qcur ,inp_pos ,rope_factors ,n_rot ,rope_type ,n_ctx_orig ,freq_base , 129freq_scale ,ext_factor ,attn_factor ,beta_fast ,beta_slow ); 130 131Kcur = ggml_rope_ext (ctx0 ,Kcur ,inp_pos ,rope_factors ,n_rot ,rope_type ,n_ctx_orig ,freq_base , 132freq_scale ,ext_factor ,attn_factor ,beta_fast ,beta_slow ); 133 } 134cb (Qcur ,"Qcur" ,il ); 135cb (Kcur ,"Kcur" ,il ); 136cb (Vcur ,"Vcur" ,il ); 137 138cur = build_attn (inp_attn , 139model .layers [il ].wo ,NULL ,model .layers [il ].wo_s , 140Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,1.0f /sqrtf (float (n_embd_head )),il ); 141cb (cur ,"attn_out" ,il ); 142 } 143if (il == n_layer - 1 && inp_out_ids ) { 144cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 145inpSA = ggml_get_rows (ctx0 ,inpSA ,inp_out_ids ); 146 } 147cur = build_norm (cur ,model .layers [il ].attn_post_norm ,NULL ,LLM_NORM_RMS ,il ); 148cb (cur ,"attn_post_norm" ,il ); 149 150ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 151cb (ffn_inp ,"ffn_inp" ,il ); 152 153// feed-forward network 154cur = build_ffn (ffn_inp , 155model .layers [il ].ffn_up ,NULL ,NULL , 156model .layers [il ].ffn_gate ,NULL ,NULL , 157model .layers [il ].ffn_down ,NULL ,NULL ,NULL , 158LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 159cb (cur ,"ffn_out" ,il ); 160 161cur = build_norm (cur ,model .layers [il ].ffn_post_norm ,NULL ,LLM_NORM_RMS ,-1 ); 162cb (cur ,"ffn_post_norm" ,-1 ); 163 164cur = ggml_add (ctx0 ,cur ,ffn_inp ); 165 166cur = build_cvec (cur ,il ); 167cb (cur ,"l_out" ,il ); 168 169// input for next layer 170inpL = cur ; 171 } 172cur = inpL ; 173 174cur = build_norm (cur ,model .output_norm ,NULL ,LLM_NORM_RMS ,-1 ); 175 176cb (cur ,"result_norm" ,-1 ); 177res -> t_embd = cur ; 178 179// lm_head 180cur = build_lora_mm (model .output ,cur ,model .output_s ); 181 182cb (cur ,"result_output" ,-1 ); 183res -> t_logits = cur ; 184 185ggml_build_forward_expand (gf ,cur ); 186} 187 188// Explicit template instantiations 189template struct llama_model_exaone4::graph < false> ; 190template struct llama_model_exaone4:: graph < true > ;