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