/src/llama.cpp/src/models/plm.cpp
Line | Count | Source |
1 | | #include "models.h" |
2 | | |
3 | 0 | void llama_model_plm::load_arch_hparams(llama_model_loader & ml) { |
4 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
5 | 0 | ml.get_key(LLM_KV_ATTENTION_KV_LORA_RANK, hparams.n_lora_kv); |
6 | |
|
7 | 0 | switch (hparams.n_layer()) { |
8 | 0 | case 32: type = LLM_TYPE_1_8B; break; |
9 | 0 | default: type = LLM_TYPE_UNKNOWN; |
10 | 0 | } |
11 | 0 | } |
12 | | |
13 | 0 | void llama_model_plm::load_arch_tensors(llama_model_loader &) { |
14 | 0 | LLAMA_LOAD_LOCALS; |
15 | |
|
16 | 0 | const int64_t n_embd_head_qk_rope = hparams.n_rot(); |
17 | 0 | const int64_t n_embd_head_qk_nope = hparams.n_embd_head_k() - hparams.n_rot(); |
18 | 0 | const int64_t kv_lora_rank = hparams.n_lora_kv; |
19 | |
|
20 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
21 | | |
22 | | // output |
23 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
24 | | // output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0); |
25 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
26 | |
|
27 | 0 | for (int i = 0; i < n_layer; ++i) { |
28 | 0 | auto & layer = layers[i]; |
29 | |
|
30 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
31 | |
|
32 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0); |
33 | 0 | layer.wkv_a_mqa = create_tensor(tn(LLM_TENSOR_ATTN_KV_A_MQA, "weight", i), {n_embd, kv_lora_rank + (n_embd_head_qk_rope)}, 0); |
34 | 0 | layer.attn_kv_a_norm = create_tensor(tn(LLM_TENSOR_ATTN_KV_A_NORM, "weight", i), {kv_lora_rank}, 0); |
35 | 0 | layer.wkv_b = create_tensor(tn(LLM_TENSOR_ATTN_KV_B, "weight", i), {kv_lora_rank, n_head * (n_embd_head_qk_nope + n_embd_head_v)}, 0); |
36 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_head * ( n_embd_head_v), n_embd}, 0); |
37 | |
|
38 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
39 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
40 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
41 | 0 | } |
42 | 0 | } |
43 | | |
44 | 0 | std::unique_ptr<llm_graph_context> llama_model_plm::build_arch_graph(const llm_graph_params & params) const { |
45 | 0 | return std::make_unique<graph>(*this, params); |
46 | 0 | } |
47 | | |
48 | 0 | llama_model_plm::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) { |
49 | 0 | const float kq_scale = 1.0f/sqrtf(float(hparams.n_embd_head_k())); |
50 | |
|
51 | 0 | const uint32_t n_embd_head_qk_rope = hparams.n_rot(); |
52 | 0 | const uint32_t n_embd_head_qk_nope = hparams.n_embd_head_k() - hparams.n_rot(); |
53 | |
|
54 | 0 | const uint32_t kv_lora_rank = hparams.n_lora_kv; |
55 | |
|
56 | 0 | ggml_tensor * cur; |
57 | 0 | ggml_tensor * inpL; |
58 | | |
59 | | // {n_embd, n_tokens} |
60 | 0 | inpL = build_inp_embd(model.tok_embd); |
61 | | |
62 | | // inp_pos - contains the positions |
63 | 0 | ggml_tensor * inp_pos = build_inp_pos(); |
64 | |
|
65 | 0 | auto * inp_attn = build_attn_inp_kv(); |
66 | |
|
67 | 0 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
68 | |
|
69 | 0 | for (int il = 0; il < n_layer; ++il) { |
70 | 0 | ggml_tensor * inpSA = inpL; |
71 | | |
72 | | // norm |
73 | 0 | cur = build_norm(inpL, |
74 | 0 | model.layers[il].attn_norm, NULL, |
75 | 0 | LLM_NORM_RMS, il); |
76 | 0 | cb(cur, "attn_norm", il); |
77 | | |
78 | | // self_attention |
79 | 0 | { |
80 | 0 | ggml_tensor * q = NULL; |
81 | 0 | q = ggml_mul_mat(ctx0, model.layers[il].wq, cur); |
82 | 0 | cb(q, "q", il); |
83 | | |
84 | | // split into {n_head * n_embd_head_qk_nope, n_tokens} |
85 | 0 | ggml_tensor * q_nope = ggml_view_3d(ctx0, q, n_embd_head_qk_nope, n_head, n_tokens, |
86 | 0 | ggml_row_size(q->type, hparams.n_embd_head_k()), |
87 | 0 | ggml_row_size(q->type, hparams.n_embd_head_k() * n_head), |
88 | 0 | 0); |
89 | 0 | cb(q_nope, "q_nope", il); |
90 | | |
91 | | // and {n_head * n_embd_head_qk_rope, n_tokens} |
92 | 0 | ggml_tensor * q_pe = ggml_view_3d(ctx0, q, n_embd_head_qk_rope, n_head, n_tokens, |
93 | 0 | ggml_row_size(q->type, hparams.n_embd_head_k()), |
94 | 0 | ggml_row_size(q->type, hparams.n_embd_head_k() * n_head), |
95 | 0 | ggml_row_size(q->type, n_embd_head_qk_nope)); |
96 | 0 | cb(q_pe, "q_pe", il); |
97 | | |
98 | | // {n_embd, kv_lora_rank + n_embd_head_qk_rope} * {n_embd, n_tokens} -> {kv_lora_rank + n_embd_head_qk_rope, n_tokens} |
99 | 0 | ggml_tensor * kv_pe_compresseed = ggml_mul_mat(ctx0, model.layers[il].wkv_a_mqa, cur); |
100 | 0 | cb(kv_pe_compresseed, "kv_pe_compresseed", il); |
101 | | |
102 | | // split into {kv_lora_rank, n_tokens} |
103 | 0 | ggml_tensor * kv_compressed = ggml_view_2d(ctx0, kv_pe_compresseed, kv_lora_rank, n_tokens, |
104 | 0 | kv_pe_compresseed->nb[1], |
105 | 0 | 0); |
106 | 0 | cb(kv_compressed, "kv_compressed", il); |
107 | | |
108 | | // and {n_embd_head_qk_rope, n_tokens} |
109 | 0 | ggml_tensor * k_pe = ggml_view_3d(ctx0, kv_pe_compresseed, n_embd_head_qk_rope, 1, n_tokens, |
110 | 0 | kv_pe_compresseed->nb[1], |
111 | 0 | kv_pe_compresseed->nb[1], |
112 | 0 | ggml_row_size(kv_pe_compresseed->type, kv_lora_rank)); |
113 | 0 | cb(k_pe, "k_pe", il); |
114 | |
|
115 | 0 | kv_compressed = build_norm(kv_compressed, |
116 | 0 | model.layers[il].attn_kv_a_norm, NULL, |
117 | 0 | LLM_NORM_RMS, il); |
118 | 0 | cb(kv_compressed, "kv_compressed", il); |
119 | | |
120 | | // {kv_lora_rank, n_head * (n_embd_head_qk_nope + n_embd_head_v)} * {kv_lora_rank, n_tokens} -> {n_head * (n_embd_head_qk_nope + n_embd_head_v), n_tokens} |
121 | 0 | ggml_tensor * kv = ggml_mul_mat(ctx0, model.layers[il].wkv_b, kv_compressed); |
122 | 0 | cb(kv, "kv", il); |
123 | | |
124 | | // split into {n_head * n_embd_head_qk_nope, n_tokens} |
125 | 0 | ggml_tensor * k_nope = ggml_view_3d(ctx0, kv, n_embd_head_qk_nope, n_head, n_tokens, |
126 | 0 | ggml_row_size(kv->type, n_embd_head_qk_nope + hparams.n_embd_head_v()), |
127 | 0 | ggml_row_size(kv->type, n_head * (n_embd_head_qk_nope + hparams.n_embd_head_v())), |
128 | 0 | 0); |
129 | 0 | cb(k_nope, "k_nope", il); |
130 | | |
131 | | // and {n_head * n_embd_head_v, n_tokens} |
132 | 0 | ggml_tensor * v_states = ggml_view_3d(ctx0, kv, hparams.n_embd_head_v(), n_head, n_tokens, |
133 | 0 | ggml_row_size(kv->type, (n_embd_head_qk_nope + hparams.n_embd_head_v())), |
134 | 0 | ggml_row_size(kv->type, (n_embd_head_qk_nope + hparams.n_embd_head_v())*n_head), |
135 | 0 | ggml_row_size(kv->type, (n_embd_head_qk_nope))); |
136 | 0 | cb(v_states, "v_states", il); |
137 | |
|
138 | 0 | v_states = ggml_cont(ctx0, v_states); |
139 | 0 | cb(v_states, "v_states", il); |
140 | |
|
141 | 0 | v_states = ggml_view_2d(ctx0, v_states, hparams.n_embd_head_v() * n_head, n_tokens, |
142 | 0 | ggml_row_size(kv->type, hparams.n_embd_head_v() * n_head), |
143 | 0 | 0); |
144 | 0 | cb(v_states, "v_states", il); |
145 | |
|
146 | 0 | q_pe = ggml_rope_ext( |
147 | 0 | ctx0, q_pe, inp_pos, nullptr, |
148 | 0 | n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
149 | 0 | ext_factor, attn_factor, beta_fast, beta_slow |
150 | 0 | ); |
151 | 0 | cb(q_pe, "q_pe", il); |
152 | | |
153 | | // shared RoPE key |
154 | 0 | k_pe = ggml_rope_ext( |
155 | 0 | ctx0, k_pe, inp_pos, nullptr, |
156 | 0 | n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
157 | 0 | ext_factor, attn_factor, beta_fast, beta_slow |
158 | 0 | ); |
159 | 0 | cb(k_pe, "k_pe", il); |
160 | |
|
161 | 0 | ggml_tensor * q_states = ggml_concat(ctx0, q_nope, q_pe, 0); |
162 | 0 | cb(q_states, "q_states", il); |
163 | |
|
164 | 0 | ggml_tensor * k_states = ggml_concat(ctx0, k_nope, ggml_repeat(ctx0, k_pe, q_pe), 0); |
165 | 0 | cb(k_states, "k_states", il); |
166 | |
|
167 | 0 | cur = build_attn(inp_attn, |
168 | 0 | model.layers[il].wo, NULL, model.layers[il].wo_s, |
169 | 0 | q_states, k_states, v_states, nullptr, nullptr, nullptr, kq_scale, il); |
170 | 0 | } |
171 | 0 | if (il == n_layer - 1 && inp_out_ids) { |
172 | 0 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
173 | 0 | inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids); |
174 | 0 | } |
175 | 0 | ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA); |
176 | 0 | cb(ffn_inp, "ffn_inp", il); |
177 | |
|
178 | 0 | cur = build_norm(ffn_inp, |
179 | 0 | model.layers[il].ffn_norm, NULL, |
180 | 0 | LLM_NORM_RMS, il); |
181 | 0 | cb(cur, "ffn_norm", il); |
182 | |
|
183 | 0 | cur = build_ffn(cur, |
184 | 0 | model.layers[il].ffn_up, NULL, NULL, |
185 | 0 | NULL, NULL, NULL, |
186 | 0 | model.layers[il].ffn_down, NULL, NULL, |
187 | 0 | NULL, |
188 | 0 | LLM_FFN_RELU_SQR, LLM_FFN_SEQ, il); |
189 | 0 | cb(cur, "ffn_out", il); |
190 | |
|
191 | 0 | cur = ggml_add(ctx0, cur, ffn_inp); |
192 | |
|
193 | 0 | cur = build_cvec(cur, il); |
194 | 0 | cb(cur, "l_out", il); |
195 | | |
196 | | // input for next layer |
197 | 0 | inpL = cur; |
198 | 0 | } |
199 | 0 | cur = inpL; |
200 | |
|
201 | 0 | cur = build_norm(cur, |
202 | 0 | model.output_norm, NULL, |
203 | 0 | LLM_NORM_RMS, -1); |
204 | |
|
205 | 0 | cb(cur, "result_norm", -1); |
206 | 0 | res->t_embd = cur; |
207 | |
|
208 | 0 | cur = build_lora_mm(model.output, cur, model.output_s); |
209 | |
|
210 | 0 | cb(cur, "result_output", -1); |
211 | 0 | res->t_logits = cur; |
212 | |
|
213 | 0 | ggml_build_forward_expand(gf, cur); |
214 | 0 | } |