| 5966 | } |
| 5967 | |
| 5968 | struct ggml_cgraph * build_mpt() { |
| 5969 | struct ggml_cgraph * gf = ggml_new_graph_custom(ctx0, LLAMA_MAX_NODES, false); |
| 5970 | |
| 5971 | struct ggml_tensor * cur; |
| 5972 | struct ggml_tensor * inpL; |
| 5973 | |
| 5974 | inpL = llm_build_inp_embd(ctx0, hparams, batch, model.tok_embd, cb); |
| 5975 | cb(inpL, "inp_embd", -1); |
| 5976 | |
| 5977 | // KQ_scale |
| 5978 | struct ggml_tensor * KQ_scale = ggml_new_tensor_1d(ctx0, GGML_TYPE_F32, 1); |
| 5979 | cb(KQ_scale, "KQ_scale", -1); |
| 5980 | |
| 5981 | // KQ_mask (mask for 1 head, it will be broadcasted to all heads) |
| 5982 | struct ggml_tensor * KQ_mask = ggml_new_tensor_3d(ctx0, GGML_TYPE_F32, n_kv, n_tokens, 1); |
| 5983 | cb(KQ_mask, "KQ_mask", -1); |
| 5984 | |
| 5985 | for (int il = 0; il < n_layer; ++il) { |
| 5986 | struct ggml_tensor * attn_norm; |
| 5987 | |
| 5988 | attn_norm = llm_build_norm(ctx0, inpL, hparams, |
| 5989 | model.layers[il].attn_norm, |
| 5990 | NULL, |
| 5991 | LLM_NORM, cb, il); |
| 5992 | cb(attn_norm, "attn_norm", il); |
| 5993 | |
| 5994 | // self-attention |
| 5995 | { |
| 5996 | cur = attn_norm; |
| 5997 | |
| 5998 | cur = ggml_mul_mat(ctx0, model.layers[il].wqkv, cur); |
| 5999 | cb(cur, "wqkv", il); |
| 6000 | |
| 6001 | if (hparams.f_clamp_kqv > 0.0f) { |
| 6002 | cur = ggml_clamp(ctx0, cur, -hparams.f_clamp_kqv, hparams.f_clamp_kqv); |
| 6003 | cb(cur, "wqkv_clamped", il); |
| 6004 | } |
| 6005 | |
| 6006 | struct ggml_tensor * Qcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd, n_tokens, cur->nb[1], 0*sizeof(float)*(n_embd))); |
| 6007 | struct ggml_tensor * Kcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd))); |
| 6008 | struct ggml_tensor * Vcur = ggml_cont(ctx0, ggml_view_2d(ctx0, cur, n_embd_gqa, n_tokens, cur->nb[1], 1*sizeof(float)*(n_embd + n_embd_gqa))); |
| 6009 | |
| 6010 | cb(Qcur, "Qcur", il); |
| 6011 | cb(Kcur, "Kcur", il); |
| 6012 | cb(Vcur, "Vcur", il); |
| 6013 | |
| 6014 | Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens); |
| 6015 | |
| 6016 | llm_build_kv_store(ctx0, hparams, kv_self, gf, Kcur, Vcur, n_ctx, n_tokens, kv_head, cb, il); |
| 6017 | |
| 6018 | cur = llm_build_kqv(ctx0, hparams, kv_self, |
| 6019 | model.layers[il].wo, NULL, |
| 6020 | Qcur, KQ_scale, KQ_mask, n_ctx, n_tokens, n_kv, hparams.f_max_alibi_bias, cb, il); |
| 6021 | cb(cur, "kqv_out", il); |
| 6022 | } |
| 6023 | |
| 6024 | // Add the input |
| 6025 | struct ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL); |
no test coverage detected