| 5204 | } |
| 5205 | |
| 5206 | struct ggml_cgraph * build_baichuan() { |
| 5207 | struct ggml_cgraph * gf = ggml_new_graph_custom(ctx0, LLAMA_MAX_NODES, false); |
| 5208 | |
| 5209 | struct ggml_tensor * cur; |
| 5210 | struct ggml_tensor * inpL; |
| 5211 | |
| 5212 | inpL = llm_build_inp_embd(ctx0, hparams, batch, model.tok_embd, cb); |
| 5213 | cb(inpL, "inp_embd", -1); |
| 5214 | |
| 5215 | // inp_pos - contains the positions |
| 5216 | struct ggml_tensor * inp_pos = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, n_tokens); |
| 5217 | cb(inp_pos, "inp_pos", -1); |
| 5218 | |
| 5219 | // KQ_scale |
| 5220 | struct ggml_tensor * KQ_scale = ggml_new_tensor_1d(ctx0, GGML_TYPE_F32, 1); |
| 5221 | cb(KQ_scale, "KQ_scale", -1); |
| 5222 | |
| 5223 | // KQ_mask (mask for 1 head, it will be broadcasted to all heads) |
| 5224 | struct ggml_tensor * KQ_mask = ggml_new_tensor_3d(ctx0, GGML_TYPE_F32, n_kv, n_tokens, 1); |
| 5225 | cb(KQ_mask, "KQ_mask", -1); |
| 5226 | |
| 5227 | // shift the entire K-cache if needed |
| 5228 | if (do_rope_shift) { |
| 5229 | llm_build_k_shift(ctx0, hparams, cparams, kv_self, gf, LLM_ROPE, n_ctx, n_embd_head, freq_base, freq_scale, cb); |
| 5230 | } |
| 5231 | |
| 5232 | for (int il = 0; il < n_layer; ++il) { |
| 5233 | struct ggml_tensor * inpSA = inpL; |
| 5234 | |
| 5235 | cur = llm_build_norm(ctx0, inpL, hparams, |
| 5236 | model.layers[il].attn_norm, NULL, |
| 5237 | LLM_NORM_RMS, cb, il); |
| 5238 | cb(cur, "attn_norm", il); |
| 5239 | |
| 5240 | // self-attention |
| 5241 | { |
| 5242 | struct ggml_tensor * Qcur = ggml_mul_mat(ctx0, model.layers[il].wq, cur); |
| 5243 | cb(Qcur, "Qcur", il); |
| 5244 | |
| 5245 | struct ggml_tensor * Kcur = ggml_mul_mat(ctx0, model.layers[il].wk, cur); |
| 5246 | cb(Kcur, "Kcur", il); |
| 5247 | |
| 5248 | struct ggml_tensor * Vcur = ggml_mul_mat(ctx0, model.layers[il].wv, cur); |
| 5249 | cb(Vcur, "Vcur", il); |
| 5250 | |
| 5251 | switch (model.type) { |
| 5252 | case MODEL_7B: |
| 5253 | Qcur = ggml_rope_custom( |
| 5254 | ctx0, ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens), inp_pos, |
| 5255 | n_embd_head, 0, 0, n_orig_ctx, freq_base, freq_scale, |
| 5256 | ext_factor, attn_factor, beta_fast, beta_slow |
| 5257 | ); |
| 5258 | Kcur = ggml_rope_custom( |
| 5259 | ctx0, ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens), inp_pos, |
| 5260 | n_embd_head, 0, 0, n_orig_ctx, freq_base, freq_scale, |
| 5261 | ext_factor, attn_factor, beta_fast, beta_slow |
| 5262 | ); |
| 5263 | break; |
no test coverage detected