1#include "models.h"
2
3template <bool iswa>
4llm_build_smallthinker<iswa>::llm_build_smallthinker(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params){
5 const int64_t n_embd_head = hparams.n_embd_head_v;
6
7 GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
8 GGML_ASSERT(n_embd_head == hparams.n_rot);
9
10 ggml_tensor * cur;
11 ggml_tensor * inpL;
12
13 inpL = build_inp_embd(model.tok_embd);
14
15 // inp_pos - contains the positions
16 ggml_tensor * inp_pos = build_inp_pos();
17
18 using inp_attn_type = std::conditional_t<iswa, llm_graph_input_attn_kv_iswa, llm_graph_input_attn_kv>;
19 inp_attn_type * inp_attn = nullptr;
20
21 if constexpr (iswa) {
22 inp_attn = build_attn_inp_kv_iswa();
23 } else {
24 inp_attn = build_attn_inp_kv();
25 }
26 ggml_tensor * inp_out_ids = build_inp_out_ids();
27
28 for (int il = 0; il < n_layer; ++il) {
29 const float freq_base_l = model.get_rope_freq_base (cparams, il);
30 const float freq_scale_l = model.get_rope_freq_scale(cparams, il);
31
32 ggml_tensor * inpSA = inpL;
33
34 // This overlaps with SWA layers in current models, so get_rope_freq_base/scale may be superfluous
35 const bool use_rope = hparams.n_no_rope_layer_step == n_layer ||
36 il % hparams.n_no_rope_layer_step != 0;
37
38 ggml_tensor * probs = build_lora_mm(model.layers[il].ffn_gate_inp, inpL); // [n_expert, n_tokens]
39 cb(probs, "ffn_moe_logits", il);
40
41 // norm
42 cur = build_norm(inpL,model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
43 cb(cur, "attn_norm", il);
44
45 // self_attention
46 {
47 // compute Q and K and RoPE them
48 struct ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
49 cb(Qcur, "Qcur", il);
50
51 struct ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
52 cb(Kcur, "Kcur", il);
53
54 struct ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
55 cb(Vcur, "Vcur", il);
56
57 Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
58 Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
59 Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
60
61 if (use_rope) {
62 Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
63 ext_factor, attn_factor, beta_fast, beta_slow);
64
65 Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
66 ext_factor, attn_factor, beta_fast, beta_slow);
67 }
68 cb(Qcur, "Qcur", il);
69 cb(Kcur, "Kcur", il);
70
71 cur = build_attn(inp_attn,
72 model.layers[il].wo, model.layers[il].bo,
73 Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
74 }
75 if (il == n_layer - 1 && inp_out_ids) {
76 cur = ggml_get_rows(ctx0, cur, inp_out_ids);
77 inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
78 probs = ggml_get_rows(ctx0, probs, inp_out_ids);
79 }
80 ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
81 cb(ffn_inp, "ffn_inp", il);
82
83 // MoE branch
84 cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
85 cb(cur, "ffn_norm", il);
86
87 ggml_tensor * ffn_out =
88 build_moe_ffn(cur,
89 nullptr,
90 model.layers[il].ffn_up_exps,
91 model.layers[il].ffn_gate_exps,
92 model.layers[il].ffn_down_exps,
93 nullptr,
94 n_expert, n_expert_used,
95 LLM_FFN_RELU, true,
96 false, 0.0,
97 static_cast<llama_expert_gating_func_type>(hparams.expert_gating_func),
98 il, probs);
99
100 cb(ffn_out, "ffn_out", il);
101 cur = ffn_out;
102
103 cur = ggml_add(ctx0, cur, ffn_inp);
104 cur = build_cvec(cur, il);
105 cb(cur, "l_out", il);
106
107 // input for next layer
108 inpL = cur;
109 }
110 cur = inpL;
111
112 cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
113 cb(cur, "result_norm", -1);
114 res->t_embd = cur;
115
116 // lm_head
117 cur = build_lora_mm(model.output, cur);
118 cb(cur, "result_output", -1);
119 res->t_logits = cur;
120
121 ggml_build_forward_expand(gf, cur);
122}
123
124// Explicit template instantiations
125template struct llm_build_smallthinker<false>;
126template struct llm_build_smallthinker<true>;