aboutsummaryrefslogtreecommitdiff
path: root/llama.cpp/src/models/gemma.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'llama.cpp/src/models/gemma.cpp')
-rw-r--r--llama.cpp/src/models/gemma.cpp112
1 files changed, 112 insertions, 0 deletions
diff --git a/llama.cpp/src/models/gemma.cpp b/llama.cpp/src/models/gemma.cpp
new file mode 100644
index 0000000..4893d9a
--- /dev/null
+++ b/llama.cpp/src/models/gemma.cpp
@@ -0,0 +1,112 @@
1#include "models.h"
2
3
4llm_build_gemma::llm_build_gemma(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
5 const int64_t n_embd_head = hparams.n_embd_head_v;
6
7 ggml_tensor * cur;
8 ggml_tensor * inpL;
9
10 inpL = build_inp_embd(model.tok_embd);
11
12 inpL = ggml_scale(ctx0, inpL, sqrtf(n_embd));
13 cb(inpL, "inp_scaled", -1);
14
15 // inp_pos - contains the positions
16 ggml_tensor * inp_pos = build_inp_pos();
17
18 auto * inp_attn = build_attn_inp_kv();
19
20 ggml_tensor * inp_out_ids = build_inp_out_ids();
21
22 for (int il = 0; il < n_layer; ++il) {
23 // norm
24 cur = build_norm(inpL,
25 model.layers[il].attn_norm, NULL,
26 LLM_NORM_RMS, il);
27 cb(cur, "attn_norm", il);
28
29 // self-attention
30 {
31 // compute Q and K and RoPE them
32 ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
33 cb(Qcur, "Qcur", il);
34
35 ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
36 cb(Kcur, "Kcur", il);
37
38 ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
39 cb(Vcur, "Vcur", il);
40
41 Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
42 Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
43 Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
44
45 Qcur = ggml_rope_ext(
46 ctx0, Qcur, inp_pos, nullptr,
47 n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
48 ext_factor, attn_factor, beta_fast, beta_slow);
49
50 Kcur = ggml_rope_ext(
51 ctx0, Kcur, inp_pos, nullptr,
52 n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
53 ext_factor, attn_factor, beta_fast, beta_slow);
54
55 cb(Qcur, "Qcur", il);
56 cb(Kcur, "Kcur", il);
57 cb(Vcur, "Vcur", il);
58
59 Qcur = ggml_scale(ctx0, Qcur, 1.0f / sqrtf(float(n_embd_head)));
60 cb(Qcur, "Qcur_scaled", il);
61
62 cur = build_attn(inp_attn,
63 model.layers[il].wo, NULL,
64 Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f, il);
65 }
66 if (il == n_layer - 1 && inp_out_ids) {
67 cur = ggml_get_rows(ctx0, cur, inp_out_ids);
68 inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
69 }
70 ggml_tensor * sa_out = ggml_add(ctx0, cur, inpL);
71 cb(sa_out, "sa_out", il);
72
73 cur = build_norm(sa_out,
74 model.layers[il].ffn_norm, NULL,
75 LLM_NORM_RMS, il);
76 cb(cur, "ffn_norm", il);
77
78 // feed-forward network
79 {
80 cur = build_ffn(cur,
81 model.layers[il].ffn_up, NULL, NULL,
82 model.layers[il].ffn_gate, NULL, NULL,
83 model.layers[il].ffn_down, NULL, NULL,
84 NULL,
85 LLM_FFN_GELU, LLM_FFN_PAR, il);
86 cb(cur, "ffn_out", il);
87 }
88 cur = ggml_add(ctx0, cur, sa_out);
89
90 cur = build_cvec(cur, il);
91 cb(cur, "l_out", il);
92
93 // input for next layer
94 inpL = cur;
95 }
96 cur = inpL;
97
98 cur = build_norm(cur,
99 model.output_norm, NULL,
100 LLM_NORM_RMS, -1);
101
102 cb(cur, "result_norm", -1);
103 res->t_embd = cur;
104
105 // lm_head
106 cur = build_lora_mm(model.output, cur);
107
108 cb(cur, "result_output", -1);
109 res->t_logits = cur;
110
111 ggml_build_forward_expand(gf, cur);
112}