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