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