1#include "models.h"
2
3
4
5llm_build_dots1::llm_build_dots1(const llama_model & model, const llm_graph_params & params) :
6 llm_graph_context(params) {
7 const int64_t n_embd_head = hparams.n_embd_head_v;
8
9 GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
10 GGML_ASSERT(n_embd_head == hparams.n_rot);
11
12 ggml_tensor * cur;
13 ggml_tensor * inpL;
14
15 inpL = build_inp_embd(tok_embd: model.tok_embd);
16
17 // inp_pos - contains the positions
18 ggml_tensor * inp_pos = build_inp_pos();
19
20 auto * inp_attn = build_attn_inp_kv();
21
22 ggml_tensor * inp_out_ids = build_inp_out_ids();
23
24 for (int il = 0; il < n_layer; ++il) {
25 ggml_tensor * inpSA = inpL;
26
27 // norm
28 cur = build_norm(cur: inpL, mw: model.layers[il].attn_norm, NULL, 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 = build_norm(cur: Qcur, mw: model.layers[il].attn_q_norm, NULL, type: LLM_NORM_RMS, il);
48 cb(cur: Qcur, name: "Qcur_normed", il);
49
50 Qcur = ggml_rope_ext(ctx: ctx0, a: Qcur, b: inp_pos, c: nullptr, n_dims: n_rot, mode: rope_type, n_ctx_orig, freq_base, freq_scale,
51 ext_factor, attn_factor, beta_fast, beta_slow);
52
53 Kcur = build_norm(cur: Kcur, mw: model.layers[il].attn_k_norm, NULL, type: LLM_NORM_RMS, il);
54 cb(cur: Kcur, name: "Kcur_normed", il);
55
56 Kcur = ggml_rope_ext(ctx: ctx0, a: Kcur, b: inp_pos, c: nullptr, n_dims: n_rot, mode: rope_type, n_ctx_orig, freq_base, freq_scale,
57 ext_factor, attn_factor, beta_fast, beta_slow);
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, wo_b: model.layers[il].bo,
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 if (il == n_layer - 1 && inp_out_ids) {
68 cur = ggml_get_rows(ctx: ctx0, a: cur, b: inp_out_ids);
69 inpSA = ggml_get_rows(ctx: ctx0, a: inpSA, b: inp_out_ids);
70 }
71 ggml_tensor * ffn_inp = ggml_add(ctx: ctx0, a: cur, b: inpSA);
72 cb(cur: ffn_inp, name: "ffn_inp", il);
73
74 // MoE branch
75 cur = build_norm(cur: ffn_inp, mw: model.layers[il].ffn_norm, NULL, type: LLM_NORM_RMS, il);
76 cb(cur, name: "ffn_norm", il);
77
78 if ((uint32_t) il < hparams.n_layer_dense_lead) {
79 cur = build_ffn(cur,
80 up: model.layers[il].ffn_up, NULL, NULL,
81 gate: model.layers[il].ffn_gate, NULL, NULL,
82 down: model.layers[il].ffn_down, NULL, NULL,
83 NULL, type_op: LLM_FFN_SILU, type_gate: LLM_FFN_PAR, il);
84 cb(cur, name: "ffn_out", il);
85 } else {
86 ggml_tensor * moe_out = build_moe_ffn(cur,
87 gate_inp: model.layers[il].ffn_gate_inp,
88 up_exps: model.layers[il].ffn_up_exps,
89 gate_exps: model.layers[il].ffn_gate_exps,
90 down_exps: model.layers[il].ffn_down_exps,
91 exp_probs_b: model.layers[il].ffn_exp_probs_b,
92 n_expert, n_expert_used,
93 type_op: LLM_FFN_SILU, norm_w: hparams.expert_weights_norm,
94 scale_w: true, w_scale: hparams.expert_weights_scale,
95 gating_op: (llama_expert_gating_func_type) hparams.expert_gating_func,
96 il);
97 cb(cur: moe_out, name: "ffn_moe_out", il);
98
99 {
100 ggml_tensor * ffn_shexp =
101 build_ffn(cur,
102 up: model.layers[il].ffn_up_shexp, NULL, NULL,
103 gate: model.layers[il].ffn_gate_shexp, NULL, NULL,
104 down: model.layers[il].ffn_down_shexp, NULL, NULL,
105 NULL, type_op: LLM_FFN_SILU, type_gate: LLM_FFN_PAR, il);
106 cb(cur: ffn_shexp, name: "ffn_shexp", il);
107
108 cur = ggml_add(ctx: ctx0, a: moe_out, b: ffn_shexp);
109 cb(cur, name: "ffn_out", il);
110 }
111 }
112 cur = ggml_add(ctx: ctx0, a: cur, b: ffn_inp);
113
114 cur = build_cvec(cur, il);
115 cb(cur, name: "l_out", il);
116
117 // input for next layer
118 inpL = cur;
119 }
120 cur = inpL;
121
122 cur = build_norm(cur, mw: model.output_norm, NULL, type: LLM_NORM_RMS, il: -1);
123
124 cb(cur, name: "result_norm", il: -1);
125 res->t_embd = cur;
126
127 // lm_head
128 cur = build_lora_mm(w: model.output, cur);
129
130 cb(cur, name: "result_output", il: -1);
131 res->t_logits = cur;
132
133 ggml_build_forward_expand(cgraph: gf, tensor: cur);
134}
135