/src/llama.cpp/src/models/gemma-embedding.cpp
Line | Count | Source |
1 | | #include "models.h" |
2 | | |
3 | | |
4 | | |
5 | | llm_build_gemma_embedding::llm_build_gemma_embedding(const llama_model & model, const llm_graph_params & params) : |
6 | 0 | llm_graph_context(params) { |
7 | 0 | const int64_t n_embd_head = hparams.n_embd_head_k; |
8 | |
|
9 | 0 | ggml_tensor * cur; |
10 | 0 | ggml_tensor * inpL; |
11 | |
|
12 | 0 | inpL = build_inp_embd(model.tok_embd); |
13 | | |
14 | | // important: do not normalize weights for raw embeddings input (i.e. encoded image emdeddings) |
15 | 0 | if (ubatch.token) { |
16 | 0 | inpL = ggml_scale(ctx0, inpL, sqrtf(n_embd)); |
17 | 0 | cb(inpL, "inp_scaled", -1); |
18 | 0 | } |
19 | | |
20 | | // inp_pos - contains the positions |
21 | 0 | ggml_tensor * inp_pos = build_inp_pos(); |
22 | |
|
23 | 0 | auto * inp_attn = build_attn_inp_no_cache(); |
24 | |
|
25 | 0 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
26 | |
|
27 | 0 | for (int il = 0; il < n_layer; ++il) { |
28 | 0 | const float freq_base_l = model.get_rope_freq_base(cparams, il); |
29 | 0 | const float freq_scale_l = model.get_rope_freq_scale(cparams, il); |
30 | | |
31 | | // norm |
32 | 0 | cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il); |
33 | 0 | cb(cur, "attn_norm", il); |
34 | | |
35 | | // self-attention |
36 | 0 | { |
37 | | // compute Q and K and RoPE them |
38 | 0 | ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur); |
39 | 0 | cb(Qcur, "Qcur", il); |
40 | |
|
41 | 0 | ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur); |
42 | 0 | cb(Kcur, "Kcur", il); |
43 | |
|
44 | 0 | ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur); |
45 | 0 | cb(Vcur, "Vcur", il); |
46 | |
|
47 | 0 | Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens); |
48 | 0 | Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens); |
49 | 0 | Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens); |
50 | |
|
51 | 0 | Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il); |
52 | 0 | cb(Qcur, "Qcur_normed", il); |
53 | |
|
54 | 0 | Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l, |
55 | 0 | ext_factor, attn_factor, beta_fast, beta_slow); |
56 | |
|
57 | 0 | Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il); |
58 | 0 | cb(Kcur, "Kcur_normed", il); |
59 | |
|
60 | 0 | Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l, |
61 | 0 | ext_factor, attn_factor, beta_fast, beta_slow); |
62 | |
|
63 | 0 | cb(Qcur, "Qcur", il); |
64 | 0 | cb(Kcur, "Kcur", il); |
65 | 0 | cb(Vcur, "Vcur", il); |
66 | | |
67 | | // ref: https://github.com/google/gemma_pytorch/blob/014acb7ac4563a5f77c76d7ff98f31b568c16508/gemma/model.py#L315 |
68 | 0 | Qcur = ggml_scale(ctx0, Qcur, hparams.f_attention_scale); |
69 | |
|
70 | 0 | cur = |
71 | 0 | build_attn(inp_attn, |
72 | 0 | model.layers[il].wo, NULL, |
73 | 0 | Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f, il); |
74 | 0 | } |
75 | |
|
76 | 0 | if (il == n_layer - 1 && inp_out_ids) { |
77 | 0 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
78 | 0 | inpL = ggml_get_rows(ctx0, inpL, inp_out_ids); |
79 | 0 | } |
80 | |
|
81 | 0 | cur = build_norm(cur, model.layers[il].attn_post_norm, NULL, LLM_NORM_RMS, il); |
82 | 0 | cb(cur, "attn_post_norm", il); |
83 | |
|
84 | 0 | ggml_tensor * sa_out = ggml_add(ctx0, cur, inpL); |
85 | 0 | cb(sa_out, "sa_out", il); |
86 | |
|
87 | 0 | cur = build_norm(sa_out, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il); |
88 | 0 | cb(cur, "ffn_norm", il); |
89 | | |
90 | | // feed-forward network |
91 | 0 | { |
92 | 0 | cur = build_ffn(cur, |
93 | 0 | model.layers[il].ffn_up, NULL, NULL, |
94 | 0 | model.layers[il].ffn_gate, NULL, NULL, |
95 | 0 | model.layers[il].ffn_down, NULL, NULL, |
96 | 0 | NULL, LLM_FFN_GELU, LLM_FFN_PAR, il); |
97 | 0 | cb(cur, "ffn_out", il); |
98 | 0 | } |
99 | |
|
100 | 0 | cur = build_norm(cur, model.layers[il].ffn_post_norm, NULL, LLM_NORM_RMS, -1); |
101 | 0 | cb(cur, "ffn_post_norm", -1); |
102 | |
|
103 | 0 | cur = ggml_add(ctx0, cur, sa_out); |
104 | |
|
105 | 0 | cur = build_cvec(cur, il); |
106 | 0 | cb(cur, "l_out", il); |
107 | | |
108 | | // input for next layer |
109 | 0 | inpL = cur; |
110 | 0 | } |
111 | |
|
112 | 0 | cur = inpL; |
113 | |
|
114 | 0 | cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1); |
115 | |
|
116 | 0 | cb(cur, "result_norm", -1); |
117 | 0 | res->t_embd = cur; |
118 | |
|
119 | 0 | ggml_build_forward_expand(gf, cur); |
120 | 0 | } |