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