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
8.3 KiB203 linesraw
1#include "models.h"
2
3void llama_model_muse_glimmer::load_arch_hparams(llama_model_loader & ml) {
4    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
5    ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW,    hparams.n_swa);
6    ml.get_key(LLM_KV_FINAL_LOGIT_SOFTCAPPING,     hparams.f_final_logit_softcapping, false);
7    ml.get_key(LLM_KV_LOGIT_SCALE,                 hparams.f_logit_scale);
8
9    hparams.rope_freq_base_train_swa = hparams.rope_freq_base_train;
10    ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA, hparams.rope_freq_base_train_swa, false);
11
12    hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
13    load_swa_pattern(ml, 4);
14
15    switch (hparams.n_layer()) {
16        case 52: type = LLM_TYPE_30B; break;
17        default: type = LLM_TYPE_UNKNOWN;
18    }
19}
20
21void llama_model_muse_glimmer::load_arch_tensors(llama_model_loader &) {
22    LLAMA_LOAD_LOCALS;
23
24    tok_embd    = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD,  "weight"), {n_embd, n_vocab}, 0);
25    output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
26    output      = create_tensor(tn(LLM_TENSOR_OUTPUT,      "weight"), {n_embd, n_vocab}, 0);
27
28    for (int i = 0; i < n_layer; ++i) {
29        auto & layer = layers[i];
30
31        // Pre/post-attention norms (Muse Glimmer's `weight + 1` applied at conversion time).
32        layer.attn_norm      = create_tensor(tn(LLM_TENSOR_ATTN_NORM,      "weight", i), {n_embd}, 0);
33        layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0);
34
35        // Q/K/V/O projections.
36        create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head, n_embd_k_gqa, n_embd_v_gqa, 0);
37        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
38
39        // QK-norm. Weights are synthesized at conversion time to absorb `qk_scale_factor`.
40        layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
41        layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
42
43        // Attention output gate: sigmoid(gate) * attn_out before o_proj (same as afmoe).
44        layer.wqkv_gate = create_tensor(tn(LLM_TENSOR_ATTN_GATE, "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
45
46        // Pre/post-FFN norms (FFN_PRE_NORM is aliased to LLM_TENSOR_FFN_NORM).
47        layer.ffn_norm      = create_tensor(tn(LLM_TENSOR_FFN_NORM,      "weight", i), {n_embd}, 0);
48        layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0);
49
50        // Dense FFN (unlike afmoe, no MoE branches).
51        layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
52        layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
53        layer.ffn_up   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd, n_ff}, 0);
54    }
55}
56
57llama_model_muse_glimmer::graph::graph(const llama_model & model, const llm_graph_params & params)
58    : llm_graph_context(params) {
59    const int64_t n_embd_head = hparams.n_embd_head_v();
60    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());
61
62    // Different to f_norm_rms_eps for post-attn / post-FFN norms
63    const float post_norm_eps = 1e-8f;
64
65    ggml_tensor * cur;
66    ggml_tensor * inpL;
67
68    inpL = build_inp_embd(model.tok_embd);
69    inpL = build_norm(inpL, nullptr, nullptr, LLM_NORM_RMS, -1);
70    cb(inpL, "embd_norm", -1);
71
72    ggml_tensor * inp_pos = build_inp_pos();
73    auto * inp_attn = build_attn_inp_kv_iswa();
74    ggml_tensor * inp_out_ids = build_inp_out_ids();
75
76    const float kq_scale = 1.0f / sqrtf(float(n_embd_head));
77
78    for (int il = 0; il < n_layer; ++il) {
79        // expose per-layer residual for speculative drafts (see LLM_KV_TARGET_LAYERS).
80        res->t_layer_inp[il] = inpL;
81
82        const float freq_base_l  = model.get_rope_freq_base (cparams, il);
83        const float freq_scale_l = model.get_rope_freq_scale(cparams, il);
84
85        ggml_tensor * inpSA = inpL;
86
87        // RoPE runs on the SWA layers, NoPE on full ones.
88        const bool use_rope = hparams.is_swa(il);
89
90        // pre-attention norm (weight+1 folded at conversion time)
91        cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
92        cb(cur, "attn_norm", il);
93
94        // self-attention: attention output gate around SDPA (afmoe.cpp:147-191)
95        {
96            ggml_tensor * attn_inp = cur;  // save input for gate computation
97
98            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,
99                    n_embd_head, n_head, n_head_kv, il);
100
101            // gate = wqkv_gate @ attn_inp (from pre-attn hidden state)
102            ggml_tensor * gate = build_lora_mm(model.layers[il].wqkv_gate, attn_inp);
103            cb(gate, "attn_gate_proj", il);
104
105            // QK-norm. attn_q_norm weight was synthesized at conversion to broadcast
106            // qk_scale_factor across head_dim; attn_k_norm is identity (ones).
107            Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
108            Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
109            cb(Qcur, "Qcur_normed", il);
110            cb(Kcur, "Kcur_normed", il);
111
112            if (use_rope) {
113                Qcur = ggml_rope_ext(
114                        ctx0, Qcur, inp_pos, nullptr,
115                        n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
116                        ext_factor, attn_factor, beta_fast, beta_slow);
117                cb(Qcur, "Qcur_rope", il);
118
119                Kcur = ggml_rope_ext(
120                        ctx0, Kcur, inp_pos, nullptr,
121                        n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
122                        ext_factor, attn_factor, beta_fast, beta_slow);
123                cb(Kcur, "Kcur_rope", il);
124            }
125
126            // SDPA. wo is deferred; the gate goes between attn_out and o_proj.
127            cur = build_attn(inp_attn,
128                    NULL, NULL, NULL,
129                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
130            cb(cur, "attn_out", il);
131
132            gate = ggml_sigmoid(ctx0, gate);
133            cb(gate, "attn_gate_sig", il);
134            cur = ggml_mul(ctx0, cur, gate);
135            cb(cur, "attn_gated", il);
136
137            cur = build_lora_mm(model.layers[il].wo, cur, model.layers[il].wo_s);
138            cb(cur, "attn_o_proj", il);
139        }
140
141        cur = ggml_rms_norm(ctx0, cur, post_norm_eps);
142        cur = ggml_mul(ctx0, cur, model.layers[il].attn_post_norm);
143        cb(cur, "attn_post_norm", il);
144
145        if (il == n_layer - 1 && inp_out_ids) {
146            cur   = ggml_get_rows(ctx0,   cur, inp_out_ids);
147            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
148        }
149
150        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
151        cb(ffn_inp, "ffn_inp", il);
152
153        // pre-FFN norm
154        cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
155        cb(cur, "ffn_norm", il);
156
157        // SwiGLU dense FFN
158        cur = build_ffn(cur,
159                model.layers[il].ffn_up,   NULL, NULL,
160                model.layers[il].ffn_gate, NULL, NULL,
161                model.layers[il].ffn_down, NULL, NULL,
162                NULL,
163                LLM_FFN_SILU, LLM_FFN_PAR, il);
164        cb(cur, "ffn_out", il);
165
166        cur = ggml_rms_norm(ctx0, cur, post_norm_eps);
167        cur = ggml_mul(ctx0, cur, model.layers[il].ffn_post_norm);
168        cb(cur, "ffn_post_norm", il);
169
170        cur = ggml_add(ctx0, cur, ffn_inp);
171        cur = build_cvec(cur, il);
172        cb(cur, "l_out", il);
173
174        inpL = cur;
175    }
176
177    cur = inpL;
178
179    // final norm
180    cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
181    cb(cur, "result_norm", -1);
182    res->t_embd = cur;
183
184    // lm_head, followed by output multiplier
185    cur = build_lora_mm(model.output, cur, model.output_s);
186    cur = ggml_scale(ctx0, cur, hparams.f_logit_scale);
187
188    // Final logit tanh softcap (from gemma3.cpp).
189    if (hparams.f_final_logit_softcapping) {
190        cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_final_logit_softcapping);
191        cur = ggml_tanh(ctx0, cur);
192        cur = ggml_scale(ctx0, cur, hparams.f_final_logit_softcapping);
193    }
194
195    cb(cur, "result_output", -1);
196    res->t_logits = cur;
197
198    ggml_build_forward_expand(gf, cur);
199}
200
201std::unique_ptr<llm_graph_context> llama_model_muse_glimmer::build_arch_graph(const llm_graph_params & params) const {
202    return std::make_unique<graph>(*this, params);
203}