Brunobkr/llama.cpp_AlgMor24_github
ΩFFFΣLLIa • llama.cpp • AlgMor24 ██████╗ ███████╗███████╗███████╗██╗ ██╗ ██╗ █████╗ ██╔═══██╗██╔════╝██╔════╝██╔════╝██║ ██║ ██║██╔══██╗ ██║ ██║█████╗ █████╗ █████╗ ██║ ██║ ██║███████║ ██║ ██║██╔══╝ ██╔══╝ ██╔══╝ ██║ ██║ ██║██╔══██║ ╚██████╔╝██║ ██║ ███████╗███████╗███████╗██║██║ ██║ ╚═════╝ ╚═╝ ╚═╝ ╚══════╝╚══════╝╚══════╝╚═╝╚═╝ ╚═╝ High-Performance LLM / VLM Inference & Autonomous Agentic Ecosystem… See the full description on the dataset page: https://huggingface.co/datasets/Brunobkr/llama.cpp_AlgMor24_github.
03.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;20 21 // A layer is recurrent IFF the n_head_kv value is set to 022 for (uint32_t i = 0; i < hparams.n_layer(); ++i) {23 hparams.is_recr_impl[i] = hparams.n_head_kv(i) == 0;24 }25 26 ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);27 28 switch (hparams.n_embd) {29 case 768: type = LLM_TYPE_350M; break;30 case 1536: type = (hparams.n_ff() == 512 ? LLM_TYPE_7B_A1B : LLM_TYPE_1B); break;31 case 2048: case 2560: type = LLM_TYPE_3B; break;32 case 4096: type = LLM_TYPE_32B; break;33 default: type = LLM_TYPE_UNKNOWN;34 }35 36 // For Granite MoE Shared37 ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp, /* required */ false);38}39 40void llama_model_granite_hybrid::load_arch_tensors(llama_model_loader &) {41 LLAMA_LOAD_LOCALS;42 43 // mamba2 Mixer SSM params44 // NOTE: int64_t for tensor dimensions45 const int64_t d_conv = hparams.ssm_d_conv;46 const int64_t d_inner = hparams.ssm_d_inner;47 const int64_t d_state = hparams.ssm_d_state;48 const int64_t n_ssm_head = hparams.ssm_dt_rank;49 const int64_t n_group = hparams.ssm_n_group;50 const int64_t d_in_proj = 2*d_inner + 2*n_group*d_state + n_ssm_head;51 52 // only an expansion factor of 2 is supported for now53 GGML_ASSERT(2 * n_embd == d_inner);54 55 // embeddings56 tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);57 58 // output59 {60 output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);61 output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);62 // if output is NULL, init from the input tok embed, duplicated to allow offloading63 if (output == NULL) {64 output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);65 }66 }67 68 for (int i = 0; i < n_layer; ++i) {69 auto & layer = layers[i];70 71 // norm72 layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);73 74 if (hparams.is_recr(i)) {75 // ssm layers76 layer.ssm_in = create_tensor(tn(LLM_TENSOR_SSM_IN, "weight", i), {n_embd, d_in_proj}, 0);77 78 layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), {d_conv, d_inner + 2*n_group*d_state}, 0);79 layer.ssm_conv1d_b = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "bias", i), {d_inner + 2*n_group*d_state}, TENSOR_NOT_REQUIRED);80 81 layer.ssm_dt_b = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), {n_ssm_head}, 0);82 83 // no "weight" suffix for these84 layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A, i), {1, n_ssm_head}, 0);85 layer.ssm_d = create_tensor(tn(LLM_TENSOR_SSM_D, i), {1, n_ssm_head}, 0);86 87 layer.ssm_norm = create_tensor(tn(LLM_TENSOR_SSM_NORM, "weight", i), {d_inner / n_group, n_group}, 0);88 89 // out_proj90 layer.ssm_out = create_tensor(tn(LLM_TENSOR_SSM_OUT, "weight", i), {d_inner, n_embd}, 0);91 } else {92 // attention layers (with optional bias)93 const int64_t n_head_i = hparams.n_head(i);94 const int64_t n_embd_k_gqa_i = hparams.n_embd_k_gqa(i);95 const int64_t n_embd_v_gqa_i = hparams.n_embd_v_gqa(i);96 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);97 layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head_i, n_embd}, 0);98 layer.wo_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);99 }100 101 // feed forward (w/ optional biases)102 if (n_expert > 0) {103 // MoE FFN104 layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);105 layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));106 layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);107 layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, TENSOR_NOT_REQUIRED);108 layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff, n_embd, n_expert}, 0);109 layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);110 111 // For Granite MoE Shared112 if (hparams.n_ff_shexp > 0) {113 layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, hparams.n_ff_shexp}, 0);114 layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, hparams.n_ff_shexp}, 0);115 layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {hparams.n_ff_shexp, n_embd}, 0);116 }117 } else {118 layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);119 layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));120 layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);121 layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);122 layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);123 layer.ffn_gate_b = create_tensor(tn(LLM_TENSOR_FFN_GATE, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);124 layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);125 layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);126 }127 }128}129 130std::unique_ptr<llm_graph_context> llama_model_granite_hybrid::build_arch_graph(const llm_graph_params & params) const {131 return std::make_unique<graph>(*this, params);132}133 134llama_model_granite_hybrid::graph::graph(const llama_model & model, const llm_graph_params & params) :135 llm_build_mamba_base(params) {136 const int64_t n_embd_head = hparams.n_embd_head_v();137 GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());138 139 ggml_tensor * cur;140 ggml_tensor * inpL;141 142 inpL = build_inp_embd(model.tok_embd);143 144 auto * inp = build_inp_mem_hybrid();145 146 ggml_tensor * inp_out_ids = build_inp_out_ids();147 148 // Positional embeddings populated if rope enabled149 ggml_tensor * inp_pos = nullptr;150 if (hparams.rope_finetuned) {151 inp_pos = build_inp_pos();152 }153 154 for (int il = 0; il < n_layer; ++il) {155 struct ggml_tensor * inpSA = inpL;156 157 // norm158 cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);159 cb(cur, "attn_norm", il);160 161 if (hparams.is_recr(il)) {162 // ssm layer //163 cur = build_mamba2_layer(inp->get_recr(), cur, model, ubatch, il);164 } else {165 // attention layer //166 cur = build_attention_layer(cur, inp_pos, inp->get_attn(), model, n_embd_head, il);167 }168 169 if (il == n_layer - 1 && inp_out_ids) {170 cur = ggml_get_rows(ctx0, cur, inp_out_ids);171 inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);172 }173 174 // ffn175 cur = build_layer_ffn(cur, inpSA, model, il);176 177 // input for next layer178 inpL = cur;179 }180 181 cur = inpL;182 183 cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);184 185 cb(cur, "result_norm", -1);186 res->t_embd = cur;187 188 // lm_head189 cur = build_lora_mm(model.output, cur, model.output_s);190 191 // For Granite architectures - scale logits192 if (hparams.f_logit_scale) {193 cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_logit_scale);194 }195 cb(cur, "result_output", -1);196 res->t_logits = cur;197 198 ggml_build_forward_expand(gf, cur);199}200 201ggml_tensor * llama_model_granite_hybrid::graph::build_attention_layer(ggml_tensor * cur,202 ggml_tensor * inp_pos,203 llm_graph_input_attn_kv * inp_attn,204 const llama_model & model,205 const int64_t n_embd_head,206 const int il) {207 auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur, n_embd_head, hparams.n_head(il), hparams.n_head_kv(il), il);208 209 const bool use_rope = hparams.rope_finetuned;210 if (use_rope) {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 