okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b2de2641f
main
1#include "models.h" 2 3void llama_model_llama4::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_EXPERT_FEED_FORWARD_LENGTH ,hparams .n_ff_exp_arr ,hparams .n_layer_all ); 6ml .get_key (LLM_KV_INTERLEAVE_MOE_LAYER_STEP ,hparams .n_moe_layer_step ); 7 8const bool found_swa = ml .get_key (LLM_KV_ATTENTION_SLIDING_WINDOW ,hparams .n_swa , false); 9if (found_swa && hparams .n_swa == 0 ) { 10hparams .swa_type = LLAMA_SWA_TYPE_NONE ; 11hparams .n_no_rope_layer_step = hparams .n_layer ();// always use rope 12 }else { 13hparams .swa_type = LLAMA_SWA_TYPE_CHUNKED ; 14hparams .n_swa = 8192 ; 15hparams .n_attn_temp_floor_scale = 8192 ; 16hparams .f_attn_temp_scale = 0.1f ; 17hparams .f_attn_temp_offset = 1.0f ; 18 19load_swa_pattern (ml ,4 );// pattern: 3 chunked - 1 full 20 21hparams .rope_freq_base_train_swa = hparams .rope_freq_base_train ; 22hparams .rope_freq_scale_train_swa = hparams .rope_freq_scale_train ; 23ml .get_key (LLM_KV_ROPE_FREQ_BASE_SWA ,hparams .rope_freq_base_train_swa , false); 24 } 25 26switch (hparams .n_expert ) { 27case 0 : { 28// MobileLLM (no MoE) 29switch (hparams .n_embd ) { 30case 2048 :type = LLM_TYPE_140M ;break ; 31case 4096 :type = LLM_TYPE_360M ;break ; 32case 6144 :type = LLM_TYPE_950M ;break ; 33default :type = LLM_TYPE_UNKNOWN ; 34 } 35 }break ; 36case 16 :type = LLM_TYPE_17B_16E ;break ; 37case 128 :type = LLM_TYPE_17B_128E ;break ; 38default :type = LLM_TYPE_UNKNOWN ; 39 } 40 41hparams .use_kq_norm = type != LLM_TYPE_17B_128E ; 42} 43 44void llama_model_llama4::load_arch_tensors (llama_model_loader & ) { 45LLAMA_LOAD_LOCALS ; 46 47if (n_expert == 0 ) { 48throw std::runtime_error (arch_name ()+ " model cannot have zero experts" ); 49 } 50tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 51 52// output 53output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 54output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED ); 55 56// if output is NULL, init from the input tok embed 57if (output == NULL ) { 58output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED ); 59 } 60 61for (int i = 0 ;i < n_layer ;++ i ) { 62const bool is_moe_layer = hparams .n_moe_layer_step > 0 && (i + 1 ) %hparams .n_moe_layer_step == 0 ; 63 64auto & layer = layers [i ]; 65 66layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 67 68create_tensor_qkv (layer ,i ,n_embd ,n_embd_head_k * n_head ,n_embd_k_gqa ,n_embd_v_gqa ,0 ); 69layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd_head_k * n_head ,n_embd },0 ); 70 71layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 72 73layer .rope_freqs = create_tensor (tn (LLM_TENSOR_ROPE_FREQS ,"weight" ,i ), {n_rot /2 },TENSOR_NOT_REQUIRED | (i != 0 ?TENSOR_DUPLICATED :0 )); 74 75if (is_moe_layer ) { 76const int64_t n_ff_exp = hparams .n_ff_exp (); 77 78layer .ffn_gate_inp = create_tensor (tn (LLM_TENSOR_FFN_GATE_INP ,"weight" ,i ), {n_embd ,n_expert },0 ); 79layer .ffn_gate_exps = create_tensor (tn (LLM_TENSOR_FFN_GATE_EXPS ,"weight" ,i ), {n_embd ,n_ff_exp ,n_expert },0 ); 80layer .ffn_down_exps = create_tensor (tn (LLM_TENSOR_FFN_DOWN_EXPS ,"weight" ,i ), {n_ff_exp ,n_embd ,n_expert },0 ); 81layer .ffn_up_exps = create_tensor (tn (LLM_TENSOR_FFN_UP_EXPS ,"weight" ,i ), {n_embd ,n_ff_exp ,n_expert },0 ); 82 83// Shared expert 84const int64_t n_ff_shexp = n_ff_exp ; 85layer .ffn_gate_shexp = create_tensor (tn (LLM_TENSOR_FFN_GATE_SHEXP ,"weight" ,i ), {n_embd ,n_ff_shexp },0 ); 86layer .ffn_down_shexp = create_tensor (tn (LLM_TENSOR_FFN_DOWN_SHEXP ,"weight" ,i ), {n_ff_shexp ,n_embd },0 ); 87layer .ffn_up_shexp = create_tensor (tn (LLM_TENSOR_FFN_UP_SHEXP ,"weight" ,i ), {n_embd ,n_ff_shexp },0 ); 88 }else { 89layer .ffn_gate = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"weight" ,i ), {n_embd ,n_ff },0 ); 90layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },0 ); 91layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },0 ); 92 } 93 } 94} 95 96std::unique_ptr < llm_graph_context > llama_model_llama4::build_arch_graph (const llm_graph_params & params )const { 97if (hparams .swa_type == LLAMA_SWA_TYPE_NONE ) { 98return std::make_unique < graph < false>> (* this ,params ); 99 }else { 100return std::make_unique < graph < true>> (* this ,params ); 101 } 102} 103 104template < bool iswa > 105llama_model_llama4::graph < iswa > ::graph (const llama_model & model ,const llm_graph_params & params ) :llm_graph_context (params ) { 106const int64_t n_embd_head = hparams .n_embd_head_v (); 107 108GGML_ASSERT (n_embd_head == hparams .n_embd_head_k ()); 109GGML_ASSERT (n_embd_head == n_rot ); 110 111ggml_tensor * cur ; 112ggml_tensor * inpL ; 113 114inpL = build_inp_embd (model .tok_embd ); 115 116// inp_pos - contains the positions 117ggml_tensor * inp_pos = build_inp_pos (); 118 119// temperature tuning 120ggml_tensor * inp_attn_scale = nullptr ; 121inp_attn_scale = build_inp_attn_scale (); 122 123using inp_attn_type = std::conditional_t < iswa ,llm_graph_input_attn_kv_iswa ,llm_graph_input_attn_kv > ; 124inp_attn_type * inp_attn = nullptr ; 125 126if constexpr (iswa ) { 127inp_attn = build_attn_inp_kv_iswa (); 128 }else { 129inp_attn = build_attn_inp_kv (); 130 } 131 132const float kq_scale = hparams .f_attention_scale == 0.0f ?1.0f /sqrtf (float (n_embd_head )) :hparams .f_attention_scale ; 133 134ggml_tensor * inp_out_ids = build_inp_out_ids (); 135 136for (int il = 0 ;il < n_layer ;++ il ) { 137const float freq_base_l = model .get_rope_freq_base (cparams ,il ); 138const float freq_scale_l = model .get_rope_freq_scale (cparams ,il ); 139 140ggml_tensor * inpSA = inpL ; 141 142// This overlaps with SWA layers in current models, so get_rope_freq_base/scale may be superfluous 143const bool use_rope = hparams .n_no_rope_layer_step > 0 && 144 (il + 1 ) %hparams .n_no_rope_layer_step != 0 ; 145 146// norm 147cur = build_norm (inpL , 148model .layers [il ].attn_norm ,NULL , 149LLM_NORM_RMS ,il ); 150cb (cur ,"attn_norm" ,il ); 151 152// self-attention 153 { 154// rope freq factors for llama3; may return nullptr for llama2 and other models 155ggml_tensor * rope_factors = model .get_rope_factors (cparams ,il ); 156 157// compute Q and K and RoPE them 158auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 159n_embd_head ,n_head ,n_head_kv ,il ); 160 161if (use_rope ) { 162Qcur = ggml_rope_ext ( 163ctx0 ,Qcur ,inp_pos ,rope_factors , 164n_rot ,rope_type ,n_ctx_orig ,freq_base_l ,freq_scale_l , 165ext_factor ,attn_factor ,beta_fast ,beta_slow 166 ); 167 168Kcur = ggml_rope_ext ( 169ctx0 ,Kcur ,inp_pos ,rope_factors , 170n_rot ,rope_type ,n_ctx_orig ,freq_base_l ,freq_scale_l , 171ext_factor ,attn_factor ,beta_fast ,beta_slow 172 ); 173 }else if (inp_attn_scale ) { 174Qcur = ggml_mul (ctx0 ,Qcur ,inp_attn_scale ); 175 } 176cb (Qcur ,"Qcur" ,il ); 177cb (Kcur ,"Kcur" ,il ); 178cb (Vcur ,"Vcur" ,il ); 179 180if (use_rope && hparams .use_kq_norm ) { 181// Llama4TextL2Norm 182Qcur = ggml_rms_norm (ctx0 ,Qcur ,hparams .f_norm_rms_eps ); 183Kcur = ggml_rms_norm (ctx0 ,Kcur ,hparams .f_norm_rms_eps ); 184cb (Qcur ,"Qcur_normed" ,il ); 185cb (Kcur ,"Kcur_normed" ,il ); 186 } 187cur = build_attn (inp_attn , 188model .layers [il ].wo ,model .layers [il ].wo_b ,model .layers [il ].wo_s , 189Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,kq_scale ,il ); 190cb (cur ,"attn_out" ,il ); 191 } 192if (il == n_layer - 1 && inp_out_ids ) { 193cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 194inpSA = ggml_get_rows (ctx0 ,inpSA ,inp_out_ids ); 195 } 196ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 197cb (ffn_inp ,"ffn_inp" ,il ); 198 199// feed-forward network (non-MoE) 200if (model .layers [il ].ffn_gate_inp == nullptr ) { 201cur = build_norm (ffn_inp , 202model .layers [il ].ffn_norm ,NULL , 203LLM_NORM_RMS ,il ); 204cb (cur ,"ffn_norm" ,il ); 205 206cur = build_ffn (cur , 207model .layers [il ].ffn_up ,model .layers [il ].ffn_up_b ,NULL , 208model .layers [il ].ffn_gate ,model .layers [il ].ffn_gate_b ,NULL , 209model .layers [il ].ffn_down ,model .layers [il ].ffn_down_b ,NULL , 210NULL , 211LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 212cb (cur ,"ffn_out" ,il ); 213 }else { 214ggml_tensor * ffn_inp_normed = build_norm (ffn_inp , 215model .layers [il ].ffn_norm ,NULL , 216LLM_NORM_RMS ,il ); 217cb (cur ,"ffn_norm" ,il ); 218 219ggml_tensor * moe_out = build_moe_ffn (ffn_inp_normed , 220model .layers [il ].ffn_gate_inp , 221model .layers [il ].ffn_up_exps , 222model .layers [il ].ffn_gate_exps , 223model .layers [il ].ffn_down_exps , 224nullptr , 225n_expert ,n_expert_used , 226LLM_FFN_SILU , false, 227hparams .expert_weights_scale , 228LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID , 229il ); 230 231// Shared experts 232ggml_tensor * shexp_out = build_ffn (ffn_inp_normed , 233model .layers [il ].ffn_up_shexp ,NULL ,NULL , 234model .layers [il ].ffn_gate_shexp ,NULL ,NULL , 235model .layers [il ].ffn_down_shexp ,NULL ,NULL , 236NULL , 237LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 238cb (shexp_out ,"ffn_moe_shexp" ,il ); 239 240cur = ggml_add (ctx0 ,moe_out ,shexp_out ); 241cb (cur ,"ffn_moe_out_merged" ,il ); 242 } 243cur = ggml_add (ctx0 ,cur ,ffn_inp ); 244cb (cur ,"ffn_out" ,il ); 245 246cur = build_cvec (cur ,il ); 247cb (cur ,"l_out" ,il ); 248 249// input for next layer 250inpL = cur ; 251 } 252cur = inpL ; 253 254cur = build_norm (cur , 255model .output_norm ,NULL , 256LLM_NORM_RMS ,-1 ); 257 258cb (cur ,"result_norm" ,-1 ); 259res -> t_embd = cur ; 260 261// lm_head 262cur = build_lora_mm (model .output ,cur ,model .output_s ); 263 264cb (cur ,"result_output" ,-1 ); 265res -> t_logits = cur ; 266 267ggml_build_forward_expand (gf ,cur ); 268} 269 270// Explicit template instantiations 271template struct llama_model_llama4::graph < false> ; 272template struct llama_model_llama4:: graph < true > ;