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
6.3 KiB174 linesraw
1#include "models.h"
2
3void llama_model_modern_bert::load_arch_hparams(llama_model_loader & ml) {
4    const bool found_swa = ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false);
5    if (found_swa && hparams.n_swa > 0) {
6        hparams.swa_type = LLAMA_SWA_TYPE_SYMMETRIC;
7        ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA, hparams.rope_freq_base_train_swa, false);
8        uint32_t swa_period = 3;
9        ml.get_key_or_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, swa_period, false);
10        hparams.set_swa_pattern(swa_period, true);
11    } else {
12        hparams.swa_type = LLAMA_SWA_TYPE_NONE;
13    }
14
15    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
16
17    // Some ModernBert derivatives (e.g. IBM Granite Embedding 97m R2) use
18    // SiLU/SwiGLU in the FFN instead of the default GELU/GeGLU.
19    hparams.llm_ffn_op = LLM_FFN_GEGLU;
20    std::string hidden_act;
21    if (ml.get_key(LLM_KV_HIDDEN_ACT, hidden_act, false)) {
22        hparams.llm_ffn_op = llm_ffn_op_type_from_string(hidden_act, LLM_FFN_GEGLU);
23    }
24
25    switch (hparams.n_layer()) {
26        case 12:
27            type = LLM_TYPE_47M; break; // granite-embedding-small
28        case 22:
29            type = LLM_TYPE_149M; break; // modern-bert-base
30        case 28:
31            type = LLM_TYPE_395M; break; // modern-bert-large
32        default: type = LLM_TYPE_UNKNOWN;
33    }
34}
35
36void llama_model_modern_bert::load_arch_tensors(llama_model_loader &) {
37    LLAMA_LOAD_LOCALS;
38
39    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
40    tok_norm = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight", 0), {n_embd}, 0);
41
42    output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
43
44    for(int i = 0; i < n_layer; ++i) {
45        auto& layer = layers[i];
46
47        if ( i != 0 ) {
48            layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
49        } else{
50            // layer 0 uses identity
51            layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);
52        }
53
54
55        layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, 3 * n_embd }, 0);
56        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT,   "weight", i), {n_embd, n_embd}, 0);
57
58        layer.ffn_up   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd, 2 * n_ff}, 0);
59        layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
60        layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
61    }
62
63    cls_out   = create_tensor(tn(LLM_TENSOR_CLS_OUT,  "weight"), {n_embd, hparams.n_cls_out}, TENSOR_NOT_REQUIRED);
64    cls_out_b = create_tensor(tn(LLM_TENSOR_CLS_OUT,  "bias"),   {hparams.n_cls_out},         TENSOR_NOT_REQUIRED);
65    cls       = create_tensor(tn(LLM_TENSOR_CLS,      "weight"), {n_embd, n_embd},            TENSOR_NOT_REQUIRED);
66    cls_norm  = create_tensor(tn(LLM_TENSOR_CLS_NORM, "weight"), {n_embd},                    TENSOR_NOT_REQUIRED);
67
68}
69
70std::unique_ptr<llm_graph_context> llama_model_modern_bert::build_arch_graph(const llm_graph_params & params) const {
71    return std::make_unique<graph>(*this, params);
72}
73
74llama_model_modern_bert::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
75    const int64_t n_embd_head = hparams.n_embd_head_v();
76
77    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());
78
79    ggml_tensor * cur;
80    ggml_tensor * inpL;
81    ggml_tensor * inp_pos = build_inp_pos();
82
83    // construct input embeddings (token, type, position)
84    inpL = build_inp_embd(model.tok_embd);
85    cb(inpL, "inp_embd", -1);
86
87    // embed layer norm
88    inpL = build_norm(inpL, model.tok_norm, nullptr, LLM_NORM, 0);
89    cb(inpL, "inp_norm", 0);
90
91    ggml_tensor * inp_out_ids = build_inp_out_ids();
92
93    auto * inp_attn = build_attn_inp_no_cache();
94
95    for (int il = 0; il < n_layer; ++il) {
96        const float freq_base_l  = model.get_rope_freq_base(cparams, il);
97        const float freq_scale_l = model.get_rope_freq_scale(cparams, il);
98
99        cur = inpL;
100
101        // attention layer norm
102        if (model.layers[il].attn_norm) {
103            cur = build_norm(inpL,
104                    model.layers[il].attn_norm, NULL,
105                    LLM_NORM, il);
106            cb(cur, "attn_norm", il);
107        }
108
109        // self attention
110        auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,
111                n_embd_head, n_head, n_head_kv, il);
112
113        // RoPE
114        Qcur = ggml_rope_ext(
115                ctx0, Qcur, inp_pos, nullptr,
116                n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
117                ext_factor, attn_factor, beta_fast, beta_slow
118                );
119
120        Kcur = ggml_rope_ext(
121                ctx0, Kcur, inp_pos, nullptr,
122                n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
123                ext_factor, attn_factor, beta_fast, beta_slow
124                );
125
126        cb(Qcur, "Qcur", il);
127        cb(Kcur, "Kcur", il);
128        cb(Vcur, "Vcur", il);
129
130        cur = build_attn(inp_attn,
131                    model.layers[il].wo, nullptr, model.layers[il].wo_s,
132                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
133        cb(cur, "kqv_out", il);
134
135        if (il == n_layer - 1 && inp_out_ids) {
136            cur  = ggml_get_rows(ctx0,  cur, inp_out_ids);
137            inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
138        }
139
140        // re-add the layer input
141        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
142        cb(ffn_inp, "ffn_inp", il);
143
144        // attention layer norm
145        cur = build_norm(ffn_inp,
146                model.layers[il].ffn_norm, NULL,
147                LLM_NORM, il);
148        cb(cur, "ffn_norm", il);
149
150        cur = build_ffn(cur,
151                model.layers[il].ffn_up,   NULL, NULL,
152                NULL,                      NULL, NULL,
153                model.layers[il].ffn_down, NULL, NULL,
154                NULL,
155                hparams.llm_ffn_op,
156                LLM_FFN_SEQ, il);
157
158        // attentions bypass the intermediate layer
159        cur = ggml_add(ctx0, cur, ffn_inp);
160
161        // input for next layer
162        inpL = cur;
163    }
164
165    cur = inpL;
166
167    cur = build_norm(cur,
168            model.output_norm, NULL,
169            LLM_NORM, -1);
170    cb(cur, "final_norm_out", -1);
171
172    res->t_embd = cur;
173    ggml_build_forward_expand(gf, cur);
174}