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