1#include "models.h"
2
3llm_build_gpt2::llm_build_gpt2(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 const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
6
7 GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
8
9 ggml_tensor * cur;
10 ggml_tensor * pos;
11 ggml_tensor * inpL;
12
13 inpL = build_inp_embd(tok_embd: model.tok_embd);
14
15 // inp_pos - contains the positions
16 ggml_tensor * inp_pos = build_inp_pos();
17
18 auto * inp_attn = build_attn_inp_kv();
19
20 pos = ggml_get_rows(ctx: ctx0, a: model.pos_embd, b: inp_pos);
21 cb(cur: pos, name: "pos_embd", il: -1);
22
23 inpL = ggml_add(ctx: ctx0, a: inpL, b: pos);
24 cb(cur: inpL, name: "inpL", il: -1);
25
26 ggml_tensor * inp_out_ids = build_inp_out_ids();
27
28 for (int il = 0; il < n_layer; ++il) {
29 cur = build_norm(cur: inpL,
30 mw: model.layers[il].attn_norm,
31 mb: model.layers[il].attn_norm_b,
32 type: LLM_NORM, il);
33 cb(cur, name: "attn_norm", il);
34
35 // self-attention
36 {
37 cur = build_lora_mm(w: model.layers[il].wqkv, cur);
38 cb(cur, name: "wqkv", il);
39
40 cur = ggml_add(ctx: ctx0, a: cur, b: model.layers[il].bqkv);
41 cb(cur, name: "bqkv", il);
42
43 ggml_tensor * Qcur = ggml_view_3d(ctx: ctx0, a: cur, ne0: n_embd_head, ne1: n_head, ne2: n_tokens, nb1: n_embd_head*sizeof(float), nb2: cur->nb[1], offset: 0*sizeof(float)*(n_embd));
44 ggml_tensor * Kcur = ggml_view_3d(ctx: ctx0, a: cur, ne0: n_embd_head, ne1: n_head_kv, ne2: n_tokens, nb1: n_embd_head*sizeof(float), nb2: cur->nb[1], offset: 1*sizeof(float)*(n_embd));
45 ggml_tensor * Vcur = ggml_view_3d(ctx: ctx0, a: cur, ne0: n_embd_head, ne1: n_head_kv, ne2: n_tokens, nb1: n_embd_head*sizeof(float), nb2: cur->nb[1], offset: 1*sizeof(float)*(n_embd + n_embd_gqa));
46
47 cb(cur: Qcur, name: "Qcur", il);
48 cb(cur: Kcur, name: "Kcur", il);
49 cb(cur: Vcur, name: "Vcur", il);
50
51 cur = build_attn(inp: inp_attn,
52 wo: model.layers[il].wo, wo_b: model.layers[il].bo,
53 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);
54 }
55
56 if (il == n_layer - 1 && inp_out_ids) {
57 cur = ggml_get_rows(ctx: ctx0, a: cur, b: inp_out_ids);
58 inpL = ggml_get_rows(ctx: ctx0, a: inpL, b: inp_out_ids);
59 }
60
61 // add the input
62 ggml_tensor * ffn_inp = ggml_add(ctx: ctx0, a: cur, b: inpL);
63 cb(cur: ffn_inp, name: "ffn_inp", il);
64
65 // FF
66 {
67 cur = build_norm(cur: ffn_inp,
68 mw: model.layers[il].ffn_norm,
69 mb: model.layers[il].ffn_norm_b,
70 type: LLM_NORM, il);
71 cb(cur, name: "ffn_norm", il);
72
73 cur = build_ffn(cur,
74 up: model.layers[il].ffn_up, up_b: model.layers[il].ffn_up_b, NULL,
75 NULL, NULL, NULL,
76 down: model.layers[il].ffn_down, down_b: model.layers[il].ffn_down_b, NULL,
77 NULL,
78 type_op: LLM_FFN_GELU, type_gate: LLM_FFN_SEQ, il);
79 cb(cur, name: "ffn_out", il);
80 }
81
82 cur = ggml_add(ctx: ctx0, a: cur, b: ffn_inp);
83
84 cur = build_cvec(cur, il);
85 cb(cur, name: "l_out", il);
86
87 // input for next layer
88 inpL = cur;
89 }
90
91 cur = build_norm(cur: inpL,
92 mw: model.output_norm,
93 mb: model.output_norm_b,
94 type: LLM_NORM, il: -1);
95
96 cb(cur, name: "result_norm", il: -1);
97 res->t_embd = cur;
98
99 cur = build_lora_mm(w: model.output, cur);
100
101 cb(cur, name: "result_output", il: -1);
102 res->t_logits = cur;
103
104 ggml_build_forward_expand(cgraph: gf, tensor: cur);
105}
106