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