MCPcopy Create free account
hub / github.com/appdevforall/CodeOnTheGo / llm_build_cogvlm

Method llm_build_cogvlm

subprojects/llama.cpp/src/models/cogvlm.cpp:3–102  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1#include "models.h"
2
3llm_build_cogvlm::llm_build_cogvlm(const llama_model & model, const llm_graph_params & params) :
4 llm_graph_context(params) {
5 const int64_t n_embd_head = hparams.n_embd_head_v;
6 const float kq_scale = 1.0f / sqrtf(float(n_embd_head));
7
8 GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
9 GGML_ASSERT(n_embd_head == hparams.n_rot);
10
11 ggml_tensor * inpL;
12 ggml_tensor * cur;
13
14 inpL = build_inp_embd(model.tok_embd);
15
16 ggml_tensor * inp_pos = build_inp_pos();
17
18 auto * inp_attn = build_attn_inp_kv();
19
20 // check ubatch to see if we have input tokens (text)
21 // or an input embedding vector (image)
22 bool is_text;
23 if (ubatch.token) {
24 is_text = true;
25 } else {
26 is_text = false;
27 }
28
29 for (int il = 0; il < n_layer; ++il) {
30 // get either the text or image weight tensors
31 ggml_tensor *wqkv, *wo;
32 ggml_tensor *ffn_gate, *ffn_down, *ffn_up;
33
34 if (is_text) {
35 wqkv = model.layers[il].wqkv;
36 wo = model.layers[il].wo;
37 ffn_gate = model.layers[il].ffn_gate;
38 ffn_down = model.layers[il].ffn_down;
39 ffn_up = model.layers[il].ffn_up;
40 } else {
41 wqkv = model.layers[il].visexp_attn_wqkv;
42 wo = model.layers[il].visexp_attn_wo;
43 ffn_gate = model.layers[il].visexp_ffn_gate;
44 ffn_down = model.layers[il].visexp_ffn_down;
45 ffn_up = model.layers[il].visexp_ffn_up;
46 }
47
48 ggml_tensor * inpSA = inpL;
49 cur = build_norm(inpSA, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
50
51 // build self attention
52 {
53 ggml_tensor * qkv = build_lora_mm(wqkv, cur);
54
55 // split qkv into Q, K, V along the first dimension
56 ggml_tensor * Qcur =
57 ggml_view_3d(ctx0, qkv, n_embd_head, n_head, n_tokens, n_embd_head * sizeof(float), qkv->nb[1], 0);
58 ggml_tensor * Kcur = ggml_view_3d(ctx0, qkv, n_embd_head, n_head_kv, n_tokens, n_embd_head * sizeof(float),
59 qkv->nb[1], n_embd * ggml_element_size(qkv));
60 ggml_tensor * Vcur = ggml_view_3d(ctx0, qkv, n_embd_head, n_head_kv, n_tokens, n_embd_head * sizeof(float),

Callers

nothing calls this directly

Calls 5

ggml_view_3dFunction · 0.85
ggml_element_sizeFunction · 0.85
ggml_ropeFunction · 0.85
ggml_addFunction · 0.85

Tested by

no test coverage detected