echodict/llama.cpp
version https://git-lfs.github.com/spec/v1 oid sha256:cfc44b7ba25614df70e6b65e3341cae0310163bd32fd31a6b928a542df433faf size 30786
0610
1#include "models.h"2 3#include "../llama-memory-hybrid-iswa.h"4#include "../llama-memory-hybrid.h"5 6template <bool iswa>7llm_build_lfm2<iswa>::llm_build_lfm2(const llama_model & model, const llm_graph_params & params) :8 llm_graph_context(params) {9 using inp_hybrid_type = std::conditional_t<iswa, llm_graph_input_mem_hybrid_iswa, llm_graph_input_mem_hybrid>;10 using inp_attn_type = std::conditional_t<iswa, llm_graph_input_attn_kv_iswa, llm_graph_input_attn_kv>;11 using mem_hybrid_ctx = std::conditional_t<iswa, llama_memory_hybrid_iswa_context, llama_memory_hybrid_context>;12 13 // lambda helpers for readability14 auto build_dense_feed_forward = [&model, this](ggml_tensor * cur, int il) -> ggml_tensor * {15 GGML_ASSERT(!model.layers[il].ffn_up_b);16 GGML_ASSERT(!model.layers[il].ffn_gate_b);17 GGML_ASSERT(!model.layers[il].ffn_down_b);18 return build_ffn(cur,19 model.layers[il].ffn_up, NULL, NULL,20 model.layers[il].ffn_gate, NULL, NULL,21 model.layers[il].ffn_down, NULL, NULL,22 NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);23 };24 auto build_moe_feed_forward = [&model, this](ggml_tensor * cur, int il) -> ggml_tensor * {25 return build_moe_ffn(cur,26 model.layers[il].ffn_gate_inp,27 model.layers[il].ffn_up_exps,28 model.layers[il].ffn_gate_exps,29 model.layers[il].ffn_down_exps,30 model.layers[il].ffn_exp_probs_b,31 n_expert, n_expert_used,32 LLM_FFN_SILU, true,33 hparams.expert_weights_scale,34 static_cast<llama_expert_gating_func_type>(hparams.expert_gating_func),35 il);36 };37 auto build_attn_block = [&model, this](ggml_tensor * cur,38 ggml_tensor * inp_pos,39 inp_attn_type * inp_attn,40 int il) -> ggml_tensor * {41 GGML_ASSERT(hparams.n_embd_v_gqa(il) == hparams.n_embd_k_gqa(il));42 const auto n_embd_head = hparams.n_embd_head_v();43 const auto n_head_kv = hparams.n_head_kv(il);44 45 auto [q, k, v] = build_qkv(model.layers[il], cur,46 n_embd_head, n_head, n_head_kv, il);47 48 // qk norm49 q = build_norm(q, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);50 cb(q, "model.layers.{}.self_attn.q_layernorm", il);51 k = build_norm(k, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);52 cb(k, "model.layers.{}.self_attn.k_layernorm", il);53 54 // RoPE55 q = ggml_rope_ext(ctx0, q, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, ext_factor,56 attn_factor, beta_fast, beta_slow);57 k = ggml_rope_ext(ctx0, k, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, ext_factor,58 attn_factor, beta_fast, beta_slow);59 60 cur = build_attn(inp_attn,61 model.layers[il].wo, NULL, model.layers[il].wo_s,62 q, k, v, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);63 64 cb(cur, "model.layers.{}.self_attn.out_proj", il);65 66 return cur;67 };68 auto build_shortconv_block = [&model, this](ggml_tensor * cur,69 llm_graph_input_rs * inp_recr,70 int il) -> ggml_tensor * {71 const auto * mctx_cur = static_cast<const mem_hybrid_ctx *>(mctx)->get_recr();72 const uint32_t kv_head = mctx_cur->get_head();73 const int64_t n_seq_tokens = ubatch.n_seq_tokens;74 const int64_t n_seqs = ubatch.n_seqs;75 GGML_ASSERT(n_seqs != 0);76 GGML_ASSERT(ubatch.equal_seqs());77 GGML_ASSERT(ubatch.n_tokens == n_seq_tokens * n_seqs);78 79 GGML_ASSERT(hparams.n_shortconv_l_cache > 1);80 const uint32_t d_conv = hparams.n_shortconv_l_cache - 1;81 82 // {n_embd, n_tokens} => {n_embd, n_seq_tokens, n_seqs}83 cur = ggml_reshape_3d(ctx0, cur, cur->ne[0], n_seq_tokens, n_seqs);84 85 auto * bcx = build_lora_mm(model.layers[il].shortconv.in_proj, cur);86 cb(bcx, "model.layers.{}.conv.in_proj", il);87 88 constexpr auto n_chunks = 3;89 GGML_ASSERT(bcx->ne[0] % n_chunks == 0);90 const auto chunk_size = bcx->ne[0] / n_chunks;91 auto * b = ggml_view_3d(ctx0, bcx, chunk_size, bcx->ne[1], bcx->ne[2], bcx->nb[1], bcx->nb[2],92 0 * chunk_size * ggml_element_size(bcx));93 auto * c = ggml_view_3d(ctx0, bcx, chunk_size, bcx->ne[1], bcx->ne[2], bcx->nb[1], bcx->nb[2],94 1 * chunk_size * ggml_element_size(bcx));95 auto * x = ggml_view_3d(ctx0, bcx, chunk_size, bcx->ne[1], bcx->ne[2], bcx->nb[1], bcx->nb[2],96 2 * chunk_size * ggml_element_size(bcx));97 98 auto * bx = ggml_transpose(ctx0, ggml_mul(ctx0, b, x));99 100 // read conv state101 auto * conv_state = mctx_cur->get_r_l(il);102 auto * conv_rs = build_rs(inp_recr, conv_state, hparams.n_embd_r(), n_seqs);103 auto * conv = ggml_reshape_3d(ctx0, conv_rs, d_conv, hparams.n_embd, n_seqs);104 105 bx = ggml_concat(ctx0, conv, bx, 0);106 GGML_ASSERT(bx->ne[0] > conv->ne[0]);107 108 // last d_conv columns is a new conv state109 auto * new_conv = ggml_view_3d(ctx0, bx, conv->ne[0], bx->ne[1], bx->ne[2], bx->nb[1], bx->nb[2],110 (bx->ne[0] - conv->ne[0]) * ggml_element_size(bx));111 GGML_ASSERT(ggml_are_same_shape(conv, new_conv));112 113 // write new conv conv state114 ggml_build_forward_expand(gf, ggml_cpy(ctx0, new_conv,115 ggml_view_1d(ctx0, conv_state, ggml_nelements(new_conv),116 kv_head * d_conv * n_embd * ggml_element_size(new_conv))));117 118 auto * conv_kernel = model.layers[il].shortconv.conv;119 auto * conv_out = ggml_ssm_conv(ctx0, bx, conv_kernel);120 cb(conv_out, "model.layers.{}.conv.conv", il);121 122 auto * y = ggml_mul(ctx0, c, conv_out);123 y = build_lora_mm(model.layers[il].shortconv.out_proj, y);124 cb(y, "model.layers.{}.conv.out_proj", il);125 // {n_embd, n_seq_tokens, n_seqs} => {n_embd, n_tokens}126 y = ggml_reshape_2d(ctx0, y, y->ne[0], n_seq_tokens * n_seqs);127 128 return y;129 };130 131 // actual graph construction starts here132 ggml_tensor * cur = build_inp_embd(model.tok_embd);133 cb(cur, "model.embed_tokens", -1);134 135 ggml_build_forward_expand(gf, cur);136 137 inp_hybrid_type * inp_hybrid = nullptr;138 if constexpr (iswa) {139 inp_hybrid = build_inp_mem_hybrid_iswa();140 } else {141 inp_hybrid = build_inp_mem_hybrid();142 }143 144 ggml_tensor * inp_pos = build_inp_pos();145 ggml_tensor * inp_out_ids = build_inp_out_ids();146 147 for (int il = 0; il < n_layer; ++il) {148 const bool is_moe_layer = il >= static_cast<int>(hparams.n_layer_dense_lead);149 150 auto * prev_cur = cur;151 cur = build_norm(cur, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);152 cb(cur, "model.layers.{}.operator_norm", il);153 154 cur = hparams.is_recurrent(il) ? build_shortconv_block(cur, inp_hybrid->get_recr(), il) :155 build_attn_block(cur, inp_pos, inp_hybrid->get_attn(), il);156 157 if (il == n_layer - 1 && inp_out_ids) {158 cur = ggml_get_rows(ctx0, cur, inp_out_ids);159 prev_cur = ggml_get_rows(ctx0, prev_cur, inp_out_ids);160 }161 162 cur = ggml_add(ctx0, prev_cur, cur);163 164 auto * ffn_norm_out = build_norm(cur, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);165 cb(ffn_norm_out, "model.layers.{}.ffn_norm", il);166 167 ggml_tensor * ffn_out =168 is_moe_layer ? build_moe_feed_forward(ffn_norm_out, il) : build_dense_feed_forward(ffn_norm_out, il);169 cb(ffn_norm_out, "model.layers.{}.ffn_out", il);170 171 cur = ggml_add(ctx0, cur, ffn_out);172 173 cur = build_cvec(cur, il);174 cb(cur, "l_out", il);175 }176 177 cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);178 cb(cur, "result_norm", -1);179 res->t_embd = cur;180 181 cur = build_lora_mm(model.output, cur);182 cb(cur, "result_output", -1);183 184 res->t_logits = cur;185 186 ggml_build_forward_expand(gf, cur);187}188 189// Explicit template instantiations190template struct llm_build_lfm2<true>;191template struct llm_build_lfm2<false>;192 