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