char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
62bf73d25
char/aliceai-t5-moe
1#include "models.h" 2 3void llama_model_muse_glimmer::load_arch_hparams (llama_model_loader & ml ) { 4ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps ); 5ml .get_key (LLM_KV_ATTENTION_SLIDING_WINDOW ,hparams .n_swa ); 6ml .get_key (LLM_KV_FINAL_LOGIT_SOFTCAPPING ,hparams .f_final_logit_softcapping , false); 7ml .get_key (LLM_KV_LOGIT_SCALE ,hparams .f_logit_scale ); 8 9hparams .rope_freq_base_train_swa = hparams .rope_freq_base_train ; 10ml .get_key (LLM_KV_ROPE_FREQ_BASE_SWA ,hparams .rope_freq_base_train_swa , false); 11 12hparams .swa_type = LLAMA_SWA_TYPE_STANDARD ; 13uint32_t swa_period = 4 ; 14if (ml .get_key_or_arr (LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN ,swa_period , false)) { 15hparams .set_swa_pattern (swa_period ); 16 }else { 17ml .get_key_or_arr (LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN ,hparams .is_swa_impl ,hparams .n_layer ()); 18 } 19 20switch (hparams .n_layer ()) { 21case 52 :type = LLM_TYPE_30B ;break ; 22default :type = LLM_TYPE_UNKNOWN ; 23 } 24} 25 26void llama_model_muse_glimmer::load_arch_tensors (llama_model_loader & ) { 27LLAMA_LOAD_LOCALS ; 28 29tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 30output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 31output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },0 ); 32 33for (int i = 0 ;i < n_layer ;++ i ) { 34auto & layer = layers [i ]; 35 36// Pre/post-attention norms (Muse Glimmer's `weight + 1` applied at conversion time). 37layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 38layer .attn_post_norm = create_tensor (tn (LLM_TENSOR_ATTN_POST_NORM ,"weight" ,i ), {n_embd },0 ); 39 40// Q/K/V/O projections. 41create_tensor_qkv (layer ,i ,n_embd ,n_embd_head_k * n_head ,n_embd_k_gqa ,n_embd_v_gqa ,0 ); 42layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd_head_k * n_head ,n_embd },0 ); 43 44// QK-norm. Weights are synthesized at conversion time to absorb `qk_scale_factor`. 45layer .attn_q_norm = create_tensor (tn (LLM_TENSOR_ATTN_Q_NORM ,"weight" ,i ), {n_embd_head_k },0 ); 46layer .attn_k_norm = create_tensor (tn (LLM_TENSOR_ATTN_K_NORM ,"weight" ,i ), {n_embd_head_k },0 ); 47 48// Attention output gate: sigmoid(gate) * attn_out before o_proj (same as afmoe). 49layer .wqkv_gate = create_tensor (tn (LLM_TENSOR_ATTN_GATE ,"weight" ,i ), {n_embd ,n_embd_head_k * n_head },0 ); 50 51// Pre/post-FFN norms (FFN_PRE_NORM is aliased to LLM_TENSOR_FFN_NORM). 52layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 53layer .ffn_post_norm = create_tensor (tn (LLM_TENSOR_FFN_POST_NORM ,"weight" ,i ), {n_embd },0 ); 54 55// Dense FFN (unlike afmoe, no MoE branches). 56layer .ffn_gate = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"weight" ,i ), {n_embd ,n_ff },0 ); 57layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },0 ); 58layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },0 ); 59 } 60} 61 62llama_model_muse_glimmer::graph::graph (const llama_model & model ,const llm_graph_params & params ) 63 :llm_graph_context (params ) { 64const int64_t n_embd_head = hparams .n_embd_head_v (); 65GGML_ASSERT (n_embd_head == hparams .n_embd_head_k ()); 66 67// Different to f_norm_rms_eps for post-attn / post-FFN norms 68const float post_norm_eps = 1e-8f ; 69 70ggml_tensor * cur ; 71ggml_tensor * inpL ; 72 73inpL = build_inp_embd (model .tok_embd ); 74inpL = build_norm (inpL ,nullptr ,nullptr ,LLM_NORM_RMS ,-1 ); 75cb (inpL ,"embd_norm" ,-1 ); 76 77ggml_tensor * inp_pos = build_inp_pos (); 78auto * inp_attn = build_attn_inp_kv_iswa (); 79ggml_tensor * inp_out_ids = build_inp_out_ids (); 80 81const float kq_scale = 1.0f /sqrtf (float (n_embd_head )); 82 83for (int il = 0 ;il < n_layer ;++ il ) { 84// expose per-layer residual for speculative drafts (see LLM_KV_TARGET_LAYERS). 85res -> t_layer_inp [il ]= inpL ; 86 87const float freq_base_l = model .get_rope_freq_base (cparams ,il ); 88const float freq_scale_l = model .get_rope_freq_scale (cparams ,il ); 89 90ggml_tensor * inpSA = inpL ; 91 92// RoPE runs on the SWA layers, NoPE on full ones. 93const bool use_rope = hparams .is_swa (il ); 94 95// pre-attention norm (weight+1 folded at conversion time) 96cur = build_norm (inpL ,model .layers [il ].attn_norm ,NULL ,LLM_NORM_RMS ,il ); 97cb (cur ,"attn_norm" ,il ); 98 99// self-attention: attention output gate around SDPA (afmoe.cpp:147-191) 100 { 101ggml_tensor * attn_inp = cur ;// save input for gate computation 102 103auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 104n_embd_head ,n_head ,n_head_kv ,il ); 105 106// gate = wqkv_gate @ attn_inp (from pre-attn hidden state) 107ggml_tensor * gate = build_lora_mm (model .layers [il ].wqkv_gate ,attn_inp ); 108cb (gate ,"attn_gate_proj" ,il ); 109 110// QK-norm. attn_q_norm weight was synthesized at conversion to broadcast 111// qk_scale_factor across head_dim; attn_k_norm is identity (ones). 112Qcur = build_norm (Qcur ,model .layers [il ].attn_q_norm ,NULL ,LLM_NORM_RMS ,il ); 113Kcur = build_norm (Kcur ,model .layers [il ].attn_k_norm ,NULL ,LLM_NORM_RMS ,il ); 114cb (Qcur ,"Qcur_normed" ,il ); 115cb (Kcur ,"Kcur_normed" ,il ); 116 117if (use_rope ) { 118Qcur = ggml_rope_ext ( 119ctx0 ,Qcur ,inp_pos ,nullptr , 120n_rot ,rope_type ,n_ctx_orig ,freq_base_l ,freq_scale_l , 121ext_factor ,attn_factor ,beta_fast ,beta_slow ); 122cb (Qcur ,"Qcur_rope" ,il ); 123 124Kcur = ggml_rope_ext ( 125ctx0 ,Kcur ,inp_pos ,nullptr , 126n_rot ,rope_type ,n_ctx_orig ,freq_base_l ,freq_scale_l , 127ext_factor ,attn_factor ,beta_fast ,beta_slow ); 128cb (Kcur ,"Kcur_rope" ,il ); 129 } 130 131// SDPA. wo is deferred; the gate goes between attn_out and o_proj. 132cur = build_attn (inp_attn , 133NULL ,NULL ,NULL , 134Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,kq_scale ,il ); 135cb (cur ,"attn_out" ,il ); 136 137gate = ggml_sigmoid (ctx0 ,gate ); 138cb (gate ,"attn_gate_sig" ,il ); 139cur = ggml_mul (ctx0 ,cur ,gate ); 140cb (cur ,"attn_gated" ,il ); 141 142cur = build_lora_mm (model .layers [il ].wo ,cur ,model .layers [il ].wo_s ); 143cb (cur ,"attn_o_proj" ,il ); 144 } 145 146cur = ggml_rms_norm (ctx0 ,cur ,post_norm_eps ); 147cur = ggml_mul (ctx0 ,cur ,model .layers [il ].attn_post_norm ); 148cb (cur ,"attn_post_norm" ,il ); 149 150if (il == n_layer - 1 && inp_out_ids ) { 151cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 152inpSA = ggml_get_rows (ctx0 ,inpSA ,inp_out_ids ); 153 } 154 155ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 156cb (ffn_inp ,"ffn_inp" ,il ); 157 158// pre-FFN norm 159cur = build_norm (ffn_inp ,model .layers [il ].ffn_norm ,NULL ,LLM_NORM_RMS ,il ); 160cb (cur ,"ffn_norm" ,il ); 161 162// SwiGLU dense FFN 163cur = build_ffn (cur , 164model .layers [il ].ffn_up ,NULL ,NULL , 165model .layers [il ].ffn_gate ,NULL ,NULL , 166model .layers [il ].ffn_down ,NULL ,NULL , 167NULL , 168LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 169cb (cur ,"ffn_out" ,il ); 170 171cur = ggml_rms_norm (ctx0 ,cur ,post_norm_eps ); 172cur = ggml_mul (ctx0 ,cur ,model .layers [il ].ffn_post_norm ); 173cb (cur ,"ffn_post_norm" ,il ); 174 175cur = ggml_add (ctx0 ,cur ,ffn_inp ); 176cur = build_cvec (cur ,il ); 177cb (cur ,"l_out" ,il ); 178 179inpL = cur ; 180 } 181 182cur = inpL ; 183 184// final norm 185cur = build_norm (cur ,model .output_norm ,NULL ,LLM_NORM_RMS ,-1 ); 186cb (cur ,"result_norm" ,-1 ); 187res -> t_embd = cur ; 188 189// lm_head, followed by output multiplier 190cur = build_lora_mm (model .output ,cur ,model .output_s ); 191cur = ggml_scale (ctx0 ,cur ,hparams .f_logit_scale ); 192 193// Final logit tanh softcap (from gemma3.cpp). 194if (hparams .f_final_logit_softcapping ) { 195cur = ggml_scale (ctx0 ,cur ,1.0f /hparams .f_final_logit_softcapping ); 196cur = ggml_tanh (ctx0 ,cur ); 197cur = ggml_scale (ctx0 ,cur ,hparams .f_final_logit_softcapping ); 198 } 199 200cb (cur ,"result_output" ,-1 ); 201res -> t_logits = cur ; 202 203ggml_build_forward_expand (gf ,cur ); 204} 205 206std::unique_ptr < llm_graph_context > llama_model_muse_glimmer::build_arch_graph (const llm_graph_params & params )const { 207return std::make_unique < graph > (* this ,params ); 208}