1#include "models.h"
2
3
4llm_build_arctic::llm_build_arctic(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
5 const int64_t n_embd_head = hparams.n_embd_head_v;
6
7 GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
8 GGML_ASSERT(n_embd_head == hparams.n_rot);
9
10 ggml_tensor * cur;
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 ggml_tensor * inp_out_ids = build_inp_out_ids();
21
22 for (int il = 0; il < n_layer; ++il) {
23 ggml_tensor * inpSA = inpL;
24
25 // norm
26 cur = build_norm(cur: inpL,
27 mw: model.layers[il].attn_norm, NULL,
28 type: LLM_NORM_RMS, il);
29 cb(cur, name: "attn_norm", il);
30
31 // self-attention
32 {
33 // compute Q and K and RoPE them
34 ggml_tensor * Qcur = build_lora_mm(w: model.layers[il].wq, cur);
35 cb(cur: Qcur, name: "Qcur", il);
36
37 ggml_tensor * Kcur = build_lora_mm(w: model.layers[il].wk, cur);
38 cb(cur: Kcur, name: "Kcur", il);
39
40 ggml_tensor * Vcur = build_lora_mm(w: model.layers[il].wv, cur);
41 cb(cur: Vcur, name: "Vcur", il);
42
43 Qcur = ggml_reshape_3d(ctx: ctx0, a: Qcur, ne0: n_embd_head, ne1: n_head, ne2: n_tokens);
44 Kcur = ggml_reshape_3d(ctx: ctx0, a: Kcur, ne0: n_embd_head, ne1: n_head_kv, ne2: n_tokens);
45 Vcur = ggml_reshape_3d(ctx: ctx0, a: Vcur, ne0: n_embd_head, ne1: n_head_kv, ne2: n_tokens);
46
47 Qcur = ggml_rope_ext(
48 ctx: ctx0, a: Qcur, b: inp_pos, c: nullptr,
49 n_dims: n_rot, mode: rope_type, n_ctx_orig, freq_base, freq_scale,
50 ext_factor, attn_factor, beta_fast, beta_slow
51 );
52
53 Kcur = ggml_rope_ext(
54 ctx: ctx0, a: Kcur, b: inp_pos, c: nullptr,
55 n_dims: n_rot, mode: rope_type, n_ctx_orig, freq_base, freq_scale,
56 ext_factor, attn_factor, beta_fast, beta_slow
57 );
58
59 cb(cur: Qcur, name: "Qcur", il);
60 cb(cur: Kcur, name: "Kcur", il);
61 cb(cur: Vcur, name: "Vcur", il);
62
63 cur = build_attn(inp: inp_attn,
64 wo: model.layers[il].wo, NULL,
65 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);
66 }
67
68 if (il == n_layer - 1 && inp_out_ids) {
69 cur = ggml_get_rows(ctx: ctx0, a: cur, b: inp_out_ids);
70 inpSA = ggml_get_rows(ctx: ctx0, a: inpSA, b: inp_out_ids);
71 }
72
73 ggml_tensor * ffn_inp = ggml_add(ctx: ctx0, a: cur, b: inpSA);
74 cb(cur: ffn_inp, name: "ffn_inp", il);
75
76 // feed-forward network
77 cur = build_norm(cur: ffn_inp,
78 mw: model.layers[il].ffn_norm, NULL,
79 type: LLM_NORM_RMS, il);
80 cb(cur, name: "ffn_norm", il);
81
82 cur = build_ffn(cur,
83 up: model.layers[il].ffn_up, NULL, NULL,
84 gate: model.layers[il].ffn_gate, NULL, NULL,
85 down: model.layers[il].ffn_down, NULL, NULL,
86 NULL,
87 type_op: LLM_FFN_SILU, type_gate: LLM_FFN_PAR, il);
88 cb(cur, name: "ffn_out", il);
89
90 ggml_tensor * ffn_out = ggml_add(ctx: ctx0, a: cur, b: ffn_inp);
91 cb(cur: ffn_out, name: "ffn_out", il);
92
93 // MoE
94 cur = build_norm(cur: inpSA,
95 mw: model.layers[il].ffn_norm_exps, NULL,
96 type: LLM_NORM_RMS, il);
97 cb(cur, name: "ffn_norm_exps", il);
98
99 cur = build_moe_ffn(cur,
100 gate_inp: model.layers[il].ffn_gate_inp,
101 up_exps: model.layers[il].ffn_up_exps,
102 gate_exps: model.layers[il].ffn_gate_exps,
103 down_exps: model.layers[il].ffn_down_exps,
104 exp_probs_b: nullptr,
105 n_expert, n_expert_used,
106 type_op: LLM_FFN_SILU, norm_w: true,
107 scale_w: false, w_scale: 0.0,
108 gating_op: LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
109 il);
110 cb(cur, name: "ffn_moe_out", il);
111
112 cur = ggml_add(ctx: ctx0, a: cur, b: ffn_out);
113 cb(cur, name: "ffn_out", il);
114
115 cur = build_cvec(cur, il);
116 cb(cur, name: "l_out", il);
117
118 // input for next layer
119 inpL = cur;
120 }
121
122 cur = inpL;
123
124 cur = build_norm(cur,
125 mw: model.output_norm, NULL,
126 type: LLM_NORM_RMS, il: -1);
127
128 cb(cur, name: "result_norm", il: -1);
129 res->t_embd = cur;
130
131 // lm_head
132 cur = build_lora_mm(w: model.output, cur);
133
134 cb(cur, name: "result_output", il: -1);
135 res->t_logits = cur;
136
137 ggml_build_forward_expand(cgraph: gf, tensor: cur);
138}
139