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.7 KiB195 linesraw
1#include "models.h"
2
3void llama_model_plamo3::load_arch_hparams(llama_model_loader & ml) {
4    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
5    const bool found_swa = ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false);
6    if (found_swa && hparams.n_swa > 0) {
7        hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
8        ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA, hparams.rope_freq_base_train_swa, false);
9        load_swa_pattern(ml, 8);
10    } else {
11        hparams.swa_type = LLAMA_SWA_TYPE_NONE;
12    }
13
14    switch (hparams.n_layer()) {
15        case 24: type = LLM_TYPE_2B; break;
16        default: type = LLM_TYPE_UNKNOWN;
17    }
18}
19
20void llama_model_plamo3::load_arch_tensors(llama_model_loader &) {
21    LLAMA_LOAD_LOCALS;
22
23    const int64_t head_dim_q = hparams.n_embd_head_k();
24    const int64_t head_dim_v = hparams.n_embd_head_v();
25
26    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
27
28    output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
29    output      = create_tensor(tn(LLM_TENSOR_OUTPUT,      "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
30    if (output == NULL) {
31        output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
32    }
33
34    for (int i = 0; i < n_layer; ++i) {
35        auto & layer = layers[i];
36
37        const int64_t num_attention_heads = hparams.n_head(i);
38        const int64_t num_key_value_heads = hparams.n_head_kv(i);
39        const int64_t q_proj_dim = num_attention_heads * head_dim_q;
40        const int64_t k_proj_dim = num_key_value_heads * head_dim_q;
41        const int64_t v_proj_dim = num_key_value_heads * head_dim_v;
42        const int64_t n_ff_cur   = hparams.n_ff(i);
43
44        layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
45        layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i),
46                {n_embd,q_proj_dim + k_proj_dim + v_proj_dim}, 0);
47        layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {head_dim_q}, 0);
48        layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {head_dim_q}, 0);
49        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {num_attention_heads * head_dim_v, n_embd}, 0);
50        layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, i), {n_embd}, 0);
51
52        layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
53        layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, i), {n_embd}, 0);
54
55        layer.ffn_up   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd, n_ff_cur * 2}, 0);
56        layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff_cur, n_embd}, 0);
57    }
58}
59
60std::unique_ptr<llm_graph_context> llama_model_plamo3::build_arch_graph(const llm_graph_params & params) const {
61    if (hparams.swa_type != LLAMA_SWA_TYPE_NONE) {
62        return std::make_unique<graph<true>> (*this, params);
63    } else {
64        return std::make_unique<graph<false>>(*this, params);
65    }
66}
67
68template <bool iswa>
69llama_model_plamo3::graph<iswa>::graph(const llama_model & model, const llm_graph_params & params) :
70    llm_graph_context(params) {
71    const int64_t head_dim_q = hparams.n_embd_head_k();
72    const int64_t head_dim_v = hparams.n_embd_head_v();
73
74    ggml_tensor * cur;
75    ggml_tensor * inpL = build_inp_embd(model.tok_embd);
76    ggml_tensor * inp_pos = build_inp_pos();
77
78    using inp_attn_type = std::conditional_t<iswa, llm_graph_input_attn_kv_iswa, llm_graph_input_attn_kv>;
79    inp_attn_type * inp_attn = nullptr;
80
81    if constexpr (iswa) {
82        inp_attn = build_attn_inp_kv_iswa();
83    } else {
84        inp_attn = build_attn_inp_kv();
85    }
86
87    ggml_tensor * inp_out_ids = build_inp_out_ids();
88
89    for (int il = 0; il < n_layer; ++il) {
90        ggml_tensor * residual = inpL;
91
92        float freq_base_l  = 0.0f;
93        float freq_scale_l = 0.0f;
94        if constexpr (iswa) {
95            freq_base_l  = model.get_rope_freq_base (cparams, il);
96            freq_scale_l = model.get_rope_freq_scale(cparams, il);
97        } else {
98            freq_base_l  = freq_base;
99            freq_scale_l = freq_scale;
100        }
101
102        cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
103        cb(cur, "attn_norm", il);
104
105        ggml_tensor * qkv = build_lora_mm(model.layers[il].wqkv, cur);
106        cb(cur, "wqkv", il);
107
108        const int32_t n_head    = hparams.n_head(il);
109        const int32_t n_head_kv = hparams.n_head_kv(il);
110
111        const int64_t q_offset = 0;
112        const int64_t k_offset = head_dim_q * n_head;
113        const int64_t v_offset = k_offset + head_dim_q * n_head_kv;
114
115        ggml_tensor * Qcur = ggml_view_3d(ctx0, qkv, head_dim_q, n_head, n_tokens,
116                head_dim_q * sizeof(float), qkv->nb[1], q_offset * ggml_element_size(qkv));
117        ggml_tensor * Kcur = ggml_view_3d(ctx0, qkv, head_dim_q, n_head_kv, n_tokens,
118                head_dim_q * sizeof(float), qkv->nb[1], k_offset * ggml_element_size(qkv));
119        ggml_tensor * Vcur = ggml_view_3d(ctx0, qkv, head_dim_v, n_head_kv, n_tokens,
120                head_dim_v * sizeof(float), qkv->nb[1], v_offset * ggml_element_size(qkv));
121
122        cb(Qcur, "Qcur", il);
123        cb(Kcur, "Kcur", il);
124        cb(Vcur, "Vcur", il);
125
126        Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
127        cb(Qcur, "attn_q_norm", il);
128        Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
129        cb(Kcur, "attn_k_norm", il);
130
131        Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr,
132                n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
133                ext_factor, attn_factor, beta_fast, beta_slow);
134        Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr,
135                n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
136                ext_factor, attn_factor, beta_fast, beta_slow);
137
138        const float attn_scale = 1.0f / sqrtf(float(head_dim_q));
139
140        cur = build_attn(inp_attn,
141                model.layers[il].wo, NULL, model.layers[il].wo_s,
142                Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, attn_scale, il);
143        cb(cur, "attn_out", il);
144
145        if (il == n_layer - 1 && inp_out_ids) {
146            cur      = ggml_get_rows(ctx0, cur, inp_out_ids);
147            residual = ggml_get_rows(ctx0, residual, inp_out_ids);
148        }
149
150        cur = build_norm(cur, model.layers[il].attn_post_norm, NULL, LLM_NORM_RMS, il);
151        cb(cur, "attn_post_norm", il);
152
153        cur = ggml_add(ctx0, cur, residual);
154        cb(cur, "attn_residual", il);
155
156        residual = cur;
157
158        cur = build_norm(cur, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
159        cb(cur, "ffn_norm", il);
160
161        cur = build_ffn(cur,
162                model.layers[il].ffn_up,   NULL, NULL,
163                NULL,                      NULL, NULL,
164                model.layers[il].ffn_down, NULL, NULL,
165                NULL,
166                LLM_FFN_SWIGLU, LLM_FFN_SEQ, il);
167        cb(cur, "ffn_out", il);
168
169        cur = build_norm(cur, model.layers[il].ffn_post_norm, NULL, LLM_NORM_RMS, il);
170        cb(cur, "ffn_post_norm", il);
171
172        cur = ggml_add(ctx0, cur, residual);
173        cb(cur, "ffn_residual", il);
174
175        cur = build_cvec(cur, il);
176        cb(cur, "l_out", il);
177
178        // input for next layer
179        inpL = cur;
180    }
181
182    cur = inpL;
183
184    cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
185    res->t_embd = cur;
186
187    cur = build_lora_mm(model.output, cur, model.output_s);
188    res->t_logits = cur;
189
190    ggml_build_forward_expand(gf, cur);
191}
192
193// Explicit template instantiations
194template struct llama_model_plamo3::graph<false>;
195template struct llama_model_plamo3::graph<true>;