char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
97e4ca735
char/aliceai-t5-moe
1#include "models.h" 2 3void llama_model_gemma4_assistant::load_arch_hparams (llama_model_loader & ml ) { 4hparams .n_embd_inp_impl = hparams .n_embd_out (); 5 6hparams .swa_type = LLAMA_SWA_TYPE_STANDARD ; 7ml .get_arr (LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN ,hparams .is_swa_impl ); 8 9uint32_t n_kv_shared_layers = 0 ; 10ml .get_key (LLM_KV_ATTENTION_SHARED_KV_LAYERS ,n_kv_shared_layers , false); 11 12hparams .f_attention_scale = 1.0f ; 13 14ml .get_key (LLM_KV_ROPE_FREQ_BASE_SWA ,hparams .rope_freq_base_train_swa , false); 15ml .get_key (LLM_KV_ATTENTION_SLIDING_WINDOW ,hparams .n_swa ); 16ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps ); 17ml .get_key (LLM_KV_ATTENTION_KEY_LENGTH_SWA ,hparams .n_embd_head_k_swa ); 18ml .get_key (LLM_KV_ATTENTION_VALUE_LENGTH_SWA ,hparams .n_embd_head_v_swa ); 19} 20 21void llama_model_gemma4_assistant::load_arch_tensors (llama_model_loader & ) { 22LLAMA_LOAD_LOCALS ; 23 24if (n_embd_head_k != n_embd_head_v ) { 25throw std::runtime_error ("Gemma 4 assistant requires n_embd_head_k == n_embd_head_v" ); 26 } 27if (hparams .n_embd_head_k_swa != hparams .n_embd_head_v_swa ) { 28throw std::runtime_error ("Gemma 4 assistant requires n_embd_head_k_swa == n_embd_head_v_swa" ); 29 } 30if (hparams .n_embd_out ()== n_embd ) { 31throw std::runtime_error ("Gemma 4 assistant requires embedding_length_out to carry the target hidden size" ); 32 } 33 34tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 35output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED ); 36 37output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 38 39create_tensor (tn (LLM_TENSOR_MASKED_EMBD_CENTROIDS ,"weight" ), {},TENSOR_NOT_REQUIRED ); 40create_tensor (tn (LLM_TENSOR_MASKED_EMBD_ORDERING ), {},TENSOR_NOT_REQUIRED ); 41 42const int64_t n_embd_backbone = hparams .n_embd_inp (); 43nextn_proj_post = create_tensor (tn (LLM_TENSOR_NEXTN_PROJ_POST ,"weight" ), {n_embd ,n_embd_backbone },0 ); 44 45int rope_freqs_flag = 0 ; 46 47for (int i = 0 ;i < n_layer_nextn ;++ i ) { 48auto & layer = layers [i ]; 49 50const int64_t n_head = hparams .n_head (i ); 51const int64_t n_embd_head = hparams .n_embd_head_k (i ); 52const int64_t n_ff = hparams .n_ff (i ); 53 54if (i == 0 ) { 55nextn_proj_pre = create_tensor (tn (LLM_TENSOR_NEXTN_PROJ_PRE ,"weight" ,i ), {2 * n_embd_backbone ,n_embd },0 ); 56 } 57 58layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 59layer .wq = create_tensor (tn (LLM_TENSOR_ATTN_Q ,"weight" ,i ), {n_embd ,n_embd_head * n_head },0 ); 60layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd_head * n_head ,n_embd },0 ); 61 62layer .attn_q_norm = create_tensor (tn (LLM_TENSOR_ATTN_Q_NORM ,"weight" ,i ), {n_embd_head },0 ); 63layer .attn_post_norm = create_tensor (tn (LLM_TENSOR_ATTN_POST_NORM ,"weight" ,i ), {n_embd },0 ); 64 65layer .out_scale = create_tensor (tn (LLM_TENSOR_LAYER_OUT_SCALE ,"weight" ,i ), {1u },0 ); 66 67if (!hparams .is_swa (i )) { 68layer .rope_freqs = create_tensor (tn (LLM_TENSOR_ROPE_FREQS ,"weight" ,i ), {n_embd_head /2 },rope_freqs_flag ); 69rope_freqs_flag = TENSOR_DUPLICATED ; 70 } 71 72layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 73layer .ffn_gate = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"weight" ,i ), {n_embd ,n_ff },0 ); 74layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },0 ); 75layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },0 ); 76layer .ffn_post_norm = create_tensor (tn (LLM_TENSOR_FFN_POST_NORM ,"weight" ,i ), {n_embd },0 ); 77 } 78} 79 80std::unique_ptr < llm_graph_context > llama_model_gemma4_assistant::build_arch_graph (const llm_graph_params & params )const { 81return std::make_unique < graph > (* this ,params ); 82} 83 84llama_model_gemma4_assistant::graph::graph (const llama_model & model ,const llm_graph_params & params ) : 85llm_graph_context (params ) { 86const int64_t n_embd_backbone = hparams .n_embd_inp (); 87 88ggml_tensor * inp_tokens ; 89ggml_tensor * inp_h ; 90 { 91auto inp = std::make_unique < llm_graph_input_embd > (n_embd_backbone ); 92 93inp -> tokens = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_I32 ,ubatch .n_tokens ); 94cb (inp -> tokens ,"inp_tokens" ,-1 ); 95ggml_set_input (inp -> tokens ); 96inp_tokens = inp -> tokens ; 97res -> t_inp_tokens = inp -> tokens ; 98 99inp -> embd = ggml_new_tensor_2d (ctx0 ,GGML_TYPE_F32 ,n_embd_backbone ,ubatch .n_tokens ); 100cb (inp -> embd ,"inp_h" ,-1 ); 101ggml_set_input (inp -> embd ); 102inp_h = inp -> embd ; 103res -> t_inp_embd = inp -> embd ; 104 105res -> add_input (std::move (inp )); 106 } 107 108GGML_ASSERT (cparams .ctx_other != nullptr ); 109const auto * model_other = llama_get_model (cparams .ctx_other ); 110 111ggml_tensor * x = ggml_get_rows (ctx0 ,model_other -> tok_embd ,inp_tokens ); 112x = ggml_scale (ctx0 ,x ,sqrtf ((float )n_embd_backbone )); 113cb (x ,"inp_embd_target" ,-1 ); 114 115ggml_tensor * xh = ggml_concat (ctx0 ,x ,inp_h ,0 ); 116cb (xh ,"inp_xh" ,-1 ); 117 118ggml_tensor * cur = ggml_mul_mat (ctx0 ,model .nextn_proj_pre ,xh ); 119cb (cur ,"pre_proj" ,-1 ); 120 121auto * inp_attn = build_attn_inp_kv_iswa (); 122ggml_tensor * inp_pos = build_inp_pos (); 123ggml_tensor * inp_out_ids = build_inp_out_ids (); 124 125ggml_tensor * inpL = cur ; 126 127for (int il = 0 ;il < n_layer_nextn ;++ il ) { 128const bool is_swa = hparams .is_swa (il ); 129 130const int64_t n_embd_head = hparams .n_embd_head_k (il ); 131const int64_t n_head = hparams .n_head (il ); 132 133const float freq_base_l = model .get_rope_freq_base (cparams ,il ); 134const float freq_scale_l = model .get_rope_freq_scale (cparams ,il ); 135const int n_rot_l = hparams .n_rot (il ); 136 137ggml_tensor * cur_norm = build_norm (inpL ,model .layers [il ].attn_norm ,nullptr ,LLM_NORM_RMS ,il ); 138cb (cur_norm ,"attn_norm" ,il ); 139 140ggml_tensor * Qcur = build_lora_mm (model .layers [il ].wq ,cur_norm ); 141Qcur = ggml_reshape_3d (ctx0 ,Qcur ,n_embd_head ,n_head ,n_tokens ); 142Qcur = build_norm (Qcur ,model .layers [il ].attn_q_norm ,nullptr ,LLM_NORM_RMS ,il ); 143cb (Qcur ,"Qcur_normed" ,il ); 144 145ggml_tensor * freq_factors = is_swa ?nullptr :model .layers [il ].rope_freqs ; 146Qcur = ggml_rope_ext (ctx0 ,Qcur ,inp_pos ,freq_factors ,n_rot_l ,rope_type ,n_ctx_orig , 147freq_base_l ,freq_scale_l ,ext_factor ,attn_factor ,beta_fast ,beta_slow ); 148cb (Qcur ,"Qcur_pos" ,il ); 149 150cur = build_attn (inp_attn ,model .layers [il ].wo ,nullptr ,nullptr , 151Qcur ,nullptr ,nullptr ,nullptr ,nullptr ,nullptr ,hparams .f_attention_scale ,il ); 152 153if (il == n_layer_nextn - 1 && inp_out_ids ) { 154cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 155inpL = ggml_get_rows (ctx0 ,inpL ,inp_out_ids ); 156 } 157 158cur = build_norm (cur ,model .layers [il ].attn_post_norm ,nullptr ,LLM_NORM_RMS ,il ); 159cb (cur ,"attn_post_norm" ,il ); 160 161ggml_tensor * attn_out = ggml_add (ctx0 ,cur ,inpL ); 162cb (attn_out ,"attn_out" ,il ); 163 164cur = build_norm (attn_out ,model .layers [il ].ffn_norm ,nullptr ,LLM_NORM_RMS ,il ); 165cb (cur ,"ffn_norm" ,il ); 166 167cur = build_ffn (cur , 168model .layers [il ].ffn_up ,nullptr ,nullptr , 169model .layers [il ].ffn_gate ,nullptr ,nullptr , 170model .layers [il ].ffn_down ,nullptr ,nullptr , 171nullptr , 172LLM_FFN_GELU ,LLM_FFN_PAR ,il ); 173cb (cur ,"ffn_out" ,il ); 174 175cur = build_norm (cur ,model .layers [il ].ffn_post_norm ,nullptr ,LLM_NORM_RMS ,-1 ); 176cb (cur ,"ffn_post_norm" ,il ); 177 178cur = ggml_add (ctx0 ,cur ,attn_out ); 179 180cur = ggml_mul (ctx0 ,cur ,model .layers [il ].out_scale ); 181cb (cur ,"out_scaled" ,il ); 182 183inpL = cur ; 184 } 185cur = inpL ; 186 187cur = build_norm (cur ,model .output_norm ,nullptr ,LLM_NORM_RMS ,-1 ); 188cb (cur ,"result_norm" ,-1 ); 189 190ggml_tensor * logits = build_lora_mm (model .output ,cur ); 191cb (logits ,"result_output" ,-1 ); 192res -> t_logits = logits ; 193 194ggml_tensor * h_next = ggml_mul_mat (ctx0 ,model .nextn_proj_post ,cur ); 195cb (h_next ,"h_nextn" ,-1 ); 196res -> t_h_nextn = h_next ; 197 198ggml_build_forward_expand (gf ,logits ); 199ggml_build_forward_expand (gf ,h_next ); 200}