| 1 | #include "models.h" |
| 2 | |
| 3 | llm_build_plamo::llm_build_plamo(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) { |
| 4 | const int64_t n_embd_head = hparams.n_embd_head_v; |
| 5 | |
| 6 | GGML_ASSERT(n_embd_head == hparams.n_embd_head_k); |
| 7 | GGML_ASSERT(n_embd_head == hparams.n_rot); |
| 8 | |
| 9 | ggml_tensor * cur; |
| 10 | ggml_tensor * inpL; |
| 11 | |
| 12 | inpL = build_inp_embd(tok_embd: model.tok_embd); |
| 13 | |
| 14 | // inp_pos - contains the positions |
| 15 | ggml_tensor * inp_pos = build_inp_pos(); |
| 16 | |
| 17 | auto * inp_attn = build_attn_inp_kv(); |
| 18 | |
| 19 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
| 20 | |
| 21 | for (int il = 0; il < n_layer; ++il) { |
| 22 | // norm |
| 23 | cur = build_norm(cur: inpL, |
| 24 | mw: model.layers[il].attn_norm, NULL, |
| 25 | type: LLM_NORM_RMS, il); |
| 26 | cb(cur, name: "attn_norm" , il); |
| 27 | |
| 28 | ggml_tensor * sa_inp = cur; |
| 29 | |
| 30 | // self-attention |
| 31 | { |
| 32 | // compute Q and K and RoPE them |
| 33 | ggml_tensor * Qcur = build_lora_mm(w: model.layers[il].wq, cur); |
| 34 | cb(cur: Qcur, name: "Qcur" , il); |
| 35 | |
| 36 | ggml_tensor * Kcur = build_lora_mm(w: model.layers[il].wk, cur); |
| 37 | cb(cur: Kcur, name: "Kcur" , il); |
| 38 | |
| 39 | ggml_tensor * Vcur = build_lora_mm(w: model.layers[il].wv, cur); |
| 40 | cb(cur: Vcur, name: "Vcur" , il); |
| 41 | |
| 42 | Qcur = ggml_reshape_3d(ctx: ctx0, a: Qcur, ne0: n_embd_head, ne1: n_head, ne2: n_tokens); |
| 43 | Kcur = ggml_reshape_3d(ctx: ctx0, a: Kcur, ne0: n_embd_head, ne1: n_head_kv, ne2: n_tokens); |
| 44 | Vcur = ggml_reshape_3d(ctx: ctx0, a: Vcur, ne0: n_embd_head, ne1: n_head_kv, ne2: n_tokens); |
| 45 | |
| 46 | Qcur = ggml_rope_ext( |
| 47 | ctx: ctx0, a: Qcur, b: inp_pos, c: nullptr, |
| 48 | n_dims: n_embd_head, mode: rope_type, n_ctx_orig, freq_base, freq_scale, |
| 49 | ext_factor, attn_factor, beta_fast, beta_slow |
| 50 | ); |
| 51 | |
| 52 | Kcur = ggml_rope_ext( |
| 53 | ctx: ctx0, a: Kcur, b: inp_pos, c: nullptr, |
| 54 | n_dims: n_embd_head, mode: rope_type, n_ctx_orig, freq_base, freq_scale, |
| 55 | ext_factor, attn_factor, beta_fast, beta_slow |
| 56 | ); |
| 57 | |
| 58 | cb(cur: Qcur, name: "Qcur" , il); |
| 59 | cb(cur: Kcur, name: "Kcur" , il); |
| 60 | cb(cur: Vcur, name: "Vcur" , il); |
| 61 | |
| 62 | cur = build_attn(inp: inp_attn, |
| 63 | wo: model.layers[il].wo, NULL, |
| 64 | q_cur: Qcur, k_cur: Kcur, v_cur: Vcur, kq_b: nullptr, sinks: nullptr, v_mla: nullptr, kq_scale: 1.0f/sqrtf(x: float(n_embd_head)), il); |
| 65 | } |
| 66 | if (il == n_layer - 1 && inp_out_ids) { |
| 67 | cur = ggml_get_rows(ctx: ctx0, a: cur, b: inp_out_ids); |
| 68 | sa_inp = ggml_get_rows(ctx: ctx0, a: sa_inp, b: inp_out_ids); |
| 69 | inpL = ggml_get_rows(ctx: ctx0, a: inpL, b: inp_out_ids); |
| 70 | } |
| 71 | ggml_tensor * sa_out = cur; |
| 72 | |
| 73 | cur = sa_inp; |
| 74 | |
| 75 | // feed-forward network |
| 76 | { |
| 77 | cur = build_ffn(cur, |
| 78 | up: model.layers[il].ffn_up, NULL, NULL, |
| 79 | gate: model.layers[il].ffn_gate, NULL, NULL, |
| 80 | down: model.layers[il].ffn_down, NULL, NULL, |
| 81 | NULL, |
| 82 | type_op: LLM_FFN_SILU, type_gate: LLM_FFN_PAR, il); |
| 83 | cb(cur, name: "ffn_out" , il); |
| 84 | } |
| 85 | cur = ggml_add(ctx: ctx0, a: cur, b: sa_out); |
| 86 | cur = ggml_add(ctx: ctx0, a: cur, b: inpL); |
| 87 | |
| 88 | cur = build_cvec(cur, il); |
| 89 | cb(cur, name: "l_out" , il); |
| 90 | |
| 91 | // input for next layer |
| 92 | inpL = cur; |
| 93 | } |
| 94 | cur = inpL; |
| 95 | |
| 96 | cur = build_norm(cur, |
| 97 | mw: model.output_norm, NULL, |
| 98 | type: LLM_NORM_RMS, il: -1); |
| 99 | |
| 100 | cb(cur, name: "result_norm" , il: -1); |
| 101 | res->t_embd = cur; |
| 102 | |
| 103 | // lm_head |
| 104 | cur = build_lora_mm(w: model.output, cur); |
| 105 | |
| 106 | cb(cur, name: "result_output" , il: -1); |
| 107 | res->t_logits = cur; |
| 108 | |
| 109 | ggml_build_forward_expand(cgraph: gf, tensor: cur); |
| 110 | } |
| 111 | |