okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-own-1b (033cfdd8c) into f-own-int: nothing downloads llama.cppb2de2641f

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