|
1 | 1 | #include "models.h" |
2 | 2 |
|
3 | | -llm_build_t5_dec::llm_build_t5_dec(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) { |
| 3 | +template <> |
| 4 | +llm_build_t5<false>::llm_build_t5(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) { |
4 | 5 | const int64_t n_embd_head = hparams.n_embd_head_v(); |
5 | 6 | //const int64_t n_embd_gqa = hparams.n_embd_v_gqa(); |
6 | 7 |
|
@@ -153,3 +154,99 @@ llm_build_t5_dec::llm_build_t5_dec(const llama_model & model, const llm_graph_pa |
153 | 154 |
|
154 | 155 | ggml_build_forward_expand(gf, cur); |
155 | 156 | } |
| 157 | + |
| 158 | +template <> |
| 159 | +llm_build_t5<true>::llm_build_t5(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) { |
| 160 | + const int64_t n_embd_head = hparams.n_embd_head_v(); |
| 161 | + |
| 162 | + GGML_ASSERT(n_embd_head == hparams.n_embd_head_k()); |
| 163 | + |
| 164 | + ggml_tensor * cur; |
| 165 | + ggml_tensor * inpL; |
| 166 | + |
| 167 | + inpL = build_inp_embd(model.tok_embd); |
| 168 | + |
| 169 | + ggml_tensor * pos_bucket_enc = build_inp_pos_bucket_enc(); |
| 170 | + |
| 171 | + auto * inp_attn = build_attn_inp_no_cache(); |
| 172 | + |
| 173 | + ggml_tensor * inp_out_ids = build_inp_out_ids(); |
| 174 | + |
| 175 | + for (int il = 0; il < n_layer; ++il) { |
| 176 | + ggml_tensor * inpSA = inpL; |
| 177 | + |
| 178 | + // norm |
| 179 | + cur = build_norm(inpL, |
| 180 | + model.layers[il].attn_norm_enc, NULL, |
| 181 | + LLM_NORM_RMS, il); |
| 182 | + cb(cur, "attn_norm", il); |
| 183 | + |
| 184 | + // self-attention |
| 185 | + { |
| 186 | + ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq_enc, cur); |
| 187 | + cb(Qcur, "Qcur", il); |
| 188 | + |
| 189 | + ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk_enc, cur); |
| 190 | + cb(Kcur, "Kcur", il); |
| 191 | + |
| 192 | + ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv_enc, cur); |
| 193 | + cb(Vcur, "Vcur", il); |
| 194 | + |
| 195 | + Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens); |
| 196 | + Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens); |
| 197 | + Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens); |
| 198 | + |
| 199 | + ggml_tensor * attn_rel_b = model.layers[il].attn_rel_b_enc ? model.layers[il].attn_rel_b_enc : model.layers[0].attn_rel_b_enc; |
| 200 | + ggml_tensor * kq_b = build_pos_bias(pos_bucket_enc, attn_rel_b); |
| 201 | + |
| 202 | + cur = build_attn(inp_attn, |
| 203 | + model.layers[il].wo_enc, nullptr, nullptr, |
| 204 | + Qcur, Kcur, Vcur, kq_b, nullptr, nullptr, 1.0f, il); |
| 205 | + cb(cur, "kqv_out", il); |
| 206 | + } |
| 207 | + if (il == n_layer - 1 && inp_out_ids) { |
| 208 | + cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
| 209 | + inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids); |
| 210 | + } |
| 211 | + ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA); |
| 212 | + cb(ffn_inp, "ffn_inp", il); |
| 213 | + |
| 214 | + // feed-forward network |
| 215 | + { |
| 216 | + cur = build_norm(ffn_inp, |
| 217 | + model.layers[il].ffn_norm_enc, NULL, |
| 218 | + LLM_NORM_RMS, il); |
| 219 | + cb(cur, "ffn_norm", il); |
| 220 | + |
| 221 | + // T5 uses relu, flan-T5 uses gelu-gated |
| 222 | + cur = build_ffn(cur, |
| 223 | + model.layers[il].ffn_up_enc, NULL, NULL, |
| 224 | + model.layers[il].ffn_gate_enc, NULL, NULL, |
| 225 | + model.layers[il].ffn_down_enc, NULL, NULL, |
| 226 | + NULL, |
| 227 | + model.layers[il].ffn_gate_enc ? LLM_FFN_GELU : LLM_FFN_RELU, |
| 228 | + model.layers[il].ffn_gate_enc ? LLM_FFN_PAR : LLM_FFN_SEQ, |
| 229 | + il); |
| 230 | + cb(cur, "ffn_out", il); |
| 231 | + } |
| 232 | + cur = ggml_add(ctx0, cur, ffn_inp); |
| 233 | + cb(cur, "ffn_out", il); |
| 234 | + |
| 235 | + cur = build_cvec(cur, il); |
| 236 | + cb(cur, "l_out", il); |
| 237 | + |
| 238 | + // input for next layer |
| 239 | + inpL = cur; |
| 240 | + } |
| 241 | + cur = inpL; |
| 242 | + cb(cur, "result_embd", -1); |
| 243 | + |
| 244 | + cur = build_norm(cur, |
| 245 | + model.output_norm_enc, NULL, |
| 246 | + LLM_NORM_RMS, -1); |
| 247 | + |
| 248 | + cb(cur, "result_norm", -1); |
| 249 | + res->t_embd = cur; |
| 250 | + |
| 251 | + ggml_build_forward_expand(gf, cur); |
| 252 | +} |
0 commit comments