1#include "models.h"
2
3llm_build_neo_bert::llm_build_neo_bert(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 ggml_tensor * inp_pos = build_inp_pos();
12
13 // construct input embeddings (token, type, position)
14 inpL = build_inp_embd(tok_embd: model.tok_embd);
15 cb(cur: inpL, name: "inp_embd", il: -1);
16
17 auto * inp_attn = build_attn_inp_no_cache();
18
19 ggml_tensor * inp_out_ids = build_inp_out_ids();
20
21 for (int il = 0; il < n_layer; ++il) {
22 ggml_tensor * cur = inpL;
23
24 // pre-norm
25 cur = build_norm(cur: inpL,
26 mw: model.layers[il].attn_norm, NULL,
27 type: LLM_NORM_RMS, il);
28
29 {
30 ggml_tensor * Qcur;
31 ggml_tensor * Kcur;
32 ggml_tensor * Vcur;
33
34 // self-attention
35 cur = build_lora_mm(w: model.layers[il].wqkv, cur);
36 cb(cur, name: "wqkv", il);
37
38 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));
39 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));
40 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));
41
42 // RoPE
43 Qcur = ggml_rope_ext(
44 ctx: ctx0, a: Qcur, b: inp_pos, c: nullptr,
45 n_dims: n_rot, mode: rope_type, n_ctx_orig, freq_base, freq_scale,
46 ext_factor, attn_factor, beta_fast, beta_slow
47 );
48
49 Kcur = ggml_rope_ext(
50 ctx: ctx0, a: Kcur, b: inp_pos, c: nullptr,
51 n_dims: n_rot, mode: rope_type, n_ctx_orig, freq_base, freq_scale,
52 ext_factor, attn_factor, beta_fast, beta_slow
53 );
54
55 cb(cur: Qcur, name: "Qcur", il);
56 cb(cur: Kcur, name: "Kcur", il);
57 cb(cur: Vcur, name: "Vcur", il);
58
59 cur = build_attn(inp: inp_attn,
60 wo: model.layers[il].wo, wo_b: nullptr,
61 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);
62 cb(cur, name: "kqv_out", il);
63 }
64 if (il == n_layer - 1 && inp_out_ids) {
65 cur = ggml_get_rows(ctx: ctx0, a: cur, b: inp_out_ids);
66 inpL = ggml_get_rows(ctx: ctx0, a: inpL, b: inp_out_ids);
67 }
68 // re-add the layer input
69 cur = ggml_add(ctx: ctx0, a: cur, b: inpL);
70
71 ggml_tensor * ffn_inp = cur;
72 cb(cur: ffn_inp, name: "ffn_inp", il);
73
74 // pre-norm
75 cur = build_norm(cur: ffn_inp,
76 mw: model.layers[il].ffn_norm, NULL,
77 type: LLM_NORM_RMS, il);
78 cb(cur, name: "ffn_norm", il);
79
80 // feed-forward network
81 cur = build_ffn(cur,
82 up: model.layers[il].ffn_up,
83 NULL, NULL, NULL, NULL, NULL,
84 down: model.layers[il].ffn_down,
85 NULL, NULL, NULL,
86 type_op: LLM_FFN_SWIGLU, type_gate: LLM_FFN_SEQ, il);
87
88 // attentions bypass the intermediate layer
89 cur = ggml_add(ctx: ctx0, a: cur, b: ffn_inp);
90
91 // input for next layer
92 inpL = cur;
93 }
94 cur = inpL;
95
96 cur = build_norm(cur,
97 mw: model.output_norm_enc, NULL,
98 type: LLM_NORM_RMS, il: -1);
99
100 cb(cur, name: "result_embd", il: -1);
101 res->t_embd = cur;
102
103 ggml_build_forward_expand(cgraph: gf, tensor: cur);
104}
105