/src/llama.cpp/src/models/plamo3.cpp
Line | Count | Source |
1 | | #include "models.h" |
2 | | |
3 | 0 | void llama_model_plamo3::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 | const bool found_swa = ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false); |
6 | 0 | if (found_swa && hparams.n_swa > 0) { |
7 | 0 | hparams.swa_type = LLAMA_SWA_TYPE_STANDARD; |
8 | 0 | ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA, hparams.rope_freq_base_train_swa, false); |
9 | 0 | uint32_t swa_period = 8; |
10 | 0 | ml.get_key_or_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, swa_period, false); |
11 | 0 | hparams.set_swa_pattern(swa_period); |
12 | 0 | } else { |
13 | 0 | hparams.swa_type = LLAMA_SWA_TYPE_NONE; |
14 | 0 | } |
15 | |
|
16 | 0 | switch (hparams.n_layer()) { |
17 | 0 | case 24: type = LLM_TYPE_2B; break; |
18 | 0 | default: type = LLM_TYPE_UNKNOWN; |
19 | 0 | } |
20 | 0 | } |
21 | | |
22 | 0 | void llama_model_plamo3::load_arch_tensors(llama_model_loader &) { |
23 | 0 | LLAMA_LOAD_LOCALS; |
24 | |
|
25 | 0 | const int64_t head_dim_q = hparams.n_embd_head_k(); |
26 | 0 | const int64_t head_dim_v = hparams.n_embd_head_v(); |
27 | |
|
28 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
29 | |
|
30 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
31 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
32 | 0 | if (output == NULL) { |
33 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
34 | 0 | } |
35 | |
|
36 | 0 | for (int i = 0; i < n_layer; ++i) { |
37 | 0 | auto & layer = layers[i]; |
38 | |
|
39 | 0 | const int64_t num_attention_heads = hparams.n_head(i); |
40 | 0 | const int64_t num_key_value_heads = hparams.n_head_kv(i); |
41 | 0 | const int64_t q_proj_dim = num_attention_heads * head_dim_q; |
42 | 0 | const int64_t k_proj_dim = num_key_value_heads * head_dim_q; |
43 | 0 | const int64_t v_proj_dim = num_key_value_heads * head_dim_v; |
44 | 0 | const int64_t n_ff_cur = hparams.n_ff(i); |
45 | |
|
46 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
47 | 0 | layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), |
48 | 0 | {n_embd,q_proj_dim + k_proj_dim + v_proj_dim}, 0); |
49 | 0 | layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {head_dim_q}, 0); |
50 | 0 | layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {head_dim_q}, 0); |
51 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {num_attention_heads * head_dim_v, n_embd}, 0); |
52 | 0 | layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, i), {n_embd}, 0); |
53 | |
|
54 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
55 | 0 | layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, i), {n_embd}, 0); |
56 | |
|
57 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff_cur * 2}, 0); |
58 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff_cur, n_embd}, 0); |
59 | 0 | } |
60 | 0 | } |
61 | | |
62 | 0 | std::unique_ptr<llm_graph_context> llama_model_plamo3::build_arch_graph(const llm_graph_params & params) const { |
63 | 0 | if (hparams.swa_type != LLAMA_SWA_TYPE_NONE) { |
64 | 0 | return std::make_unique<graph<true>> (*this, params); |
65 | 0 | } else { |
66 | 0 | return std::make_unique<graph<false>>(*this, params); |
67 | 0 | } |
68 | 0 | } |
69 | | |
70 | | template <bool iswa> |
71 | | llama_model_plamo3::graph<iswa>::graph(const llama_model & model, const llm_graph_params & params) : |
72 | 0 | llm_graph_context(params) { |
73 | 0 | const int64_t head_dim_q = hparams.n_embd_head_k(); |
74 | 0 | const int64_t head_dim_v = hparams.n_embd_head_v(); |
75 | |
|
76 | 0 | ggml_tensor * cur; |
77 | 0 | ggml_tensor * inpL = build_inp_embd(model.tok_embd); |
78 | 0 | ggml_tensor * inp_pos = build_inp_pos(); |
79 | |
|
80 | 0 | using inp_attn_type = std::conditional_t<iswa, llm_graph_input_attn_kv_iswa, llm_graph_input_attn_kv>; |
81 | 0 | inp_attn_type * inp_attn = nullptr; |
82 | |
|
83 | 0 | if constexpr (iswa) { |
84 | 0 | inp_attn = build_attn_inp_kv_iswa(); |
85 | 0 | } else { |
86 | 0 | inp_attn = build_attn_inp_kv(); |
87 | 0 | } |
88 | |
|
89 | 0 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
90 | |
|
91 | 0 | for (int il = 0; il < n_layer; ++il) { |
92 | 0 | ggml_tensor * residual = inpL; |
93 | |
|
94 | 0 | float freq_base_l = 0.0f; |
95 | 0 | float freq_scale_l = 0.0f; |
96 | 0 | if constexpr (iswa) { |
97 | 0 | freq_base_l = model.get_rope_freq_base (cparams, il); |
98 | 0 | freq_scale_l = model.get_rope_freq_scale(cparams, il); |
99 | 0 | } else { |
100 | 0 | freq_base_l = freq_base; |
101 | 0 | freq_scale_l = freq_scale; |
102 | 0 | } |
103 | |
|
104 | 0 | cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il); |
105 | 0 | cb(cur, "attn_norm", il); |
106 | |
|
107 | 0 | ggml_tensor * qkv = build_lora_mm(model.layers[il].wqkv, cur); |
108 | 0 | cb(cur, "wqkv", il); |
109 | |
|
110 | 0 | const int32_t n_head = hparams.n_head(il); |
111 | 0 | const int32_t n_head_kv = hparams.n_head_kv(il); |
112 | |
|
113 | 0 | const int64_t q_offset = 0; |
114 | 0 | const int64_t k_offset = head_dim_q * n_head; |
115 | 0 | const int64_t v_offset = k_offset + head_dim_q * n_head_kv; |
116 | |
|
117 | 0 | ggml_tensor * Qcur = ggml_view_3d(ctx0, qkv, head_dim_q, n_head, n_tokens, |
118 | 0 | head_dim_q * sizeof(float), qkv->nb[1], q_offset * ggml_element_size(qkv)); |
119 | 0 | ggml_tensor * Kcur = ggml_view_3d(ctx0, qkv, head_dim_q, n_head_kv, n_tokens, |
120 | 0 | head_dim_q * sizeof(float), qkv->nb[1], k_offset * ggml_element_size(qkv)); |
121 | 0 | ggml_tensor * Vcur = ggml_view_3d(ctx0, qkv, head_dim_v, n_head_kv, n_tokens, |
122 | 0 | head_dim_v * sizeof(float), qkv->nb[1], v_offset * ggml_element_size(qkv)); |
123 | |
|
124 | 0 | cb(Qcur, "Qcur", il); |
125 | 0 | cb(Kcur, "Kcur", il); |
126 | 0 | cb(Vcur, "Vcur", il); |
127 | |
|
128 | 0 | Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il); |
129 | 0 | cb(Qcur, "attn_q_norm", il); |
130 | 0 | Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il); |
131 | 0 | cb(Kcur, "attn_k_norm", il); |
132 | |
|
133 | 0 | Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, |
134 | 0 | n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l, |
135 | 0 | ext_factor, attn_factor, beta_fast, beta_slow); |
136 | 0 | Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, |
137 | 0 | n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l, |
138 | 0 | ext_factor, attn_factor, beta_fast, beta_slow); |
139 | |
|
140 | 0 | const float attn_scale = 1.0f / sqrtf(float(head_dim_q)); |
141 | |
|
142 | 0 | cur = build_attn(inp_attn, |
143 | 0 | model.layers[il].wo, NULL, model.layers[il].wo_s, |
144 | 0 | Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, attn_scale, il); |
145 | 0 | cb(cur, "attn_out", il); |
146 | |
|
147 | 0 | if (il == n_layer - 1 && inp_out_ids) { |
148 | 0 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
149 | 0 | residual = ggml_get_rows(ctx0, residual, inp_out_ids); |
150 | 0 | } |
151 | |
|
152 | 0 | cur = build_norm(cur, model.layers[il].attn_post_norm, NULL, LLM_NORM_RMS, il); |
153 | 0 | cb(cur, "attn_post_norm", il); |
154 | |
|
155 | 0 | cur = ggml_add(ctx0, cur, residual); |
156 | 0 | cb(cur, "attn_residual", il); |
157 | |
|
158 | 0 | residual = cur; |
159 | |
|
160 | 0 | cur = build_norm(cur, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il); |
161 | 0 | cb(cur, "ffn_norm", il); |
162 | |
|
163 | 0 | cur = build_ffn(cur, |
164 | 0 | model.layers[il].ffn_up, NULL, NULL, |
165 | 0 | NULL, NULL, NULL, |
166 | 0 | model.layers[il].ffn_down, NULL, NULL, |
167 | 0 | NULL, |
168 | 0 | LLM_FFN_SWIGLU, LLM_FFN_SEQ, il); |
169 | 0 | cb(cur, "ffn_out", il); |
170 | |
|
171 | 0 | cur = build_norm(cur, model.layers[il].ffn_post_norm, NULL, LLM_NORM_RMS, il); |
172 | 0 | cb(cur, "ffn_post_norm", il); |
173 | |
|
174 | 0 | cur = ggml_add(ctx0, cur, residual); |
175 | 0 | cb(cur, "ffn_residual", il); |
176 | |
|
177 | 0 | cur = build_cvec(cur, il); |
178 | 0 | cb(cur, "l_out", il); |
179 | | |
180 | | // input for next layer |
181 | 0 | inpL = cur; |
182 | 0 | } |
183 | |
|
184 | 0 | cur = inpL; |
185 | |
|
186 | 0 | cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1); |
187 | 0 | res->t_embd = cur; |
188 | |
|
189 | 0 | cur = build_lora_mm(model.output, cur, model.output_s); |
190 | 0 | res->t_logits = cur; |
191 | |
|
192 | 0 | ggml_build_forward_expand(gf, cur); |
193 | 0 | } Unexecuted instantiation: llama_model_plamo3::graph<false>::graph(llama_model const&, llm_graph_params const&) Unexecuted instantiation: llama_model_plamo3::graph<true>::graph(llama_model const&, llm_graph_params const&) |
194 | | |
195 | | // Explicit template instantiations |
196 | | template struct llama_model_plamo3::graph<false>; |
197 | | template struct llama_model_plamo3::graph<true>; |