1#include "models.h"
2
3
4template <bool iswa>
5llm_build_exaone4<iswa>::llm_build_exaone4(const llama_model & model, const llm_graph_params & params) :
6 llm_graph_context(params) {
7 const int64_t n_embd_head = hparams.n_embd_head_k;
8
9 GGML_ASSERT(n_embd_head == hparams.n_embd_head_v);
10 GGML_ASSERT(n_embd_head == hparams.n_rot);
11
12 ggml_tensor * cur;
13 ggml_tensor * inpL;
14
15 inpL = build_inp_embd(tok_embd: model.tok_embd);
16
17 // inp_pos - contains the positions
18 ggml_tensor * inp_pos = build_inp_pos();
19
20 using inp_attn_type = std::conditional_t<iswa, llm_graph_input_attn_kv_iswa, llm_graph_input_attn_kv>;
21 inp_attn_type * inp_attn = nullptr;
22
23 if constexpr (iswa) {
24 inp_attn = build_attn_inp_kv_iswa();
25 } else {
26 inp_attn = build_attn_inp_kv();
27 }
28 ggml_tensor * inp_out_ids = build_inp_out_ids();
29
30 for (int il = 0; il < n_layer; ++il) {
31 ggml_tensor * inpSA = inpL;
32
33 // use RoPE for SWA layers or non-SWA models
34 const bool use_rope = hparams.is_swa(il) || hparams.swa_type == LLAMA_SWA_TYPE_NONE;
35
36 cur = inpL;
37
38 // self-attention
39 {
40 ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
41
42 ggml_tensor * Qcur = build_lora_mm(w: model.layers[il].wq, cur);
43 cb(cur: Qcur, name: "Qcur", il);
44
45 ggml_tensor * Kcur = build_lora_mm(w: model.layers[il].wk, cur);
46 cb(cur: Kcur, name: "Kcur", il);
47
48 ggml_tensor * Vcur = build_lora_mm(w: model.layers[il].wv, cur);
49 cb(cur: Vcur, name: "Vcur", il);
50
51 Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
52 Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
53 Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
54
55 Qcur = build_norm(cur: Qcur, mw: model.layers[il].attn_q_norm, NULL, type: LLM_NORM_RMS, il);
56 Kcur = build_norm(cur: Kcur, mw: model.layers[il].attn_k_norm, NULL, type: LLM_NORM_RMS, il);
57 cb(cur: Qcur, name: "Qcur_normed", il);
58 cb(cur: Kcur, name: "Kcur_normed", il);
59
60 if (use_rope) {
61 Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, rope_factors, n_rot, rope_type, n_ctx_orig, freq_base,
62 freq_scale, ext_factor, attn_factor, beta_fast, beta_slow);
63
64 Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, rope_factors, n_rot, rope_type, n_ctx_orig, freq_base,
65 freq_scale, ext_factor, attn_factor, beta_fast, beta_slow);
66 }
67 cb(cur: Qcur, name: "Qcur", il);
68 cb(cur: Kcur, name: "Kcur", il);
69 cb(cur: Vcur, name: "Vcur", il);
70
71 cur = build_attn(inp_attn,
72 model.layers[il].wo, NULL,
73 Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(x: float(n_embd_head)), il);
74 cb(cur, name: "attn_out", il);
75 }
76 if (il == n_layer - 1 && inp_out_ids) {
77 cur = ggml_get_rows(ctx0, cur, inp_out_ids);
78 inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
79 }
80 cur = build_norm(cur, mw: model.layers[il].attn_post_norm, NULL, type: LLM_NORM_RMS, il);
81 cb(cur, name: "attn_post_norm", il);
82
83 ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
84 cb(cur: ffn_inp, name: "ffn_inp", il);
85
86 // feed-forward network
87 cur = build_ffn(cur: ffn_inp,
88 up: model.layers[il].ffn_up, NULL, NULL,
89 gate: model.layers[il].ffn_gate, NULL, NULL,
90 down: model.layers[il].ffn_down, NULL, NULL, NULL,
91 type_op: LLM_FFN_SILU, type_gate: LLM_FFN_PAR, il);
92 cb(cur, name: "ffn_out", il);
93
94 cur = build_norm(cur, mw: model.layers[il].ffn_post_norm, NULL, type: LLM_NORM_RMS, il: -1);
95 cb(cur, name: "ffn_post_norm", il: -1);
96
97 cur = ggml_add(ctx0, cur, ffn_inp);
98
99 cur = build_cvec(cur, il);
100 cb(cur, name: "l_out", il);
101
102 // input for next layer
103 inpL = cur;
104 }
105 cur = inpL;
106
107 cur = build_norm(cur, mw: model.output_norm, NULL, type: LLM_NORM_RMS, il: -1);
108
109 cb(cur, name: "result_norm", il: -1);
110 res->t_embd = cur;
111
112 // lm_head
113 cur = build_lora_mm(w: model.output, cur);
114
115 cb(cur, name: "result_output", il: -1);
116 res->t_logits = cur;
117
118 ggml_build_forward_expand(gf, cur);
119}
120
121// Explicit template instantiations
122template struct llm_build_exaone4<false>;
123template struct llm_build_exaone4<true>;
124