| 1 | #include "models.h" |
| 2 | |
| 3 | |
| 4 | |
| 5 | llm_build_exaone::llm_build_exaone(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(tok_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 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
| 23 | |
| 24 | for (int il = 0; il < n_layer; ++il) { |
| 25 | ggml_tensor * inpSA = inpL; |
| 26 | |
| 27 | // norm |
| 28 | cur = build_norm(cur: inpL, mw: model.layers[il].attn_norm, NULL, type: LLM_NORM_RMS, il); |
| 29 | cb(cur, name: "attn_norm" , il); |
| 30 | |
| 31 | // self-attention |
| 32 | { |
| 33 | // rope freq factors for llama3; may return nullptr for llama2 and other models |
| 34 | ggml_tensor * rope_factors = model.get_rope_factors(cparams, il); |
| 35 | |
| 36 | // compute Q and K and RoPE them |
| 37 | ggml_tensor * Qcur = build_lora_mm(w: model.layers[il].wq, cur); |
| 38 | cb(cur: Qcur, name: "Qcur" , il); |
| 39 | if (model.layers[il].bq) { |
| 40 | Qcur = ggml_add(ctx: ctx0, a: Qcur, b: model.layers[il].bq); |
| 41 | cb(cur: Qcur, name: "Qcur" , il); |
| 42 | } |
| 43 | ggml_tensor * Kcur = build_lora_mm(w: model.layers[il].wk, cur); |
| 44 | cb(cur: Kcur, name: "Kcur" , il); |
| 45 | if (model.layers[il].bk) { |
| 46 | Kcur = ggml_add(ctx: ctx0, a: Kcur, b: model.layers[il].bk); |
| 47 | cb(cur: Kcur, name: "Kcur" , il); |
| 48 | } |
| 49 | ggml_tensor * Vcur = build_lora_mm(w: model.layers[il].wv, cur); |
| 50 | cb(cur: Vcur, name: "Vcur" , il); |
| 51 | if (model.layers[il].bv) { |
| 52 | Vcur = ggml_add(ctx: ctx0, a: Vcur, b: model.layers[il].bv); |
| 53 | cb(cur: Vcur, name: "Vcur" , il); |
| 54 | } |
| 55 | Qcur = ggml_reshape_3d(ctx: ctx0, a: Qcur, ne0: n_embd_head, ne1: n_head, ne2: n_tokens); |
| 56 | Kcur = ggml_reshape_3d(ctx: ctx0, a: Kcur, ne0: n_embd_head, ne1: n_head_kv, ne2: n_tokens); |
| 57 | Vcur = ggml_reshape_3d(ctx: ctx0, a: Vcur, ne0: n_embd_head, ne1: n_head_kv, ne2: n_tokens); |
| 58 | |
| 59 | Qcur = ggml_rope_ext(ctx: ctx0, a: Qcur, b: inp_pos, c: rope_factors, n_dims: n_rot, mode: rope_type, n_ctx_orig, freq_base, freq_scale, |
| 60 | ext_factor, attn_factor, beta_fast, beta_slow); |
| 61 | |
| 62 | Kcur = ggml_rope_ext(ctx: ctx0, a: Kcur, b: inp_pos, c: rope_factors, n_dims: n_rot, mode: rope_type, n_ctx_orig, freq_base, freq_scale, |
| 63 | ext_factor, attn_factor, beta_fast, beta_slow); |
| 64 | |
| 65 | cb(cur: Qcur, name: "Qcur" , il); |
| 66 | cb(cur: Kcur, name: "Kcur" , il); |
| 67 | cb(cur: Vcur, name: "Vcur" , il); |
| 68 | |
| 69 | cur = build_attn(inp: inp_attn, |
| 70 | wo: model.layers[il].wo, wo_b: model.layers[il].bo, |
| 71 | 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); |
| 72 | } |
| 73 | if (il == n_layer - 1 && inp_out_ids) { |
| 74 | cur = ggml_get_rows(ctx: ctx0, a: cur, b: inp_out_ids); |
| 75 | inpSA = ggml_get_rows(ctx: ctx0, a: inpSA, b: inp_out_ids); |
| 76 | } |
| 77 | ggml_tensor * ffn_inp = ggml_add(ctx: ctx0, a: cur, b: inpSA); |
| 78 | cb(cur: ffn_inp, name: "ffn_inp" , il); |
| 79 | |
| 80 | // feed-forward network |
| 81 | cur = build_norm(cur: ffn_inp, mw: model.layers[il].ffn_norm, NULL, type: LLM_NORM_RMS, il); |
| 82 | cb(cur, name: "ffn_norm" , il); |
| 83 | |
| 84 | cur = build_ffn(cur, |
| 85 | up: model.layers[il].ffn_up, NULL, NULL, |
| 86 | gate: model.layers[il].ffn_gate, NULL, NULL, |
| 87 | down: model.layers[il].ffn_down, NULL, NULL, |
| 88 | NULL, type_op: LLM_FFN_SILU, type_gate: LLM_FFN_PAR, il); |
| 89 | cb(cur, name: "ffn_out" , il); |
| 90 | |
| 91 | cur = ggml_add(ctx: ctx0, a: cur, b: ffn_inp); |
| 92 | cb(cur, name: "ffn_out" , il); |
| 93 | |
| 94 | cur = build_cvec(cur, il); |
| 95 | cb(cur, name: "l_out" , il); |
| 96 | |
| 97 | // input for next layer |
| 98 | inpL = cur; |
| 99 | } |
| 100 | cur = inpL; |
| 101 | |
| 102 | cur = build_norm(cur, mw: model.output_norm, NULL, type: LLM_NORM_RMS, il: -1); |
| 103 | |
| 104 | cb(cur, name: "result_norm" , il: -1); |
| 105 | res->t_embd = cur; |
| 106 | |
| 107 | // lm_head |
| 108 | cur = build_lora_mm(w: model.output, cur); |
| 109 | |
| 110 | cb(cur, name: "result_output" , il: -1); |
| 111 | res->t_logits = cur; |
| 112 | |
| 113 | ggml_build_forward_expand(cgraph: gf, tensor: cur); |
| 114 | } |
| 115 | |