Felipe97/llama-cpp-compiled
01.1k
1#include "models.h"2 3void llama_model_qwen2vl::load_arch_hparams(llama_model_loader & ml) {4 ml.get_key_or_arr(LLM_KV_ROPE_DIMENSION_SECTIONS, hparams.rope_sections, 4, true);5}6// fall through7 8void llama_model_qwen2vl::load_arch_tensors(llama_model_loader &) {9 LLAMA_LOAD_LOCALS;10 11 tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);12 13 // output14 output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);15 output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);16 output_b = create_tensor(tn(LLM_TENSOR_OUTPUT, "bias"), {n_vocab}, TENSOR_NOT_REQUIRED);17 // if output is NULL, init from the input tok embed18 if (output == NULL) {19 output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);20 }21 22 for (int i = 0; i < n_layer; ++i) {23 auto & layer = layers[i];24 25 layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);26 27 create_tensor_qkv(layer, i, n_embd, n_embd, n_embd_gqa, n_embd_gqa, 0);28 layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);29 30 layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);31 32 layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);33 layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);34 layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);35 }36}37 38std::unique_ptr<llm_graph_context> llama_model_qwen2vl::build_arch_graph(const llm_graph_params & params) const {39 return std::make_unique<graph>(*this, params);40}41 42llama_model_qwen2vl::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {43 const int64_t n_embd_head = hparams.n_embd_head_v();44 45 GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());46 GGML_ASSERT(n_embd_head == n_rot);47 48 ggml_tensor * cur;49 ggml_tensor * inpL;50 51 inpL = build_inp_embd(model.tok_embd);52 53 // inp_pos - contains the positions54 ggml_tensor * inp_pos = build_inp_pos();55 56 auto * inp_attn = build_attn_inp_kv();57 58 int sections[4];59 std::copy(std::begin(hparams.rope_sections), std::begin(hparams.rope_sections) + 4, sections);60 61 ggml_tensor * inp_out_ids = build_inp_out_ids();62 63 for (int il = 0; il < n_layer; ++il) {64 ggml_tensor * inpSA = inpL;65 66 // norm67 cur = build_norm(inpL,68 model.layers[il].attn_norm, NULL,69 LLM_NORM_RMS, il);70 cb(cur, "attn_norm", il);71 72 // self-attention73 {74 // compute Q and K and RoPE them75 auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,76 n_embd_head, n_head, n_head_kv, il);77 78 Qcur = ggml_rope_multi(79 ctx0, Qcur, inp_pos, nullptr,80 n_rot, sections, rope_type, n_ctx_orig, freq_base, freq_scale,81 ext_factor, attn_factor, beta_fast, beta_slow82 );83 84 Kcur = ggml_rope_multi(85 ctx0, Kcur, inp_pos, nullptr,86 n_rot, sections, rope_type, n_ctx_orig, freq_base, freq_scale,87 ext_factor, attn_factor, beta_fast, beta_slow88 );89 90 cb(Qcur, "Qcur", il);91 cb(Kcur, "Kcur", il);92 cb(Vcur, "Vcur", il);93 94 cur = build_attn(inp_attn,95 model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,96 Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);97 }98 if (il == n_layer - 1 && inp_out_ids) {99 cur = ggml_get_rows(ctx0, cur, inp_out_ids);100 inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);101 }102 ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);103 cb(ffn_inp, "ffn_inp", il);104 105 // feed-forward network106 cur = build_norm(ffn_inp,107 model.layers[il].ffn_norm, NULL,108 LLM_NORM_RMS, il);109 cb(cur, "ffn_norm", il);110 111 cur = build_ffn(cur,112 model.layers[il].ffn_up, NULL, NULL,113 model.layers[il].ffn_gate, NULL, NULL,114 model.layers[il].ffn_down, NULL, NULL,115 NULL,116 LLM_FFN_SILU, LLM_FFN_PAR, il);117 cb(cur, "ffn_out", il);118 119 cur = ggml_add(ctx0, cur, ffn_inp);120 121 cur = build_cvec(cur, il);122 cb(cur, "l_out", il);123 124 // input for next layer125 inpL = cur;126 }127 cur = inpL;128 129 cur = build_norm(cur,130 model.output_norm, NULL,131 LLM_NORM_RMS, -1);132 133 cb(cur, "result_norm", -1);134 res->t_embd = cur;135 136 // lm_head137 cur = build_lora_mm(model.output, cur, model.output_s);138 139 cb(cur, "result_output", -1);140 res->t_logits = cur;141 142 ggml_build_forward_expand(gf, cur);143}144 