char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
e49d2c276
char/aliceai-t5-moe
1#include "models.h" 2 3#include <algorithm> // std::max 4 5void llama_model_nemotron_h::load_arch_hparams (llama_model_loader & ml ) { 6ml .get_key (LLM_KV_SSM_CONV_KERNEL ,hparams .ssm_d_conv ); 7ml .get_key (LLM_KV_SSM_INNER_SIZE ,hparams .ssm_d_inner ); 8ml .get_key (LLM_KV_SSM_STATE_SIZE ,hparams .ssm_d_state ); 9ml .get_key (LLM_KV_SSM_TIME_STEP_RANK ,hparams .ssm_dt_rank ); 10ml .get_key (LLM_KV_SSM_GROUP_COUNT ,hparams .ssm_n_group ); 11 12// A layer is recurrent IFF the n_head_kv value is set to 0 and 13// the n_ff value is set to 0. Appended MTP blocks are dense (non-recurrent) 14for (uint32_t i = 0 ;i < hparams .n_layer_all ;++ i ) { 15hparams .is_recr_impl [i ]= i < hparams .n_layer ()&& hparams .n_head_kv (i )== 0 && hparams .n_ff (i )== 0 ; 16 } 17 18ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps ); 19ml .get_key (LLM_KV_ATTENTION_LAYERNORM_EPS ,hparams .f_norm_eps );// MTP head final_layernorm 20 21// Puzzle models set a different expert FFN size per layer 22ml .get_key_or_arr (LLM_KV_EXPERT_FEED_FORWARD_LENGTH ,hparams .n_ff_exp_arr ,hparams .n_layer_all , false); 23ml .get_key (LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH ,hparams .n_ff_shexp , false); 24ml .get_key (LLM_KV_EXPERT_SHARED_COUNT ,hparams .n_expert_shared , false); 25ml .get_key (LLM_KV_EXPERT_WEIGHTS_NORM ,hparams .expert_weights_norm , false); 26ml .get_key (LLM_KV_EXPERT_WEIGHTS_SCALE ,hparams .expert_weights_scale , false); 27ml .get_key (LLM_KV_MOE_LATENT_SIZE ,hparams .moe_latent_size , false); 28 29switch (hparams .n_layer ()) { 30case 52 :type = LLM_TYPE_31B_A3_5B ;break ;// Nemotron-H_MOE 31B 31case 56 :type = LLM_TYPE_9B ;break ; 32case 88 : 33 { 34// Nemotron 3 Super (uniform MoE) and Nemotron 3 Puzzle (per-layer 35// heterogeneous MoE) both have 88 layers; the per-layer top-k array 36// is the discriminator. 37bool heterogeneous = false; 38for (uint32_t i = 1 ;i < hparams .n_layer ();++ i ) { 39heterogeneous |=hparams .n_expert_used_arr [i ]!= hparams .n_expert_used_arr [0 ]; 40 } 41type = heterogeneous ?LLM_TYPE_75B_A9B :LLM_TYPE_120B_A12B ; 42 }break ; 43default :type = LLM_TYPE_UNKNOWN ; 44 } 45} 46 47void llama_model_nemotron_h::load_arch_tensors (llama_model_loader & ml ) { 48LLAMA_LOAD_LOCALS ; 49 50const bool mtp_only = hparams .n_layer_nextn > 0 && ml .get_weight ("blk.0.attn_norm.weight" )== nullptr ; 51const int trunk_flags = mtp_only ?TENSOR_NOT_REQUIRED :0 ; 52const int mtp_flags = !ml .load_mtp ?TENSOR_SKIP :0 ; 53 54// mamba2 Mixer SSM params 55// NOTE: int64_t for tensor dimensions 56const int64_t d_conv = hparams .ssm_d_conv ; 57const int64_t d_inner = hparams .ssm_d_inner ; 58const int64_t d_state = hparams .ssm_d_state ; 59const int64_t n_ssm_head = hparams .ssm_dt_rank ; 60const int64_t n_group = hparams .ssm_n_group ; 61const int64_t d_in_proj = 2 * d_inner + 2 * n_group * d_state + n_ssm_head ; 62const int64_t moe_n_embd = hparams .moe_latent_size > 0 ?hparams .moe_latent_size :n_embd ; 63 64// embeddings 65tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 66 67// output 68 { 69output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 70output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED ); 71// if output is NULL, init from the input tok embed, duplicated to allow offloading 72if (output == NULL ) { 73output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED ); 74 } 75 } 76 77for (int i = 0 ;i < n_layer ;++ i ) { 78auto & layer = layers [i ]; 79 80// all blocks use the attn norm 81layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },trunk_flags ); 82 83if (hparams .is_recr (i )) { 84// ssm layers 85layer .ssm_in = create_tensor (tn (LLM_TENSOR_SSM_IN ,"weight" ,i ), {n_embd ,d_in_proj },trunk_flags ); 86 87layer .ssm_conv1d = create_tensor (tn (LLM_TENSOR_SSM_CONV1D ,"weight" ,i ), {d_conv ,d_inner + 2 * n_group * d_state },trunk_flags ); 88layer .ssm_conv1d_b = create_tensor (tn (LLM_TENSOR_SSM_CONV1D ,"bias" ,i ), {d_inner + 2 * n_group * d_state },TENSOR_NOT_REQUIRED ); 89 90layer .ssm_dt_b = create_tensor (tn (LLM_TENSOR_SSM_DT ,"bias" ,i ), {n_ssm_head },trunk_flags ); 91 92// no "weight" suffix for these 93layer .ssm_a = create_tensor (tn (LLM_TENSOR_SSM_A ,i ), {1 ,n_ssm_head },trunk_flags ); 94layer .ssm_d = create_tensor (tn (LLM_TENSOR_SSM_D ,i ), {1 ,n_ssm_head },trunk_flags ); 95 96layer .ssm_norm = create_tensor (tn (LLM_TENSOR_SSM_NORM ,"weight" ,i ), {d_inner /n_group ,n_group },trunk_flags ); 97 98// out_proj 99layer .ssm_out = create_tensor (tn (LLM_TENSOR_SSM_OUT ,"weight" ,i ), {d_inner ,n_embd },trunk_flags ); 100 }else if (hparams .n_ff (i )== 0 ) { 101// attention layers (with optional bias) 102const int64_t n_head_i = hparams .n_head (i ); 103const int64_t n_embd_k_gqa_i = hparams .n_embd_k_gqa (i ); 104const int64_t n_embd_v_gqa_i = hparams .n_embd_v_gqa (i ); 105create_tensor_qkv (layer ,i ,n_embd ,n_embd_head_k * n_head_i ,n_embd_k_gqa_i ,n_embd_v_gqa_i ,trunk_flags ); 106layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd_head_k * n_head_i ,n_embd },trunk_flags ); 107layer .wo_b = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"bias" ,i ), {n_embd },TENSOR_NOT_REQUIRED ); 108 }else { 109if (n_expert != 0 ) { 110// Use per-layer n_ff_exp; fall back to n_ff/n_expert_used if absent (existing GGUFs). 111const int64_t n_ff_exp_i = hparams .n_ff_exp (i ) 112 ? (int64_t )hparams .n_ff_exp (i ) 113 :hparams .n_ff (i ) / (int64_t )hparams .n_expert_used (i ); 114const int64_t n_ff_shexp = hparams .n_ff_shexp ; 115 116layer .ffn_gate_inp = create_tensor (tn (LLM_TENSOR_FFN_GATE_INP ,"weight" ,i ), {n_embd ,n_expert },trunk_flags ); 117layer .ffn_exp_probs_b = create_tensor (tn (LLM_TENSOR_FFN_EXP_PROBS_B ,"bias" ,i ), {n_expert },trunk_flags ); 118 119// MoE branch 120layer .ffn_latent_down = create_tensor (tn (LLM_TENSOR_FFN_LATENT_DOWN ,"weight" ,i ), {n_embd ,moe_n_embd },TENSOR_NOT_REQUIRED ); 121layer .ffn_latent_up = create_tensor (tn (LLM_TENSOR_FFN_LATENT_UP ,"weight" ,i ), {moe_n_embd ,n_embd },TENSOR_NOT_REQUIRED ); 122 123layer .ffn_down_exps = create_tensor (tn (LLM_TENSOR_FFN_DOWN_EXPS ,"weight" ,i ), {n_ff_exp_i ,moe_n_embd ,n_expert },trunk_flags ); 124layer .ffn_up_exps = create_tensor (tn (LLM_TENSOR_FFN_UP_EXPS ,"weight" ,i ), {moe_n_embd ,n_ff_exp_i ,n_expert },trunk_flags ); 125 126// Shared expert branch 127layer .ffn_down_shexp = create_tensor (tn (LLM_TENSOR_FFN_DOWN_SHEXP ,"weight" ,i ), {n_ff_shexp ,n_embd },trunk_flags ); 128layer .ffn_up_shexp = create_tensor (tn (LLM_TENSOR_FFN_UP_SHEXP ,"weight" ,i ), {n_embd ,n_ff_shexp },trunk_flags ); 129 130 }else { 131// mlp layers 132layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {hparams .n_ff (i ),n_embd },trunk_flags ); 133layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,hparams .n_ff (i )},trunk_flags ); 134layer .ffn_down_b = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"bias" ,i ), {n_embd },TENSOR_NOT_REQUIRED ); 135layer .ffn_up_b = create_tensor (tn (LLM_TENSOR_FFN_UP ,"bias" ,i ), {hparams .n_ff (i )},TENSOR_NOT_REQUIRED ); 136 } 137 } 138 } 139 140// NextN/MTP draft head: each predict layer folds an attention sub-layer and a MoE 141// sub-layer into a single trailing block 142for (int i = n_layer ;i < n_layer_all ;++ i ) { 143auto & layer = layers [i ]; 144 145const int64_t n_head_i = hparams .n_head (i ); 146const int64_t n_embd_k_gqa_i = hparams .n_embd_k_gqa (i ); 147const int64_t n_embd_v_gqa_i = hparams .n_embd_v_gqa (i ); 148const int64_t n_expert_used_i = hparams .n_expert_used (i ); 149const int64_t n_ff_exp_i = hparams .n_ff_exp (i ); 150if (n_ff_exp_i == 0 && n_expert_used_i == 0 ) { 151throw std::runtime_error (format ("%s: layer %d declares neither expert_feed_forward_length nor expert_used_count, " 152"cannot determine the expert FFN size" ,__func__ ,i )); 153 } 154const int64_t n_ff_exp = n_ff_exp_i ?n_ff_exp_i :n_ff /n_expert_used_i ; 155const int64_t n_ff_shexp = hparams .n_ff_shexp ; 156 157// NextN input-fusion tensors 158layer .nextn .enorm = create_tensor (tn (LLM_TENSOR_NEXTN_ENORM ,"weight" ,i ), {n_embd },mtp_flags ); 159layer .nextn .hnorm = create_tensor (tn (LLM_TENSOR_NEXTN_HNORM ,"weight" ,i ), {n_embd },mtp_flags ); 160layer .nextn .eh_proj = create_tensor (tn (LLM_TENSOR_NEXTN_EH_PROJ ,"weight" ,i ), {2 * n_embd ,n_embd },mtp_flags ); 161layer .nextn .shared_head_norm = create_tensor (tn (LLM_TENSOR_NEXTN_SHARED_HEAD_NORM ,"weight" ,i ), {n_embd },mtp_flags ); 162 163// attention sub-layer 164layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },mtp_flags ); 165create_tensor_qkv (layer ,i ,n_embd ,n_embd_head_k * n_head_i ,n_embd_k_gqa_i ,n_embd_v_gqa_i ,mtp_flags ); 166layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd_head_k * n_head_i ,n_embd },mtp_flags ); 167layer .wo_b = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"bias" ,i ), {n_embd },mtp_flags |TENSOR_NOT_REQUIRED ); 168 169// MoE sub-layer 170layer .attn_post_norm = create_tensor (tn (LLM_TENSOR_ATTN_POST_NORM ,"weight" ,i ), {n_embd },mtp_flags ); 171layer .ffn_gate_inp = create_tensor (tn (LLM_TENSOR_FFN_GATE_INP ,"weight" ,i ), {n_embd ,n_expert },mtp_flags ); 172layer .ffn_exp_probs_b = create_tensor (tn (LLM_TENSOR_FFN_EXP_PROBS_B ,"bias" ,i ), {n_expert },mtp_flags ); 173layer .ffn_down_exps = create_tensor (tn (LLM_TENSOR_FFN_DOWN_EXPS ,"weight" ,i ), {n_ff_exp ,moe_n_embd ,n_expert },mtp_flags ); 174layer .ffn_up_exps = create_tensor (tn (LLM_TENSOR_FFN_UP_EXPS ,"weight" ,i ), {moe_n_embd ,n_ff_exp ,n_expert },mtp_flags ); 175layer .ffn_down_shexp = create_tensor (tn (LLM_TENSOR_FFN_DOWN_SHEXP ,"weight" ,i ), {n_ff_shexp ,n_embd },mtp_flags ); 176layer .ffn_up_shexp = create_tensor (tn (LLM_TENSOR_FFN_UP_SHEXP ,"weight" ,i ), {n_embd ,n_ff_shexp },mtp_flags ); 177 } 178} 179 180std::unique_ptr < llm_graph_context > llama_model_nemotron_h::build_arch_graph (const llm_graph_params & params )const { 181return std::make_unique < graph > (* this ,params ); 182} 183 184llama_model_nemotron_h::graph::graph (const llama_model & model ,const llm_graph_params & params ) : 185llm_build_mamba_base (params ) { 186const int64_t n_embd_head = hparams .n_embd_head_v (); 187GGML_ASSERT (n_embd_head == hparams .n_embd_head_k ()); 188 189ggml_tensor * cur ; 190ggml_tensor * inpL ; 191 192inpL = build_inp_embd (model .tok_embd ); 193ggml_build_forward_expand (gf ,inpL ); 194 195auto * inp = build_inp_mem_hybrid (); 196 197ggml_tensor * inp_out_ids = build_inp_out_ids (); 198const bool extract_final_inp = (size_t )n_layer < cparams .embeddings_layer_inp .size ()&& cparams .embeddings_layer_inp [n_layer ]; 199 200for (int il = 0 ;il < n_layer ;++ il ) { 201res -> t_layer_inp [il ]= inpL ; 202 203struct ggml_tensor * inpSA = inpL ; 204 205// norm 206cur = build_norm (inpL ,model .layers [il ].attn_norm ,NULL ,LLM_NORM_RMS ,il ); 207cb (cur ,"attn_norm" ,il ); 208 209if (hparams .is_recr (il )) { 210// ssm layer // 211cur = build_mamba2_layer (inp -> get_recr (),cur ,model ,ubatch ,il ); 212 }else if (hparams .n_ff (il )== 0 ) { 213// attention layer // 214cur = build_attention_layer (cur ,inp -> get_attn (),model ,n_embd_head ,il ); 215 }else { 216cur = build_ffn_layer (cur ,model ,il ); 217 } 218 219if (il == n_layer - 1 && inp_out_ids && cparams .embeddings_nextn_masked && !extract_final_inp ) { 220cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 221inpSA = ggml_get_rows (ctx0 ,inpSA ,inp_out_ids ); 222 } 223 224// add residual 225cur = ggml_add (ctx0 ,cur ,inpSA ); 226cb (cur ,"nemotron_h_block_out" ,il ); 227 228// input for next layer 229inpL = cur ; 230 } 231 232cur = inpL ; 233if (extract_final_inp ) { 234res -> t_layer_inp [n_layer ]= cur ; 235 236if (inp_out_ids && cparams .embeddings_nextn_masked ) { 237cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 238 } 239 } 240 241cur = build_norm (cur ,model .output_norm ,NULL ,LLM_NORM_RMS ,-1 ); 242 243// seed for the MTP/NextN draft head 244cb (cur ,"h_nextn" ,-1 ); 245res -> t_h_nextn = cur ; 246 247if (!cparams .embeddings_nextn_masked && inp_out_ids ) { 248cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 249 } 250 251cb (cur ,"result_norm" ,-1 ); 252res -> t_embd = cur ; 253 254// lm_head 255cur = build_lora_mm (model .output ,cur ,model .output_s ); 256cb (cur ,"result_output" ,-1 ); 257res -> t_logits = cur ; 258 259ggml_build_forward_expand (gf ,cur ); 260} 261 262ggml_tensor * llama_model_nemotron_h::graph::build_attention_layer (ggml_tensor * cur , 263llm_graph_input_attn_kv * inp_attn , 264const llama_model & model , 265int64_t n_embd_head , 266int il ) { 267auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur ,n_embd_head ,hparams .n_head (il ),hparams .n_head_kv (il ),il ); 268 269const float kq_scale = 270hparams .f_attention_scale == 0.0f ?1.0f /sqrtf (float (n_embd_head )) :hparams .f_attention_scale ; 271cur = build_attn (inp_attn , 272model .layers [il ].wo ,model .layers [il ].wo_b ,model .layers [il ].wo_s , 273Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,kq_scale ,il ); 274cb (cur ,"attn_out" ,il ); 275return cur ; 276} 277 278ggml_tensor * llama_model_nemotron_h::graph::build_ffn_layer (ggml_tensor * cur ,const llama_model & model ,int il ) { 279if (model .layers [il ].ffn_gate_inp == nullptr ) { 280cur = build_ffn (cur , 281model .layers [il ].ffn_up ,model .layers [il ].ffn_up_b ,model .layers [il ].ffn_up_s , 282NULL ,NULL ,NULL , 283model .layers [il ].ffn_down ,model .layers [il ].ffn_down_b ,model .layers [il ].ffn_down_s , 284NULL , 285LLM_FFN_RELU_SQR ,LLM_FFN_PAR ,il ); 286cb (cur ,"ffn_out" ,il ); 287 }else { 288ggml_tensor * inp_emb = cur ; 289ggml_tensor * inp_latent = cur ; 290 291if (model .layers [il ].ffn_latent_down ) { 292inp_latent = ggml_mul_mat (ctx0 ,model .layers [il ].ffn_latent_down ,cur ); 293 } 294 295ggml_tensor * router_logits = build_lora_mm (model .layers [il ].ffn_gate_inp ,cur ); 296cb (router_logits ,"ffn_moe_logits" ,il ); 297 298ggml_tensor * moe_out = 299build_moe_ffn (inp_latent , 300model .layers [il ].ffn_gate_inp , 301model .layers [il ].ffn_up_exps , 302nullptr ,// no gate 303model .layers [il ].ffn_down_exps , 304model .layers [il ].ffn_exp_probs_b , 305n_expert , (int64_t )hparams .n_expert_used (il ), 306LLM_FFN_RELU_SQR ,hparams .expert_weights_norm , 307hparams .expert_weights_scale , 308LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID , 309il , 310router_logits ,nullptr , 311model .layers [il ].ffn_up_exps_s , 312nullptr ,// no gate 313model .layers [il ].ffn_down_exps_s ); 314cb (moe_out ,"ffn_moe_out" ,il ); 315 316if (model .layers [il ].ffn_latent_up ) { 317moe_out = ggml_mul_mat (ctx0 ,model .layers [il ].ffn_latent_up ,moe_out ); 318 } 319 320ggml_tensor * ffn_shexp = build_ffn (inp_emb , 321model .layers [il ].ffn_up_shexp ,NULL ,model .layers [il ].ffn_up_shexp_s , 322NULL /* no gate */ ,NULL ,NULL , 323model .layers [il ].ffn_down_shexp ,NULL ,model .layers [il ].ffn_down_shexp_s , 324NULL , 325LLM_FFN_RELU_SQR ,LLM_FFN_PAR ,il ); 326cb (ffn_shexp ,"ffn_shexp" ,il ); 327 328cur = ggml_add (ctx0 ,moe_out ,ffn_shexp ); 329cb (cur ,"ffn_out" ,il ); 330 } 331 332cur = build_cvec (cur ,il ); 333cb (cur ,"l_out" ,il ); 334 335return cur ; 336}