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