1#include "models.h"
2
3llm_build_xverse::llm_build_xverse(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 cur = build_norm(cur: inpL,
25 mw: model.layers[il].attn_norm, NULL,
26 type: LLM_NORM_RMS, il);
27 cb(cur, name: "attn_norm", il);
28
29 // self-attention
30 {
31 ggml_tensor * Qcur = build_lora_mm(w: model.layers[il].wq, cur);
32 cb(cur: Qcur, name: "Qcur", il);
33
34 ggml_tensor * Kcur = build_lora_mm(w: model.layers[il].wk, cur);
35 cb(cur: Kcur, name: "Kcur", il);
36
37 ggml_tensor * Vcur = build_lora_mm(w: model.layers[il].wv, cur);
38 cb(cur: Vcur, name: "Vcur", il);
39
40 Qcur = ggml_reshape_3d(ctx: ctx0, a: Qcur, ne0: n_embd_head, ne1: n_head, ne2: n_tokens);
41 Kcur = ggml_reshape_3d(ctx: ctx0, a: Kcur, ne0: n_embd_head, ne1: n_head_kv, ne2: n_tokens);
42 Vcur = ggml_reshape_3d(ctx: ctx0, a: Vcur, ne0: n_embd_head, ne1: n_head_kv, ne2: n_tokens);
43
44 Qcur = ggml_rope_ext(
45 ctx: ctx0, a: Qcur, b: inp_pos, c: nullptr,
46 n_dims: n_rot, mode: rope_type, n_ctx_orig, freq_base, freq_scale,
47 ext_factor, attn_factor, beta_fast, beta_slow
48 );
49
50 Kcur = ggml_rope_ext(
51 ctx: ctx0, a: Kcur, b: inp_pos, c: nullptr,
52 n_dims: n_rot, mode: rope_type, n_ctx_orig, freq_base, freq_scale,
53 ext_factor, attn_factor, beta_fast, beta_slow
54 );
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, NULL,
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 if (il == n_layer - 1 && inp_out_ids) {
65 cur = ggml_get_rows(ctx: ctx0, a: cur, b: inp_out_ids);
66 inpSA = ggml_get_rows(ctx: ctx0, a: inpSA, b: inp_out_ids);
67 }
68 ggml_tensor * ffn_inp = ggml_add(ctx: ctx0, a: cur, b: inpSA);
69 cb(cur: ffn_inp, name: "ffn_inp", il);
70
71 // feed-forward network
72 {
73 cur = build_norm(cur: ffn_inp,
74 mw: model.layers[il].ffn_norm, NULL,
75 type: LLM_NORM_RMS, il);
76 cb(cur, name: "ffn_norm", il);
77
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,
83 type_op: LLM_FFN_SILU, type_gate: LLM_FFN_PAR, il);
84 cb(cur, name: "ffn_out", il);
85 }
86 cur = ggml_add(ctx: ctx0, a: cur, b: ffn_inp);
87
88 cur = build_cvec(cur, il);
89 cb(cur, name: "l_out", il);
90
91 // input for next layer
92 inpL = cur;
93 }
94 cur = inpL;
95
96 cur = build_norm(cur, mw: model.output_norm, NULL, type: LLM_NORM_RMS, il: -1);
97
98 cb(cur, name: "result_norm", il: -1);
99 res->t_embd = cur;
100
101 // lm_head
102 cur = build_lora_mm(w: model.output, cur);
103
104 cb(cur, name: "result_output", il: -1);
105 res->t_logits = cur;
106
107 ggml_build_forward_expand(cgraph: gf, tensor: cur);
108}
109