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