char-fork/llama.cpp

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

Georgi Gerganovhparams : refactor `hparams.n_layer` (#24060)7acb4e8cd

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