Felipe97/llama-cpp-compiled
01.1k
1#include "models.h"2 3#include <sstream>4 5void llama_model_granite_swa::load_arch_hparams(llama_model_loader & ml) {6 ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);7 ml.get_key(LLM_KV_LOGIT_SCALE, hparams.f_logit_scale);8 ml.get_key(LLM_KV_RESIDUAL_SCALE, hparams.f_residual_scale, false);9 ml.get_key(LLM_KV_EMBEDDING_SCALE, hparams.f_embedding_scale, false);10 ml.get_key(LLM_KV_ATTENTION_SCALE, hparams.f_attention_scale, false);11 12 // MoE expert configuration13 ml.get_key(LLM_KV_EXPERT_COUNT, hparams.n_expert, false);14 ml.get_key_or_arr(LLM_KV_EXPERT_USED_COUNT, hparams.n_expert_used_arr, hparams.n_layer_all, false);15 16 // iSWA configuration17 ml.get_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, hparams.is_swa_impl);18 ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa);19 hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;20 21 // Granite4 Vision uses array deepstack_mapping22 ml.get_arr(LLM_KV_DEEPSTACK_MAPPING, hparams.deepstack_mapping_arr, false);23 24 // Count the unique deepstack input indices25 std::unordered_set<uint32_t> unique_deepstack_idxs;26 for (const auto val : hparams.deepstack_mapping_arr) {27 if (val >= 0) {28 unique_deepstack_idxs.insert(val);29 }30 }31 hparams.n_deepstack_layers = unique_deepstack_idxs.size();32 33 // Ensure all values are valid (avoid overflow attacks)34 for (const auto val : unique_deepstack_idxs) {35 if (val > hparams.n_deepstack_layers) {36 std::stringstream ss;37 ss << "Invalid deepstack index: " << val << " > " << hparams.n_deepstack_layers;38 throw std::runtime_error(ss.str());39 }40 }41 42 // Per-layer RoPE pattern (optional)43 ml.get_arr(LLM_KV_ATTENTION_ROPE_PATTERN, hparams.rope_pattern, false);44 45 switch (hparams.n_layer()) {46 case 32: type = LLM_TYPE_3B; break;47 case 40: type = LLM_TYPE_3B; break;48 // Add additional layer/vocab/etc checks here for other model sizes49 default: type = LLM_TYPE_UNKNOWN;50 }51 52 // For Granite MoE Shared53 ml.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 &) {57 LLAMA_LOAD_LOCALS;58 59 tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);60 61 // output62 output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);63 output = 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 embed66 if (output == NULL) {67 output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);68 }69 70 for (int i = 0; i < n_layer; ++i) {71 auto & layer = layers[i];72 73 layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);74 75 create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head, n_embd_k_gqa, n_embd_v_gqa, 0);76 layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);77 78 // optional bias tensors79 layer.wo_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);80 81 // Per-layer attention sinks for iSWA82 layer.attn_sinks = create_tensor(tn(LLM_TENSOR_ATTN_SINKS, "weight", i), {n_head}, 0);83 84 layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);85 86 if (hparams.rope_scaling_type_train == LLAMA_ROPE_SCALING_TYPE_LONGROPE) {87 layer.rope_long = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_LONG, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));88 layer.rope_short = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_SHORT, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));89 }90 else {91 layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));92 }93 94 if (n_expert == 0) {95 layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);96 layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);97 layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);98 99 // optional MLP bias100 layer.ffn_gate_b = create_tensor(tn(LLM_TENSOR_FFN_GATE, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);101 layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);102 layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);103 } else {104 layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);105 layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff, n_embd, n_expert}, 0);106 create_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)109 if (hparams.n_ff_shexp > 0) {110 layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, 2*hparams.n_ff_shexp}, 0);111 layer.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 {118 return std::make_unique<graph>(*this, params);119}120 121llama_model_granite_swa::graph::graph(122 const llama_model & model,123 const llm_graph_params & params)124 : llm_graph_context(params) {125 126 const int64_t n_embd_head = hparams.n_embd_head_v();127 128 GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());129 GGML_ASSERT(n_embd_head == n_rot);130 131 ggml_tensor * cur;132 ggml_tensor * inpL;133 134 inpL = build_inp_embd(model.tok_embd);135 136 // inp_pos - built only if rope enabled137 ggml_tensor * inp_pos = build_inp_pos();138 auto * inp_attn = build_attn_inp_kv_iswa();139 140 ggml_tensor * inp_out_ids = build_inp_out_ids();141 142 for (int il = 0; il < n_layer; ++il) {143 144 // Granite Vision 4.1 deepstack: inject the projector stream that145 // targets decoder layer `il` before the decoder runs.146 // NOTE: skip the first deepstack layer since that's inpL147 const auto & deepstack_emb_idx = hparams.deepstack_mapping_arr[il];148 if (il > 0 && deepstack_emb_idx >= 0) {149 ggml_tensor * ds = ggml_view_2d(ctx0,150 res->t_inp_embd, n_embd, n_tokens,151 res->t_inp_embd->nb[1],152 deepstack_emb_idx * n_embd * sizeof(float));153 inpL = ggml_add(ctx0, inpL, ds);154 cb(inpL, "deepstack_in", il);155 }156 157 ggml_tensor * inpSA = inpL;158 159 // norm160 cur = build_norm(inpL,161 model.layers[il].attn_norm, NULL,162 LLM_NORM_RMS, il);163 cb(cur, "attn_norm", il);164 165 // self-attention166 cur = build_attention_layer(167 cur, inp_pos, inp_attn,168 model, n_embd_head, il);169 170 if (il == n_layer - 1 && inp_out_ids) {171 cur = ggml_get_rows(ctx0, cur, inp_out_ids);172 inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);173 }174 // ffn175 cur = build_layer_ffn(cur, inpSA, model, il);176 177 // input for next layer178 inpL = cur;179 }180 cur = inpL;181 182 cur = build_norm(cur,183 model.output_norm, NULL,184 LLM_NORM_RMS, -1);185 186 cb(cur, "result_norm", -1);187 res->t_embd = cur;188 189 // lm_head190 cur = build_lora_mm(model.output, cur, model.output_s);191 192 // For Granite architectures - scale logits193 cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_logit_scale);194 cb(cur, "result_output", -1);195 res->t_logits = cur;196 197 ggml_build_forward_expand(gf, cur);198}199 200ggml_tensor * llama_model_granite_swa::graph::build_attention_layer(201 ggml_tensor * cur,202 ggml_tensor * inp_pos,203 llm_graph_input_attn_kv_iswa * inp_attn,204 const llama_model & model,205 const int64_t n_embd_head,206 const int il) {207 208 auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,209 n_embd_head, hparams.n_head(il), hparams.n_head_kv(il), il);210 211 const bool use_rope = hparams.has_rope(il);212 if (use_rope) {213 ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);214 Qcur = ggml_rope_ext(215 ctx0, Qcur, inp_pos, rope_factors,216 n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,217 ext_factor, attn_factor, beta_fast, beta_slow218 );219 220 Kcur = ggml_rope_ext(221 ctx0, Kcur, inp_pos, rope_factors,222 n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,223 ext_factor, attn_factor, beta_fast, beta_slow224 );225 }226 227 cb(Qcur, "Qcur", il);228 cb(Kcur, "Kcur", il);229 cb(Vcur, "Vcur", il);230 231 const 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 modulation234 cur = build_attn(inp_attn,235 model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,236 Qcur, Kcur, Vcur, nullptr, model.layers[il].attn_sinks, nullptr, kq_scale, il);237 cb(cur, "attn_out", il);238 return cur;239}240 241ggml_tensor * llama_model_granite_swa::graph::build_layer_ffn(242 ggml_tensor * cur,243 ggml_tensor * inpSA,244 const llama_model & model,245 const int il) {246 247 // For Granite architectures - scale residual248 if (hparams.f_residual_scale) {249 cur = ggml_scale(ctx0, cur, hparams.f_residual_scale);250 }251 ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);252 cb(ffn_inp, "ffn_inp", il);253 254 // feed-forward network (non-MoE)255 if (model.layers[il].ffn_gate_inp == nullptr) {256 257 cur = build_norm(ffn_inp,258 model.layers[il].ffn_norm, NULL,259 LLM_NORM_RMS, il);260 cb(cur, "ffn_norm", il);261 262 cur = build_ffn(cur,263 model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,264 model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,265 model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,266 NULL,267 LLM_FFN_SILU, LLM_FFN_PAR, il);268 cb(cur, "ffn_out", il);269 270 } else {271 // MoE branch272 cur = build_norm(ffn_inp,273 model.layers[il].ffn_norm, NULL,274 LLM_NORM_RMS, il);275 cb(cur, "ffn_norm", il);276 277 ggml_tensor * moe_out = build_moe_ffn(cur,278 model.layers[il].ffn_gate_inp,279 model.layers[il].ffn_up_exps,280 model.layers[il].ffn_gate_exps,281 model.layers[il].ffn_down_exps,282 nullptr,283 n_expert, n_expert_used,284 LLM_FFN_SILU, true,285 hparams.expert_weights_scale,286 LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,287 il,288 nullptr, model.layers[il].ffn_gate_up_exps);289 cb(moe_out, "ffn_moe_out", il);290 291 // For Granite MoE Shared - gate+up kept fused in ffn_up_shexp292 if (hparams.n_ff_shexp > 0) {293 ggml_tensor * ffn_shexp = build_ffn(cur,294 model.layers[il].ffn_up_shexp, NULL, NULL,295 NULL, NULL, NULL,296 model.layers[il].ffn_down_shexp, NULL, NULL,297 NULL,298 LLM_FFN_SWIGLU, LLM_FFN_SEQ, il);299 cb(ffn_shexp, "ffn_shexp", il);300 301 cur = ggml_add(ctx0, moe_out, ffn_shexp);302 cb(cur, "ffn_out", il);303 } else {304 cur = moe_out;305 }306 }307 308 // For Granite architectures - scale residual309 if (hparams.f_residual_scale) {310 cur = ggml_scale(ctx0, cur, hparams.f_residual_scale);311 }312 cur = ggml_add(ctx0, cur, ffn_inp);313 cb(cur, "ffn_out", il);314 315 cur = build_cvec(cur, il);316 cb(cur, "l_out", il);317 318 return cur;319}320 