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.
03k
1#include "models.h"2#include "../llama-memory-hybrid-iswa.h"3#include "../llama-memory-hybrid.h"4 5void llama_model_lfm2::load_arch_hparams(llama_model_loader & ml) {6 ml.get_key(LLM_KV_SHORTCONV_L_CACHE, hparams.n_shortconv_l_cache);7 ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);8 9 for (uint32_t il = 0; il < hparams.n_layer(); ++il) {10 hparams.is_recr_impl[il] = hparams.n_head_kv(il) == 0;11 }12 13 hparams.n_layer_dense_lead = hparams.n_layer();14 15 switch (hparams.n_ff()) {16 case 2560: type = LLM_TYPE_230M; break;17 case 4608: type = LLM_TYPE_350M; break;18 case 6912: type = LLM_TYPE_700M; break;19 case 8192: type = LLM_TYPE_1_2B; break;20 case 10752: type = LLM_TYPE_2_6B; break;21 default: type = LLM_TYPE_UNKNOWN;22 }23 24 if (const auto is_swa = ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false); is_swa && hparams.n_swa > 0) {25 hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;26 for (uint32_t il = 0; il < hparams.n_layer(); ++il) {27 hparams.is_swa_impl[il] = !hparams.is_recr_impl[il];28 }29 }30}31 32void llama_model_lfm2::load_arch_tensors(llama_model_loader &) {33 LLAMA_LOAD_LOCALS;34 35 tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);36 37 output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM_LFM2, "weight"), {n_embd}, 0);38 output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);39 40 if (output == NULL) {41 output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);42 }43 44 for (int i = 0; i < n_layer; ++i) {45 auto & layer = layers[i];46 47 const bool is_moe_layer = i >= static_cast<int>(hparams.n_layer_dense_lead);48 49 // ffn/moe is same for transformer and conv layers50 layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);51 if (is_moe_layer) {52 GGML_ASSERT(n_expert && n_expert_used);53 layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);54 layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, hparams.n_ff_exp, n_expert}, 0);55 layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {hparams.n_ff_exp, n_embd, n_expert}, 0);56 layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, hparams.n_ff_exp, n_expert}, 0);57 layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, 0);58 } else { // dense59 layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);60 layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);61 layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);62 }63 64 // for operator_norm65 layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);66 67 if (!hparams.is_recr(i)) {68 layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);69 layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);70 GGML_ASSERT(n_embd_v_gqa == n_embd_k_gqa);71 72 create_tensor_qkv(layer, i, n_embd, n_embd, hparams.n_embd_k_gqa(i), hparams.n_embd_v_gqa(i), 0);73 74 layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);75 } else {76 layer.shortconv.conv = create_tensor(tn(LLM_TENSOR_SHORTCONV_CONV, "weight", i), {hparams.n_shortconv_l_cache, n_embd}, 0);77 layer.shortconv.in_proj = create_tensor(tn(LLM_TENSOR_SHORTCONV_INPROJ, "weight", i), {n_embd, 3 * n_embd}, 0);78 layer.shortconv.out_proj = create_tensor(tn(LLM_TENSOR_SHORTCONV_OUTPROJ, "weight", i), {n_embd, n_embd}, 0);79 }80 }81 82 // for LFM2-ColBert-350M83 dense_2_out_layers = create_tensor(tn(LLM_TENSOR_DENSE_2_OUT, "weight"), {n_embd, hparams.n_embd_out()}, TENSOR_NOT_REQUIRED);84 dense_2_out_layers_b = create_tensor(tn(LLM_TENSOR_DENSE_2_OUT, "bias"), {hparams.n_embd_out() }, TENSOR_NOT_REQUIRED);85}86 87std::unique_ptr<llm_graph_context> llama_model_lfm2::build_arch_graph(const llm_graph_params & params) const {88 if (hparams.swa_type == LLAMA_SWA_TYPE_STANDARD) {89 return std::make_unique<graph<true>>(*this, params);90 } else {91 return std::make_unique<graph<false>>(*this, params);92 }93}94 95template <bool iswa>96llama_model_lfm2::graph<iswa>::graph(const llama_model & model, const llm_graph_params & params) :97 llm_graph_context(params) {98 using inp_hybrid_type = std::conditional_t<iswa, llm_graph_input_mem_hybrid_iswa, llm_graph_input_mem_hybrid>;99 using inp_attn_type = std::conditional_t<iswa, llm_graph_input_attn_kv_iswa, llm_graph_input_attn_kv>;100 using mem_hybrid_ctx = std::conditional_t<iswa, llama_memory_hybrid_iswa_context, llama_memory_hybrid_context>;101 102 // lambda helpers for readability103 auto build_dense_feed_forward = [&model, this](ggml_tensor * cur, int il) -> ggml_tensor * {104 GGML_ASSERT(!model.layers[il].ffn_up_b);105 GGML_ASSERT(!model.layers[il].ffn_gate_b);106 GGML_ASSERT(!model.layers[il].ffn_down_b);107 return build_ffn(cur,108 model.layers[il].ffn_up, NULL, NULL,109 model.layers[il].ffn_gate, NULL, NULL,110 model.layers[il].ffn_down, NULL, NULL,111 NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);112 };113 auto build_moe_feed_forward = [&model, this](ggml_tensor * cur, int il) -> ggml_tensor * {114 return build_moe_ffn(cur,115 model.layers[il].ffn_gate_inp,116 model.layers[il].ffn_up_exps,117 model.layers[il].ffn_gate_exps,118 model.layers[il].ffn_down_exps,119 model.layers[il].ffn_exp_probs_b,120 n_expert, n_expert_used,121 LLM_FFN_SILU, true,122 hparams.expert_weights_scale,123 static_cast<llama_expert_gating_func_type>(hparams.expert_gating_func),124 il);125 };126 auto build_attn_block = [&model, this](ggml_tensor * cur,127 ggml_tensor * inp_pos,128 inp_attn_type * inp_attn,129 int il) -> ggml_tensor * {130 GGML_ASSERT(hparams.n_embd_v_gqa(il) == hparams.n_embd_k_gqa(il));131 const auto n_embd_head = hparams.n_embd_head_v();132 const auto n_head_kv = hparams.n_head_kv(il);133 134 auto [q, k, v] = build_qkv(model.layers[il], cur,135 n_embd_head, n_head, n_head_kv, il);136 137 // qk norm138 q = build_norm(q, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);139 cb(q, "model.layers.{}.self_attn.q_layernorm", il);140 k = build_norm(k, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);141 cb(k, "model.layers.{}.self_attn.k_layernorm", il);142 143 // RoPE144 q = ggml_rope_ext(ctx0, q, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, ext_factor,145 attn_factor, beta_fast, beta_slow);146 k = ggml_rope_ext(ctx0, k, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, ext_factor,147 attn_factor, beta_fast, beta_slow);148 149 cur = build_attn(inp_attn,150 model.layers[il].wo, NULL, model.layers[il].wo_s,151 q, k, v, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);152 153 cb(cur, "model.layers.{}.self_attn.out_proj", il);154 155 return cur;156 };157 auto build_shortconv_block = [&model, this](ggml_tensor * cur,158 llm_graph_input_rs * inp_recr,159 int il) -> ggml_tensor * {160 const auto * mctx_cur = static_cast<const mem_hybrid_ctx *>(mctx)->get_recr();161 const uint32_t kv_head = mctx_cur->get_head();162 const int64_t n_seq_tokens = ubatch.n_seq_tokens;163 const int64_t n_seqs = ubatch.n_seqs;164 GGML_ASSERT(n_seqs != 0);165 GGML_ASSERT(ubatch.equal_seqs());166 GGML_ASSERT(ubatch.n_tokens == n_seq_tokens * n_seqs);167 168 GGML_ASSERT(hparams.n_shortconv_l_cache > 1);169 const uint32_t d_conv = hparams.n_shortconv_l_cache - 1;170 171 // {n_embd, n_tokens} => {n_embd, n_seq_tokens, n_seqs}172 cur = ggml_reshape_3d(ctx0, cur, cur->ne[0], n_seq_tokens, n_seqs);173 174 auto * bcx = build_lora_mm(model.layers[il].shortconv.in_proj, cur);175 cb(bcx, "model.layers.{}.conv.in_proj", il);176 177 constexpr auto n_chunks = 3;178 GGML_ASSERT(bcx->ne[0] % n_chunks == 0);179 const auto chunk_size = bcx->ne[0] / n_chunks;180 auto * b = ggml_view_3d(ctx0, bcx, chunk_size, bcx->ne[1], bcx->ne[2], bcx->nb[1], bcx->nb[2],181 0 * chunk_size * ggml_element_size(bcx));182 auto * c = ggml_view_3d(ctx0, bcx, chunk_size, bcx->ne[1], bcx->ne[2], bcx->nb[1], bcx->nb[2],183 1 * chunk_size * ggml_element_size(bcx));184 auto * x = ggml_view_3d(ctx0, bcx, chunk_size, bcx->ne[1], bcx->ne[2], bcx->nb[1], bcx->nb[2],185 2 * chunk_size * ggml_element_size(bcx));186 187 auto * bx = ggml_transpose(ctx0, ggml_mul(ctx0, b, x));188 189 // read conv state190 auto * conv_state = mctx_cur->get_r_l(il);191 auto * conv_rs = build_rs(inp_recr, conv_state, hparams.n_embd_r(), n_seqs);192 auto * conv = ggml_reshape_3d(ctx0, conv_rs, d_conv, hparams.n_embd, n_seqs);193 194 // causal prepends the state, non-causal pads symmetrically for a centered window195 if (hparams.causal_attn) {196 bx = ggml_concat(ctx0, conv, bx, 0);197 } else {198 const int64_t pad = (hparams.n_shortconv_l_cache - 1) / 2;199 auto * left = ggml_cont(ctx0,200 ggml_view_3d(ctx0, conv, pad, hparams.n_embd, n_seqs, conv->nb[1], conv->nb[2], (d_conv - pad) * conv->nb[0]));201 bx = ggml_pad_ext(ctx0, ggml_concat(ctx0, left, bx, 0), 0, pad, 0, 0, 0, 0, 0, 0);202 }203 GGML_ASSERT(bx->ne[0] > conv->ne[0]);204 205 // last d_conv columns is a new conv state206 auto * new_conv = ggml_view_3d(ctx0, bx, conv->ne[0], bx->ne[1], bx->ne[2], bx->nb[1], bx->nb[2],207 (bx->ne[0] - conv->ne[0]) * ggml_element_size(bx));208 GGML_ASSERT(ggml_are_same_shape(conv, new_conv));209 210 // write new conv conv state211 ggml_build_forward_expand(gf, ggml_cpy(ctx0, new_conv,212 ggml_view_1d(ctx0, conv_state, ggml_nelements(new_conv),213 kv_head * d_conv * n_embd * ggml_element_size(new_conv))));214 215 auto * conv_kernel = model.layers[il].shortconv.conv;216 auto * conv_out = ggml_ssm_conv(ctx0, bx, conv_kernel);217 cb(conv_out, "model.layers.{}.conv.conv", il);218 219 auto * y = ggml_mul(ctx0, c, conv_out);220 y = build_lora_mm(model.layers[il].shortconv.out_proj, y);221 cb(y, "model.layers.{}.conv.out_proj", il);222 // {n_embd, n_seq_tokens, n_seqs} => {n_embd, n_tokens}223 y = ggml_reshape_2d(ctx0, y, y->ne[0], n_seq_tokens * n_seqs);224 225 return y;226 };227 228 // actual graph construction starts here229 ggml_tensor * cur = build_inp_embd(model.tok_embd);230 cb(cur, "model.embed_tokens", -1);231 232 ggml_build_forward_expand(gf, cur);233 234 inp_hybrid_type * inp_hybrid = nullptr;235 if constexpr (iswa) {236 inp_hybrid = build_inp_mem_hybrid_iswa();237 } else {238 inp_hybrid = build_inp_mem_hybrid();239 }240 241 ggml_tensor * inp_pos = build_inp_pos();242 ggml_tensor * inp_out_ids = build_inp_out_ids();243 244 for (int il = 0; il < n_layer; ++il) {245 const bool is_moe_layer = il >= static_cast<int>(hparams.n_layer_dense_lead);246 247 auto * prev_cur = cur;248 cur = build_norm(cur, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);249 cb(cur, "model.layers.{}.operator_norm", il);250 251 cur = hparams.is_recr(il) ? build_shortconv_block(cur, inp_hybrid->get_recr(), il) :252 build_attn_block(cur, inp_pos, inp_hybrid->get_attn(), il);253 254 if (il == n_layer - 1 && inp_out_ids) {255 cur = ggml_get_rows(ctx0, cur, inp_out_ids);256 prev_cur = ggml_get_rows(ctx0, prev_cur, inp_out_ids);257 }258 259 cur = ggml_add(ctx0, prev_cur, cur);260 261 auto * ffn_norm_out = build_norm(cur, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);262 cb(ffn_norm_out, "model.layers.{}.ffn_norm", il);263 264 ggml_tensor * ffn_out =265 is_moe_layer ? build_moe_feed_forward(ffn_norm_out, il) : build_dense_feed_forward(ffn_norm_out, il);266 cb(ffn_norm_out, "model.layers.{}.ffn_out", il);267 268 cur = ggml_add(ctx0, cur, ffn_out);269 270 cur = build_cvec(cur, il);271 cb(cur, "l_out", il);272 }273 274 cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);275 cb(cur, "result_norm", -1);276 res->t_embd = cur;277 278 if (!cparams.embeddings) {279 cur = build_lora_mm(model.output, cur, model.output_s);280 cb(cur, "result_output", -1);281 282 res->t_logits = cur;283 }284 285 ggml_build_forward_expand(gf, cur);286}287 288// Explicit template instantiations289template struct llama_model_lfm2::graph<true>;290template struct llama_model_lfm2::graph<false>;291 