char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

PikaPikachuconvert : add `--fuse-qkv` flag to fuse Q/K/V into QKV during HF-to-GGUF conversion (#22780)465e49b9c

char/aliceai-t5-moe
7.7 KiB210 linesraw
1#include "models.h"
2
3void llama_model_olmo2::load_arch_hparams(llama_model_loader & ml) {
4    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
5
6    const bool found_swa = ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false);
7    if (found_swa && hparams.n_swa > 0) {
8        hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
9        uint32_t swa_period = 4;
10        ml.get_key_or_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, swa_period, false);
11        hparams.set_swa_pattern(swa_period);
12
13        hparams.rope_freq_base_train_swa  = hparams.rope_freq_base_train;
14        hparams.rope_freq_scale_train_swa = 1.0; // See olmo2.cpp
15        ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA, hparams.rope_freq_base_train_swa, false);
16    } else {
17        hparams.swa_type = LLAMA_SWA_TYPE_NONE;
18    }
19
20    switch (hparams.n_layer()) {
21        case 16: type = LLM_TYPE_1B; break;
22        case 32: type = LLM_TYPE_7B; break;
23        case 40: type = LLM_TYPE_13B; break;
24        case 64: type = LLM_TYPE_32B; break;
25        default: type = LLM_TYPE_UNKNOWN;
26    }
27}
28
29void llama_model_olmo2::load_arch_tensors(llama_model_loader &) {
30    LLAMA_LOAD_LOCALS;
31
32    const int64_t n_embd_head = n_embd / n_head;
33
34    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
35
36    // output
37    output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
38    output      = create_tensor(tn(LLM_TENSOR_OUTPUT,      "weight"), {n_embd, n_vocab}, 0);
39
40    for (int i = 0; i < n_layer; ++i) {
41        auto & layer = layers[i];
42
43        create_tensor_qkv(layer, i, n_embd, n_embd, n_embd_gqa, n_embd_gqa, 0);
44        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
45        layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd}, 0);
46        layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_head_kv * n_embd_head}, 0);
47        layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0);
48
49        layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd,   n_ff}, 0);
50        layer.ffn_up   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd,   n_ff}, 0);
51        layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {  n_ff, n_embd}, 0);
52        layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0);
53    }
54}
55
56std::unique_ptr<llm_graph_context> llama_model_olmo2::build_arch_graph(const llm_graph_params & params) const {
57    if (hparams.swa_type == LLAMA_SWA_TYPE_STANDARD) {
58        return std::make_unique<graph<true>>(*this, params);
59    } else {
60        return std::make_unique<graph<false>>(*this, params);
61    }
62}
63
64template <bool iswa>
65llama_model_olmo2::graph<iswa>::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
66    const int64_t n_embd_head = hparams.n_embd_head_v();
67
68    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());
69    GGML_ASSERT(n_embd_head == n_rot);
70
71    ggml_tensor * cur;
72    ggml_tensor * inpL;
73
74    inpL = build_inp_embd(model.tok_embd);
75
76    // inp_pos - contains the positions
77    ggml_tensor * inp_pos = build_inp_pos();
78
79    using inp_attn_type = std::conditional_t<iswa, llm_graph_input_attn_kv_iswa, llm_graph_input_attn_kv>;
80    inp_attn_type * inp_attn = nullptr;
81
82    if constexpr (iswa) {
83        inp_attn = build_attn_inp_kv_iswa();
84    } else {
85        inp_attn = build_attn_inp_kv();
86    }
87    ggml_tensor * inp_out_ids = build_inp_out_ids();
88
89    for (int il = 0; il < n_layer; ++il) {
90        ggml_tensor * inpSA = inpL;
91
92        cur = inpL;
93
94        // self_attention
95        {
96            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,
97                    n_embd_head, n_head,
98                    n_embd_head, n_head_kv,
99                    n_embd_head, n_head_kv,
100                    il, false);
101            cb(Qcur, "Qcur", il);
102            cb(Kcur, "Kcur", il);
103            cb(Vcur, "Vcur", il);
104
105            Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL,
106                    LLM_NORM_RMS, il);
107            cb(Qcur, "Qcur_normed", il);
108
109            Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL,
110                    LLM_NORM_RMS, il);
111            cb(Kcur, "Kcur_normed", il);
112
113            Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head,    n_tokens);
114            Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
115            Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
116
117            const bool is_swa = hparams.is_swa(il);
118
119            if (is_swa) {
120                // For sliding window layers, Olmo3 use regular rope with no yarn rope scaling.
121                // This is achieved here by setting freq_scale and attn_factor to 1.
122                // We also set ext_factor to 0 to avoid a few unnecessary computations.
123                Qcur = ggml_rope_ext(
124                    ctx0, Qcur, inp_pos, nullptr,
125                    n_rot, rope_type, n_ctx_orig, freq_base, 1.0,
126                    0.0, 1.0, beta_fast, beta_slow
127                    );
128
129                Kcur = ggml_rope_ext(
130                    ctx0, Kcur, inp_pos, nullptr,
131                    n_rot, rope_type, n_ctx_orig, freq_base, 1.0,
132                    0.0, 1.0, beta_fast, beta_slow
133                    );
134            } else {
135                Qcur = ggml_rope_ext(
136                    ctx0, Qcur, inp_pos, nullptr,
137                    n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
138                    ext_factor, attn_factor, beta_fast, beta_slow
139                    );
140
141                Kcur = ggml_rope_ext(
142                    ctx0, Kcur, inp_pos, nullptr,
143                    n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
144                    ext_factor, attn_factor, beta_fast, beta_slow
145                    );
146            }
147            cb(Qcur, "Qcur", il);
148            cb(Kcur, "Kcur", il);
149            cb(Vcur, "Vcur", il);
150
151            cur = build_attn(inp_attn,
152                    model.layers[il].wo, NULL, model.layers[il].wo_s,
153                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
154        }
155        if (il == n_layer - 1 && inp_out_ids) {
156            cur   = ggml_get_rows(ctx0,   cur, inp_out_ids);
157            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
158        }
159        cur = build_norm(cur,
160                model.layers[il].attn_post_norm, NULL,
161                LLM_NORM_RMS, il);
162        cb(cur, "attn_post_norm", il);
163
164        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
165        cb(ffn_inp, "ffn_inp", il);
166
167        // feed-forward network
168        cur = build_ffn(ffn_inp,
169                model.layers[il].ffn_up,   NULL, NULL,
170                model.layers[il].ffn_gate, NULL, NULL,
171                model.layers[il].ffn_down, NULL, NULL,
172                NULL,
173                LLM_FFN_SILU, LLM_FFN_PAR, il);
174        cb(cur, "ffn_out", il);
175
176        cur = build_norm(cur,
177                model.layers[il].ffn_post_norm, NULL,
178                LLM_NORM_RMS, -1);
179        cb(cur, "ffn_post_norm", -1);
180
181        cur = ggml_add(ctx0, cur, ffn_inp);
182        cb(cur, "ffn_out", il);
183
184        cur = build_cvec(cur, il);
185        cb(cur, "l_out", il);
186
187        // input for next layer
188        inpL = cur;
189    }
190    cur = inpL;
191
192    cur = build_norm(cur,
193            model.output_norm, NULL,
194            LLM_NORM_RMS, -1);
195
196    cb(cur, "result_norm", -1);
197    res->t_embd = cur;
198
199    // lm_head
200    cur = build_lora_mm(model.output, cur, model.output_s);
201
202    cb(cur, "result_output", -1);
203    res->t_logits = cur;
204
205    ggml_build_forward_expand(gf, cur);
206}
207
208// Explicit template instantiations
209template struct llama_model_olmo2::graph<false>;
210template struct llama_model_olmo2::graph<true>;