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(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(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
29 cb(cur, "attn_norm", il);
30
31 // self_attention
32 {
33 // compute Q and K and RoPE them
34 ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
35 cb(Qcur, "Qcur", il);
36
37 ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
38 cb(Kcur, "Kcur", il);
39
40 ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
41 cb(Vcur, "Vcur", il);
42
43 Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
44 Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
45 Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
46
47 Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
48 cb(Qcur, "Qcur_normed", il);
49
50 Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
51 ext_factor, attn_factor, beta_fast, beta_slow);
52
53 Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
54 cb(Kcur, "Kcur_normed", il);
55
56 Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
57 ext_factor, attn_factor, beta_fast, beta_slow);
58
59 cb(Qcur, "Qcur", il);
60 cb(Kcur, "Kcur", il);
61 cb(Vcur, "Vcur", il);
62
63 cur = build_attn(inp_attn,
64 model.layers[il].wo, model.layers[il].bo,
65 Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
66 }
67 if (il == n_layer - 1 && inp_out_ids) {
68 cur = ggml_get_rows(ctx0, cur, inp_out_ids);
69 inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
70 }
71 ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
72 cb(ffn_inp, "ffn_inp", il);
73
74 // MoE branch
75 cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
76 cb(cur, "ffn_norm", il);
77
78 if ((uint32_t) il < hparams.n_layer_dense_lead) {
79 cur = build_ffn(cur,
80 model.layers[il].ffn_up, NULL, NULL,
81 model.layers[il].ffn_gate, NULL, NULL,
82 model.layers[il].ffn_down, NULL, NULL,
83 NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
84 cb(cur, "ffn_out", il);
85 } else {
86 ggml_tensor * moe_out = build_moe_ffn(cur,
87 model.layers[il].ffn_gate_inp,
88 model.layers[il].ffn_up_exps,
89 model.layers[il].ffn_gate_exps,
90 model.layers[il].ffn_down_exps,
91 model.layers[il].ffn_exp_probs_b,
92 n_expert, n_expert_used,
93 LLM_FFN_SILU, hparams.expert_weights_norm,
94 true, hparams.expert_weights_scale,
95 (llama_expert_gating_func_type) hparams.expert_gating_func,
96 il);
97 cb(moe_out, "ffn_moe_out", il);
98
99 {
100 ggml_tensor * ffn_shexp =
101 build_ffn(cur,
102 model.layers[il].ffn_up_shexp, NULL, NULL,
103 model.layers[il].ffn_gate_shexp, NULL, NULL,
104 model.layers[il].ffn_down_shexp, NULL, NULL,
105 NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
106 cb(ffn_shexp, "ffn_shexp", il);
107
108 cur = ggml_add(ctx0, moe_out, ffn_shexp);
109 cb(cur, "ffn_out", il);
110 }
111 }
112 cur = ggml_add(ctx0, cur, ffn_inp);
113
114 cur = build_cvec(cur, il);
115 cb(cur, "l_out", il);
116
117 // input for next layer
118 inpL = cur;
119 }
120 cur = inpL;
121
122 cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
123
124 cb(cur, "result_norm", -1);
125 res->t_embd = cur;
126
127 // lm_head
128 cur = build_lora_mm(model.output, cur);
129
130 cb(cur, "result_output", -1);
131 res->t_logits = cur;
132
133 ggml_build_forward_expand(gf, cur);
134}