char-fork/llama.cpp

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

Iggy Jacksonmodels: clean up some dead switch branches in old models (#28669)d344123fe

char/aliceai-t5-moe
9.2 KiB221 linesraw
1#include "models.h"
2
3void llama_model_bert::load_arch_hparams(llama_model_loader & ml) {
4    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
5
6    switch (hparams.n_layer()) {
7        case 3:
8            type = LLM_TYPE_17M; break; // bge-micro
9        case 6:
10            type = LLM_TYPE_22M; break; // MiniLM-L6
11        case 12:
12            switch (hparams.n_embd) {
13                case 384: type = LLM_TYPE_33M; break; // MiniLM-L12, bge-small
14                case 768: type = LLM_TYPE_109M; break; // bge-base
15                default: type = LLM_TYPE_UNKNOWN;
16            } break;
17        case 24:
18            type = LLM_TYPE_335M; break; // bge-large
19        default: type = LLM_TYPE_UNKNOWN;
20    }
21}
22
23void llama_model_bert::load_arch_tensors(llama_model_loader &) {
24    LLAMA_LOAD_LOCALS;
25
26    if (n_token_types == 0) {
27        throw std::runtime_error(arch_name() + " model needs to define token type count");
28    }
29    tok_embd     = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD,  "weight"), {n_embd, n_vocab}, 0);
30    type_embd    = create_tensor(tn(LLM_TENSOR_TOKEN_TYPES, "weight"), {n_embd, n_token_types}, TENSOR_NOT_REQUIRED);
31
32    pos_embd = create_tensor(tn(LLM_TENSOR_POS_EMBD,    "weight"), {n_embd, n_ctx_train}, 0);
33
34    cls   = create_tensor(tn(LLM_TENSOR_CLS, "weight"), {n_embd, n_embd}, TENSOR_NOT_REQUIRED);
35    cls_b = create_tensor(tn(LLM_TENSOR_CLS, "bias"),   {n_embd},         TENSOR_NOT_REQUIRED);
36
37    cls_out   = create_tensor(tn(LLM_TENSOR_CLS_OUT, "weight"), {n_embd, hparams.n_cls_out}, TENSOR_NOT_REQUIRED);
38    cls_out_b = create_tensor(tn(LLM_TENSOR_CLS_OUT, "bias"),   {hparams.n_cls_out},         TENSOR_NOT_REQUIRED);
39
40    tok_norm   = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight", 0), {n_embd}, 0);
41    tok_norm_b = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "bias",   0), {n_embd}, 0);
42
43    for (int i = 0; i < n_layer; ++i) {
44        auto & layer = layers[i];
45
46        create_tensor_qkv(layer, i, n_embd, n_embd, n_embd_gqa, n_embd_gqa, 0);
47
48        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
49        layer.wo_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
50
51        layer.attn_out_norm   = create_tensor(tn(LLM_TENSOR_ATTN_OUT_NORM, "weight", i), {n_embd}, 0);
52        layer.attn_out_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT_NORM, "bias", i),   {n_embd}, 0);
53
54        layer.ffn_up     = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd, n_ff}, 0);
55        layer.ffn_up_b   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "bias", i),   {n_ff}, TENSOR_NOT_REQUIRED);
56        layer.ffn_down   = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
57        layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i),   {n_embd}, TENSOR_NOT_REQUIRED);
58
59        layer.layer_out_norm   = create_tensor(tn(LLM_TENSOR_LAYER_OUT_NORM, "weight", i), {n_embd}, 0);
60        layer.layer_out_norm_b = create_tensor(tn(LLM_TENSOR_LAYER_OUT_NORM, "bias", i),   {n_embd}, 0);
61    }
62}
63
64std::unique_ptr<llm_graph_context> llama_model_bert::build_arch_graph(const llm_graph_params & params) const {
65    return std::make_unique<graph>(*this, params);
66}
67
68llama_model_bert::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
69    const int64_t n_embd_head = hparams.n_embd_head_v();
70
71    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());
72
73    ggml_tensor * cur;
74    ggml_tensor * inpL;
75    ggml_tensor * inp_pos = nullptr;
76
77    if (model.arch != LLM_ARCH_JINA_BERT_V2) {
78        inp_pos = build_inp_pos();
79    }
80
81    // construct input embeddings (token, type, position)
82    inpL = build_inp_embd(model.tok_embd);
83
84    // token types are hardcoded to zero ("Sentence A")
85    if (model.type_embd) {
86        ggml_tensor * type_row0 = ggml_view_1d(ctx0, model.type_embd, n_embd, 0);
87        inpL                    = ggml_add(ctx0, inpL, type_row0);
88    }
89    if (model.arch == LLM_ARCH_BERT) {
90        inpL = ggml_add(ctx0, ggml_get_rows(ctx0, model.pos_embd, inp_pos), inpL);
91    }
92    cb(inpL, "inp_embd", -1);
93
94    // embed layer norm
95    inpL = build_norm(inpL, model.tok_norm, model.tok_norm_b, LLM_NORM, 0);
96    cb(inpL, "inp_norm", 0);
97
98    auto * inp_attn = build_attn_inp_no_cache();
99
100    ggml_tensor * inp_out_ids = build_inp_out_ids();
101
102    for (int il = 0; il < n_layer; ++il) {
103        ggml_tensor * cur = inpL;
104
105        {
106            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,
107                    n_embd_head, n_head, n_head_kv, il);
108
109            if (model.layers[il].attn_q_norm) {
110                Qcur = ggml_reshape_2d(ctx0, Qcur, n_embd_head * n_head, n_tokens);
111
112                Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, model.layers[il].attn_q_norm_b, LLM_NORM, il);
113
114                Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
115            }
116
117            if (model.layers[il].attn_k_norm) {
118                Kcur = ggml_reshape_2d(ctx0, Kcur, n_embd_head * n_head_kv, n_tokens);
119
120                Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, model.layers[il].attn_k_norm_b, LLM_NORM, il);
121
122                Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
123            }
124
125            // RoPE
126            if (model.arch == LLM_ARCH_NOMIC_BERT || model.arch == LLM_ARCH_NOMIC_BERT_MOE ||
127                model.arch == LLM_ARCH_JINA_BERT_V3) {
128                Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
129                                     ext_factor, attn_factor, beta_fast, beta_slow);
130
131                Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
132                                     ext_factor, attn_factor, beta_fast, beta_slow);
133            }
134
135            cb(Qcur, "Qcur", il);
136            cb(Kcur, "Kcur", il);
137            cb(Vcur, "Vcur", il);
138
139            cur = build_attn(inp_attn,
140                    model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
141                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
142            cb(cur, "kqv_out", il);
143        }
144
145        if (il == n_layer - 1 && inp_out_ids) {
146            cur  = ggml_get_rows(ctx0, cur, inp_out_ids);
147            inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
148        }
149
150        // re-add the layer input
151        cur = ggml_add(ctx0, cur, inpL);
152
153        // attention layer norm
154        cur = build_norm(cur, model.layers[il].attn_out_norm, model.layers[il].attn_out_norm_b, LLM_NORM, il);
155
156        if (model.layers[il].attn_norm_2 != nullptr) {
157            cur = ggml_add(ctx0, cur, inpL);  // re-add the layer input
158            cur = build_norm(cur, model.layers[il].attn_norm_2, model.layers[il].attn_norm_2_b, LLM_NORM, il);
159        }
160
161        ggml_tensor * ffn_inp = cur;
162        cb(ffn_inp, "ffn_inp", il);
163
164        // feed-forward network
165        if (hparams.moe_every_n_layers > 0 && il % hparams.moe_every_n_layers == 1) {
166            // MoE branch
167            cur = build_moe_ffn(cur,
168                    model.layers[il].ffn_gate_inp,
169                    model.layers[il].ffn_up_exps,
170                    nullptr,
171                    model.layers[il].ffn_down_exps,
172                    nullptr,
173                    hparams.n_expert, hparams.n_expert_used(),
174                    LLM_FFN_GELU, false,
175                    hparams.expert_weights_scale,
176                    LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
177                    il);
178            cb(cur, "ffn_moe_out", il);
179        } else if (model.arch == LLM_ARCH_BERT || model.arch == LLM_ARCH_NOMIC_BERT_MOE ||
180                   model.arch == LLM_ARCH_JINA_BERT_V3) {
181            cur = build_ffn(cur,
182                    model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
183                    NULL, NULL, NULL,
184                    model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL, NULL,
185                    LLM_FFN_GELU, LLM_FFN_SEQ, il);
186            cb(cur, "ffn_out", il);
187        } else if (model.arch == LLM_ARCH_JINA_BERT_V2) {
188            const bool up_contains_gate = !model.layers[il].ffn_gate && model.layers[il].ffn_up->ne[1] != hparams.n_ff();
189            auto type_op = up_contains_gate ? LLM_FFN_GEGLU : LLM_FFN_GELU;
190            cur = build_ffn(cur,
191                    model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,
192                    model.layers[il].ffn_gate, NULL, NULL,
193                    model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL, NULL,
194                    type_op, LLM_FFN_PAR, il);
195            cb(cur, "ffn_out", il);
196        } else {
197            cur = build_ffn(cur,
198                model.layers[il].ffn_up, NULL, NULL,
199                model.layers[il].ffn_gate, NULL, NULL,
200                model.layers[il].ffn_down, NULL, NULL,
201                NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
202            cb(cur, "ffn_out", il);
203        }
204
205        // attentions bypass the intermediate layer
206        cur = ggml_add(ctx0, cur, ffn_inp);
207
208        // output layer norm
209        cur = build_norm(cur, model.layers[il].layer_out_norm, model.layers[il].layer_out_norm_b, LLM_NORM, il);
210
211        // input for next layer
212        inpL = cur;
213    }
214
215    cur = inpL;
216
217    cb(cur, "result_embd", -1);
218    res->t_embd = cur;
219
220    ggml_build_forward_expand(gf, cur);
221}