/src/llama.cpp/src/models/cogvlm.cpp
Line | Count | Source |
1 | | #include "models.h" |
2 | | |
3 | | llm_build_cogvlm::llm_build_cogvlm(const llama_model & model, const llm_graph_params & params) : |
4 | 0 | llm_graph_context(params) { |
5 | 0 | const int64_t n_embd_head = hparams.n_embd_head_v; |
6 | 0 | float kq_scale = 1.0f / sqrtf(float(n_embd_head)); |
7 | |
|
8 | 0 | GGML_ASSERT(n_embd_head == hparams.n_embd_head_k); |
9 | 0 | GGML_ASSERT(n_embd_head == hparams.n_rot); |
10 | |
|
11 | 0 | ggml_tensor *inpL, *cur; |
12 | 0 | inpL = build_inp_embd(model.tok_embd); |
13 | |
|
14 | 0 | ggml_tensor * inp_pos = build_inp_pos(); |
15 | |
|
16 | 0 | auto * inp_attn = build_attn_inp_kv(); |
17 | | |
18 | | // check ubatch to see if we have input tokens (text) |
19 | | // or an input embedding vector (image) |
20 | 0 | bool is_text; |
21 | 0 | if (ubatch.token) { |
22 | 0 | is_text = true; |
23 | 0 | } else { |
24 | 0 | is_text = false; |
25 | 0 | } |
26 | |
|
27 | 0 | for (int il = 0; il < n_layer; ++il) { |
28 | | // get either the text or image weight tensors |
29 | 0 | ggml_tensor *wqkv, *wo; |
30 | 0 | ggml_tensor *ffn_gate, *ffn_down, *ffn_up; |
31 | |
|
32 | 0 | if (is_text) { |
33 | 0 | wqkv = model.layers[il].wqkv; |
34 | 0 | wo = model.layers[il].wo; |
35 | 0 | ffn_gate = model.layers[il].ffn_gate; |
36 | 0 | ffn_down = model.layers[il].ffn_down; |
37 | 0 | ffn_up = model.layers[il].ffn_up; |
38 | 0 | } else { |
39 | 0 | wqkv = model.layers[il].visexp_attn_wqkv; |
40 | 0 | wo = model.layers[il].visexp_attn_wo; |
41 | 0 | ffn_gate = model.layers[il].visexp_ffn_gate; |
42 | 0 | ffn_down = model.layers[il].visexp_ffn_down; |
43 | 0 | ffn_up = model.layers[il].visexp_ffn_up; |
44 | 0 | } |
45 | |
|
46 | 0 | ggml_tensor * inpSA = inpL; |
47 | 0 | cur = build_norm(inpSA, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il); |
48 | | |
49 | | // build self attention |
50 | 0 | { |
51 | 0 | ggml_tensor * qkv = build_lora_mm(wqkv, cur); |
52 | | |
53 | | // split qkv into Q, K, V along the first dimension |
54 | 0 | ggml_tensor * Qcur = |
55 | 0 | ggml_view_3d(ctx0, qkv, n_embd_head, n_head, n_tokens, n_embd_head * sizeof(float), qkv->nb[1], 0); |
56 | 0 | ggml_tensor * Kcur = ggml_view_3d(ctx0, qkv, n_embd_head, n_head_kv, n_tokens, n_embd_head * sizeof(float), |
57 | 0 | qkv->nb[1], n_embd * ggml_element_size(qkv)); |
58 | 0 | ggml_tensor * Vcur = ggml_view_3d(ctx0, qkv, n_embd_head, n_head_kv, n_tokens, n_embd_head * sizeof(float), |
59 | 0 | qkv->nb[1], 2 * n_embd * ggml_element_size(qkv)); |
60 | |
|
61 | 0 | Qcur = ggml_rope(ctx0, Qcur, inp_pos, n_embd_head, rope_type); |
62 | 0 | Kcur = ggml_rope(ctx0, Kcur, inp_pos, n_embd_head, rope_type); |
63 | |
|
64 | 0 | cur = build_attn(inp_attn, |
65 | 0 | wo, nullptr, |
66 | 0 | Qcur, Kcur, Vcur, |
67 | 0 | nullptr, nullptr, nullptr, |
68 | 0 | kq_scale, il); |
69 | 0 | cb(cur, "attn_out", il); |
70 | 0 | } |
71 | |
|
72 | 0 | ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA); |
73 | 0 | cb(ffn_inp, "ffn_inp", il); |
74 | |
|
75 | 0 | cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il); |
76 | 0 | cb(cur, "ffn_norm", il); |
77 | |
|
78 | 0 | cur = build_ffn(cur, |
79 | 0 | ffn_up, NULL, NULL, |
80 | 0 | ffn_gate, NULL, NULL, |
81 | 0 | ffn_down, NULL, NULL, |
82 | 0 | NULL, LLM_FFN_SILU, LLM_FFN_PAR, il); |
83 | |
|
84 | 0 | cur = ggml_add(ctx0, cur, ffn_inp); |
85 | 0 | cb(cur, "ffn_out", il); |
86 | |
|
87 | 0 | inpL = cur; |
88 | 0 | } |
89 | |
|
90 | 0 | cur = inpL; |
91 | |
|
92 | 0 | cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1); |
93 | 0 | cb(cur, "result_norm", -1); |
94 | 0 | res->t_embd = cur; |
95 | |
|
96 | 0 | cur = build_lora_mm(model.output, cur); |
97 | 0 | cb(cur, "result_output", -1); |
98 | 0 | res->t_logits = cur; |
99 | 0 | ggml_build_forward_expand(gf, cur); |
100 | 0 | } |