1#include "models.h"
2
3
4
5llm_build_falcon_h1::llm_build_falcon_h1(const llama_model & model, const llm_graph_params & params) :
6 llm_graph_context_mamba(params) {
7 const int64_t n_embd_head = hparams.n_embd_head_v;
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 // Build the inputs in the recurrent & kv cache
18 auto * inp = build_inp_mem_hybrid();
19
20 const float kq_scale =
21 hparams.f_attention_scale == 0.0f ? 1.0f / sqrtf(x: float(n_embd_head)) : hparams.f_attention_scale;
22
23 ggml_tensor * inp_out_ids = build_inp_out_ids();
24
25 for (int il = 0; il < n_layer; ++il) {
26 ggml_tensor * inpSA = inpL;
27
28 cur = build_norm(cur: inpL, mw: model.layers[il].attn_norm, NULL, type: LLM_NORM_RMS, il);
29 cb(cur, name: "attn_norm", il);
30
31 // self-attention
32 ggml_tensor * Qcur = build_lora_mm(w: model.layers[il].wq, cur);
33 cb(cur: Qcur, name: "Qcur", il);
34
35 ggml_tensor * Kcur = build_lora_mm(w: model.layers[il].wk, cur);
36 cb(cur: Kcur, name: "Kcur", il);
37
38 ggml_tensor * Vcur = build_lora_mm(w: model.layers[il].wv, cur);
39 cb(cur: Vcur, name: "Vcur", il);
40
41 Qcur = ggml_reshape_3d(ctx: ctx0, a: Qcur, ne0: n_embd_head, ne1: n_head, ne2: n_tokens);
42 Kcur = ggml_reshape_3d(ctx: ctx0, a: Kcur, ne0: n_embd_head, ne1: n_head_kv, ne2: n_tokens);
43
44 Vcur = ggml_reshape_3d(ctx: ctx0, a: Vcur, ne0: n_embd_head, ne1: n_head_kv, ne2: n_tokens);
45
46 Qcur = ggml_rope_ext(ctx: ctx0, a: Qcur, b: inp_pos, c: nullptr, n_dims: n_rot, mode: hparams.rope_type, n_ctx_orig, freq_base, freq_scale,
47 ext_factor, attn_factor, beta_fast, beta_slow);
48
49 Kcur = ggml_rope_ext(ctx: ctx0, a: Kcur, b: inp_pos, c: nullptr, n_dims: n_rot, mode: hparams.rope_type, n_ctx_orig, freq_base, freq_scale,
50 ext_factor, attn_factor, beta_fast, beta_slow);
51
52 cb(cur: Qcur, name: "Qcur-post-rope", il);
53 cb(cur: Kcur, name: "Kcur-post-rope", il);
54 cb(cur: Vcur, name: "Vcur-post-rope", il);
55
56 ggml_tensor * attn_out = build_attn(inp: inp->get_attn(),
57 wo: model.layers[il].wo, NULL,
58 q_cur: Qcur, k_cur: Kcur, v_cur: Vcur, kq_b: nullptr, sinks: nullptr, v_mla: nullptr, kq_scale, il);
59 cb(cur: attn_out, name: "attn_out", il);
60
61 cur = build_norm(cur: inpL, mw: model.layers[il].attn_norm, NULL, type: LLM_NORM_RMS, il);
62 // Mamba2 layer
63 cb(cur, name: "ssm_in", il);
64
65 ggml_tensor * ssm_out = build_mamba2_layer(inp: inp->get_recr(), cur, model, ubatch, il);
66 cb(cur: ssm_out, name: "ssm_out", il);
67
68 // // Aggregation
69 cur = ggml_add(ctx: ctx0, a: attn_out, b: ssm_out);
70 inpSA = ggml_add(ctx: ctx0, a: cur, b: inpSA);
71 cb(cur, name: "layer_out", il);
72
73 if (il == n_layer - 1 && inp_out_ids) {
74 cur = ggml_get_rows(ctx: ctx0, a: cur, b: inp_out_ids);
75 inpSA = ggml_get_rows(ctx: ctx0, a: inpSA, b: inp_out_ids);
76 }
77 ggml_tensor * ffn_inp = inpSA;
78 cb(cur: ffn_inp, name: "ffn_inp", il);
79
80 // feed-forward network
81 cur = build_norm(cur: ffn_inp, mw: model.layers[il].ffn_norm, NULL, type: LLM_NORM_RMS, il);
82 cb(cur, name: "ffn_norm", il);
83
84 cur = build_ffn(cur,
85 up: model.layers[il].ffn_up, up_b: model.layers[il].ffn_up_b, NULL,
86 gate: model.layers[il].ffn_gate, gate_b: model.layers[il].ffn_gate_b, NULL,
87 down: model.layers[il].ffn_down, down_b: model.layers[il].ffn_down_b, NULL,
88 NULL, type_op: LLM_FFN_SILU, type_gate: LLM_FFN_PAR, il);
89 cb(cur, name: "ffn_out", il);
90
91 cur = ggml_add(ctx: ctx0, a: cur, b: inpSA);
92
93 cur = build_cvec(cur, il);
94 cb(cur, name: "l_out", il);
95
96 // input for next layer
97 inpL = cur;
98 }
99 cur = inpL;
100
101 cur = build_norm(cur, mw: model.output_norm, NULL, type: LLM_NORM_RMS, il: -1);
102
103 cb(cur, name: "result_norm", il: -1);
104 res->t_embd = cur;
105
106 // lm_head
107 cur = build_lora_mm(w: model.output, cur);
108
109 cb(cur, name: "result_output", il: -1);
110 res->t_logits = cur;
111
112 ggml_build_forward_expand(cgraph: gf, tensor: cur);
113}
114