/src/llama.cpp/src/models/lfm2.cpp
Line | Count | Source |
1 | | #include "models.h" |
2 | | |
3 | | #include "../llama-memory-hybrid-iswa.h" |
4 | | #include "../llama-memory-hybrid.h" |
5 | | |
6 | | template <bool iswa> |
7 | | llm_build_lfm2<iswa>::llm_build_lfm2(const llama_model & model, const llm_graph_params & params) : |
8 | 0 | llm_graph_context(params) { |
9 | 0 | using inp_hybrid_type = std::conditional_t<iswa, llm_graph_input_mem_hybrid_iswa, llm_graph_input_mem_hybrid>; |
10 | 0 | using inp_attn_type = std::conditional_t<iswa, llm_graph_input_attn_kv_iswa, llm_graph_input_attn_kv>; |
11 | 0 | using mem_hybrid_ctx = std::conditional_t<iswa, llama_memory_hybrid_iswa_context, llama_memory_hybrid_context>; |
12 | | |
13 | | // lambda helpers for readability |
14 | 0 | auto build_dense_feed_forward = [&model, this](ggml_tensor * cur, int il) -> ggml_tensor * { |
15 | 0 | GGML_ASSERT(!model.layers[il].ffn_up_b); |
16 | 0 | GGML_ASSERT(!model.layers[il].ffn_gate_b); |
17 | 0 | GGML_ASSERT(!model.layers[il].ffn_down_b); |
18 | 0 | return build_ffn(cur, |
19 | 0 | model.layers[il].ffn_up, NULL, NULL, |
20 | 0 | model.layers[il].ffn_gate, NULL, NULL, |
21 | 0 | model.layers[il].ffn_down, NULL, NULL, |
22 | 0 | NULL, LLM_FFN_SILU, LLM_FFN_PAR, il); |
23 | 0 | }; Unexecuted instantiation: llm_build_lfm2<true>::llm_build_lfm2(llama_model const&, llm_graph_params const&)::{lambda(ggml_tensor*, int)#1}::operator()(ggml_tensor*, int) constUnexecuted instantiation: llm_build_lfm2<false>::llm_build_lfm2(llama_model const&, llm_graph_params const&)::{lambda(ggml_tensor*, int)#1}::operator()(ggml_tensor*, int) const |
24 | 0 | auto build_moe_feed_forward = [&model, this](ggml_tensor * cur, int il) -> ggml_tensor * { |
25 | 0 | return build_moe_ffn(cur, |
26 | 0 | model.layers[il].ffn_gate_inp, |
27 | 0 | model.layers[il].ffn_up_exps, |
28 | 0 | model.layers[il].ffn_gate_exps, |
29 | 0 | model.layers[il].ffn_down_exps, |
30 | 0 | model.layers[il].ffn_exp_probs_b, |
31 | 0 | n_expert, n_expert_used, |
32 | 0 | LLM_FFN_SILU, true, |
33 | 0 | hparams.expert_weights_scale, |
34 | 0 | static_cast<llama_expert_gating_func_type>(hparams.expert_gating_func), |
35 | 0 | il); |
36 | 0 | }; Unexecuted instantiation: llm_build_lfm2<true>::llm_build_lfm2(llama_model const&, llm_graph_params const&)::{lambda(ggml_tensor*, int)#2}::operator()(ggml_tensor*, int) constUnexecuted instantiation: llm_build_lfm2<false>::llm_build_lfm2(llama_model const&, llm_graph_params const&)::{lambda(ggml_tensor*, int)#2}::operator()(ggml_tensor*, int) const |
37 | 0 | auto build_attn_block = [&model, this](ggml_tensor * cur, |
38 | 0 | ggml_tensor * inp_pos, |
39 | 0 | inp_attn_type * inp_attn, |
40 | 0 | int il) -> ggml_tensor * { |
41 | 0 | GGML_ASSERT(hparams.n_embd_v_gqa(il) == hparams.n_embd_k_gqa(il)); |
42 | 0 | const auto n_embd_head = hparams.n_embd_head_v(); |
43 | 0 | const auto n_head_kv = hparams.n_head_kv(il); |
44 | |
|
45 | 0 | auto * q = build_lora_mm(model.layers[il].wq, cur); |
46 | 0 | cb(q, "model.layers.{}.self_attn.q_proj", il); |
47 | 0 | auto * k = build_lora_mm(model.layers[il].wk, cur); |
48 | 0 | cb(k, "model.layers.{}.self_attn.k_proj", il); |
49 | 0 | auto * v = build_lora_mm(model.layers[il].wv, cur); |
50 | 0 | cb(v, "model.layers.{}.self_attn.v_proj", il); |
51 | |
|
52 | 0 | q = ggml_reshape_3d(ctx0, q, n_embd_head, n_head, n_tokens); |
53 | 0 | k = ggml_reshape_3d(ctx0, k, n_embd_head, n_head_kv, n_tokens); |
54 | 0 | v = ggml_reshape_3d(ctx0, v, n_embd_head, n_head_kv, n_tokens); |
55 | | |
56 | | // qk norm |
57 | 0 | q = build_norm(q, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il); |
58 | 0 | cb(q, "model.layers.{}.self_attn.q_layernorm", il); |
59 | 0 | k = build_norm(k, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il); |
60 | 0 | cb(k, "model.layers.{}.self_attn.k_layernorm", il); |
61 | | |
62 | | // RoPE |
63 | 0 | q = ggml_rope_ext(ctx0, q, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, ext_factor, |
64 | 0 | attn_factor, beta_fast, beta_slow); |
65 | 0 | k = ggml_rope_ext(ctx0, k, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, ext_factor, |
66 | 0 | attn_factor, beta_fast, beta_slow); |
67 | |
|
68 | 0 | cur = build_attn(inp_attn, |
69 | 0 | model.layers[il].wo, NULL, |
70 | 0 | q, k, v, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il); |
71 | |
|
72 | 0 | cb(cur, "model.layers.{}.self_attn.out_proj", il); |
73 | |
|
74 | 0 | return cur; |
75 | 0 | }; Unexecuted instantiation: llm_build_lfm2<true>::llm_build_lfm2(llama_model const&, llm_graph_params const&)::{lambda(ggml_tensor*, ggml_tensor*, llm_graph_input_attn_kv_iswa*, int)#1}::operator()(ggml_tensor*, ggml_tensor*, llm_graph_input_attn_kv_iswa*, int) constUnexecuted instantiation: llm_build_lfm2<false>::llm_build_lfm2(llama_model const&, llm_graph_params const&)::{lambda(ggml_tensor*, ggml_tensor*, llm_graph_input_attn_kv*, int)#1}::operator()(ggml_tensor*, ggml_tensor*, llm_graph_input_attn_kv*, int) const |
76 | 0 | auto build_shortconv_block = [&model, this](ggml_tensor * cur, |
77 | 0 | llm_graph_input_rs * inp_recr, |
78 | 0 | int il) -> ggml_tensor * { |
79 | 0 | const auto * mctx_cur = static_cast<const mem_hybrid_ctx *>(mctx)->get_recr(); |
80 | 0 | const uint32_t kv_head = mctx_cur->get_head(); |
81 | 0 | const int64_t n_seq_tokens = ubatch.n_seq_tokens; |
82 | 0 | const int64_t n_seqs = ubatch.n_seqs; |
83 | 0 | GGML_ASSERT(n_seqs != 0); |
84 | 0 | GGML_ASSERT(ubatch.equal_seqs()); |
85 | 0 | GGML_ASSERT(ubatch.n_tokens == n_seq_tokens * n_seqs); |
86 | |
|
87 | 0 | GGML_ASSERT(hparams.n_shortconv_l_cache > 1); |
88 | 0 | const uint32_t d_conv = hparams.n_shortconv_l_cache - 1; |
89 | | |
90 | | // {n_embd, n_tokens} => {n_embd, n_seq_tokens, n_seqs} |
91 | 0 | cur = ggml_reshape_3d(ctx0, cur, cur->ne[0], n_seq_tokens, n_seqs); |
92 | |
|
93 | 0 | auto * bcx = build_lora_mm(model.layers[il].shortconv.in_proj, cur); |
94 | 0 | cb(bcx, "model.layers.{}.conv.in_proj", il); |
95 | |
|
96 | 0 | constexpr auto n_chunks = 3; |
97 | 0 | GGML_ASSERT(bcx->ne[0] % n_chunks == 0); |
98 | 0 | const auto chunk_size = bcx->ne[0] / n_chunks; |
99 | 0 | auto * b = ggml_view_3d(ctx0, bcx, chunk_size, bcx->ne[1], bcx->ne[2], bcx->nb[1], bcx->nb[2], |
100 | 0 | 0 * chunk_size * ggml_element_size(bcx)); |
101 | 0 | auto * c = ggml_view_3d(ctx0, bcx, chunk_size, bcx->ne[1], bcx->ne[2], bcx->nb[1], bcx->nb[2], |
102 | 0 | 1 * chunk_size * ggml_element_size(bcx)); |
103 | 0 | auto * x = ggml_view_3d(ctx0, bcx, chunk_size, bcx->ne[1], bcx->ne[2], bcx->nb[1], bcx->nb[2], |
104 | 0 | 2 * chunk_size * ggml_element_size(bcx)); |
105 | |
|
106 | 0 | auto * bx = ggml_transpose(ctx0, ggml_mul(ctx0, b, x)); |
107 | | |
108 | | // read conv state |
109 | 0 | auto * conv_state = mctx_cur->get_r_l(il); |
110 | 0 | auto * conv_rs = build_rs(inp_recr, conv_state, hparams.n_embd_r(), n_seqs); |
111 | 0 | auto * conv = ggml_reshape_3d(ctx0, conv_rs, d_conv, hparams.n_embd, n_seqs); |
112 | |
|
113 | 0 | bx = ggml_concat(ctx0, conv, bx, 0); |
114 | 0 | GGML_ASSERT(bx->ne[0] > conv->ne[0]); |
115 | | |
116 | | // last d_conv columns is a new conv state |
117 | 0 | auto * new_conv = ggml_view_3d(ctx0, bx, conv->ne[0], bx->ne[1], bx->ne[2], bx->nb[1], bx->nb[2], |
118 | 0 | (bx->ne[0] - conv->ne[0]) * ggml_element_size(bx)); |
119 | 0 | GGML_ASSERT(ggml_are_same_shape(conv, new_conv)); |
120 | | |
121 | | // write new conv conv state |
122 | 0 | ggml_build_forward_expand(gf, ggml_cpy(ctx0, new_conv, |
123 | 0 | ggml_view_1d(ctx0, conv_state, ggml_nelements(new_conv), |
124 | 0 | kv_head * d_conv * n_embd * ggml_element_size(new_conv)))); |
125 | |
|
126 | 0 | auto * conv_kernel = model.layers[il].shortconv.conv; |
127 | 0 | auto * conv_out = ggml_ssm_conv(ctx0, bx, conv_kernel); |
128 | 0 | cb(conv_out, "model.layers.{}.conv.conv", il); |
129 | |
|
130 | 0 | auto * y = ggml_mul(ctx0, c, conv_out); |
131 | 0 | y = build_lora_mm(model.layers[il].shortconv.out_proj, y); |
132 | 0 | cb(y, "model.layers.{}.conv.out_proj", il); |
133 | | // {n_embd, n_seq_tokens, n_seqs} => {n_embd, n_tokens} |
134 | 0 | y = ggml_reshape_2d(ctx0, y, y->ne[0], n_seq_tokens * n_seqs); |
135 | |
|
136 | 0 | return y; |
137 | 0 | }; Unexecuted instantiation: llm_build_lfm2<true>::llm_build_lfm2(llama_model const&, llm_graph_params const&)::{lambda(ggml_tensor*, llm_graph_input_rs*, int)#1}::operator()(ggml_tensor*, llm_graph_input_rs*, int) constUnexecuted instantiation: llm_build_lfm2<false>::llm_build_lfm2(llama_model const&, llm_graph_params const&)::{lambda(ggml_tensor*, llm_graph_input_rs*, int)#1}::operator()(ggml_tensor*, llm_graph_input_rs*, int) const |
138 | | |
139 | | // actual graph construction starts here |
140 | 0 | ggml_tensor * cur = build_inp_embd(model.tok_embd); |
141 | 0 | cb(cur, "model.embed_tokens", -1); |
142 | |
|
143 | 0 | ggml_build_forward_expand(gf, cur); |
144 | |
|
145 | 0 | inp_hybrid_type * inp_hybrid = nullptr; |
146 | 0 | if constexpr (iswa) { |
147 | 0 | inp_hybrid = build_inp_mem_hybrid_iswa(); |
148 | 0 | } else { |
149 | 0 | inp_hybrid = build_inp_mem_hybrid(); |
150 | 0 | } |
151 | |
|
152 | 0 | ggml_tensor * inp_pos = build_inp_pos(); |
153 | 0 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
154 | |
|
155 | 0 | for (int il = 0; il < n_layer; ++il) { |
156 | 0 | const bool is_moe_layer = il >= static_cast<int>(hparams.n_layer_dense_lead); |
157 | |
|
158 | 0 | auto * prev_cur = cur; |
159 | 0 | cur = build_norm(cur, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il); |
160 | 0 | cb(cur, "model.layers.{}.operator_norm", il); |
161 | |
|
162 | 0 | cur = hparams.is_recurrent(il) ? build_shortconv_block(cur, inp_hybrid->get_recr(), il) : |
163 | 0 | build_attn_block(cur, inp_pos, inp_hybrid->get_attn(), il); |
164 | |
|
165 | 0 | if (il == n_layer - 1 && inp_out_ids) { |
166 | 0 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
167 | 0 | prev_cur = ggml_get_rows(ctx0, prev_cur, inp_out_ids); |
168 | 0 | } |
169 | |
|
170 | 0 | cur = ggml_add(ctx0, prev_cur, cur); |
171 | |
|
172 | 0 | auto * ffn_norm_out = build_norm(cur, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il); |
173 | 0 | cb(ffn_norm_out, "model.layers.{}.ffn_norm", il); |
174 | |
|
175 | 0 | ggml_tensor * ffn_out = |
176 | 0 | is_moe_layer ? build_moe_feed_forward(ffn_norm_out, il) : build_dense_feed_forward(ffn_norm_out, il); |
177 | 0 | cb(ffn_norm_out, "model.layers.{}.ffn_out", il); |
178 | |
|
179 | 0 | cur = ggml_add(ctx0, cur, ffn_out); |
180 | |
|
181 | 0 | cur = build_cvec(cur, il); |
182 | 0 | cb(cur, "l_out", il); |
183 | 0 | } |
184 | |
|
185 | 0 | cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1); |
186 | 0 | cb(cur, "result_norm", -1); |
187 | 0 | res->t_embd = cur; |
188 | |
|
189 | 0 | cur = build_lora_mm(model.output, cur); |
190 | 0 | cb(cur, "result_output", -1); |
191 | |
|
192 | 0 | res->t_logits = cur; |
193 | |
|
194 | 0 | ggml_build_forward_expand(gf, cur); |
195 | 0 | } Unexecuted instantiation: llm_build_lfm2<true>::llm_build_lfm2(llama_model const&, llm_graph_params const&) Unexecuted instantiation: llm_build_lfm2<false>::llm_build_lfm2(llama_model const&, llm_graph_params const&) |
196 | | |
197 | | // Explicit template instantiations |
198 | | template struct llm_build_lfm2<true>; |
199 | | template struct llm_build_lfm2<false>; |