1#include "models.h"
2
3llm_build_olmoe::llm_build_olmoe(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
36 ggml_tensor * Kcur = build_lora_mm(w: model.layers[il].wk, cur);
37 cb(cur: Kcur, name: "Kcur", il);
38
39 ggml_tensor * Vcur = build_lora_mm(w: model.layers[il].wv, cur);
40 cb(cur: Vcur, name: "Vcur", il);
41
42 Qcur = build_norm(cur: Qcur, mw: model.layers[il].attn_q_norm, NULL,
43 type: LLM_NORM_RMS, il);
44 cb(cur: Qcur, name: "Qcur_normed", il);
45
46 Kcur = build_norm(cur: Kcur, mw: model.layers[il].attn_k_norm, NULL,
47 type: LLM_NORM_RMS, il);
48 cb(cur: Kcur, name: "Kcur_normed", il);
49
50 Qcur = ggml_reshape_3d(ctx: ctx0, a: Qcur, ne0: n_embd_head, ne1: n_head, ne2: n_tokens);
51 Kcur = ggml_reshape_3d(ctx: ctx0, a: Kcur, ne0: n_embd_head, ne1: n_head_kv, ne2: n_tokens);
52 Vcur = ggml_reshape_3d(ctx: ctx0, a: Vcur, ne0: n_embd_head, ne1: n_head_kv, ne2: n_tokens);
53
54 Qcur = ggml_rope_ext(
55 ctx: ctx0, a: Qcur, b: inp_pos, c: nullptr,
56 n_dims: n_rot, mode: rope_type, n_ctx_orig, freq_base, freq_scale,
57 ext_factor, attn_factor, beta_fast, beta_slow
58 );
59
60 Kcur = ggml_rope_ext(
61 ctx: ctx0, a: Kcur, b: inp_pos, c: nullptr,
62 n_dims: n_rot, mode: rope_type, n_ctx_orig, freq_base, freq_scale,
63 ext_factor, attn_factor, beta_fast, beta_slow
64 );
65
66 cb(cur: Qcur, name: "Qcur", il);
67 cb(cur: Kcur, name: "Kcur", il);
68 cb(cur: Vcur, name: "Vcur", il);
69
70 cur = build_attn(inp: inp_attn,
71 wo: model.layers[il].wo, NULL,
72 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);
73 }
74 if (il == n_layer - 1 && inp_out_ids) {
75 cur = ggml_get_rows(ctx: ctx0, a: cur, b: inp_out_ids);
76 inpSA = ggml_get_rows(ctx: ctx0, a: inpSA, b: inp_out_ids);
77 }
78 ggml_tensor * ffn_inp = ggml_add(ctx: ctx0, a: cur, b: inpSA);
79 cb(cur: ffn_inp, name: "ffn_inp", il);
80
81 // MoE branch
82 cur = build_norm(cur: ffn_inp,
83 mw: model.layers[il].ffn_norm, NULL,
84 type: LLM_NORM_RMS, il);
85 cb(cur, name: "ffn_norm", il);
86
87 cur = build_moe_ffn(cur,
88 gate_inp: model.layers[il].ffn_gate_inp,
89 up_exps: model.layers[il].ffn_up_exps,
90 gate_exps: model.layers[il].ffn_gate_exps,
91 down_exps: model.layers[il].ffn_down_exps,
92 exp_probs_b: nullptr,
93 n_expert, n_expert_used,
94 type_op: LLM_FFN_SILU, norm_w: false,
95 scale_w: false, w_scale: 0.0,
96 gating_op: LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
97 il);
98 cb(cur, name: "ffn_moe_out", il);
99
100 cur = ggml_add(ctx: ctx0, a: cur, b: ffn_inp);
101
102 cur = build_cvec(cur, il);
103 cb(cur, name: "l_out", il);
104
105 // input for next layer
106 inpL = cur;
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