char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
d7e86430a
char/aliceai-t5-moe
1#include "models.h" 2 3#include <sstream> 4 5void llama_model_granite::load_arch_hparams (llama_model_loader & ml ) { 6ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps ); 7ml .get_key (LLM_KV_LOGIT_SCALE ,hparams .f_logit_scale ); 8ml .get_key (LLM_KV_RESIDUAL_SCALE ,hparams .f_residual_scale , false); 9ml .get_key (LLM_KV_EMBEDDING_SCALE ,hparams .f_embedding_scale , false); 10ml .get_key (LLM_KV_ATTENTION_SCALE ,hparams .f_attention_scale , false); 11 12// Granite4 Vision uses array deepstack_mapping 13ml .get_arr (LLM_KV_DEEPSTACK_MAPPING ,hparams .deepstack_mapping_arr , false); 14 15// Count the unique deepstack input indices 16 std::unordered_set < uint32_t > unique_deepstack_idxs ; 17for (const auto val :hparams .deepstack_mapping_arr ) { 18if (val >=0 ) { 19unique_deepstack_idxs .insert (val ); 20 } 21 } 22hparams .n_deepstack_layers = unique_deepstack_idxs .size (); 23 24// Ensure all values are valid (avoid overflow attacks) 25for (const auto val :unique_deepstack_idxs ) { 26if (val > hparams .n_deepstack_layers ) { 27 std::stringstream ss ; 28ss <<"Invalid deepstack index: " <<val <<" > " <<hparams .n_deepstack_layers ; 29throw std::runtime_error (ss .str ()); 30 } 31 } 32 33// Granite uses rope_finetuned as a switch for rope, so default to true 34bool rope_finetuned = true; 35ml .get_key (LLM_KV_ROPE_SCALING_FINETUNED ,rope_finetuned , false); 36hparams .rope_finetuned = rope_finetuned ;// needed for round trip save 37 std::fill (hparams .rope_pattern .begin (),hparams .rope_pattern .end (),rope_finetuned ); 38 39switch (hparams .n_layer ()) { 40case 32 :type = LLM_TYPE_3B ;break ; 41case 40 : { 42switch (hparams .n_embd ) { 43case 2048 :type = LLM_TYPE_2B ;break ; 44case 2560 :type = LLM_TYPE_3B ;break ; 45case 4096 :type = LLM_TYPE_8B ;break ; 46default :type = LLM_TYPE_UNKNOWN ; 47 } 48break ; 49 } 50case 64 :type = LLM_TYPE_30B ;break ; 51// Add additional layer/vocab/etc checks here for other model sizes 52default :type = LLM_TYPE_UNKNOWN ; 53 } 54 55// For Granite MoE Shared 56ml .get_key (LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH ,hparams .n_ff_shexp ,/* required */ false); 57} 58 59void llama_model_granite::load_arch_tensors (llama_model_loader & ) { 60LLAMA_LOAD_LOCALS ; 61 62tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 63 64// output 65output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 66output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED ); 67 68// if output is NULL, init from the input tok embed 69if (output == NULL ) { 70output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED ); 71 } 72 73for (int i = 0 ;i < n_layer ;++ i ) { 74auto & layer = layers [i ]; 75 76layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 77 78create_tensor_qkv (layer ,i ,n_embd ,n_embd_head_k * n_head ,n_embd_k_gqa ,n_embd_v_gqa ,0 ); 79layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd_head_k * n_head ,n_embd },0 ); 80 81// optional bias tensors 82layer .wo_b = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"bias" ,i ), {n_embd },TENSOR_NOT_REQUIRED ); 83 84layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 85 86if (hparams .rope_scaling_type_train == LLAMA_ROPE_SCALING_TYPE_LONGROPE ) { 87layer .rope_long = create_tensor (tn (LLM_TENSOR_ROPE_FACTORS_LONG ,"weight" ,i ), {n_rot /2 },TENSOR_NOT_REQUIRED | (i != 0 ?TENSOR_DUPLICATED :0 )); 88layer .rope_short = create_tensor (tn (LLM_TENSOR_ROPE_FACTORS_SHORT ,"weight" ,i ), {n_rot /2 },TENSOR_NOT_REQUIRED | (i != 0 ?TENSOR_DUPLICATED :0 )); 89 } 90else { 91layer .rope_freqs = create_tensor (tn (LLM_TENSOR_ROPE_FREQS ,"weight" ,i ), {n_rot /2 },TENSOR_NOT_REQUIRED | (i != 0 ?TENSOR_DUPLICATED :0 )); 92 } 93 94if (n_expert == 0 ) { 95layer .ffn_gate = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"weight" ,i ), {n_embd ,n_ff },0 ); 96layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },0 ); 97layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },0 ); 98 99// optional MLP bias 100layer .ffn_gate_b = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"bias" ,i ), {n_ff },TENSOR_NOT_REQUIRED ); 101layer .ffn_down_b = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"bias" ,i ), {n_embd },TENSOR_NOT_REQUIRED ); 102layer .ffn_up_b = create_tensor (tn (LLM_TENSOR_FFN_UP ,"bias" ,i ), {n_ff },TENSOR_NOT_REQUIRED ); 103 }else { 104layer .ffn_gate_inp = create_tensor (tn (LLM_TENSOR_FFN_GATE_INP ,"weight" ,i ), {n_embd ,n_expert },0 ); 105layer .ffn_gate_exps = create_tensor (tn (LLM_TENSOR_FFN_GATE_EXPS ,"weight" ,i ), {n_embd ,n_ff ,n_expert },TENSOR_NOT_REQUIRED ); 106layer .ffn_down_exps = create_tensor (tn (LLM_TENSOR_FFN_DOWN_EXPS ,"weight" ,i ), {n_ff ,n_embd ,n_expert },0 ); 107layer .ffn_up_exps = create_tensor (tn (LLM_TENSOR_FFN_UP_EXPS ,"weight" ,i ), {n_embd ,n_ff ,n_expert },0 ); 108 109// For Granite MoE Shared 110if (hparams .n_ff_shexp > 0 ) { 111layer .ffn_gate_shexp = create_tensor (tn (LLM_TENSOR_FFN_GATE_SHEXP ,"weight" ,i ), {n_embd ,hparams .n_ff_shexp },0 ); 112layer .ffn_up_shexp = create_tensor (tn (LLM_TENSOR_FFN_UP_SHEXP ,"weight" ,i ), {n_embd ,hparams .n_ff_shexp },0 ); 113layer .ffn_down_shexp = create_tensor (tn (LLM_TENSOR_FFN_DOWN_SHEXP ,"weight" ,i ), {hparams .n_ff_shexp ,n_embd },0 ); 114 } 115 } 116 } 117} 118 119std::unique_ptr < llm_graph_context > llama_model_granite::build_arch_graph (const llm_graph_params & params )const { 120return std::make_unique < graph > (* this ,params ); 121} 122 123llama_model_granite::graph::graph ( 124const llama_model & model , 125const llm_graph_params & params ) 126 :llm_graph_context (params ) { 127 128const int64_t n_embd_head = hparams .n_embd_head_v (); 129 130GGML_ASSERT (n_embd_head == hparams .n_embd_head_k ()); 131GGML_ASSERT (n_embd_head == n_rot ); 132 133ggml_tensor * cur ; 134ggml_tensor * inpL ; 135 136inpL = build_inp_embd (model .tok_embd ); 137 138// inp_pos - built only if rope enabled 139ggml_tensor * inp_pos = nullptr ; 140if (hparams .has_rope (0 )) { 141inp_pos = build_inp_pos (); 142 } 143auto * inp_attn = build_attn_inp_kv (); 144 145ggml_tensor * inp_out_ids = build_inp_out_ids (); 146 147for (int il = 0 ;il < n_layer ;++ il ) { 148 149// Granite Vision 4.1 deepstack: inject the projector stream that 150// targets decoder layer `il` before the decoder runs. 151// NOTE: skip the first deepstack layer since that's inpL 152const auto & deepstack_emb_idx = hparams .deepstack_mapping_arr [il ]; 153if (il > 0 && deepstack_emb_idx >=0 ) { 154ggml_tensor * ds = ggml_view_2d (ctx0 , 155res -> t_inp_embd ,n_embd ,n_tokens , 156res -> t_inp_embd -> nb [1 ], 157deepstack_emb_idx * n_embd * sizeof (float )); 158inpL = ggml_add (ctx0 ,inpL ,ds ); 159cb (inpL ,"deepstack_in" ,il ); 160 } 161 162ggml_tensor * inpSA = inpL ; 163 164// norm 165cur = build_norm (inpL , 166model .layers [il ].attn_norm ,NULL , 167LLM_NORM_RMS ,il ); 168cb (cur ,"attn_norm" ,il ); 169 170// self-attention 171cur = build_attention_layer ( 172cur ,inp_pos ,inp_attn , 173model ,n_embd_head ,il ); 174 175if (il == n_layer - 1 && inp_out_ids ) { 176cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 177inpSA = ggml_get_rows (ctx0 ,inpSA ,inp_out_ids ); 178 } 179// ffn 180cur = build_layer_ffn (cur ,inpSA ,model ,il ); 181 182// input for next layer 183inpL = cur ; 184 } 185cur = inpL ; 186 187cur = build_norm (cur , 188model .output_norm ,NULL , 189LLM_NORM_RMS ,-1 ); 190 191cb (cur ,"result_norm" ,-1 ); 192res -> t_embd = cur ; 193 194// lm_head 195cur = build_lora_mm (model .output ,cur ,model .output_s ); 196 197// For Granite architectures - scale logits 198cur = ggml_scale (ctx0 ,cur ,1.0f /hparams .f_logit_scale ); 199cb (cur ,"result_output" ,-1 ); 200res -> t_logits = cur ; 201 202ggml_build_forward_expand (gf ,cur ); 203} 204 205ggml_tensor * llama_model_granite::graph::build_attention_layer ( 206ggml_tensor * cur , 207ggml_tensor * inp_pos , 208llm_graph_input_attn_kv * inp_attn , 209const llama_model & model , 210const int64_t n_embd_head , 211const int il ) { 212 213auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 214n_embd_head ,hparams .n_head (il ),hparams .n_head_kv (il ),il ); 215 216if (hparams .has_rope (il )) { 217ggml_tensor * rope_factors = model .get_rope_factors (cparams ,il ); 218Qcur = ggml_rope_ext ( 219ctx0 ,Qcur ,inp_pos ,rope_factors , 220n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 221ext_factor ,attn_factor ,beta_fast ,beta_slow 222 ); 223 224Kcur = ggml_rope_ext ( 225ctx0 ,Kcur ,inp_pos ,rope_factors , 226n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 227ext_factor ,attn_factor ,beta_fast ,beta_slow 228 ); 229 } 230 231cb (Qcur ,"Qcur" ,il ); 232cb (Kcur ,"Kcur" ,il ); 233cb (Vcur ,"Vcur" ,il ); 234 235const float kq_scale = hparams .f_attention_scale == 0.0f ?1.0f /sqrtf (float (n_embd_head )) :hparams .f_attention_scale ; 236cur = build_attn (inp_attn , 237model .layers [il ].wo ,model .layers [il ].wo_b ,model .layers [il ].wo_s , 238Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,kq_scale ,il ); 239cb (cur ,"attn_out" ,il ); 240return cur ; 241} 242 243ggml_tensor * llama_model_granite::graph::build_layer_ffn ( 244ggml_tensor * cur , 245ggml_tensor * inpSA , 246const llama_model & model , 247const int il ) { 248 249// For Granite architectures - scale residual 250if (hparams .f_residual_scale ) { 251cur = ggml_scale (ctx0 ,cur ,hparams .f_residual_scale ); 252 } 253ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 254cb (ffn_inp ,"ffn_inp" ,il ); 255 256// feed-forward network (non-MoE) 257if (model .layers [il ].ffn_gate_inp == nullptr ) { 258 259cur = build_norm (ffn_inp , 260model .layers [il ].ffn_norm ,NULL , 261LLM_NORM_RMS ,il ); 262cb (cur ,"ffn_norm" ,il ); 263 264cur = build_ffn (cur , 265model .layers [il ].ffn_up ,model .layers [il ].ffn_up_b ,NULL , 266model .layers [il ].ffn_gate ,model .layers [il ].ffn_gate_b ,NULL , 267model .layers [il ].ffn_down ,model .layers [il ].ffn_down_b ,NULL , 268NULL , 269LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 270cb (cur ,"ffn_out" ,il ); 271 272 }else { 273// MoE branch 274cur = build_norm (ffn_inp , 275model .layers [il ].ffn_norm ,NULL , 276LLM_NORM_RMS ,il ); 277cb (cur ,"ffn_norm" ,il ); 278 279ggml_tensor * moe_out = build_moe_ffn (cur , 280model .layers [il ].ffn_gate_inp , 281model .layers [il ].ffn_up_exps , 282model .layers [il ].ffn_gate_exps , 283model .layers [il ].ffn_down_exps , 284nullptr , 285n_expert ,n_expert_used , 286LLM_FFN_SILU , true, 287hparams .expert_weights_scale , 288LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX , 289il ); 290cb (moe_out ,"ffn_moe_out" ,il ); 291 292// For Granite MoE Shared 293if (hparams .n_ff_shexp > 0 ) { 294ggml_tensor * ffn_shexp = build_ffn (cur , 295model .layers [il ].ffn_up_shexp ,NULL ,NULL , 296model .layers [il ].ffn_gate_shexp ,NULL ,NULL , 297model .layers [il ].ffn_down_shexp ,NULL ,NULL , 298NULL , 299LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 300cb (ffn_shexp ,"ffn_shexp" ,il ); 301 302cur = ggml_add (ctx0 ,moe_out ,ffn_shexp ); 303cb (cur ,"ffn_out" ,il ); 304 }else { 305cur = moe_out ; 306 } 307 } 308 309// For Granite architectures - scale residual 310if (hparams .f_residual_scale ) { 311cur = ggml_scale (ctx0 ,cur ,hparams .f_residual_scale ); 312 } 313cur = ggml_add (ctx0 ,cur ,ffn_inp ); 314cb (cur ,"ffn_out" ,il ); 315 316cur = build_cvec (cur ,il ); 317cb (cur ,"l_out" ,il ); 318 319return cur ; 320}