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.4 KiB204 linesraw
1#include "models.h"
2#include <float.h>
3
4void llama_model_chameleon::load_arch_hparams(llama_model_loader & ml) {
5    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
6    hparams.f_norm_eps = 1e-5;  // eps for qk-norm, torch default
7    ml.get_key(LLM_KV_SWIN_NORM, hparams.swin_norm, false);
8
9    switch (hparams.n_layer()) {
10        case 32: type = LLM_TYPE_7B; break;
11        case 48: type = LLM_TYPE_34B; break;
12        default: type = LLM_TYPE_UNKNOWN;
13   }
14}
15
16void llama_model_chameleon::load_arch_tensors(llama_model_loader &) {
17    LLAMA_LOAD_LOCALS;
18
19    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
20
21    // output
22    output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
23    output      = create_tensor(tn(LLM_TENSOR_OUTPUT,      "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
24    // if output is NULL, init from the input tok embed
25    if (output == NULL) {
26        output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
27    }
28
29    for (int i = 0; i < n_layer; ++i) {
30        auto & layer = layers[i];
31
32        layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
33        layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k, n_head}, 0);
34        layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k, n_head_kv}, 0);
35        layer.attn_q_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "bias", i),  {n_embd_head_k, n_head}, TENSOR_NOT_REQUIRED);
36        layer.attn_k_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "bias", i),  {n_embd_head_k, n_head_kv}, TENSOR_NOT_REQUIRED);
37
38        create_tensor_qkv(layer, i, n_embd, n_embd, n_embd_gqa, n_embd_gqa, 0);
39        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
40
41        layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
42
43        layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd,   n_ff}, 0);
44        layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {  n_ff, n_embd}, 0);
45        layer.ffn_up   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd,   n_ff}, 0);
46    }
47}
48
49std::unique_ptr<llm_graph_context> llama_model_chameleon::build_arch_graph(const llm_graph_params & params) const {
50    return std::make_unique<graph>(*this, params);
51}
52
53llama_model_chameleon::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
54    const int64_t n_embd_head = hparams.n_embd_head_v();
55
56    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());
57    GGML_ASSERT(n_embd_head == n_rot);
58
59    ggml_tensor * cur;
60    ggml_tensor * inpL;
61
62    inpL = build_inp_embd(model.tok_embd);
63
64    // inp_pos - contains the positions
65    ggml_tensor * inp_pos = build_inp_pos();
66
67    auto * inp_attn = build_attn_inp_kv();
68
69    ggml_tensor * inp_out_ids = build_inp_out_ids();
70
71    for (int il = 0; il < n_layer; ++il) {
72        ggml_tensor * inpSA = inpL;
73
74        // norm
75        if (hparams.swin_norm) {
76            cur = inpL;
77        } else {
78            cur = build_norm(inpL,
79                    model.layers[il].attn_norm, NULL,
80                    LLM_NORM_RMS, il);
81            cb(cur, "attn_norm", il);
82        }
83
84        // self-attention
85        {
86            // compute Q and K and RoPE them
87            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,
88                    n_embd_head, n_head, n_head_kv, il);
89
90            if (model.layers[il].attn_q_norm) {
91                Qcur = build_norm(Qcur,
92                        model.layers[il].attn_q_norm,
93                        model.layers[il].attn_q_norm_b,
94                        LLM_NORM, il);
95                cb(Qcur, "Qcur", il);
96            }
97
98            if (model.layers[il].attn_k_norm) {
99                Kcur = build_norm(Kcur,
100                        model.layers[il].attn_k_norm,
101                        model.layers[il].attn_k_norm_b,
102                        LLM_NORM, il);
103                cb(Kcur, "Kcur", il);
104            }
105
106            Qcur = ggml_rope_ext(
107                    ctx0, Qcur, inp_pos, nullptr,
108                    n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
109                    ext_factor, attn_factor, beta_fast, beta_slow
110                    );
111
112            Kcur = ggml_rope_ext(
113                    ctx0, Kcur, inp_pos, nullptr,
114                    n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
115                    ext_factor, attn_factor, beta_fast, beta_slow
116                    );
117
118            cb(Qcur, "Qcur", il);
119            cb(Kcur, "Kcur", il);
120            cb(Vcur, "Vcur", il);
121
122            cur = build_attn(inp_attn,
123                    model.layers[il].wo, nullptr, model.layers[il].wo_s,
124                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
125        }
126
127        if (il == n_layer - 1 && inp_out_ids) {
128            cur   = ggml_get_rows(ctx0,   cur, inp_out_ids);
129            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
130        }
131
132        if (hparams.swin_norm) {
133            cur = build_norm(cur,
134                    model.layers[il].attn_norm, NULL,
135                    LLM_NORM_RMS, il);
136        }
137
138        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
139        cb(ffn_inp, "ffn_inp", il);
140
141        // feed-forward network
142        if (!hparams.swin_norm) {
143            cur = build_norm(ffn_inp,
144                    model.layers[il].ffn_norm, NULL,
145                    LLM_NORM_RMS, il);
146            cb(cur, "ffn_norm", il);
147        }
148
149        cur = build_ffn(cur,
150                model.layers[il].ffn_up,   NULL, NULL,
151                model.layers[il].ffn_gate, NULL, NULL,
152                model.layers[il].ffn_down, NULL, NULL,
153                NULL,
154                LLM_FFN_SILU, LLM_FFN_PAR, il);
155        cb(cur, "ffn_out", il);
156
157        if (hparams.swin_norm) {
158            cur = build_norm(cur,
159                    model.layers[il].ffn_norm, NULL,
160                    LLM_NORM_RMS, il);
161            cb(cur, "ffn_norm", il);
162        }
163
164        cur = ggml_add(ctx0, cur, ffn_inp);
165        cb(cur, "ffn_out", il);
166
167        cur = build_cvec(cur, il);
168        cb(cur, "l_out", il);
169
170        // input for next layer
171        inpL = cur;
172    }
173
174    cur = inpL;
175
176    cur = build_norm(cur,
177            model.output_norm, NULL,
178            LLM_NORM_RMS, -1);
179
180    cb(cur, "result_norm", -1);
181    res->t_embd = cur;
182
183    // lm_head
184    cur = build_lora_mm(model.output, cur, model.output_s);
185    cb(cur, "result_output_with_img_logits", -1);
186
187    // TODO: this suppresses the output of image tokens, which is required to enable text-only outputs.
188    // Needs to be removed once image outputs are supported.
189    int img_token_end_idx = 8196;
190    int img_token_start_idx = 4;
191    int num_img_tokens = img_token_end_idx - img_token_start_idx;
192    // creates 1d tensor of size num_img_tokens and values -FLT_MAX,
193    // which ensures that text token values are always at least larger than image token values
194    ggml_tensor * img_logits = ggml_new_tensor_1d(ctx0, GGML_TYPE_F32, num_img_tokens);
195    img_logits = ggml_clamp(ctx0, img_logits, -FLT_MAX, -FLT_MAX);
196    cb(img_logits, "img_logits", -1);
197
198    cur = ggml_set_1d(ctx0, cur, img_logits, ggml_element_size(cur) * img_token_start_idx);
199
200    cb(cur, "result_output", -1);
201    res->t_logits = cur;
202
203    ggml_build_forward_expand(gf, cur);
204}