/src/llama.cpp/src/models/exaone4.cpp
Line | Count | Source |
1 | | #include "models.h" |
2 | | |
3 | 0 | void llama_model_exaone4::load_arch_hparams(llama_model_loader & ml) { |
4 | 0 | if (hparams.n_layer() == 64) { // 32B |
5 | 0 | hparams.swa_type = LLAMA_SWA_TYPE_STANDARD; |
6 | 0 | hparams.n_swa = 4096; |
7 | 0 | uint32_t swa_period = 4; |
8 | 0 | ml.get_key_or_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, swa_period, false); |
9 | 0 | hparams.set_swa_pattern(swa_period); |
10 | |
|
11 | 0 | hparams.rope_freq_base_train_swa = hparams.rope_freq_base_train; |
12 | 0 | hparams.rope_freq_scale_train_swa = hparams.rope_freq_scale_train; |
13 | 0 | ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA, hparams.rope_freq_base_train_swa, false); |
14 | 0 | } |
15 | |
|
16 | 0 | ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false); |
17 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
18 | 0 | ml.get_key(LLM_KV_NEXTN_PREDICT_LAYERS, hparams.n_layer_nextn, false); |
19 | |
|
20 | 0 | GGML_ASSERT(hparams.n_layer_nextn < hparams.n_layer_all && "n_layer_nextn must be < n_layer"); |
21 | |
|
22 | 0 | switch (hparams.n_layer()) { |
23 | 0 | case 30: type = LLM_TYPE_1_2B; 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_exaone4::load_arch_tensors(llama_model_loader &) { |
30 | 0 | LLAMA_LOAD_LOCALS; |
31 | |
|
32 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
33 | | |
34 | | // output |
35 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
36 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
37 | | |
38 | | // if output is NULL, init from the input tok embed |
39 | 0 | if (output == NULL) { |
40 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
41 | 0 | } |
42 | |
|
43 | 0 | for (int i = 0; i < n_layer_all; ++i) { |
44 | 0 | const bool is_nextn = i >= n_layer; |
45 | 0 | int flags = 0; |
46 | 0 | if (is_nextn) { |
47 | | // NextN/MTP layers are preserved in GGUF but are not executed yet. |
48 | 0 | flags |= TENSOR_SKIP; |
49 | 0 | } |
50 | |
|
51 | 0 | auto & layer = layers[i]; |
52 | |
|
53 | 0 | create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head, n_embd_k_gqa, n_embd_v_gqa, flags); |
54 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, flags); |
55 | |
|
56 | 0 | if (!is_nextn) { |
57 | 0 | layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0)); |
58 | 0 | } |
59 | |
|
60 | 0 | layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, flags); |
61 | 0 | layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, flags); |
62 | 0 | layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, flags); |
63 | |
|
64 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, flags); |
65 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, flags); |
66 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, flags); |
67 | 0 | layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, flags); |
68 | |
|
69 | 0 | if (is_nextn) { |
70 | 0 | layer.nextn.eh_proj = create_tensor(tn(LLM_TENSOR_NEXTN_EH_PROJ, "weight", i), {2 * n_embd, n_embd}, flags); |
71 | 0 | layer.nextn.enorm = create_tensor(tn(LLM_TENSOR_NEXTN_ENORM, "weight", i), {n_embd}, flags); |
72 | 0 | layer.nextn.hnorm = create_tensor(tn(LLM_TENSOR_NEXTN_HNORM, "weight", i), {n_embd}, flags); |
73 | 0 | layer.nextn.shared_head_norm = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_NORM, "weight", i), {n_embd}, flags | TENSOR_NOT_REQUIRED); |
74 | 0 | } |
75 | 0 | } |
76 | 0 | } |
77 | | |
78 | 0 | std::unique_ptr<llm_graph_context> llama_model_exaone4::build_arch_graph(const llm_graph_params & params) const { |
79 | 0 | if (hparams.swa_type == LLAMA_SWA_TYPE_STANDARD) { |
80 | 0 | return std::make_unique<graph<true>>(*this, params); |
81 | 0 | } else { |
82 | 0 | return std::make_unique<graph<false>>(*this, params); |
83 | 0 | } |
84 | 0 | } |
85 | | |
86 | | template <bool iswa> |
87 | | llama_model_exaone4::graph<iswa>::graph(const llama_model & model, const llm_graph_params & params) : |
88 | 0 | llm_graph_context(params) { |
89 | 0 | const int64_t n_embd_head = hparams.n_embd_head_k(); |
90 | |
|
91 | 0 | GGML_ASSERT(n_embd_head == hparams.n_embd_head_v()); |
92 | 0 | GGML_ASSERT(n_embd_head == n_rot); |
93 | |
|
94 | 0 | ggml_tensor * cur; |
95 | 0 | ggml_tensor * inpL; |
96 | |
|
97 | 0 | inpL = build_inp_embd(model.tok_embd); |
98 | | |
99 | | // inp_pos - contains the positions |
100 | 0 | ggml_tensor * inp_pos = build_inp_pos(); |
101 | |
|
102 | 0 | using inp_attn_type = std::conditional_t<iswa, llm_graph_input_attn_kv_iswa, llm_graph_input_attn_kv>; |
103 | 0 | inp_attn_type * inp_attn = nullptr; |
104 | |
|
105 | 0 | if constexpr (iswa) { |
106 | 0 | inp_attn = build_attn_inp_kv_iswa(); |
107 | 0 | } else { |
108 | 0 | inp_attn = build_attn_inp_kv(); |
109 | 0 | } |
110 | 0 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
111 | |
|
112 | 0 | for (int il = 0; il < n_layer; ++il) { |
113 | 0 | ggml_tensor * inpSA = inpL; |
114 | | |
115 | | // use RoPE for SWA layers or non-SWA models |
116 | 0 | const bool use_rope = hparams.is_swa(il) || hparams.swa_type == LLAMA_SWA_TYPE_NONE; |
117 | |
|
118 | 0 | cur = inpL; |
119 | | |
120 | | // self-attention |
121 | 0 | { |
122 | 0 | ggml_tensor * rope_factors = model.get_rope_factors(cparams, il); |
123 | |
|
124 | 0 | auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur, |
125 | 0 | n_embd_head, n_head, n_head_kv, il); |
126 | |
|
127 | 0 | Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il); |
128 | 0 | Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il); |
129 | 0 | cb(Qcur, "Qcur_normed", il); |
130 | 0 | cb(Kcur, "Kcur_normed", il); |
131 | |
|
132 | 0 | if (use_rope) { |
133 | 0 | Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, rope_factors, n_rot, rope_type, n_ctx_orig, freq_base, |
134 | 0 | freq_scale, ext_factor, attn_factor, beta_fast, beta_slow); |
135 | |
|
136 | 0 | Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, rope_factors, n_rot, rope_type, n_ctx_orig, freq_base, |
137 | 0 | freq_scale, ext_factor, attn_factor, beta_fast, beta_slow); |
138 | 0 | } |
139 | 0 | cb(Qcur, "Qcur", il); |
140 | 0 | cb(Kcur, "Kcur", il); |
141 | 0 | cb(Vcur, "Vcur", il); |
142 | |
|
143 | 0 | cur = build_attn(inp_attn, |
144 | 0 | model.layers[il].wo, NULL, model.layers[il].wo_s, |
145 | 0 | Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il); |
146 | 0 | cb(cur, "attn_out", il); |
147 | 0 | } |
148 | 0 | if (il == n_layer - 1 && inp_out_ids) { |
149 | 0 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
150 | 0 | inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids); |
151 | 0 | } |
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 | ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA); |
156 | 0 | cb(ffn_inp, "ffn_inp", il); |
157 | | |
158 | | // feed-forward network |
159 | 0 | cur = build_ffn(ffn_inp, |
160 | 0 | model.layers[il].ffn_up, NULL, NULL, |
161 | 0 | model.layers[il].ffn_gate, NULL, NULL, |
162 | 0 | model.layers[il].ffn_down, NULL, NULL, NULL, |
163 | 0 | LLM_FFN_SILU, LLM_FFN_PAR, il); |
164 | 0 | cb(cur, "ffn_out", il); |
165 | |
|
166 | 0 | cur = build_norm(cur, model.layers[il].ffn_post_norm, NULL, LLM_NORM_RMS, -1); |
167 | 0 | cb(cur, "ffn_post_norm", -1); |
168 | |
|
169 | 0 | cur = ggml_add(ctx0, cur, ffn_inp); |
170 | |
|
171 | 0 | cur = build_cvec(cur, il); |
172 | 0 | cb(cur, "l_out", il); |
173 | | |
174 | | // input for next layer |
175 | 0 | inpL = cur; |
176 | 0 | } |
177 | 0 | cur = inpL; |
178 | |
|
179 | 0 | cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1); |
180 | |
|
181 | 0 | cb(cur, "result_norm", -1); |
182 | 0 | res->t_embd = cur; |
183 | | |
184 | | // lm_head |
185 | 0 | cur = build_lora_mm(model.output, cur, model.output_s); |
186 | |
|
187 | 0 | cb(cur, "result_output", -1); |
188 | 0 | res->t_logits = cur; |
189 | |
|
190 | 0 | ggml_build_forward_expand(gf, cur); |
191 | 0 | } Unexecuted instantiation: llama_model_exaone4::graph<false>::graph(llama_model const&, llm_graph_params const&) Unexecuted instantiation: llama_model_exaone4::graph<true>::graph(llama_model const&, llm_graph_params const&) |
192 | | |
193 | | // Explicit template instantiations |
194 | | template struct llama_model_exaone4::graph<false>; |
195 | | template struct llama_model_exaone4::graph<true>; |