| 3 | |
| 4 | |
| 5 | llm_build_deepseek::llm_build_deepseek(const llama_model & model, const llm_graph_params & params) : |
| 6 | llm_graph_context(params) { |
| 7 | const int64_t n_embd_head = hparams.n_embd_head_v; |
| 8 | |
| 9 | GGML_ASSERT(n_embd_head == hparams.n_embd_head_k); |
| 10 | GGML_ASSERT(n_embd_head == hparams.n_rot); |
| 11 | |
| 12 | ggml_tensor * cur; |
| 13 | ggml_tensor * inpL; |
| 14 | |
| 15 | inpL = build_inp_embd(model.tok_embd); |
| 16 | |
| 17 | // inp_pos - contains the positions |
| 18 | ggml_tensor * inp_pos = build_inp_pos(); |
| 19 | |
| 20 | auto * inp_attn = build_attn_inp_kv(); |
| 21 | |
| 22 | const float kq_scale = |
| 23 | hparams.f_attention_scale == 0.0f ? 1.0f / sqrtf(float(n_embd_head)) : hparams.f_attention_scale; |
| 24 | |
| 25 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
| 26 | |
| 27 | for (int il = 0; il < n_layer; ++il) { |
| 28 | ggml_tensor * inpSA = inpL; |
| 29 | |
| 30 | // norm |
| 31 | cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il); |
| 32 | cb(cur, "attn_norm", il); |
| 33 | |
| 34 | // self-attention |
| 35 | { |
| 36 | // rope freq factors for llama3; may return nullptr for llama2 and other models |
| 37 | ggml_tensor * rope_factors = model.get_rope_factors(cparams, il); |
| 38 | |
| 39 | // compute Q and K and RoPE them |
| 40 | ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur); |
| 41 | cb(Qcur, "Qcur", il); |
| 42 | if (model.layers[il].bq) { |
| 43 | Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq); |
| 44 | cb(Qcur, "Qcur", il); |
| 45 | } |
| 46 | ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur); |
| 47 | cb(Kcur, "Kcur", il); |
| 48 | if (model.layers[il].bk) { |
| 49 | Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk); |
| 50 | cb(Kcur, "Kcur", il); |
| 51 | } |
| 52 | ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur); |
| 53 | cb(Vcur, "Vcur", il); |
| 54 | if (model.layers[il].bv) { |
| 55 | Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv); |
| 56 | cb(Vcur, "Vcur", il); |
| 57 | } |
| 58 | Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens); |
| 59 | Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens); |
| 60 | Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens); |
| 61 | |
| 62 | Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, rope_factors, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
nothing calls this directly
no test coverage detected