/src/llama.cpp/src/models/smallthinker.cpp
Line | Count | Source |
1 | | #include "models.h" |
2 | | |
3 | 0 | void llama_model_smallthinker::load_arch_hparams(llama_model_loader & ml) { |
4 | 0 | const bool found_swa = ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false); |
5 | |
|
6 | 0 | if (found_swa && hparams.n_swa > 0) { |
7 | 0 | hparams.swa_type = LLAMA_SWA_TYPE_STANDARD; |
8 | 0 | hparams.n_swa = 4096; |
9 | 0 | uint32_t swa_period = 4; |
10 | 0 | ml.get_key_or_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, swa_period, false); |
11 | 0 | hparams.set_swa_pattern(swa_period, true); |
12 | |
|
13 | 0 | hparams.rope_freq_base_train_swa = hparams.rope_freq_base_train; |
14 | 0 | hparams.rope_freq_scale_train_swa = hparams.rope_freq_scale_train; |
15 | 0 | ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA, hparams.rope_freq_base_train_swa, false); |
16 | 0 | } else { |
17 | 0 | hparams.swa_type = LLAMA_SWA_TYPE_NONE; |
18 | 0 | hparams.n_no_rope_layer_step = hparams.n_layer(); |
19 | 0 | } |
20 | |
|
21 | 0 | ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp, false); |
22 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
23 | 0 | ml.get_key(LLM_KV_EXPERT_GATING_FUNC, hparams.expert_gating_func, false); |
24 | |
|
25 | 0 | switch (hparams.n_layer()) { |
26 | 0 | case 32: type = LLM_TYPE_4B; break; |
27 | 0 | case 52: type = LLM_TYPE_20B; break; |
28 | 0 | default: type = LLM_TYPE_UNKNOWN; |
29 | 0 | } |
30 | 0 | } |
31 | | |
32 | 0 | void llama_model_smallthinker::load_arch_tensors(llama_model_loader &) { |
33 | 0 | LLAMA_LOAD_LOCALS; |
34 | |
|
35 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0); |
36 | | |
37 | | // output |
38 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0); |
39 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
40 | | |
41 | | // if output is NULL, init from the input tok embed |
42 | 0 | if (output == NULL) { |
43 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
44 | 0 | } |
45 | |
|
46 | 0 | for (int i = 0; i < n_layer; ++i) { |
47 | 0 | auto & layer = layers[i]; |
48 | |
|
49 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, 0); |
50 | |
|
51 | 0 | create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head, n_embd_gqa, n_embd_gqa, 0); |
52 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd_head_k * n_head, n_embd }, 0); |
53 | |
|
54 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), { n_embd }, 0); |
55 | |
|
56 | 0 | GGML_ASSERT(n_expert > 0 && "n_expert must be > 0 for SMALLTHINKER"); |
57 | 0 | GGML_ASSERT(n_expert_used > 0 && "n_expert_used must be > 0 for SMALLTHINKER"); |
58 | | |
59 | | // MoE branch |
60 | 0 | const int64_t n_ff_exp = hparams.n_ff_exp; |
61 | 0 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), { n_embd, n_expert }, 0); |
62 | 0 | layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert }, 0); |
63 | 0 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff_exp, n_embd, n_expert }, 0); |
64 | 0 | layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert }, 0); |
65 | 0 | } |
66 | 0 | } |
67 | | |
68 | 0 | std::unique_ptr<llm_graph_context> llama_model_smallthinker::build_arch_graph(const llm_graph_params & params) const { |
69 | 0 | if (hparams.swa_type == LLAMA_SWA_TYPE_STANDARD) { |
70 | 0 | return std::make_unique<graph<true>> (*this, params); |
71 | 0 | } else { |
72 | 0 | return std::make_unique<graph<false>>(*this, params); |
73 | 0 | } |
74 | 0 | } |
75 | | |
76 | | template <bool iswa> |
77 | 0 | llama_model_smallthinker::graph<iswa>::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params){ |
78 | 0 | const int64_t n_embd_head = hparams.n_embd_head_v(); |
79 | |
|
80 | 0 | GGML_ASSERT(n_embd_head == hparams.n_embd_head_k()); |
81 | 0 | GGML_ASSERT(n_embd_head == n_rot); |
82 | |
|
83 | 0 | ggml_tensor * cur; |
84 | 0 | ggml_tensor * inpL; |
85 | |
|
86 | 0 | inpL = build_inp_embd(model.tok_embd); |
87 | | |
88 | | // inp_pos - contains the positions |
89 | 0 | ggml_tensor * inp_pos = build_inp_pos(); |
90 | |
|
91 | 0 | using inp_attn_type = std::conditional_t<iswa, llm_graph_input_attn_kv_iswa, llm_graph_input_attn_kv>; |
92 | 0 | inp_attn_type * inp_attn = nullptr; |
93 | |
|
94 | 0 | if constexpr (iswa) { |
95 | 0 | inp_attn = build_attn_inp_kv_iswa(); |
96 | 0 | } else { |
97 | 0 | inp_attn = build_attn_inp_kv(); |
98 | 0 | } |
99 | 0 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
100 | |
|
101 | 0 | for (int il = 0; il < n_layer; ++il) { |
102 | 0 | const float freq_base_l = model.get_rope_freq_base (cparams, il); |
103 | 0 | const float freq_scale_l = model.get_rope_freq_scale(cparams, il); |
104 | |
|
105 | 0 | ggml_tensor * inpSA = inpL; |
106 | | |
107 | | // This overlaps with SWA layers in current models, so get_rope_freq_base/scale may be superfluous |
108 | 0 | const bool use_rope = hparams.n_no_rope_layer_step == n_layer || |
109 | 0 | il % hparams.n_no_rope_layer_step != 0; |
110 | |
|
111 | 0 | ggml_tensor * probs = build_lora_mm(model.layers[il].ffn_gate_inp, inpL); // [n_expert, n_tokens] |
112 | 0 | cb(probs, "ffn_moe_logits", il); |
113 | | |
114 | | // norm |
115 | 0 | cur = build_norm(inpL,model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il); |
116 | 0 | cb(cur, "attn_norm", il); |
117 | | |
118 | | // self_attention |
119 | 0 | { |
120 | | // compute Q and K and RoPE them |
121 | 0 | auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur, |
122 | 0 | n_embd_head, n_head, n_head_kv, il); |
123 | |
|
124 | 0 | if (use_rope) { |
125 | 0 | Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l, |
126 | 0 | ext_factor, attn_factor, beta_fast, beta_slow); |
127 | |
|
128 | 0 | Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l, |
129 | 0 | ext_factor, attn_factor, beta_fast, beta_slow); |
130 | 0 | } |
131 | 0 | cb(Qcur, "Qcur", il); |
132 | 0 | cb(Kcur, "Kcur", il); |
133 | |
|
134 | 0 | cur = build_attn(inp_attn, |
135 | 0 | model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s, |
136 | 0 | Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il); |
137 | 0 | } |
138 | 0 | if (il == n_layer - 1 && inp_out_ids) { |
139 | 0 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
140 | 0 | inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids); |
141 | 0 | probs = ggml_get_rows(ctx0, probs, inp_out_ids); |
142 | 0 | } |
143 | 0 | ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA); |
144 | 0 | cb(ffn_inp, "ffn_inp", il); |
145 | | |
146 | | // MoE branch |
147 | 0 | cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il); |
148 | 0 | cb(cur, "ffn_norm", il); |
149 | |
|
150 | 0 | ggml_tensor * ffn_out = |
151 | 0 | build_moe_ffn(cur, |
152 | 0 | nullptr, |
153 | 0 | model.layers[il].ffn_up_exps, |
154 | 0 | model.layers[il].ffn_gate_exps, |
155 | 0 | model.layers[il].ffn_down_exps, |
156 | 0 | nullptr, |
157 | 0 | n_expert, n_expert_used, |
158 | 0 | LLM_FFN_RELU, true, |
159 | 0 | hparams.expert_weights_scale, |
160 | 0 | static_cast<llama_expert_gating_func_type>(hparams.expert_gating_func), |
161 | 0 | il, probs); |
162 | |
|
163 | 0 | cb(ffn_out, "ffn_out", il); |
164 | 0 | cur = ffn_out; |
165 | |
|
166 | 0 | cur = ggml_add(ctx0, cur, ffn_inp); |
167 | |
|
168 | 0 | cur = build_cvec(cur, il); |
169 | 0 | cb(cur, "l_out", il); |
170 | | |
171 | | // input for next layer |
172 | 0 | inpL = cur; |
173 | 0 | } |
174 | 0 | cur = inpL; |
175 | |
|
176 | 0 | cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1); |
177 | 0 | cb(cur, "result_norm", -1); |
178 | 0 | res->t_embd = cur; |
179 | | |
180 | | // lm_head |
181 | 0 | cur = build_lora_mm(model.output, cur, model.output_s); |
182 | 0 | cb(cur, "result_output", -1); |
183 | 0 | res->t_logits = cur; |
184 | |
|
185 | 0 | ggml_build_forward_expand(gf, cur); |
186 | 0 | } Unexecuted instantiation: llama_model_smallthinker::graph<false>::graph(llama_model const&, llm_graph_params const&) Unexecuted instantiation: llama_model_smallthinker::graph<true>::graph(llama_model const&, llm_graph_params const&) |
187 | | |
188 | | // Explicit template instantiations |
189 | | template struct llama_model_smallthinker::graph<false>; |
190 | | template struct llama_model_smallthinker::graph<true>; |