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