okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b2de2641f
main
1#include "models.h" 2 3void llama_model_llama::load_arch_hparams (llama_model_loader & ml ) { 4uint32_t n_vocab = 0 ; 5ml .get_key (LLM_KV_VOCAB_SIZE ,n_vocab , false)|| ml .get_arr_n (LLM_KV_TOKENIZER_LIST ,n_vocab , false); 6 7ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps ); 8 9if (hparams .n_expert == 8 ) { 10switch (hparams .n_layer ()) { 11case 32 :type = LLM_TYPE_8x7B ;break ; 12case 56 :type = LLM_TYPE_8x22B ;break ; 13default :type = LLM_TYPE_UNKNOWN ; 14 } 15 }else { 16switch (hparams .n_layer ()) { 17case 16 :type = LLM_TYPE_1B ;break ;// Llama 3.2 1B 18case 22 :type = LLM_TYPE_1B ;break ; 19case 26 :type = LLM_TYPE_3B ;break ; 20case 28 :type = LLM_TYPE_3B ;break ;// Llama 3.2 3B 21case 30 :type = LLM_TYPE_256M ;break ;// smoldocling 256M 22// granite uses a vocab with len 49152 23case 32 :type = n_vocab == 49152 ?LLM_TYPE_3B : (n_vocab < 40000 ?LLM_TYPE_7B :LLM_TYPE_8B );break ; 24case 36 :type = LLM_TYPE_8B ;break ;// granite 25case 40 :type = LLM_TYPE_13B ;break ; 26case 48 :type = LLM_TYPE_34B ;break ; 27case 60 :type = LLM_TYPE_30B ;break ; 28case 80 :type = hparams .n_head ()== hparams .n_head_kv () ?LLM_TYPE_65B :LLM_TYPE_70B ;break ; 29default :type = LLM_TYPE_UNKNOWN ; 30 } 31 } 32} 33 34void llama_model_llama::load_arch_tensors (llama_model_loader & ) { 35LLAMA_LOAD_LOCALS ; 36 37tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 38 39// output 40output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 41output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED ); 42 43// if output is NULL, init from the input tok embed 44if (output == NULL ) { 45output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED ); 46 } 47 48for (int i = 0 ;i < n_layer ;++ i ) { 49auto & layer = layers [i ]; 50 51layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 52 53create_tensor_qkv (layer ,i ,n_embd ,n_embd_head_k * n_head ,n_embd_k_gqa ,n_embd_v_gqa ,0 ); 54layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd_head_k * n_head ,n_embd },0 ); 55 56// optional bias tensors 57layer .wo_b = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"bias" ,i ), {n_embd },TENSOR_NOT_REQUIRED ); 58 59layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 60 61if (hparams .rope_scaling_type_train == LLAMA_ROPE_SCALING_TYPE_LONGROPE ) { 62layer .rope_long = create_tensor (tn (LLM_TENSOR_ROPE_FACTORS_LONG ,"weight" ,i ), {n_rot /2 },TENSOR_NOT_REQUIRED | (i != 0 ?TENSOR_DUPLICATED :0 )); 63layer .rope_short = create_tensor (tn (LLM_TENSOR_ROPE_FACTORS_SHORT ,"weight" ,i ), {n_rot /2 },TENSOR_NOT_REQUIRED | (i != 0 ?TENSOR_DUPLICATED :0 )); 64 } 65else { 66layer .rope_freqs = create_tensor (tn (LLM_TENSOR_ROPE_FREQS ,"weight" ,i ), {n_rot /2 },TENSOR_NOT_REQUIRED | (i != 0 ?TENSOR_DUPLICATED :0 )); 67 } 68 69if (n_expert == 0 ) { 70layer .ffn_gate = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"weight" ,i ), {n_embd ,n_ff },0 ); 71layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },0 ); 72layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },0 ); 73 74// optional MLP bias 75layer .ffn_gate_b = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"bias" ,i ), {n_ff },TENSOR_NOT_REQUIRED ); 76layer .ffn_down_b = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"bias" ,i ), {n_embd },TENSOR_NOT_REQUIRED ); 77layer .ffn_up_b = create_tensor (tn (LLM_TENSOR_FFN_UP ,"bias" ,i ), {n_ff },TENSOR_NOT_REQUIRED ); 78 }else { 79layer .ffn_gate_inp = create_tensor (tn (LLM_TENSOR_FFN_GATE_INP ,"weight" ,i ), {n_embd ,n_expert },0 ); 80layer .ffn_gate_exps = create_tensor (tn (LLM_TENSOR_FFN_GATE_EXPS ,"weight" ,i ), {n_embd ,n_ff ,n_expert },TENSOR_NOT_REQUIRED ); 81layer .ffn_down_exps = create_tensor (tn (LLM_TENSOR_FFN_DOWN_EXPS ,"weight" ,i ), {n_ff ,n_embd ,n_expert },0 ); 82layer .ffn_up_exps = create_tensor (tn (LLM_TENSOR_FFN_UP_EXPS ,"weight" ,i ), {n_embd ,n_ff ,n_expert },0 ); 83 84// For Granite MoE Shared 85if (hparams .n_ff_shexp > 0 ) { 86layer .ffn_gate_shexp = create_tensor (tn (LLM_TENSOR_FFN_GATE_SHEXP ,"weight" ,i ), {n_embd ,hparams .n_ff_shexp },0 ); 87layer .ffn_up_shexp = create_tensor (tn (LLM_TENSOR_FFN_UP_SHEXP ,"weight" ,i ), {n_embd ,hparams .n_ff_shexp },0 ); 88layer .ffn_down_shexp = create_tensor (tn (LLM_TENSOR_FFN_DOWN_SHEXP ,"weight" ,i ), {hparams .n_ff_shexp ,n_embd },0 ); 89 } 90 } 91 } 92} 93 94std::unique_ptr < llm_graph_context > llama_model_llama::build_arch_graph (const llm_graph_params & params )const { 95return std::make_unique < graph < false>> (* this ,params ); 96} 97 98template < bool embed > 99llama_model_llama::graph < embed > ::graph (const llama_model & model ,const llm_graph_params & params ) :llm_graph_context (params ) { 100const int64_t n_embd_head = hparams .n_embd_head_v (); 101 102GGML_ASSERT (n_embd_head == hparams .n_embd_head_k ()); 103GGML_ASSERT (n_embd_head == n_rot ); 104 105ggml_tensor * cur ; 106ggml_tensor * inpL ; 107 108inpL = build_inp_embd (model .tok_embd ); 109 110// inp_pos - contains the positions 111ggml_tensor * inp_pos = build_inp_pos (); 112 113using inp_attn_type = std::conditional_t < embed ,llm_graph_input_attn_no_cache ,llm_graph_input_attn_kv > ; 114 115inp_attn_type * inp_attn = nullptr ; 116if constexpr (embed ) { 117inp_attn = build_attn_inp_no_cache (); 118 }else { 119inp_attn = build_attn_inp_kv (); 120 } 121 122const float kq_scale = hparams .f_attention_scale == 0.0f ?1.0f /sqrtf (float (n_embd_head )) :hparams .f_attention_scale ; 123 124ggml_tensor * inp_out_ids = build_inp_out_ids (); 125 126for (int il = 0 ;il < n_layer ;++ il ) { 127res -> t_layer_inp [il ]= inpL ; 128 129ggml_tensor * inpSA = inpL ; 130 131// norm 132cur = build_norm (inpL , 133model .layers [il ].attn_norm ,NULL , 134LLM_NORM_RMS ,il ); 135cb (cur ,"attn_norm" ,il ); 136 137// self-attention 138 { 139// rope freq factors for llama3; may return nullptr for llama2 and other models 140ggml_tensor * rope_factors = model .get_rope_factors (cparams ,il ); 141 142// compute Q and K and RoPE them 143auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 144n_embd_head ,n_head ,n_head_kv ,il ); 145 146Qcur = ggml_rope_ext ( 147ctx0 ,Qcur ,inp_pos ,rope_factors , 148n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 149ext_factor ,attn_factor ,beta_fast ,beta_slow 150 ); 151 152Kcur = ggml_rope_ext ( 153ctx0 ,Kcur ,inp_pos ,rope_factors , 154n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 155ext_factor ,attn_factor ,beta_fast ,beta_slow 156 ); 157 158cb (Qcur ,"Qcur" ,il ); 159cb (Kcur ,"Kcur" ,il ); 160cb (Vcur ,"Vcur" ,il ); 161 162if (hparams .use_kq_norm ) { 163// Llama4TextL2Norm 164Qcur = ggml_rms_norm (ctx0 ,Qcur ,hparams .f_norm_rms_eps ); 165Kcur = ggml_rms_norm (ctx0 ,Kcur ,hparams .f_norm_rms_eps ); 166cb (Qcur ,"Qcur_normed" ,il ); 167cb (Kcur ,"Kcur_normed" ,il ); 168 } 169cur = build_attn (inp_attn , 170model .layers [il ].wo ,model .layers [il ].wo_b ,model .layers [il ].wo_s , 171Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,kq_scale ,il ); 172cb (cur ,"attn_out" ,il ); 173 } 174if (il == n_layer - 1 && inp_out_ids ) { 175cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 176inpSA = ggml_get_rows (ctx0 ,inpSA ,inp_out_ids ); 177 } 178ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 179cb (ffn_inp ,"ffn_inp" ,il ); 180 181// feed-forward network (non-MoE) 182if (model .layers [il ].ffn_gate_inp == nullptr ) { 183 184cur = build_norm (ffn_inp , 185model .layers [il ].ffn_norm ,NULL , 186LLM_NORM_RMS ,il ); 187cb (cur ,"ffn_norm" ,il ); 188 189cur = build_ffn (cur , 190model .layers [il ].ffn_up ,model .layers [il ].ffn_up_b ,model .layers [il ].ffn_up_s , 191model .layers [il ].ffn_gate ,model .layers [il ].ffn_gate_b ,model .layers [il ].ffn_gate_s , 192model .layers [il ].ffn_down ,model .layers [il ].ffn_down_b ,model .layers [il ].ffn_down_s , 193NULL , 194LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 195cb (cur ,"ffn_out" ,il ); 196 }else { 197// MoE branch 198cur = build_norm (ffn_inp , 199model .layers [il ].ffn_norm ,NULL , 200LLM_NORM_RMS ,il ); 201cb (cur ,"ffn_norm" ,il ); 202 203cur = build_moe_ffn (cur , 204model .layers [il ].ffn_gate_inp , 205model .layers [il ].ffn_up_exps , 206model .layers [il ].ffn_gate_exps , 207model .layers [il ].ffn_down_exps , 208nullptr , 209n_expert ,n_expert_used , 210LLM_FFN_SILU , true, 211hparams .expert_weights_scale , 212LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX , 213il , 214nullptr ,nullptr , 215model .layers [il ].ffn_up_exps_s , 216model .layers [il ].ffn_gate_exps_s , 217model .layers [il ].ffn_down_exps_s ); 218cb (cur ,"ffn_moe_out" ,il ); 219 } 220cur = ggml_add (ctx0 ,cur ,ffn_inp ); 221cb (cur ,"ffn_out" ,il ); 222 223cur = build_cvec (cur ,il ); 224cb (cur ,"l_out" ,il ); 225 226// input for next layer 227inpL = cur ; 228 } 229cur = inpL ; 230 231cur = build_norm (cur , 232model .output_norm ,NULL , 233LLM_NORM_RMS ,-1 ); 234 235cb (cur ,"result_norm" ,-1 ); 236res -> t_embd = cur ; 237 238if constexpr (!embed ) { 239// lm_head 240cur = build_lora_mm (model .output ,cur ,model .output_s ); 241 242cb (cur ,"result_output" ,-1 ); 243res -> t_logits = cur ; 244 } 245 246ggml_build_forward_expand (gf ,cur ); 247} 248 249template struct llama_model_llama::graph < false> ; 250template struct llama_model_llama:: graph < true > ;