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