1#include "models.h"
2
3
4
5llm_build_apertus::llm_build_apertus(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
6 const int64_t n_embd_head = hparams.n_embd_head_v;
7
8 GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
9 GGML_ASSERT(n_embd_head == hparams.n_rot);
10
11 ggml_tensor * cur;
12 ggml_tensor * inpL;
13
14 inpL = build_inp_embd(tok_embd: model.tok_embd);
15
16 ggml_tensor * inp_pos = build_inp_pos();
17 auto * inp_attn = build_attn_inp_kv();
18
19 const float kq_scale =
20 hparams.f_attention_scale == 0.0f ? 1.0f / sqrtf(x: float(n_embd_head)) : hparams.f_attention_scale;
21
22 ggml_tensor * inp_out_ids = build_inp_out_ids();
23
24 for (int il = 0; il < n_layer; ++il) {
25 ggml_tensor * inpSA = inpL;
26
27 cur = build_norm(cur: inpL, mw: model.layers[il].attn_norm, mb: nullptr, type: LLM_NORM_RMS, il);
28 cb(cur, name: "attn_norm", il);
29
30 // self-attention
31 {
32 ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
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
38 ggml_tensor * Kcur = build_lora_mm(w: model.layers[il].wk, cur);
39 cb(cur: Kcur, name: "Kcur", il);
40
41 ggml_tensor * Vcur = build_lora_mm(w: model.layers[il].wv, cur);
42 cb(cur: Vcur, name: "Vcur", il);
43
44 Qcur = ggml_reshape_3d(ctx: ctx0, a: Qcur, ne0: n_embd_head, ne1: n_head, ne2: n_tokens);
45 Qcur = build_norm(cur: Qcur, mw: model.layers[il].attn_q_norm, NULL, type: LLM_NORM_RMS, il);
46 cb(cur: Qcur, name: "Qcur_normed", il);
47
48 Kcur = ggml_reshape_3d(ctx: ctx0, a: Kcur, ne0: n_embd_head, ne1: n_head_kv, ne2: n_tokens);
49 Kcur = build_norm(cur: Kcur, mw: model.layers[il].attn_k_norm, NULL, type: LLM_NORM_RMS, il);
50 cb(cur: Kcur, name: "Kcur_normed", il);
51
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(ctx: ctx0, a: Qcur, b: inp_pos, c: rope_factors, n_dims: n_rot, mode: rope_type, n_ctx_orig, freq_base, freq_scale,
55 ext_factor, attn_factor, beta_fast, beta_slow);
56
57 Kcur = ggml_rope_ext(ctx: ctx0, a: Kcur, b: inp_pos, c: rope_factors, 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 cb(cur: Qcur, name: "Qcur_pos", il);
61 cb(cur: Kcur, name: "Kcur_pos", il);
62 cb(cur: Vcur, name: "Vcur_pos", il);
63
64 cur = build_attn(inp: inp_attn,
65 wo: model.layers[il].wo, wo_b: model.layers[il].bo,
66 q_cur: Qcur, k_cur: Kcur, v_cur: Vcur, kq_b: nullptr, sinks: nullptr, v_mla: nullptr, kq_scale, il);
67 cb(cur, name: "attn_out", il);
68 }
69
70 if (il == n_layer - 1 && inp_out_ids) {
71 cur = ggml_get_rows(ctx: ctx0, a: cur, b: inp_out_ids);
72 inpSA = ggml_get_rows(ctx: ctx0, a: inpSA, b: inp_out_ids);
73 }
74
75 ggml_tensor * ffn_inp = ggml_add(ctx: ctx0, a: cur, b: inpSA);
76 cb(cur: ffn_inp, name: "ffn_inp", il);
77
78 // feed-forward network with xIELU activation
79 {
80 cur = build_norm(cur: ffn_inp, mw: model.layers[il].ffn_norm, mb: nullptr, type: LLM_NORM_RMS, il);
81 cb(cur, name: "ffn_norm", il);
82
83 // Up projection
84 ggml_tensor * up = build_lora_mm(w: model.layers[il].ffn_up, cur);
85 cb(cur: up, name: "ffn_up", il);
86
87 float alpha_n_val = hparams.xielu_alpha_n[il];
88 float alpha_p_val = hparams.xielu_alpha_p[il];
89 float beta_val = hparams.xielu_beta[il];
90 float eps_val = hparams.xielu_eps[il];
91
92 // Apply xIELU activation
93 ggml_tensor * activated = ggml_xielu(ctx: ctx0, a: up, alpha_n: alpha_n_val, alpha_p: alpha_p_val, beta: beta_val, eps: eps_val);
94 cb(cur: activated, name: "ffn_xielu", il);
95
96 // Down projection
97 cur = build_lora_mm(w: model.layers[il].ffn_down, cur: activated);
98 cb(cur, name: "ffn_down", il);
99 }
100
101 cur = ggml_add(ctx: ctx0, a: cur, b: ffn_inp);
102 cb(cur, name: "ffn_out", il);
103
104 cur = build_cvec(cur, il);
105 cb(cur, name: "l_out", il);
106
107 // input for next layer
108 inpL = cur;
109 }
110
111 cur = inpL;
112
113 cur = build_norm(cur, mw: model.output_norm, mb: nullptr, type: LLM_NORM_RMS, il: -1);
114
115 cb(cur, name: "result_norm", il: -1);
116 res->t_embd = cur;
117
118 // lm_head
119 cur = build_lora_mm(w: model.output, cur);
120
121 cb(cur, name: "result_output", il: -1);
122 res->t_logits = cur;
123
124 ggml_build_forward_expand(cgraph: gf, tensor: cur);
125}
126