/src/llama.cpp/src/models/llama.cpp
Line | Count | Source |
1 | | #include "models.h" |
2 | | |
3 | | template <bool embed> |
4 | 0 | llm_build_llama<embed>::llm_build_llama(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) { |
5 | 0 | const int64_t n_embd_head = hparams.n_embd_head_v; |
6 | |
|
7 | 0 | GGML_ASSERT(n_embd_head == hparams.n_embd_head_k); |
8 | 0 | GGML_ASSERT(n_embd_head == hparams.n_rot); |
9 | |
|
10 | 0 | ggml_tensor * cur; |
11 | 0 | ggml_tensor * inpL; |
12 | |
|
13 | 0 | inpL = build_inp_embd(model.tok_embd); |
14 | | |
15 | | // inp_pos - contains the positions |
16 | 0 | ggml_tensor * inp_pos = build_inp_pos(); |
17 | |
|
18 | 0 | using inp_attn_type = std::conditional_t<embed, llm_graph_input_attn_no_cache, llm_graph_input_attn_kv>; |
19 | |
|
20 | 0 | inp_attn_type * inp_attn = nullptr; |
21 | 0 | if constexpr (embed) { |
22 | 0 | inp_attn = build_attn_inp_no_cache(); |
23 | 0 | } else { |
24 | 0 | inp_attn = build_attn_inp_kv(); |
25 | 0 | } |
26 | |
|
27 | 0 | const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale; |
28 | |
|
29 | 0 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
30 | |
|
31 | 0 | for (int il = 0; il < n_layer; ++il) { |
32 | 0 | ggml_tensor * inpSA = inpL; |
33 | | |
34 | | // norm |
35 | 0 | cur = build_norm(inpL, |
36 | 0 | model.layers[il].attn_norm, NULL, |
37 | 0 | LLM_NORM_RMS, il); |
38 | 0 | cb(cur, "attn_norm", il); |
39 | | |
40 | | // self-attention |
41 | 0 | { |
42 | | // rope freq factors for llama3; may return nullptr for llama2 and other models |
43 | 0 | ggml_tensor * rope_factors = model.get_rope_factors(cparams, il); |
44 | | |
45 | | // compute Q and K and RoPE them |
46 | 0 | ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur); |
47 | 0 | cb(Qcur, "Qcur", il); |
48 | 0 | if (model.layers[il].bq) { |
49 | 0 | Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq); |
50 | 0 | cb(Qcur, "Qcur", il); |
51 | 0 | } |
52 | 0 | ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur); |
53 | 0 | cb(Kcur, "Kcur", il); |
54 | 0 | if (model.layers[il].bk) { |
55 | 0 | Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk); |
56 | 0 | cb(Kcur, "Kcur", il); |
57 | 0 | } |
58 | 0 | ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur); |
59 | 0 | cb(Vcur, "Vcur", il); |
60 | 0 | if (model.layers[il].bv) { |
61 | 0 | Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv); |
62 | 0 | cb(Vcur, "Vcur", il); |
63 | 0 | } |
64 | 0 | Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens); |
65 | 0 | Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens); |
66 | 0 | Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens); |
67 | |
|
68 | 0 | Qcur = ggml_rope_ext( |
69 | 0 | ctx0, Qcur, inp_pos, rope_factors, |
70 | 0 | n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
71 | 0 | ext_factor, attn_factor, beta_fast, beta_slow |
72 | 0 | ); |
73 | |
|
74 | 0 | Kcur = ggml_rope_ext( |
75 | 0 | ctx0, Kcur, inp_pos, rope_factors, |
76 | 0 | n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
77 | 0 | ext_factor, attn_factor, beta_fast, beta_slow |
78 | 0 | ); |
79 | |
|
80 | 0 | cb(Qcur, "Qcur", il); |
81 | 0 | cb(Kcur, "Kcur", il); |
82 | 0 | cb(Vcur, "Vcur", il); |
83 | |
|
84 | 0 | if (hparams.use_kq_norm) { |
85 | | // Llama4TextL2Norm |
86 | 0 | Qcur = ggml_rms_norm(ctx0, Qcur, hparams.f_norm_rms_eps); |
87 | 0 | Kcur = ggml_rms_norm(ctx0, Kcur, hparams.f_norm_rms_eps); |
88 | 0 | cb(Qcur, "Qcur_normed", il); |
89 | 0 | cb(Kcur, "Kcur_normed", il); |
90 | 0 | } |
91 | 0 | cur = build_attn(inp_attn, |
92 | 0 | model.layers[il].wo, model.layers[il].bo, |
93 | 0 | Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il); |
94 | 0 | cb(cur, "attn_out", il); |
95 | 0 | } |
96 | 0 | if (il == n_layer - 1 && inp_out_ids) { |
97 | 0 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
98 | 0 | inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids); |
99 | 0 | } |
100 | 0 | ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA); |
101 | 0 | cb(ffn_inp, "ffn_inp", il); |
102 | | |
103 | | // feed-forward network (non-MoE) |
104 | 0 | if (model.layers[il].ffn_gate_inp == nullptr) { |
105 | |
|
106 | 0 | cur = build_norm(ffn_inp, |
107 | 0 | model.layers[il].ffn_norm, NULL, |
108 | 0 | LLM_NORM_RMS, il); |
109 | 0 | cb(cur, "ffn_norm", il); |
110 | |
|
111 | 0 | cur = build_ffn(cur, |
112 | 0 | model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL, |
113 | 0 | model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL, |
114 | 0 | model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL, |
115 | 0 | NULL, |
116 | 0 | LLM_FFN_SILU, LLM_FFN_PAR, il); |
117 | 0 | cb(cur, "ffn_out", il); |
118 | 0 | } else { |
119 | | // MoE branch |
120 | 0 | cur = build_norm(ffn_inp, |
121 | 0 | model.layers[il].ffn_norm, NULL, |
122 | 0 | LLM_NORM_RMS, il); |
123 | 0 | cb(cur, "ffn_norm", il); |
124 | |
|
125 | 0 | cur = build_moe_ffn(cur, |
126 | 0 | model.layers[il].ffn_gate_inp, |
127 | 0 | model.layers[il].ffn_up_exps, |
128 | 0 | model.layers[il].ffn_gate_exps, |
129 | 0 | model.layers[il].ffn_down_exps, |
130 | 0 | nullptr, |
131 | 0 | n_expert, n_expert_used, |
132 | 0 | LLM_FFN_SILU, true, |
133 | 0 | false, 0.0, |
134 | 0 | LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX, |
135 | 0 | il); |
136 | 0 | cb(cur, "ffn_moe_out", il); |
137 | 0 | } |
138 | 0 | cur = ggml_add(ctx0, cur, ffn_inp); |
139 | 0 | cb(cur, "ffn_out", il); |
140 | |
|
141 | 0 | cur = build_cvec(cur, il); |
142 | 0 | cb(cur, "l_out", il); |
143 | | |
144 | | // input for next layer |
145 | 0 | inpL = cur; |
146 | 0 | } |
147 | 0 | cur = inpL; |
148 | |
|
149 | 0 | cur = build_norm(cur, |
150 | 0 | model.output_norm, NULL, |
151 | 0 | LLM_NORM_RMS, -1); |
152 | |
|
153 | 0 | cb(cur, "result_norm", -1); |
154 | 0 | res->t_embd = cur; |
155 | |
|
156 | 0 | if constexpr (!embed) { |
157 | | // lm_head |
158 | 0 | cur = build_lora_mm(model.output, cur); |
159 | |
|
160 | 0 | cb(cur, "result_output", -1); |
161 | 0 | res->t_logits = cur; |
162 | 0 | } |
163 | |
|
164 | 0 | ggml_build_forward_expand(gf, cur); |
165 | 0 | } Unexecuted instantiation: llm_build_llama<false>::llm_build_llama(llama_model const&, llm_graph_params const&) Unexecuted instantiation: llm_build_llama<true>::llm_build_llama(llama_model const&, llm_graph_params const&) |
166 | | |
167 | | template struct llm_build_llama<false>; |
168 | | template struct llm_build_llama<true>; |