1#include "models.h"
2
3
4
5llm_build_bailingmoe2::llm_build_bailingmoe2(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 const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
9
10 GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
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 const int n_transformer_layers = n_layer - hparams.nextn_predict_layers;
25 for (int il = 0; il < n_transformer_layers; ++il) {
26 ggml_tensor * inpSA = inpL;
27
28 // norm
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 cur = build_lora_mm(w: model.layers[il].wqkv, cur);
35 cb(cur, name: "wqkv", il);
36
37 ggml_tensor * Qcur = ggml_view_3d(ctx: ctx0, a: cur, ne0: n_embd_head, ne1: n_head, ne2: n_tokens, nb1: n_embd_head * sizeof(float),
38 nb2: cur->nb[1], offset: 0 * sizeof(float) * (n_embd));
39 ggml_tensor * Kcur = ggml_view_3d(ctx: ctx0, a: cur, ne0: n_embd_head, ne1: n_head_kv, ne2: n_tokens, nb1: n_embd_head * sizeof(float),
40 nb2: cur->nb[1], offset: 1 * sizeof(float) * (n_embd));
41 ggml_tensor * Vcur = ggml_view_3d(ctx: ctx0, a: cur, ne0: n_embd_head, ne1: n_head_kv, ne2: n_tokens, nb1: n_embd_head * sizeof(float),
42 nb2: cur->nb[1], offset: 1 * sizeof(float) * (n_embd + n_embd_gqa));
43
44 Qcur = build_norm(cur: Qcur, mw: model.layers[il].attn_q_norm, NULL, type: LLM_NORM_RMS, il);
45 cb(cur: Qcur, name: "Qcur_normed", il);
46
47 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,
48 ext_factor, attn_factor, beta_fast, beta_slow);
49
50 Kcur = build_norm(cur: Kcur, mw: model.layers[il].attn_k_norm, NULL, type: LLM_NORM_RMS, il);
51 cb(cur: Kcur, name: "Kcur_normed", il);
52
53 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,
54 ext_factor, attn_factor, beta_fast, beta_slow);
55
56 cb(cur: Qcur, name: "Qcur", il);
57 cb(cur: Kcur, name: "Kcur", il);
58 cb(cur: Vcur, name: "Vcur", il);
59
60 cur = build_attn(inp: inp_attn,
61 wo: model.layers[il].wo, wo_b: model.layers[il].bo,
62 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);
63 }
64
65 if (il == n_transformer_layers - 1 && inp_out_ids) {
66 cur = ggml_get_rows(ctx: ctx0, a: cur, b: inp_out_ids);
67 inpSA = ggml_get_rows(ctx: ctx0, a: inpSA, b: inp_out_ids);
68 }
69
70 ggml_tensor * sa_out = ggml_add(ctx: ctx0, a: cur, b: inpSA);
71 cb(cur: sa_out, name: "sa_out", il);
72
73 // MoE branch
74 cur = build_norm(cur: sa_out, mw: model.layers[il].ffn_norm, NULL, type: LLM_NORM_RMS, il);
75 cb(cur, name: "ffn_norm", il);
76
77 if (static_cast<uint32_t>(il) < hparams.n_layer_dense_lead) {
78 cur = build_ffn(cur,
79 up: model.layers[il].ffn_up, NULL, NULL,
80 gate: model.layers[il].ffn_gate, NULL, NULL,
81 down: model.layers[il].ffn_down, NULL, NULL,
82 NULL, type_op: LLM_FFN_SILU, type_gate: LLM_FFN_PAR, il);
83 cb(cur, name: "ffn_out", il);
84 } else {
85 ggml_tensor * moe_out = build_moe_ffn(cur,
86 gate_inp: model.layers[il].ffn_gate_inp,
87 up_exps: model.layers[il].ffn_up_exps,
88 gate_exps: model.layers[il].ffn_gate_exps,
89 down_exps: model.layers[il].ffn_down_exps,
90 exp_probs_b: model.layers[il].ffn_exp_probs_b,
91 n_expert, n_expert_used,
92 type_op: LLM_FFN_SILU, norm_w: hparams.expert_weights_norm,
93 scale_w: true, w_scale: hparams.expert_weights_scale,
94 gating_op: (llama_expert_gating_func_type) hparams.expert_gating_func,
95 il);
96 cb(cur: moe_out, name: "ffn_moe_out", il);
97
98 {
99 ggml_tensor * ffn_shexp =
100 build_ffn(cur,
101 up: model.layers[il].ffn_up_shexp, NULL, NULL,
102 gate: model.layers[il].ffn_gate_shexp, NULL, NULL,
103 down: model.layers[il].ffn_down_shexp, NULL, NULL,
104 NULL, type_op: LLM_FFN_SILU, type_gate: LLM_FFN_PAR, il);
105 cb(cur: ffn_shexp, name: "ffn_shexp", il);
106
107 cur = ggml_add(ctx: ctx0, a: moe_out, b: ffn_shexp);
108 cb(cur, name: "ffn_out", il);
109 }
110 }
111
112 cur = ggml_add(ctx: ctx0, a: cur, b: sa_out);
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
121 cur = inpL;
122
123 cur = build_norm(cur, mw: model.output_norm, NULL, type: LLM_NORM_RMS, il: -1);
124
125 cb(cur, name: "result_norm", il: -1);
126 res->t_embd = cur;
127
128 // lm_head
129 cur = build_lora_mm(w: model.output, cur);
130
131 cb(cur, name: "result_output", il: -1);
132 res->t_logits = cur;
133
134 ggml_build_forward_expand(cgraph: gf, tensor: cur);
135}
136