char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
1aca1f9fc
char/aliceai-t5-moe
1#include "models.h" 2 3void llama_model_mimo2::load_arch_hparams (llama_model_loader & ml ) { 4ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps ); 5 6hparams .swa_type = LLAMA_SWA_TYPE_STANDARD ; 7 8ml .get_key_or_arr (LLM_KV_EXPERT_FEED_FORWARD_LENGTH ,hparams .n_ff_exp_arr ,hparams .n_layer_all ); 9ml .get_key (LLM_KV_ATTENTION_SLIDING_WINDOW ,hparams .n_swa ); 10ml .get_key (LLM_KV_ROPE_FREQ_BASE_SWA ,hparams .rope_freq_base_train_swa , false); 11 12ml .get_arr (LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN ,hparams .is_swa_impl ); 13 14float value_scale = 0.0f ; 15if (ml .get_key (LLM_KV_ATTENTION_VALUE_SCALE ,value_scale , false)&& value_scale != 1.0f ) { 16hparams .f_attn_value_scale = value_scale ; 17 } 18 19switch (hparams .n_layer ()) { 20case 48 :type = LLM_TYPE_310B_A15B ;break ; 21default :type = LLM_TYPE_UNKNOWN ; 22 } 23} 24 25void llama_model_mimo2::load_arch_tensors (llama_model_loader & ml ) { 26LLAMA_LOAD_LOCALS ; 27 28const std::string mtp_probe = "blk." + std::to_string (n_layer )+ ".nextn.eh_proj.weight" ; 29const bool trunk_only = (hparams .n_layer_nextn > 0 )&& (ml .get_weight (mtp_probe .c_str ())== nullptr ); 30int mtp_flags = trunk_only ?TENSOR_NOT_REQUIRED :0 ; 31 32if (!ml .load_mtp ) { 33mtp_flags |=TENSOR_SKIP ; 34 } 35 36tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 37 38// output 39output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 40output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },0 ); 41 42for (int i = 0 ;i < n_layer_all ;++ i ) { 43auto & layer = layers [i ]; 44uint32_t n_embd_k_gqa = hparams .n_embd_k_gqa (i ); 45uint32_t n_embd_v_gqa = hparams .n_embd_v_gqa (i ); 46uint32_t n_head = hparams .n_head (i ); 47 48const bool is_nextn = i >=n_layer ; 49const int flags = is_nextn ?mtp_flags :0 ; 50 51create_tensor_qkv (layer ,i ,n_embd ,n_embd_head_k * n_head ,n_embd_k_gqa ,n_embd_v_gqa ,flags ); 52layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd_head_v * n_head ,n_embd },flags ); 53 54layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },flags ); 55layer .attn_sinks = create_tensor (tn (LLM_TENSOR_ATTN_SINKS ,"weight" ,i ), {n_head },TENSOR_NOT_REQUIRED |flags ); 56 57layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },flags ); 58 59// non-MoE branch 60layer .ffn_gate = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"weight" ,i ), {n_embd ,n_ff },TENSOR_NOT_REQUIRED |flags ); 61layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },TENSOR_NOT_REQUIRED |flags ); 62layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },TENSOR_NOT_REQUIRED |flags ); 63 64// MoE branch 65int64_t n_ff_exp = hparams .n_ff_exp (); 66layer .ffn_gate_inp = create_tensor (tn (LLM_TENSOR_FFN_GATE_INP ,"weight" ,i ), {n_embd ,n_expert },TENSOR_NOT_REQUIRED |flags ); 67layer .ffn_gate_exps = create_tensor (tn (LLM_TENSOR_FFN_GATE_EXPS ,"weight" ,i ), {n_embd ,n_ff_exp ,n_expert },TENSOR_NOT_REQUIRED |flags ); 68layer .ffn_down_exps = create_tensor (tn (LLM_TENSOR_FFN_DOWN_EXPS ,"weight" ,i ), {n_ff_exp ,n_embd ,n_expert },TENSOR_NOT_REQUIRED |flags ); 69layer .ffn_up_exps = create_tensor (tn (LLM_TENSOR_FFN_UP_EXPS ,"weight" ,i ), {n_embd ,n_ff_exp ,n_expert },TENSOR_NOT_REQUIRED |flags ); 70layer .ffn_exp_probs_b = create_tensor (tn (LLM_TENSOR_FFN_EXP_PROBS_B ,"bias" ,i ), {n_expert },TENSOR_NOT_REQUIRED |flags ); 71 72if (is_nextn ) { 73layer .nextn .eh_proj = create_tensor (tn (LLM_TENSOR_NEXTN_EH_PROJ ,"weight" ,i ), {2 * n_embd ,n_embd },flags ); 74layer .nextn .enorm = create_tensor (tn (LLM_TENSOR_NEXTN_ENORM ,"weight" ,i ), {n_embd },flags ); 75layer .nextn .hnorm = create_tensor (tn (LLM_TENSOR_NEXTN_HNORM ,"weight" ,i ), {n_embd },flags ); 76layer .nextn .embed_tokens = create_tensor (tn (LLM_TENSOR_NEXTN_EMBED_TOKENS ,"weight" ,i ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED |flags ); 77layer .nextn .shared_head_head = create_tensor (tn (LLM_TENSOR_NEXTN_SHARED_HEAD_HEAD ,"weight" ,i ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED |flags ); 78layer .nextn .shared_head_norm = create_tensor (tn (LLM_TENSOR_NEXTN_SHARED_HEAD_NORM ,"weight" ,i ), {n_embd },TENSOR_NOT_REQUIRED |flags ); 79layer .layer_out_norm = create_tensor (tn (LLM_TENSOR_LAYER_OUT_NORM ,"weight" ,i ), {n_embd },TENSOR_NOT_REQUIRED |flags ); 80 } 81 } 82} 83 84std::unique_ptr < llm_graph_context > llama_model_mimo2::build_arch_graph (const llm_graph_params & params )const { 85if (params .gtype == LLM_GRAPH_TYPE_DECODER_MTP ) { 86return std::make_unique < graph_mtp > (* this ,params ); 87 } 88return std::make_unique < graph > (* this ,params ); 89} 90 91llama_model_mimo2::graph::graph (const llama_model & model ,const llm_graph_params & params ) :llm_graph_context (params ) { 92ggml_tensor * cur ; 93ggml_tensor * inpL ; 94 95inpL = build_inp_embd (model .tok_embd ); 96 97ggml_tensor * inp_pos = build_inp_pos (); 98auto * inp_attn = build_attn_inp_kv_iswa (); 99ggml_tensor * inp_out_ids = build_inp_out_ids (); 100 101const float v_scale = hparams .f_attn_value_scale ; 102const bool emit_h_nextn = cparams .embeddings_nextn ; 103const bool crop_last_layer = inp_out_ids && (!emit_h_nextn || cparams .embeddings_nextn_masked ); 104 105for (int il = 0 ;il < n_layer ;++ il ) { 106ggml_tensor * inpSA = inpL ; 107 108uint32_t n_head_l = hparams .n_head (il ); 109uint32_t n_head_kv_l = hparams .n_head_kv (il ); 110const float freq_base_l = model .get_rope_freq_base (cparams ,il ); 111const float freq_scale_l = model .get_rope_freq_scale (cparams ,il ); 112 113cur = inpL ; 114 115// self_attention 116 { 117cur = build_norm (inpL ,model .layers [il ].attn_norm ,NULL ,LLM_NORM_RMS ,il ); 118cb (cur ,"attn_norm" ,il ); 119 120ggml_tensor * Qcur ; 121ggml_tensor * Kcur ; 122ggml_tensor * Vcur ; 123 124if (model .layers [il ].wqkv ) { 125// Fused qkv_proj - Q/K share head_dim_k, V uses head_dim_v 126ggml_tensor * qkv = build_lora_mm (model .layers [il ].wqkv ,cur ); 127cb (qkv ,"wqkv" ,il ); 128 129const size_t row_k = ggml_row_size (qkv -> type ,n_embd_head_k ); 130const size_t row_v = ggml_row_size (qkv -> type ,n_embd_head_v ); 131const size_t row_full = qkv -> nb [1 ]; 132const size_t k_off = row_k * n_head_l ; 133const size_t v_off = k_off + row_k * n_head_kv_l ; 134 135Qcur = ggml_view_3d (ctx0 ,qkv ,n_embd_head_k ,n_head_l ,n_tokens ,row_k ,row_full ,0 ); 136Kcur = ggml_view_3d (ctx0 ,qkv ,n_embd_head_k ,n_head_kv_l ,n_tokens ,row_k ,row_full ,k_off ); 137Vcur = ggml_view_3d (ctx0 ,qkv ,n_embd_head_v ,n_head_kv_l ,n_tokens ,row_v ,row_full ,v_off ); 138 }else { 139// Split path 140Qcur = build_lora_mm (model .layers [il ].wq ,cur ); 141cb (Qcur ,"Qcur" ,il ); 142 143Kcur = build_lora_mm (model .layers [il ].wk ,cur ); 144cb (Kcur ,"Kcur" ,il ); 145 146Vcur = build_lora_mm (model .layers [il ].wv ,cur ); 147cb (Vcur ,"Vcur" ,il ); 148 149Qcur = ggml_reshape_3d (ctx0 ,Qcur ,n_embd_head_k ,n_head_l ,n_tokens ); 150Kcur = ggml_reshape_3d (ctx0 ,Kcur ,n_embd_head_k ,n_head_kv_l ,n_tokens ); 151Vcur = ggml_reshape_3d (ctx0 ,Vcur ,n_embd_head_v ,n_head_kv_l ,n_tokens ); 152 } 153 154Qcur = ggml_rope_ext ( 155ctx0 ,Qcur ,inp_pos ,nullptr , 156n_rot ,rope_type ,n_ctx_orig ,freq_base_l ,freq_scale_l , 157ext_factor ,attn_factor ,beta_fast ,beta_slow 158 ); 159 160Kcur = ggml_rope_ext ( 161ctx0 ,Kcur ,inp_pos ,nullptr , 162n_rot ,rope_type ,n_ctx_orig ,freq_base_l ,freq_scale_l , 163ext_factor ,attn_factor ,beta_fast ,beta_slow 164 ); 165 166cb (Qcur ,"Qcur" ,il ); 167cb (Kcur ,"Kcur" ,il ); 168cb (Vcur ,"Vcur" ,il ); 169 170ggml_tensor * sinks = model .layers [il ].attn_sinks ; 171 172cur = build_attn (inp_attn , 173model .layers [il ].wo ,NULL ,model .layers [il ].wo_s , 174Qcur ,Kcur ,Vcur ,nullptr ,sinks ,nullptr ,1.0f /sqrtf (float (n_embd_head_k )),il ); 175cb (cur ,"attn_out" ,il ); 176 177if (v_scale ) { 178cur = ggml_scale (ctx0 ,cur ,v_scale ); 179cb (cur ,"attn_out_scaled" ,il ); 180 } 181 } 182 183if (il == n_layer - 1 && crop_last_layer ) { 184cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 185inpSA = ggml_get_rows (ctx0 ,inpSA ,inp_out_ids ); 186 } 187 188ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 189cb (ffn_inp ,"ffn_inp" ,il ); 190 191cur = build_norm (ffn_inp , 192model .layers [il ].ffn_norm ,NULL , 193LLM_NORM_RMS ,il ); 194cb (cur ,"ffn_norm" ,il ); 195 196// feed-forward network 197if (model .layers [il ].ffn_gate_inp == nullptr ) { 198// dense branch 199cur = build_ffn (cur , 200model .layers [il ].ffn_up ,model .layers [il ].ffn_up_b ,NULL , 201model .layers [il ].ffn_gate ,model .layers [il ].ffn_gate_b ,NULL , 202model .layers [il ].ffn_down ,model .layers [il ].ffn_down_b ,NULL , 203NULL , 204LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 205cb (cur ,"ffn_out" ,il ); 206 }else { 207// MoE branch 208cur = build_moe_ffn (cur , 209model .layers [il ].ffn_gate_inp , 210model .layers [il ].ffn_up_exps , 211model .layers [il ].ffn_gate_exps , 212model .layers [il ].ffn_down_exps , 213model .layers [il ].ffn_exp_probs_b , 214n_expert ,n_expert_used , 215LLM_FFN_SILU , true, 216hparams .expert_weights_scale , 217LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID , 218il ); 219cb (cur ,"ffn_moe_out" ,il ); 220 } 221 222cur = ggml_add (ctx0 ,cur ,ffn_inp ); 223 224cur = build_cvec (cur ,il ); 225cb (cur ,"l_out" ,il ); 226 227// input for next layer 228inpL = cur ; 229 } 230 231cur = inpL ; 232 233if (emit_h_nextn ) { 234cb (cur ,"h_nextn" ,-1 ); 235res -> t_h_nextn = cur ; 236 237if (!cparams .embeddings_nextn_masked && inp_out_ids ) { 238cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 239 } 240 } 241 242cur = build_norm (cur , 243model .output_norm ,NULL , 244LLM_NORM_RMS ,-1 ); 245 246cb (cur ,"result_norm" ,-1 ); 247res -> t_embd = cur ; 248 249// lm_head 250cur = build_lora_mm (model .output ,cur ,model .output_s ); 251 252cb (cur ,"result_output" ,-1 ); 253res -> t_logits = cur ; 254 255ggml_build_forward_expand (gf ,cur ); 256} 257 258// Mirrors MiMo's appended NextN block: normalize and fuse token and hidden inputs, run the decoder block, 259// expose its pre-head-norm state to the next draft step, then apply the shared output norm and LM head. 260// Converted checkpoints may store that shared norm as layer_out_norm, so it remains in the fallback chain. 261llama_model_mimo2::graph_mtp::graph_mtp (const llama_model & model ,const llm_graph_params & params ) 262 :llm_graph_context (params ) { 263GGML_ASSERT (hparams .n_layer_nextn > 0 && "MIMO2 MTP requires n_layer_nextn > 0" ); 264 265const int il = hparams .n_layer ()+ cparams .nextn_layer_offset ; 266GGML_ASSERT (cparams .nextn_layer_offset >=0 && 267cparams .nextn_layer_offset < (int )hparams .n_layer_nextn && 268"nextn_layer_offset out of range [0, n_layer_nextn)" ); 269 270const auto & layer = model .layers [il ]; 271GGML_ASSERT (layer .nextn .eh_proj && "MIMO2 MTP block missing nextn.eh_proj" ); 272GGML_ASSERT (layer .nextn .enorm && "MIMO2 MTP block missing nextn.enorm" ); 273GGML_ASSERT (layer .nextn .hnorm && "MIMO2 MTP block missing nextn.hnorm" ); 274GGML_ASSERT (layer .wqkv && "MIMO2 MTP requires fused attn_qkv" ); 275 276const uint32_t n_head_l = hparams .n_head (il ); 277const uint32_t n_head_kv_l = hparams .n_head_kv (il ); 278 279const float freq_base_l = model .get_rope_freq_base (cparams ,il ); 280const float freq_scale_l = model .get_rope_freq_scale (cparams ,il ); 281const float v_scale = hparams .f_attn_value_scale ; 282 283auto inp = std::make_unique < llm_graph_input_embd > (hparams .n_embd ); 284 285inp -> tokens = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_I32 ,n_tokens ); 286ggml_set_input (inp -> tokens ); 287 288inp -> embd = ggml_new_tensor_2d (ctx0 ,GGML_TYPE_F32 ,hparams .n_embd ,n_tokens ); 289ggml_set_input (inp -> embd ); 290ggml_set_name (inp -> embd ,"mtp_h_input" ); 291 292ggml_tensor * tok_embd_w = layer .nextn .embed_tokens ?layer .nextn .embed_tokens :model .tok_embd ; 293ggml_tensor * h_input = inp -> embd ; 294ggml_tensor * tok_embd = ggml_get_rows (ctx0 ,tok_embd_w ,inp -> tokens ); 295cb (tok_embd ,"mtp_tok_embd" ,il ); 296 297res -> add_input (std::move (inp )); 298 299ggml_tensor * inp_pos = build_inp_pos (); 300ggml_tensor * inp_out_ids = build_inp_out_ids (); 301auto * inp_attn = build_attn_inp_kv_iswa (); 302 303ggml_tensor * h_norm = build_norm (h_input ,layer .nextn .hnorm ,nullptr ,LLM_NORM_RMS ,il ); 304cb (h_norm ,"mtp_hnorm" ,il ); 305 306ggml_tensor * e_norm = build_norm (tok_embd ,layer .nextn .enorm ,nullptr ,LLM_NORM_RMS ,il ); 307cb (e_norm ,"mtp_enorm" ,il ); 308 309ggml_tensor * concat = ggml_concat (ctx0 ,e_norm ,h_norm ,/*dim=*/ 0 ); 310cb (concat ,"mtp_concat" ,il ); 311 312ggml_tensor * cur = build_lora_mm (layer .nextn .eh_proj ,concat ,layer .nextn .eh_proj_s ); 313cb (cur ,"mtp_eh_proj" ,il ); 314 315ggml_tensor * inpSA = cur ; 316 317cur = build_norm (cur ,layer .attn_norm ,nullptr ,LLM_NORM_RMS ,il ); 318cb (cur ,"mtp_attn_norm" ,il ); 319 320ggml_tensor * qkv = build_lora_mm (layer .wqkv ,cur ,layer .wqkv_s ); 321cb (qkv ,"mtp_wqkv" ,il ); 322 323const size_t row_k = ggml_row_size (qkv -> type ,n_embd_head_k ); 324const size_t row_v = ggml_row_size (qkv -> type ,n_embd_head_v ); 325const size_t row_full = qkv -> nb [1 ]; 326const size_t k_off = row_k * n_head_l ; 327const size_t v_off = k_off + row_k * n_head_kv_l ; 328 329ggml_tensor * Qcur = ggml_view_3d (ctx0 ,qkv ,n_embd_head_k ,n_head_l ,n_tokens ,row_k ,row_full ,0 ); 330ggml_tensor * Kcur = ggml_view_3d (ctx0 ,qkv ,n_embd_head_k ,n_head_kv_l ,n_tokens ,row_k ,row_full ,k_off ); 331ggml_tensor * Vcur = ggml_view_3d (ctx0 ,qkv ,n_embd_head_v ,n_head_kv_l ,n_tokens ,row_v ,row_full ,v_off ); 332 333Qcur = ggml_rope_ext ( 334ctx0 ,Qcur ,inp_pos ,nullptr , 335n_rot ,rope_type ,n_ctx_orig ,freq_base_l ,freq_scale_l , 336ext_factor ,attn_factor ,beta_fast ,beta_slow ); 337 338Kcur = ggml_rope_ext ( 339ctx0 ,Kcur ,inp_pos ,nullptr , 340n_rot ,rope_type ,n_ctx_orig ,freq_base_l ,freq_scale_l , 341ext_factor ,attn_factor ,beta_fast ,beta_slow ); 342 343cb (Qcur ,"mtp_Qcur" ,il ); 344cb (Kcur ,"mtp_Kcur" ,il ); 345cb (Vcur ,"mtp_Vcur" ,il ); 346 347cur = build_attn (inp_attn , 348layer .wo ,nullptr ,layer .wo_s , 349Qcur ,Kcur ,Vcur ,nullptr ,layer .attn_sinks ,nullptr , 3501.0f /sqrtf (float (n_embd_head_k )),il ); 351cb (cur ,"mtp_attn_out" ,il ); 352 353if (v_scale ) { 354cur = ggml_scale (ctx0 ,cur ,v_scale ); 355cb (cur ,"mtp_attn_out_scaled" ,il ); 356 } 357 358ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 359cb (ffn_inp ,"mtp_ffn_inp" ,il ); 360 361cur = build_norm (ffn_inp ,layer .ffn_norm ,nullptr ,LLM_NORM_RMS ,il ); 362cb (cur ,"mtp_ffn_norm" ,il ); 363 364GGML_ASSERT (layer .ffn_gate && layer .ffn_down && layer .ffn_up && "MIMO2 MTP requires dense FFN tensors" ); 365cur = build_ffn (cur , 366layer .ffn_up ,layer .ffn_up_b ,nullptr , 367layer .ffn_gate ,layer .ffn_gate_b ,nullptr , 368layer .ffn_down ,layer .ffn_down_b ,nullptr , 369nullptr , 370LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 371cb (cur ,"mtp_ffn_out" ,il ); 372 373cur = ggml_add (ctx0 ,cur ,ffn_inp ); 374cb (cur ,"mtp_post_ffn" ,il ); 375 376cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 377 378cb (cur ,"h_nextn" ,-1 ); 379res -> t_h_nextn = cur ; 380 381ggml_tensor * head_norm_w = layer .nextn .shared_head_norm 382 ?layer .nextn .shared_head_norm 383 : (layer .layer_out_norm ?layer .layer_out_norm :model .output_norm ); 384GGML_ASSERT (head_norm_w && "MIMO2 MTP missing head norm fallback" ); 385cur = build_norm (cur ,head_norm_w ,nullptr ,LLM_NORM_RMS ,-1 ); 386cb (cur ,"mtp_shared_head_norm" ,-1 ); 387 388ggml_tensor * head_w = layer .nextn .shared_head_head ?layer .nextn .shared_head_head :model .output ; 389ggml_tensor * head_s = layer .nextn .shared_head_head ?layer .nextn .shared_head_head_s :model .output_s ; 390GGML_ASSERT (head_w && "MIMO2 MTP missing LM head fallback" ); 391cur = build_lora_mm (head_w ,cur ,head_s ); 392cb (cur ,"result_output" ,-1 ); 393 394res -> t_logits = cur ; 395ggml_build_forward_expand (gf ,cur ); 396}