/src/llama.cpp/src/models/olmo2.cpp
Line | Count | Source |
1 | | #include "models.h" |
2 | | |
3 | 0 | void llama_model_olmo2::load_arch_hparams(llama_model_loader & ml) { |
4 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
5 | |
|
6 | 0 | const bool found_swa = ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false); |
7 | 0 | if (found_swa && hparams.n_swa > 0) { |
8 | 0 | hparams.swa_type = LLAMA_SWA_TYPE_STANDARD; |
9 | 0 | uint32_t swa_period = 4; |
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 | |
|
13 | 0 | hparams.rope_freq_base_train_swa = hparams.rope_freq_base_train; |
14 | 0 | hparams.rope_freq_scale_train_swa = 1.0; // See olmo2.cpp |
15 | 0 | ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA, hparams.rope_freq_base_train_swa, false); |
16 | 0 | } else { |
17 | 0 | hparams.swa_type = LLAMA_SWA_TYPE_NONE; |
18 | 0 | } |
19 | |
|
20 | 0 | switch (hparams.n_layer()) { |
21 | 0 | case 16: type = LLM_TYPE_1B; break; |
22 | 0 | case 32: type = LLM_TYPE_7B; break; |
23 | 0 | case 40: type = LLM_TYPE_13B; break; |
24 | 0 | case 64: type = LLM_TYPE_32B; break; |
25 | 0 | default: type = LLM_TYPE_UNKNOWN; |
26 | 0 | } |
27 | 0 | } |
28 | | |
29 | 0 | void llama_model_olmo2::load_arch_tensors(llama_model_loader &) { |
30 | 0 | LLAMA_LOAD_LOCALS; |
31 | |
|
32 | 0 | const int64_t n_embd_head = n_embd / n_head; |
33 | |
|
34 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
35 | | |
36 | | // output |
37 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
38 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0); |
39 | |
|
40 | 0 | for (int i = 0; i < n_layer; ++i) { |
41 | 0 | auto & layer = layers[i]; |
42 | |
|
43 | 0 | create_tensor_qkv(layer, i, n_embd, n_embd, n_embd_gqa, n_embd_gqa, 0); |
44 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
45 | 0 | layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd}, 0); |
46 | 0 | layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_head_kv * n_embd_head}, 0); |
47 | 0 | layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0); |
48 | |
|
49 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
50 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
51 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
52 | 0 | layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0); |
53 | 0 | } |
54 | 0 | } |
55 | | |
56 | 0 | std::unique_ptr<llm_graph_context> llama_model_olmo2::build_arch_graph(const llm_graph_params & params) const { |
57 | 0 | if (hparams.swa_type == LLAMA_SWA_TYPE_STANDARD) { |
58 | 0 | return std::make_unique<graph<true>>(*this, params); |
59 | 0 | } else { |
60 | 0 | return std::make_unique<graph<false>>(*this, params); |
61 | 0 | } |
62 | 0 | } |
63 | | |
64 | | template <bool iswa> |
65 | 0 | llama_model_olmo2::graph<iswa>::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) { |
66 | 0 | const int64_t n_embd_head = hparams.n_embd_head_v(); |
67 | |
|
68 | 0 | GGML_ASSERT(n_embd_head == hparams.n_embd_head_k()); |
69 | 0 | GGML_ASSERT(n_embd_head == n_rot); |
70 | |
|
71 | 0 | ggml_tensor * cur; |
72 | 0 | ggml_tensor * inpL; |
73 | |
|
74 | 0 | inpL = build_inp_embd(model.tok_embd); |
75 | | |
76 | | // inp_pos - contains the positions |
77 | 0 | ggml_tensor * inp_pos = build_inp_pos(); |
78 | |
|
79 | 0 | using inp_attn_type = std::conditional_t<iswa, llm_graph_input_attn_kv_iswa, llm_graph_input_attn_kv>; |
80 | 0 | inp_attn_type * inp_attn = nullptr; |
81 | |
|
82 | 0 | if constexpr (iswa) { |
83 | 0 | inp_attn = build_attn_inp_kv_iswa(); |
84 | 0 | } else { |
85 | 0 | inp_attn = build_attn_inp_kv(); |
86 | 0 | } |
87 | 0 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
88 | |
|
89 | 0 | for (int il = 0; il < n_layer; ++il) { |
90 | 0 | ggml_tensor * inpSA = inpL; |
91 | |
|
92 | 0 | cur = inpL; |
93 | | |
94 | | // self_attention |
95 | 0 | { |
96 | | // compute Q and K and RoPE them |
97 | 0 | ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur); |
98 | 0 | cb(Qcur, "Qcur", il); |
99 | |
|
100 | 0 | ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur); |
101 | 0 | cb(Kcur, "Kcur", il); |
102 | |
|
103 | 0 | ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur); |
104 | 0 | cb(Vcur, "Vcur", il); |
105 | |
|
106 | 0 | Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, |
107 | 0 | LLM_NORM_RMS, il); |
108 | 0 | cb(Qcur, "Qcur_normed", il); |
109 | |
|
110 | 0 | Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, |
111 | 0 | LLM_NORM_RMS, il); |
112 | 0 | cb(Kcur, "Kcur_normed", il); |
113 | |
|
114 | 0 | Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens); |
115 | 0 | Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens); |
116 | 0 | Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens); |
117 | |
|
118 | 0 | const bool is_swa = hparams.is_swa(il); |
119 | |
|
120 | 0 | if (is_swa) { |
121 | | // For sliding window layers, Olmo3 use regular rope with no yarn rope scaling. |
122 | | // This is achieved here by setting freq_scale and attn_factor to 1. |
123 | | // We also set ext_factor to 0 to avoid a few unnecessary computations. |
124 | 0 | Qcur = ggml_rope_ext( |
125 | 0 | ctx0, Qcur, inp_pos, nullptr, |
126 | 0 | n_rot, rope_type, n_ctx_orig, freq_base, 1.0, |
127 | 0 | 0.0, 1.0, beta_fast, beta_slow |
128 | 0 | ); |
129 | |
|
130 | 0 | Kcur = ggml_rope_ext( |
131 | 0 | ctx0, Kcur, inp_pos, nullptr, |
132 | 0 | n_rot, rope_type, n_ctx_orig, freq_base, 1.0, |
133 | 0 | 0.0, 1.0, beta_fast, beta_slow |
134 | 0 | ); |
135 | 0 | } else { |
136 | 0 | Qcur = ggml_rope_ext( |
137 | 0 | ctx0, Qcur, inp_pos, nullptr, |
138 | 0 | n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
139 | 0 | ext_factor, attn_factor, beta_fast, beta_slow |
140 | 0 | ); |
141 | |
|
142 | 0 | Kcur = ggml_rope_ext( |
143 | 0 | ctx0, Kcur, inp_pos, nullptr, |
144 | 0 | n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
145 | 0 | ext_factor, attn_factor, beta_fast, beta_slow |
146 | 0 | ); |
147 | 0 | } |
148 | 0 | cb(Qcur, "Qcur", il); |
149 | 0 | cb(Kcur, "Kcur", il); |
150 | 0 | cb(Vcur, "Vcur", il); |
151 | |
|
152 | 0 | cur = build_attn(inp_attn, |
153 | 0 | model.layers[il].wo, NULL, model.layers[il].wo_s, |
154 | 0 | Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il); |
155 | 0 | } |
156 | 0 | if (il == n_layer - 1 && inp_out_ids) { |
157 | 0 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
158 | 0 | inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids); |
159 | 0 | } |
160 | 0 | cur = build_norm(cur, |
161 | 0 | model.layers[il].attn_post_norm, NULL, |
162 | 0 | LLM_NORM_RMS, il); |
163 | 0 | cb(cur, "attn_post_norm", il); |
164 | |
|
165 | 0 | ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA); |
166 | 0 | cb(ffn_inp, "ffn_inp", il); |
167 | | |
168 | | // feed-forward network |
169 | 0 | cur = build_ffn(ffn_inp, |
170 | 0 | model.layers[il].ffn_up, NULL, NULL, |
171 | 0 | model.layers[il].ffn_gate, NULL, NULL, |
172 | 0 | model.layers[il].ffn_down, NULL, NULL, |
173 | 0 | NULL, |
174 | 0 | LLM_FFN_SILU, LLM_FFN_PAR, il); |
175 | 0 | cb(cur, "ffn_out", il); |
176 | |
|
177 | 0 | cur = build_norm(cur, |
178 | 0 | model.layers[il].ffn_post_norm, NULL, |
179 | 0 | LLM_NORM_RMS, -1); |
180 | 0 | cb(cur, "ffn_post_norm", -1); |
181 | |
|
182 | 0 | cur = ggml_add(ctx0, cur, ffn_inp); |
183 | 0 | cb(cur, "ffn_out", il); |
184 | |
|
185 | 0 | cur = build_cvec(cur, il); |
186 | 0 | cb(cur, "l_out", il); |
187 | | |
188 | | // input for next layer |
189 | 0 | inpL = cur; |
190 | 0 | } |
191 | 0 | cur = inpL; |
192 | |
|
193 | 0 | cur = build_norm(cur, |
194 | 0 | model.output_norm, NULL, |
195 | 0 | LLM_NORM_RMS, -1); |
196 | |
|
197 | 0 | cb(cur, "result_norm", -1); |
198 | 0 | res->t_embd = cur; |
199 | | |
200 | | // lm_head |
201 | 0 | cur = build_lora_mm(model.output, cur, model.output_s); |
202 | |
|
203 | 0 | cb(cur, "result_output", -1); |
204 | 0 | res->t_logits = cur; |
205 | |
|
206 | 0 | ggml_build_forward_expand(gf, cur); |
207 | 0 | } Unexecuted instantiation: llama_model_olmo2::graph<false>::graph(llama_model const&, llm_graph_params const&) Unexecuted instantiation: llama_model_olmo2::graph<true>::graph(llama_model const&, llm_graph_params const&) |
208 | | |
209 | | // Explicit template instantiations |
210 | | template struct llama_model_olmo2::graph<false>; |
211 | | template struct llama_model_olmo2::graph<true>; |