char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
7221e24f5
char/aliceai-t5-moe
1#include "models.h" 2 3#include <cmath> 4 5void llama_model_granite_switch::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 12bool rope_finetuned = true; 13ml .get_key (LLM_KV_ROPE_SCALING_FINETUNED ,rope_finetuned , false); 14hparams .rope_finetuned = rope_finetuned ;// needed for round trip save 15 std::fill (hparams .rope_pattern .begin (),hparams .rope_pattern .end (),rope_finetuned ); 16 17switch (hparams .n_layer ()) { 18case 40 :type = hparams .n_embd == 4096 ?LLM_TYPE_8B :LLM_TYPE_3B ;break ; 19case 64 :type = LLM_TYPE_30B ;break ; 20default :type = LLM_TYPE_UNKNOWN ; 21 } 22 23ml .get_key (LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH ,hparams .n_ff_shexp ,/* required */ false); 24 25ml .get_key (LLM_KV_ADAPTER_COUNT ,n_adapters ); 26ml .get_key (LLM_KV_ADAPTER_LORA_RANK ,max_lora_rank ); 27ml .get_key (LLM_KV_ADAPTER_ROUTER_GAIN ,router_gain ,/* required */ false); 28 29// bound counts that size tensors 30if (n_adapters > 4096 ) { 31throw std::runtime_error (format ("graniteswitch: invalid adapter count %u" ,n_adapters )); 32 } 33if (max_lora_rank > 4096 ) { 34throw std::runtime_error (format ("graniteswitch: invalid lora rank %u" ,max_lora_rank )); 35 } 36 37 std::vector < llama_token > token_ids ; 38 std::vector < llama_token > substitute_ids ; 39ml .get_arr (LLM_KV_ADAPTER_TOKEN_IDS_ACTIVATE ,token_ids ); 40ml .get_arr (LLM_KV_ADAPTER_TOKEN_IDS_SUBSTITUTE ,substitute_ids ); 41 42if (token_ids .size ()!= n_adapters || substitute_ids .size ()!= n_adapters ) { 43throw std::runtime_error (format ( 44"graniteswitch: adapter token id arrays (%zu activate, %zu substitute) do not match adapter count %u" , 45token_ids .size (),substitute_ids .size (),n_adapters )); 46 } 47 48adapter_token_to_slot .clear (); 49adapter_token_to_substitute .clear (); 50for (uint32_t i = 0 ;i < n_adapters ;++ i ) { 51// adapter i -> stacked slot i+1 (slot 0 is the base/zero delta) 52adapter_token_to_slot [token_ids [i ]]= (int32_t ) (i + 1 ); 53adapter_token_to_substitute [token_ids [i ]]= substitute_ids [i ]; 54 } 55 56// extra single-head attention layer at the END (index n_real) holds the router 57// K/V. reusing n_layer_nextn keeps n_layer() == n_real, so the regular layers 58// keep their indices and the KV cache shift/defrag skips the router layer. 59// n_layer_nextn is repurposed here (no MTP): it leaks as 1 into the 60// llama_model_n_layer_nextn() getter and a re-saved nextn_predict_layers 61const uint32_t n_real = hparams .n_layer (); 62if (n_real >=LLAMA_MAX_LAYERS ) { 63throw std::runtime_error (format ("graniteswitch: block count %u exceeds LLAMA_MAX_LAYERS" ,n_real )); 64 } 65hparams .router_layer = (int32_t )n_real ; 66hparams .n_layer_all = n_real + 1 ; 67hparams .n_layer_nextn = 1 ; 68 69hparams .n_head_arr [n_real ]= 1 ; 70hparams .n_head_kv_arr [n_real ]= 1 ; 71hparams .n_ff_arr [n_real ]= 0 ; 72} 73 74void llama_model_granite_switch::load_arch_tensors (llama_model_loader & ) { 75LLAMA_LOAD_LOCALS ; 76 77const int64_t n_slots = (int64_t )n_adapters + 1 ;// slot 0 = base/zero delta 78const int64_t n_rank = (int64_t )max_lora_rank ; 79const int64_t n_embd_q = n_embd_head_k * n_head ; 80const int64_t n_embd_kv = n_embd_k_gqa ; 81 82tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 83 84// substitute ids index tok_embd rows directly; range-check against n_vocab 85for (const auto & kv :adapter_token_to_substitute ) { 86const llama_token sub = kv .second ; 87if (sub < 0 || (int64_t )sub >=n_vocab ) { 88throw std::runtime_error (format ( 89"graniteswitch: substitute token id %d out of range [0, %d)" ,sub , (int )n_vocab )); 90 } 91 } 92 93output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 94output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED ); 95if (output == NULL ) { 96output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED ); 97 } 98 99for (int i = 0 ;i < n_layer ;++ i ) { 100auto & layer = layers [i ]; 101 102layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 103 104layer .wqkv = create_tensor (tn (LLM_TENSOR_ATTN_QKV ,"weight" ,i ), {n_embd ,n_embd_q + 2 * n_embd_kv },0 ); 105layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd_q ,n_embd },0 ); 106 107layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 108 109layer .ffn_gate = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"weight" ,i ), {n_embd ,n_ff },0 ); 110layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },0 ); 111layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },0 ); 112 113auto & sl = layer .switch_lora ; 114 115sl .a_q = create_tensor (tn (LLM_TENSOR_ATTN_Q ,"lora_a" ,i ), {n_embd ,n_rank ,n_slots },0 ); 116sl .b_q = create_tensor (tn (LLM_TENSOR_ATTN_Q ,"lora_b" ,i ), {n_rank ,n_embd_q ,n_slots },0 ); 117sl .a_k = create_tensor (tn (LLM_TENSOR_ATTN_K ,"lora_a" ,i ), {n_embd ,n_rank ,n_slots },0 ); 118sl .b_k = create_tensor (tn (LLM_TENSOR_ATTN_K ,"lora_b" ,i ), {n_rank ,n_embd_kv ,n_slots },0 ); 119sl .a_v = create_tensor (tn (LLM_TENSOR_ATTN_V ,"lora_a" ,i ), {n_embd ,n_rank ,n_slots },0 ); 120sl .b_v = create_tensor (tn (LLM_TENSOR_ATTN_V ,"lora_b" ,i ), {n_rank ,n_embd_kv ,n_slots },0 ); 121 122sl .a_o = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"lora_a" ,i ), {n_embd_q ,n_rank ,n_slots },0 ); 123sl .b_o = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"lora_b" ,i ), {n_rank ,n_embd ,n_slots },0 ); 124 125sl .a_gate = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"lora_a" ,i ), {n_embd ,n_rank ,n_slots },0 ); 126sl .b_gate = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"lora_b" ,i ), {n_rank ,n_ff ,n_slots },0 ); 127sl .a_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"lora_a" ,i ), {n_embd ,n_rank ,n_slots },0 ); 128sl .b_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"lora_b" ,i ), {n_rank ,n_ff ,n_slots },0 ); 129sl .a_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"lora_a" ,i ), {n_ff ,n_rank ,n_slots },0 ); 130sl .b_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"lora_b" ,i ), {n_rank ,n_embd ,n_slots },0 ); 131 } 132} 133 134class llm_graph_input_switch :public llm_graph_input_i { 135public : 136llm_graph_input_switch (const llama_model_granite_switch & smodel ) :smodel (smodel ) {} 137virtual ~llm_graph_input_switch ()= default ; 138 139void set_input (const llama_ubatch * ubatch )override ; 140 141ggml_tensor * sub_tokens = nullptr ;// I32 [n_tokens] adapter-substituted token ids 142ggml_tensor * router_ksig = nullptr ;// F32 [n_tokens] router K signal (+/-gain) 143ggml_tensor * router_vval = nullptr ;// F32 [n_tokens] router V value (adapter slot / 0) 144ggml_tensor * router_q = nullptr ;// F32 [n_tokens] router Q value (constant 1.0) 145 146const llama_model_granite_switch & smodel ; 147}; 148 149// K dim-0 is +gain for an adapter token, -gain otherwise; the causal softmax then 150// lets a single visible adapter token dominate so the readback recovers its slot. 151void llm_graph_input_switch::set_input (const llama_ubatch * ubatch ) { 152if (!ubatch -> token ) { 153return ; 154 } 155 156const int64_t n_tokens = ubatch -> n_tokens ; 157 158 std::vector < int32_t > sub (n_tokens ); 159 std::vector < float > ksig (n_tokens ); 160 std::vector < float > vval (n_tokens ); 161 std::vector < float > q (n_tokens ,1.0f ); 162 163for (int64_t i = 0 ;i < n_tokens ;++ i ) { 164const llama_token tok = ubatch -> token [i ]; 165 166const auto it = smodel .adapter_token_to_slot .find (tok ); 167if (it != smodel .adapter_token_to_slot .end ()) { 168ksig [i ]= + smodel .router_gain ; 169vval [i ]= (float )it -> second ; 170 }else { 171ksig [i ]= - smodel .router_gain ; 172vval [i ]= 0.0f ; 173 } 174 175const auto sit = smodel .adapter_token_to_substitute .find (tok ); 176sub [i ]= (sit != smodel .adapter_token_to_substitute .end ()) 177 ? (int32_t )sit -> second 178 : (int32_t )tok ; 179 } 180 181ggml_backend_tensor_set (sub_tokens ,sub .data (),0 ,n_tokens * ggml_element_size (sub_tokens )); 182ggml_backend_tensor_set (router_ksig ,ksig .data (),0 ,n_tokens * ggml_element_size (router_ksig )); 183ggml_backend_tensor_set (router_vval ,vval .data (),0 ,n_tokens * ggml_element_size (router_vval )); 184ggml_backend_tensor_set (router_q ,q .data (),0 ,n_tokens * ggml_element_size (router_q )); 185} 186 187std::unique_ptr < llm_graph_context > llama_model_granite_switch::build_arch_graph (const llm_graph_params & params )const { 188return std::make_unique < graph > (* this ,params ); 189} 190 191// per-token switched LoRA delta: B_a*(A_a*x), adapter selected per token via ids. 192// cur: {n_in, n_tokens}, ids: {n_tokens} -> {n_out, n_tokens} 193ggml_tensor * llama_model_granite_switch::graph::build_switched_lora_delta ( 194ggml_tensor * lora_a , 195ggml_tensor * lora_b , 196ggml_tensor * cur , 197ggml_tensor * ids ) { 198const int64_t n_in = cur -> ne [0 ]; 199const int64_t n_tokens = cur -> ne [1 ]; 200 201ggml_tensor * x = ggml_reshape_3d (ctx0 ,cur ,n_in ,1 ,n_tokens ); 202ggml_tensor * ids2 = ggml_reshape_2d (ctx0 ,ids ,1 ,n_tokens ); 203 204ggml_tensor * a = ggml_mul_mat_id (ctx0 ,lora_a ,x ,ids2 );// {max_rank, 1, n_tokens} 205ggml_tensor * d = ggml_mul_mat_id (ctx0 ,lora_b ,a ,ids2 );// {n_out, 1, n_tokens} 206 207return ggml_reshape_2d (ctx0 ,d ,d -> ne [0 ],n_tokens ); 208} 209 210ggml_tensor * llama_model_granite_switch::graph::build_switched_lora_mm ( 211ggml_tensor * w , 212ggml_tensor * lora_a , 213ggml_tensor * lora_b , 214ggml_tensor * cur , 215ggml_tensor * ids ) { 216ggml_tensor * base = ggml_mul_mat (ctx0 ,w ,cur ); 217ggml_tensor * delta = build_switched_lora_delta (lora_a ,lora_b ,cur ,ids ); 218return ggml_add (ctx0 ,base ,delta ); 219} 220 221llama_model_granite_switch::graph::graph ( 222const llama_model & model , 223const llm_graph_params & params ) 224 :llm_graph_context (params ) { 225 226const auto & smodel = static_cast < const llama_model_granite_switch &> (model ); 227 228// TODO: support raw embedding input (multimodal / pre-embedded tokens) when needed 229GGML_ASSERT (ubatch .token && "granite-switch requires token input" ); 230 231const int64_t n_embd_head = hparams .n_embd_head_v (); 232GGML_ASSERT (n_embd_head == hparams .n_embd_head_k ()); 233GGML_ASSERT (n_embd_head == n_rot ); 234 235auto inp_switch = std::make_unique < llm_graph_input_switch > (smodel ); 236inp_switch -> sub_tokens = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_I32 ,n_tokens ); 237inp_switch -> router_ksig = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_F32 ,n_tokens ); 238inp_switch -> router_vval = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_F32 ,n_tokens ); 239inp_switch -> router_q = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_F32 ,n_tokens ); 240ggml_set_input (inp_switch -> sub_tokens ); 241ggml_set_input (inp_switch -> router_ksig ); 242ggml_set_input (inp_switch -> router_vval ); 243ggml_set_input (inp_switch -> router_q ); 244ggml_tensor * sub_tokens = inp_switch -> sub_tokens ; 245ggml_tensor * router_ksig = inp_switch -> router_ksig ; 246ggml_tensor * router_vval = inp_switch -> router_vval ; 247ggml_tensor * router_q = inp_switch -> router_q ; 248res -> add_input (std::move (inp_switch )); 249 250// embed the substituted ids directly; build_inp_embd would embed the raw tokens 251ggml_tensor * inpL = ggml_get_rows (ctx0 ,model .tok_embd ,sub_tokens ); 252if (hparams .f_embedding_scale != 0.0f ) { 253inpL = ggml_scale (ctx0 ,inpL ,hparams .f_embedding_scale ); 254 } 255cb (inpL ,"inp_embd" ,-1 ); 256 257ggml_tensor * inp_pos = nullptr ; 258if (hparams .has_rope (0 )) { 259inp_pos = build_inp_pos (); 260 } 261auto * inp_attn = build_attn_inp_kv (); 262 263// single causal head at layer R recovers the adapter index in-graph: only dim 0 264// carries signal (Q[0]=1, K[0]=+/-gain, V[0]=slot/0), the rest is zero-padded. 265const int R = hparams .router_layer ; 266GGML_ASSERT (R >=0 ); 267auto router_lane = [& ](ggml_tensor * sig1d ) { 268ggml_tensor * t = ggml_reshape_3d (ctx0 ,sig1d ,1 ,1 ,n_tokens ); 269return ggml_pad (ctx0 ,t , (int )n_embd_head - 1 ,0 ,0 ,0 ); 270 }; 271ggml_tensor * Qr = router_lane (router_q ); 272ggml_tensor * Kr = router_lane (router_ksig ); 273ggml_tensor * Vr = router_lane (router_vval ); 274 275ggml_tensor * router_out = build_attn (inp_attn , 276nullptr ,nullptr ,nullptr , 277Qr ,Kr ,Vr ,nullptr ,nullptr ,nullptr ,/*kq_scale=*/ 1.0f ,/*il=*/ R ); 278cb (router_out ,"router_out" ,R ); 279 280// row 0 of router_out is the attended slot; clamp+round to an I32 index 281ggml_tensor * slot_f = ggml_cont (ctx0 , 282ggml_view_2d (ctx0 ,router_out ,1 ,n_tokens ,router_out -> nb [1 ],0 )); 283slot_f = ggml_reshape_1d (ctx0 ,slot_f ,n_tokens ); 284slot_f = ggml_clamp (ctx0 ,slot_f ,0.0f , (float )smodel .n_adapters ); 285slot_f = ggml_round (ctx0 ,slot_f ); 286ggml_tensor * adapter_ids = ggml_cast (ctx0 ,slot_f ,GGML_TYPE_I32 ); 287cb (adapter_ids ,"adapter_ids" ,-1 ); 288 289ggml_tensor * inp_out_ids = build_inp_out_ids (); 290 291ggml_tensor * cur ; 292 293for (int il = 0 ;il < n_layer ;++ il ) { 294ggml_tensor * inpSA = inpL ; 295 296cur = build_norm (inpL ,model .layers [il ].attn_norm ,NULL ,LLM_NORM_RMS ,il ); 297cb (cur ,"attn_norm" ,il ); 298 299cur = build_attention_layer (cur ,inp_pos ,adapter_ids ,inp_attn ,model ,n_embd_head ,il ); 300 301if (il == n_layer - 1 && inp_out_ids ) { 302cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 303inpSA = ggml_get_rows (ctx0 ,inpSA ,inp_out_ids ); 304// keep adapter_ids aligned to the kept rows (2D round-trip for get_rows) 305const int64_t n_out = inp_out_ids -> ne [0 ]; 306adapter_ids = ggml_get_rows (ctx0 , 307ggml_reshape_2d (ctx0 ,adapter_ids ,1 ,adapter_ids -> ne [0 ]),inp_out_ids ); 308adapter_ids = ggml_reshape_1d (ctx0 ,adapter_ids ,n_out ); 309 } 310 311cur = build_layer_ffn (cur ,inpSA ,adapter_ids ,model ,il ); 312 313inpL = cur ; 314 } 315 316cur = inpL ; 317 318cur = build_norm (cur ,model .output_norm ,NULL ,LLM_NORM_RMS ,-1 ); 319cb (cur ,"result_norm" ,-1 ); 320res -> t_embd = cur ; 321 322cur = build_lora_mm (model .output ,cur ,model .output_s ); 323 324cur = ggml_scale (ctx0 ,cur ,1.0f /hparams .f_logit_scale ); 325cb (cur ,"result_output" ,-1 ); 326res -> t_logits = cur ; 327 328ggml_build_forward_expand (gf ,cur ); 329} 330 331ggml_tensor * llama_model_granite_switch::graph::build_attention_layer ( 332ggml_tensor * cur , 333ggml_tensor * inp_pos , 334ggml_tensor * adapter_ids , 335llm_graph_input_attn_kv * inp_attn , 336const llama_model & model , 337const int64_t n_embd_head , 338const int il ) { 339 340const auto & layer = model .layers [il ]; 341const auto & sl = layer .switch_lora ; 342 343const int64_t n_head = hparams .n_head (il ); 344const int64_t n_head_kv = hparams .n_head_kv (il ); 345 346ggml_tensor * qkv = ggml_mul_mat (ctx0 ,layer .wqkv ,cur ); 347cb (qkv ,"wqkv" ,il ); 348 349const int64_t n_embd_q = n_embd_head * n_head ; 350const int64_t n_embd_kv = n_embd_head * n_head_kv ; 351 352// slice fused qkv into Q/K/V, made contiguous so LoRA deltas can be added 353ggml_tensor * Qcur = ggml_cont (ctx0 ,ggml_view_2d (ctx0 ,qkv ,n_embd_q ,qkv -> ne [1 ],qkv -> nb [1 ],0 )); 354ggml_tensor * Kcur = ggml_cont (ctx0 ,ggml_view_2d (ctx0 ,qkv ,n_embd_kv ,qkv -> ne [1 ],qkv -> nb [1 ],n_embd_q * ggml_element_size (qkv ))); 355ggml_tensor * Vcur = ggml_cont (ctx0 ,ggml_view_2d (ctx0 ,qkv ,n_embd_kv ,qkv -> ne [1 ],qkv -> nb [1 ], (n_embd_q + n_embd_kv )* ggml_element_size (qkv ))); 356 357Qcur = ggml_add (ctx0 ,Qcur ,build_switched_lora_delta (sl .a_q ,sl .b_q ,cur ,adapter_ids )); 358Kcur = ggml_add (ctx0 ,Kcur ,build_switched_lora_delta (sl .a_k ,sl .b_k ,cur ,adapter_ids )); 359Vcur = ggml_add (ctx0 ,Vcur ,build_switched_lora_delta (sl .a_v ,sl .b_v ,cur ,adapter_ids )); 360 361Qcur = ggml_reshape_3d (ctx0 ,Qcur ,n_embd_head ,n_head ,n_tokens ); 362Kcur = ggml_reshape_3d (ctx0 ,Kcur ,n_embd_head ,n_head_kv ,n_tokens ); 363Vcur = ggml_reshape_3d (ctx0 ,Vcur ,n_embd_head ,n_head_kv ,n_tokens ); 364 365if (hparams .has_rope (il )) { 366ggml_tensor * rope_factors = model .get_rope_factors (cparams ,il ); 367Qcur = ggml_rope_ext (ctx0 ,Qcur ,inp_pos ,rope_factors , 368n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 369ext_factor ,attn_factor ,beta_fast ,beta_slow ); 370Kcur = ggml_rope_ext (ctx0 ,Kcur ,inp_pos ,rope_factors , 371n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 372ext_factor ,attn_factor ,beta_fast ,beta_slow ); 373 } 374cb (Qcur ,"Qcur" ,il ); 375cb (Kcur ,"Kcur" ,il ); 376cb (Vcur ,"Vcur" ,il ); 377 378const float kq_scale = hparams .f_attention_scale == 0.0f 379 ?1.0f /sqrtf (float (n_embd_head )) :hparams .f_attention_scale ; 380 381// wo = nullptr so build_attn returns concatenated heads; o-proj is switched below 382ggml_tensor * attn = build_attn (inp_attn , 383nullptr ,nullptr ,nullptr , 384Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,kq_scale ,il ); 385cb (attn ,"attn_pre_o" ,il ); 386 387cur = build_switched_lora_mm (layer .wo ,sl .a_o ,sl .b_o ,attn ,adapter_ids ); 388cb (cur ,"attn_out" ,il ); 389return cur ; 390} 391 392ggml_tensor * llama_model_granite_switch::graph::build_layer_ffn ( 393ggml_tensor * cur , 394ggml_tensor * inpSA , 395ggml_tensor * adapter_ids , 396const llama_model & model , 397const int il ) { 398 399const auto & layer = model .layers [il ]; 400const auto & sl = layer .switch_lora ; 401 402if (hparams .f_residual_scale ) { 403cur = ggml_scale (ctx0 ,cur ,hparams .f_residual_scale ); 404 } 405ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 406cb (ffn_inp ,"ffn_inp" ,il ); 407 408cur = build_norm (ffn_inp ,layer .ffn_norm ,NULL ,LLM_NORM_RMS ,il ); 409cb (cur ,"ffn_norm" ,il ); 410 411ggml_tensor * g = build_switched_lora_mm (layer .ffn_gate ,sl .a_gate ,sl .b_gate ,cur ,adapter_ids ); 412ggml_tensor * u = build_switched_lora_mm (layer .ffn_up ,sl .a_up ,sl .b_up ,cur ,adapter_ids ); 413g = ggml_silu (ctx0 ,g ); 414ggml_tensor * gu = ggml_mul (ctx0 ,g ,u ); 415cur = build_switched_lora_mm (layer .ffn_down ,sl .a_down ,sl .b_down ,gu ,adapter_ids ); 416cb (cur ,"ffn_out" ,il ); 417 418if (hparams .f_residual_scale ) { 419cur = ggml_scale (ctx0 ,cur ,hparams .f_residual_scale ); 420 } 421cur = ggml_add (ctx0 ,cur ,ffn_inp ); 422 423cur = build_cvec (cur ,il ); 424cb (cur ,"l_out" ,il ); 425 426return cur ; 427}