Felipe97/llama-cpp-compiled
01.1k
1#include "models.h"2 3void llama_model_granite_hybrid::load_arch_hparams(llama_model_loader & ml) {4 ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);5 ml.get_key(LLM_KV_LOGIT_SCALE, hparams.f_logit_scale, /* required */ false);6 ml.get_key(LLM_KV_RESIDUAL_SCALE, hparams.f_residual_scale, /* required */ false);7 ml.get_key(LLM_KV_EMBEDDING_SCALE, hparams.f_embedding_scale, /* required */ false);8 ml.get_key(LLM_KV_ATTENTION_SCALE, hparams.f_attention_scale, /* required */ false);9 10 ml.get_key(LLM_KV_SSM_CONV_KERNEL, hparams.ssm_d_conv);11 ml.get_key(LLM_KV_SSM_INNER_SIZE, hparams.ssm_d_inner);12 ml.get_key(LLM_KV_SSM_STATE_SIZE, hparams.ssm_d_state);13 ml.get_key(LLM_KV_SSM_TIME_STEP_RANK, hparams.ssm_dt_rank);14 ml.get_key(LLM_KV_SSM_GROUP_COUNT, hparams.ssm_n_group);15 16 // Granite uses rope_finetuned as a switch for rope, so default to true17 bool rope_finetuned = true;18 ml.get_key(LLM_KV_ROPE_SCALING_FINETUNED, rope_finetuned, false);19 hparams.rope_finetuned = rope_finetuned; // needed for round trip save20 std::fill(hparams.rope_pattern.begin(), hparams.rope_pattern.end(), rope_finetuned);21 22 // A layer is recurrent IFF the n_head_kv value is set to 023 for (uint32_t i = 0; i < hparams.n_layer(); ++i) {24 hparams.is_recr_impl[i] = hparams.n_head_kv(i) == 0;25 }26 27 ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);28 29 switch (hparams.n_embd) {30 case 768: type = LLM_TYPE_350M; break;31 case 1536: type = (hparams.n_ff() == 512 ? LLM_TYPE_7B_A1B : LLM_TYPE_1B); break;32 case 2048: case 2560: type = LLM_TYPE_3B; break;33 case 4096: type = LLM_TYPE_32B_A9B; break;34 default: type = LLM_TYPE_UNKNOWN;35 }36 37 // For Granite MoE Shared38 ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp, /* required */ false);39}40 41void llama_model_granite_hybrid::load_arch_tensors(llama_model_loader &) {42 LLAMA_LOAD_LOCALS;43 44 // mamba2 Mixer SSM params45 // NOTE: int64_t for tensor dimensions46 const int64_t d_conv = hparams.ssm_d_conv;47 const int64_t d_inner = hparams.ssm_d_inner;48 const int64_t d_state = hparams.ssm_d_state;49 const int64_t n_ssm_head = hparams.ssm_dt_rank;50 const int64_t n_group = hparams.ssm_n_group;51 const int64_t d_in_proj = 2*d_inner + 2*n_group*d_state + n_ssm_head;52 53 // only an expansion factor of 2 is supported for now54 GGML_ASSERT(2 * n_embd == d_inner);55 56 // embeddings57 tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);58 59 // output60 {61 output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);62 output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);63 // if output is NULL, init from the input tok embed, duplicated to allow offloading64 if (output == NULL) {65 output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);66 }67 }68 69 for (int i = 0; i < n_layer; ++i) {70 auto & layer = layers[i];71 72 // norm73 layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);74 75 if (hparams.is_recr(i)) {76 // ssm layers77 layer.ssm_in = create_tensor(tn(LLM_TENSOR_SSM_IN, "weight", i), {n_embd, d_in_proj}, 0);78 79 layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), {d_conv, d_inner + 2*n_group*d_state}, 0);80 layer.ssm_conv1d_b = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "bias", i), {d_inner + 2*n_group*d_state}, TENSOR_NOT_REQUIRED);81 82 layer.ssm_dt_b = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), {n_ssm_head}, 0);83 84 // no "weight" suffix for these85 layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A, i), {1, n_ssm_head}, 0);86 layer.ssm_d = create_tensor(tn(LLM_TENSOR_SSM_D, i), {1, n_ssm_head}, 0);87 88 layer.ssm_norm = create_tensor(tn(LLM_TENSOR_SSM_NORM, "weight", i), {d_inner / n_group, n_group}, 0);89 90 // out_proj91 layer.ssm_out = create_tensor(tn(LLM_TENSOR_SSM_OUT, "weight", i), {d_inner, n_embd}, 0);92 } else {93 // attention layers (with optional bias)94 const int64_t n_head_i = hparams.n_head(i);95 const int64_t n_embd_k_gqa_i = hparams.n_embd_k_gqa(i);96 const int64_t n_embd_v_gqa_i = hparams.n_embd_v_gqa(i);97 create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head_i, n_embd_k_gqa_i, n_embd_v_gqa_i, 0);98 layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head_i, n_embd}, 0);99 layer.wo_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);100 }101 102 // feed forward (w/ optional biases)103 if (n_expert > 0) {104 // MoE FFN105 layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);106 layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));107 layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);108 layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, TENSOR_NOT_REQUIRED);109 layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff, n_embd, n_expert}, 0);110 layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);111 112 // For Granite MoE Shared113 if (hparams.n_ff_shexp > 0) {114 layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, hparams.n_ff_shexp}, 0);115 layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, hparams.n_ff_shexp}, 0);116 layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {hparams.n_ff_shexp, n_embd}, 0);117 }118 } else {119 layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);120 layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));121 layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);122 layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);123 layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);124 layer.ffn_gate_b = create_tensor(tn(LLM_TENSOR_FFN_GATE, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);125 layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);126 layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);127 }128 }129}130 131std::unique_ptr<llm_graph_context> llama_model_granite_hybrid::build_arch_graph(const llm_graph_params & params) const {132 return std::make_unique<graph>(*this, params);133}134 135llama_model_granite_hybrid::graph::graph(const llama_model & model, const llm_graph_params & params) :136 llm_build_mamba_base(params) {137 const int64_t n_embd_head = hparams.n_embd_head_v();138 GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());139 140 ggml_tensor * cur;141 ggml_tensor * inpL;142 143 inpL = build_inp_embd(model.tok_embd);144 145 auto * inp = build_inp_mem_hybrid();146 147 ggml_tensor * inp_out_ids = build_inp_out_ids();148 149 // Positional embeddings populated if rope enabled150 ggml_tensor * inp_pos = nullptr;151 if (hparams.has_rope(0)) {152 inp_pos = build_inp_pos();153 }154 155 for (int il = 0; il < n_layer; ++il) {156 struct ggml_tensor * inpSA = inpL;157 158 // norm159 cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);160 cb(cur, "attn_norm", il);161 162 if (hparams.is_recr(il)) {163 // ssm layer //164 cur = build_mamba2_layer(inp->get_recr(), cur, model, ubatch, il);165 } else {166 // attention layer //167 cur = build_attention_layer(cur, inp_pos, inp->get_attn(), model, n_embd_head, il);168 }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 175 // ffn176 cur = build_layer_ffn(cur, inpSA, model, il);177 178 // input for next layer179 inpL = cur;180 }181 182 cur = inpL;183 184 cur = build_norm(cur, model.output_norm, NULL, 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 if (hparams.f_logit_scale) {194 cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_logit_scale);195 }196 cb(cur, "result_output", -1);197 res->t_logits = cur;198 199 ggml_build_forward_expand(gf, cur);200}201 202ggml_tensor * llama_model_granite_hybrid::graph::build_attention_layer(ggml_tensor * cur,203 ggml_tensor * inp_pos,204 llm_graph_input_attn_kv * inp_attn,205 const llama_model & model,206 const int64_t n_embd_head,207 const int il) {208 auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur, n_embd_head, hparams.n_head(il), hparams.n_head_kv(il), il);209 210 if (hparams.has_rope(il)) {211 ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);212 Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, rope_factors, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,213 ext_factor, attn_factor, beta_fast, beta_slow);214 215 Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, rope_factors, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,216 ext_factor, attn_factor, beta_fast, beta_slow);217 }218 219 cb(Qcur, "Qcur", il);220 cb(Kcur, "Kcur", il);221 cb(Vcur, "Vcur", il);222 223 const float kq_scale =224 hparams.f_attention_scale == 0.0f ? 1.0f / sqrtf(float(n_embd_head)) : hparams.f_attention_scale;225 cur = build_attn(inp_attn,226 model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,227 Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);228 cb(cur, "attn_out", il);229 return cur;230}231 232ggml_tensor * llama_model_granite_hybrid::graph::build_layer_ffn(ggml_tensor * cur,233 ggml_tensor * inpSA,234 const llama_model & model,235 const int il) {236 // For Granite architectures - scale residual237 if (hparams.f_residual_scale) {238 cur = ggml_scale(ctx0, cur, hparams.f_residual_scale);239 }240 ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);241 cb(ffn_inp, "ffn_inp", il);242 243 // feed-forward network (non-MoE)244 if (model.layers[il].ffn_gate_inp == nullptr) {245 cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);246 cb(cur, "ffn_norm", il);247 248 cur = build_ffn(cur,249 model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,250 model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,251 model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,252 NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);253 cb(cur, "ffn_out", il);254 255 } else {256 // MoE branch257 cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);258 cb(cur, "ffn_norm", il);259 260 ggml_tensor * moe_out =261 build_moe_ffn(cur,262 model.layers[il].ffn_gate_inp,263 model.layers[il].ffn_up_exps,264 model.layers[il].ffn_gate_exps,265 model.layers[il].ffn_down_exps,266 nullptr,267 n_expert, n_expert_used,268 LLM_FFN_SILU, true,269 hparams.expert_weights_scale,270 LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,271 il);272 cb(moe_out, "ffn_moe_out", il);273 274 // For Granite MoE Shared275 if (hparams.n_ff_shexp > 0) {276 ggml_tensor * ffn_shexp =277 build_ffn(cur,278 model.layers[il].ffn_up_shexp, NULL, NULL,279 model.layers[il].ffn_gate_shexp, NULL, NULL,280 model.layers[il].ffn_down_shexp, NULL, NULL,281 NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);282 cb(ffn_shexp, "ffn_shexp", il);283 284 cur = ggml_add(ctx0, moe_out, ffn_shexp);285 cb(cur, "ffn_out", il);286 } else {287 cur = moe_out;288 }289 }290 291 // For Granite architectures - scale residual292 if (hparams.f_residual_scale) {293 cur = ggml_scale(ctx0, cur, hparams.f_residual_scale);294 }295 cur = ggml_add(ctx0, cur, ffn_inp);296 cb(cur, "ffn_out", il);297 298 cur = build_cvec(cur, il);299 cb(cur, "l_out", il);300 301 return cur;302}303 