1#include "models.h"
2
3llm_build_jamba::llm_build_jamba(const llama_model & model, const llm_graph_params & params) : llm_graph_context_mamba(params) {
4 const int64_t n_embd_head = hparams.n_embd_head_v;
5
6 ggml_tensor * cur;
7 ggml_tensor * inpL;
8
9 // {n_embd, n_tokens}
10 inpL = build_inp_embd(tok_embd: model.tok_embd);
11
12 auto * inp_hybrid = build_inp_mem_hybrid();
13
14 ggml_tensor * inp_out_ids = build_inp_out_ids();
15
16 for (int il = 0; il < n_layer; ++il) {
17 const int64_t n_head_kv = hparams.n_head_kv(il);
18
19 cur = build_norm(cur: inpL, mw: model.layers[il].attn_norm, NULL, type: LLM_NORM_RMS, il);
20 cb(cur, name: "attn_norm", il);
21
22 if (n_head_kv == 0) {
23 cur = build_mamba_layer(inp: inp_hybrid->get_recr(), cur, model, ubatch, il);
24 } else {
25 // Attention
26
27 struct ggml_tensor * Qcur = build_lora_mm(w: model.layers[il].wq, cur);
28 struct ggml_tensor * Kcur = build_lora_mm(w: model.layers[il].wk, cur);
29 struct ggml_tensor * Vcur = build_lora_mm(w: model.layers[il].wv, cur);
30
31 cb(cur: Qcur, name: "Qcur", il);
32 cb(cur: Kcur, name: "Kcur", il);
33 cb(cur: Vcur, name: "Vcur", il);
34
35 Qcur = ggml_reshape_3d(ctx: ctx0, a: Qcur, ne0: n_embd_head, ne1: n_head, ne2: n_tokens);
36 Kcur = ggml_reshape_3d(ctx: ctx0, a: Kcur, ne0: n_embd_head, ne1: n_head_kv, ne2: n_tokens);
37 Vcur = ggml_reshape_3d(ctx: ctx0, a: Vcur, ne0: n_embd_head, ne1: n_head_kv, ne2: n_tokens);
38
39 cb(cur: Qcur, name: "Qcur", il);
40 cb(cur: Kcur, name: "Kcur", il);
41 cb(cur: Vcur, name: "Vcur", il);
42
43 // No RoPE :)
44 cur = build_attn(inp: inp_hybrid->get_attn(),
45 wo: model.layers[il].wo, NULL,
46 q_cur: Qcur, k_cur: Kcur, v_cur: Vcur, NULL, NULL, NULL, kq_scale: 1.0f/sqrtf(x: float(n_embd_head)), il);
47 }
48 if (il == n_layer - 1 && inp_out_ids) {
49 cur = ggml_get_rows(ctx: ctx0, a: cur, b: inp_out_ids);
50 inpL = ggml_get_rows(ctx: ctx0, a: inpL, b: inp_out_ids);
51 }
52 // residual
53 struct ggml_tensor * ffn_inp = ggml_add(ctx: ctx0, a: inpL, b: cur);
54 cb(cur, name: "ffn_inp", il);
55
56 cur = build_norm(cur: ffn_inp, mw: model.layers[il].ffn_norm, NULL, type: LLM_NORM_RMS, il);
57 cb(cur, name: "ffn_norm", il);
58
59 // feed-forward network
60 if (model.layers[il].ffn_gate_inp == nullptr) {
61 // FFN
62 cur = build_ffn(cur,
63 up: model.layers[il].ffn_up, NULL, NULL,
64 gate: model.layers[il].ffn_gate, NULL, NULL,
65 down: model.layers[il].ffn_down, NULL, NULL,
66 NULL,
67 type_op: LLM_FFN_SILU, type_gate: LLM_FFN_PAR, il);
68 cb(cur, name: "ffn_out", il);
69 } else {
70 // MoE branch
71 cur = build_moe_ffn(cur,
72 gate_inp: model.layers[il].ffn_gate_inp,
73 up_exps: model.layers[il].ffn_up_exps,
74 gate_exps: model.layers[il].ffn_gate_exps,
75 down_exps: model.layers[il].ffn_down_exps,
76 exp_probs_b: nullptr,
77 n_expert, n_expert_used,
78 type_op: LLM_FFN_SILU, norm_w: false,
79 scale_w: false, w_scale: 0.0,
80 gating_op: LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
81 il);
82 cb(cur, name: "ffn_moe_out", il);
83 }
84 // residual
85 cur = ggml_add(ctx: ctx0, a: ffn_inp, b: cur);
86
87 cur = build_cvec(cur, il);
88 cb(cur, name: "l_out", il);
89
90 // input for next layer
91 inpL = cur;
92 }
93 // final rmsnorm
94 cur = build_norm(cur: inpL, mw: model.output_norm, NULL, type: LLM_NORM_RMS, il: -1);
95
96 cb(cur, name: "result_norm", il: -1);
97 res->t_embd = cur;
98
99 // lm_head
100 cur = build_lora_mm(w: model.output, cur);
101
102 cb(cur, name: "result_output", il: -1);
103 res->t_logits = cur;
104
105 ggml_build_forward_expand(cgraph: gf, tensor: cur);
106}
107