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_glm4::load_arch_hparams (llama_model_loader & ml ) { 4ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps ); 5ml .get_key_or_arr (LLM_KV_ROPE_DIMENSION_SECTIONS ,hparams .rope_sections ,4 , false); 6 7switch (hparams .n_layer ()) { 8case 17 :type = LLM_TYPE_1B ;break ;// GLM-OCR 9case 40 :type = LLM_TYPE_9B ;break ; 10case 61 :type = LLM_TYPE_32B ;break ; 11default :type = LLM_TYPE_UNKNOWN ; 12 } 13} 14 15void llama_model_glm4::load_arch_tensors (llama_model_loader & ) { 16LLAMA_LOAD_LOCALS ; 17 18tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 19 20// output 21output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 22output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED ); 23// if output is NULL, init from the input tok embed 24if (output == NULL ) { 25output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED ); 26 } 27 28for (int i = 0 ;i < n_layer_all ;++ i ) { 29int flags = 0 ; 30if (i >=n_layer ) { 31// skip all tensors in the NextN layers 32flags |=TENSOR_SKIP ; 33 } 34 35auto & layer = layers [i ]; 36 37layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },flags ); 38create_tensor_qkv (layer ,i ,n_embd ,n_embd_head_k * n_head ,n_embd_k_gqa ,n_embd_v_gqa ,flags ); 39 40layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd_head_k * n_head ,n_embd },flags ); 41 42layer .attn_post_norm = create_tensor (tn (LLM_TENSOR_ATTN_POST_NORM ,"weight" ,i ), {n_embd },flags ); 43 44layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },flags ); 45layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },flags ); 46layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff * 2 },flags ); 47 48layer .ffn_post_norm = create_tensor (tn (LLM_TENSOR_FFN_POST_NORM ,"weight" ,i ), {n_embd },flags ); 49 50// NextN/MTP tensors (preserved but unused) - conditionally load for last nextn_predict_layers 51if (i >=n_layer ) { 52layer .nextn .eh_proj = create_tensor (tn (LLM_TENSOR_NEXTN_EH_PROJ ,"weight" ,i ), {2 * n_embd ,n_embd },flags ); 53layer .nextn .enorm = create_tensor (tn (LLM_TENSOR_NEXTN_ENORM ,"weight" ,i ), {n_embd },flags ); 54layer .nextn .hnorm = create_tensor (tn (LLM_TENSOR_NEXTN_HNORM ,"weight" ,i ), {n_embd },flags ); 55 56// Optional tensors 57layer .nextn .embed_tokens = create_tensor (tn (LLM_TENSOR_NEXTN_EMBED_TOKENS ,"weight" ,i ), {n_embd ,n_vocab },flags |TENSOR_NOT_REQUIRED ); 58layer .nextn .shared_head_head = create_tensor (tn (LLM_TENSOR_NEXTN_SHARED_HEAD_HEAD ,"weight" ,i ), {n_embd ,n_vocab },flags |TENSOR_NOT_REQUIRED ); 59layer .nextn .shared_head_norm = create_tensor (tn (LLM_TENSOR_NEXTN_SHARED_HEAD_NORM ,"weight" ,i ), {n_embd },flags |TENSOR_NOT_REQUIRED ); 60 } 61 } 62} 63 64std::unique_ptr < llm_graph_context > llama_model_glm4::build_arch_graph (const llm_graph_params & params )const { 65return std::make_unique < graph > (* this ,params ); 66} 67 68llama_model_glm4::graph::graph (const llama_model & model ,const llm_graph_params & params ) :llm_graph_context (params ) { 69const int64_t n_embd_head = hparams .n_embd_head_v (); 70 71GGML_ASSERT (n_embd_head == hparams .n_embd_head_k ()); 72 73int sections [4 ]; 74 std::copy (std::begin (hparams .rope_sections ), std::begin (hparams .rope_sections )+ 4 ,sections ); 75 76ggml_tensor * cur ; 77ggml_tensor * inpL ; 78 79inpL = build_inp_embd (model .tok_embd ); 80 81bool use_mrope = hparams .use_mrope (); 82if (ubatch .embd && !use_mrope ) { 83// unfortunately, we need to forcefully stop here, to avoid users complaining about wrong results 84GGML_ABORT ("This GGUF does not support multimodal. Please reconvert it." ); 85 } 86 87// inp_pos - contains the positions 88ggml_tensor * inp_pos = build_inp_pos (); 89 90auto * inp_attn = build_attn_inp_kv (); 91 92ggml_tensor * inp_out_ids = build_inp_out_ids (); 93 94// Only process up to last layer (skip final NextN layer) 95// Final layer tensors are loaded but not processed in forward pass 96for (int il = 0 ;il < n_layer ;++ il ) { 97ggml_tensor * inpSA = inpL ; 98 99// Pre-attention norm 100cur = build_norm (inpL ,model .layers [il ].attn_norm ,NULL ,LLM_NORM_RMS ,il ); 101cb (cur ,"attn_norm" ,il ); 102 103// self-attention 104 { 105auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 106n_embd_head ,n_head ,n_head_kv ,il ); 107 108if (use_mrope ) { 109Qcur = ggml_rope_multi (ctx0 ,Qcur ,inp_pos ,nullptr , 110n_rot ,sections ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 111ext_factor ,attn_factor ,beta_fast ,beta_slow ); 112 113Kcur = ggml_rope_multi (ctx0 ,Kcur ,inp_pos ,nullptr , 114n_rot ,sections ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 115ext_factor ,attn_factor ,beta_fast ,beta_slow ); 116 }else { 117// Normal RoPE 118Qcur = ggml_rope_ext (ctx0 ,Qcur ,inp_pos ,nullptr ,n_rot , 119rope_type ,n_ctx_orig ,freq_base ,freq_scale , 120ext_factor ,attn_factor ,beta_fast ,beta_slow ); 121 122Kcur = ggml_rope_ext (ctx0 ,Kcur ,inp_pos ,nullptr ,n_rot , 123rope_type ,n_ctx_orig ,freq_base ,freq_scale , 124ext_factor ,attn_factor ,beta_fast ,beta_slow ); 125 } 126 127cb (Qcur ,"Qcur" ,il ); 128cb (Kcur ,"Kcur" ,il ); 129cb (Vcur ,"Vcur" ,il ); 130 131cur = build_attn (inp_attn , 132model .layers [il ].wo ,NULL ,model .layers [il ].wo_s , 133Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,1.0f /sqrtf (float (n_embd_head )),il ); 134 } 135if (il == n_layer - 1 && inp_out_ids ) { 136cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 137inpSA = ggml_get_rows (ctx0 ,inpSA ,inp_out_ids ); 138 } 139// Post-attention norm (new!) 140cur = build_norm (cur ,model .layers [il ].attn_post_norm ,NULL ,LLM_NORM_RMS ,il ); 141cb (cur ,"post_attn_norm" ,il ); 142 143// Add the input (residual connection after post-attention norm) 144ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 145cb (ffn_inp ,"ffn_inp" ,il ); 146 147// FF 148 { 149// Pre-MLP norm 150cur = build_norm (ffn_inp ,model .layers [il ].ffn_norm ,NULL ,LLM_NORM_RMS ,il ); 151cb (cur ,"ffn_norm" ,il ); 152 153// MLP 154cur = build_ffn (cur , 155model .layers [il ].ffn_up ,NULL ,NULL , 156NULL ,NULL ,NULL , 157model .layers [il ].ffn_down ,NULL ,NULL , 158NULL ,LLM_FFN_SWIGLU ,LLM_FFN_SEQ ,il ); 159cb (cur ,"ffn_out" ,il ); 160 161// Post-MLP norm 162cur = build_norm (cur ,model .layers [il ].ffn_post_norm ,NULL ,LLM_NORM_RMS ,il ); 163cb (cur ,"post_mlp_norm" ,il ); 164 } 165cur = ggml_add (ctx0 ,cur ,ffn_inp ); 166 167cur = build_cvec (cur ,il ); 168cb (cur ,"l_out" ,il ); 169 170// input for next layer 171inpL = cur ; 172 } 173// Final norm 174cur = build_norm (inpL ,model .output_norm ,NULL ,LLM_NORM_RMS ,-1 ); 175 176cb (cur ,"result_norm" ,-1 ); 177res -> t_embd = cur ; 178 179// Output projection 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}