1
2#include "models.h"
3
4llm_build_minimax_m2::llm_build_minimax_m2(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
5 const int64_t n_embd_head = hparams.n_embd_head_v;
6
7 GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
8 // GGML_ASSERT(n_embd_head == hparams.n_rot); this is wrong in case of minimax, head_dim = 128, n_rot = 64
9
10 ggml_tensor * cur;
11 ggml_tensor * inpL;
12
13 inpL = build_inp_embd(tok_embd: model.tok_embd);
14
15 ggml_tensor * inp_pos = build_inp_pos();
16 auto inp_attn = build_attn_inp_kv();
17 ggml_tensor * inp_out_ids = build_inp_out_ids();
18
19 for (int il = 0; il < n_layer; ++il) {
20 ggml_tensor * inpSA = inpL;
21
22 cur = inpL;
23
24 // self_attention
25 {
26 cur = build_norm(cur: inpL, mw: model.layers[il].attn_norm, NULL, type: LLM_NORM_RMS, il);
27 cb(cur, name: "attn_norm", il);
28
29 // compute Q and K and RoPE them
30 ggml_tensor * Qcur = build_lora_mm(w: model.layers[il].wq, cur);
31 cb(cur: Qcur, name: "Qcur", il);
32
33 ggml_tensor * Kcur = build_lora_mm(w: model.layers[il].wk, cur);
34 cb(cur: Kcur, name: "Kcur", il);
35
36 ggml_tensor * Vcur = build_lora_mm(w: model.layers[il].wv, cur);
37 cb(cur: Vcur, name: "Vcur", il);
38
39 Qcur = build_norm(cur: Qcur, mw: model.layers[il].attn_q_norm, NULL,
40 type: LLM_NORM_RMS, il);
41 cb(cur: Qcur, name: "Qcur_normed", il);
42
43 Kcur = build_norm(cur: Kcur, mw: model.layers[il].attn_k_norm, NULL,
44 type: LLM_NORM_RMS, il);
45 cb(cur: Kcur, name: "Kcur_normed", il);
46
47 Qcur = ggml_reshape_3d(ctx: ctx0, a: Qcur, ne0: n_embd_head, ne1: n_head, ne2: n_tokens);
48 Kcur = ggml_reshape_3d(ctx: ctx0, a: Kcur, ne0: n_embd_head, ne1: n_head_kv, ne2: n_tokens);
49 Vcur = ggml_reshape_3d(ctx: ctx0, a: Vcur, ne0: n_embd_head, ne1: n_head_kv, ne2: n_tokens);
50
51 Qcur = ggml_rope_ext(
52 ctx: ctx0, a: Qcur, b: inp_pos, c: nullptr,
53 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
57 Kcur = ggml_rope_ext(
58 ctx: ctx0, a: Kcur, b: inp_pos, c: nullptr,
59 n_dims: n_rot, mode: rope_type, n_ctx_orig, freq_base, freq_scale,
60 ext_factor, attn_factor, beta_fast, beta_slow
61 );
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 }
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
77 ggml_tensor * ffn_inp = ggml_add(ctx: ctx0, a: cur, b: inpSA);
78 cb(cur: ffn_inp, name: "ffn_inp", il);
79
80 // MoE branch
81 cur = build_norm(cur: ffn_inp,
82 mw: model.layers[il].ffn_norm, NULL,
83 type: LLM_NORM_RMS, il);
84 cb(cur, name: "ffn_norm", il);
85
86 cur = 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: true,
94 scale_w: false, w_scale: 0.0,
95 gating_op: (llama_expert_gating_func_type) hparams.expert_gating_func,
96 il);
97 cb(cur, name: "ffn_moe_out", il);
98
99 cur = ggml_add(ctx: ctx0, a: cur, b: ffn_inp);
100
101 cur = build_cvec(cur, il);
102 cb(cur, name: "l_out", il);
103
104 // input for next layer
105 inpL = cur;
106 }
107
108 cur = inpL;
109
110 cur = build_norm(cur,
111 mw: model.output_norm, NULL,
112 type: LLM_NORM_RMS, il: -1);
113
114 cb(cur, name: "result_norm", il: -1);
115 res->t_embd = cur;
116
117 // lm_head
118 cur = build_lora_mm(w: model.output, cur);
119
120 cb(cur, name: "result_output", il: -1);
121 res->t_logits = cur;
122
123 ggml_build_forward_expand(cgraph: gf, tensor: cur);
124}
125