Felipe97/llama-cpp-compiled
01.1k
1#include "models.h"2 3// backbone of the pocket-tts CALM pipeline: the "text" side of a flow language model.4// it has no lm_head, the audio latents are produced by the flow net inside the mmproj5 6void llama_model_pockettts::load_arch_hparams(llama_model_loader & ml) {7 ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);8 9 switch (hparams.n_layer()) {10 case 6: type = LLM_TYPE_109M; break;11 case 24: type = LLM_TYPE_335M; break;12 default: type = LLM_TYPE_UNKNOWN;13 }14}15 16void llama_model_pockettts::load_arch_tensors(llama_model_loader &) {17 LLAMA_LOAD_LOCALS;18 19 tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);20 21 output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);22 output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0);23 // no output head, the logits are unused; reuse the embedding table so a sampler can still run24 output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);25 26 for (int i = 0; i < n_layer; ++i) {27 auto & layer = layers[i];28 29 layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);30 layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0);31 32 create_tensor_qkv(layer, i, n_embd, n_embd, n_embd_gqa, n_embd_gqa, TENSOR_NOT_REQUIRED);33 34 layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);35 36 layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);37 layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0);38 39 layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);40 layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);41 }42}43 44std::unique_ptr<llm_graph_context> llama_model_pockettts::build_arch_graph(const llm_graph_params & params) const {45 return std::make_unique<graph>(*this, params);46}47 48llama_model_pockettts::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {49 const int64_t n_embd_head = hparams.n_embd_head_v();50 51 GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());52 GGML_ASSERT(n_embd_head == n_rot);53 54 ggml_tensor * cur;55 ggml_tensor * inpL;56 57 inpL = build_inp_embd(model.tok_embd);58 59 ggml_tensor * inp_pos = build_inp_pos();60 61 auto * inp_attn = build_attn_inp_kv();62 63 ggml_tensor * inp_out_ids = build_inp_out_ids();64 65 for (int il = 0; il < n_layer; ++il) {66 cur = build_norm(inpL,67 model.layers[il].attn_norm,68 model.layers[il].attn_norm_b,69 LLM_NORM, il);70 cb(cur, "attn_norm", il);71 72 // self-attention73 {74 auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,75 n_embd_head, n_head, n_head_kv, il);76 77 Qcur = ggml_rope_ext(78 ctx0, Qcur, inp_pos, nullptr,79 n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,80 ext_factor, attn_factor, beta_fast, beta_slow81 );82 83 Kcur = ggml_rope_ext(84 ctx0, Kcur, inp_pos, nullptr,85 n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,86 ext_factor, attn_factor, beta_fast, beta_slow87 );88 89 cb(Qcur, "Qcur", il);90 cb(Kcur, "Kcur", il);91 cb(Vcur, "Vcur", il);92 93 cur = build_attn(inp_attn,94 model.layers[il].wo, NULL, model.layers[il].wo_s,95 Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);96 }97 98 if (il == n_layer - 1 && inp_out_ids) {99 cur = ggml_get_rows(ctx0, cur, inp_out_ids);100 inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);101 }102 103 ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);104 cb(ffn_inp, "ffn_inp", il);105 106 // FF107 {108 cur = build_norm(ffn_inp,109 model.layers[il].ffn_norm,110 model.layers[il].ffn_norm_b,111 LLM_NORM, il);112 cb(cur, "ffn_norm", il);113 114 cur = build_ffn(cur,115 model.layers[il].ffn_up, NULL, NULL,116 NULL, NULL, NULL,117 model.layers[il].ffn_down, NULL, NULL,118 NULL,119 LLM_FFN_GELU, LLM_FFN_SEQ, il);120 cb(cur, "ffn_out", il);121 }122 123 cur = ggml_add(ctx0, cur, ffn_inp);124 125 cur = build_cvec(cur, il);126 cb(cur, "l_out", il);127 128 // input for next layer129 inpL = cur;130 }131 132 cur = build_norm(inpL,133 model.output_norm,134 model.output_norm_b,135 LLM_NORM, -1);136 137 cb(cur, "result_norm", -1);138 res->t_embd = cur;139 140 cur = build_lora_mm(model.output, cur, model.output_s);141 142 cb(cur, "result_output", -1);143 res->t_logits = cur;144 145 ggml_build_forward_expand(gf, cur);146}147 