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
4.1 KiB124 linesraw
1#include "models.h"
2
3void llama_model_eurobert::load_arch_hparams(llama_model_loader & ml) {
4    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
5
6    if (hparams.n_layer() == 12) {
7        type = LLM_TYPE_SMALL;  // 0.2B
8    }
9}
10
11void llama_model_eurobert::load_arch_tensors(llama_model_loader &) {
12    LLAMA_LOAD_LOCALS;
13
14    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
15
16    output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
17
18    for (int i = 0; i < n_layer; ++i) {
19        auto & layer = layers[i];
20
21        layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
22
23        create_tensor_qkv(layer, i, n_embd, n_embd, n_embd_gqa, n_embd_gqa, 0);
24        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
25
26        layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
27
28        layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
29        layer.ffn_up   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd, n_ff}, 0);
30        layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
31    }
32}
33
34std::unique_ptr<llm_graph_context> llama_model_eurobert::build_arch_graph(const llm_graph_params & params) const {
35    return std::make_unique<graph>(*this, params);
36}
37
38llama_model_eurobert::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
39    const int64_t n_embd_head = hparams.n_embd_head_v();
40
41    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());
42
43    ggml_tensor * cur;
44    ggml_tensor * inpL;
45    ggml_tensor * inp_pos = build_inp_pos();
46
47    inpL = build_inp_embd(model.tok_embd);
48    cb(inpL, "inp_embd", -1);
49
50    auto * inp_attn = build_attn_inp_no_cache();
51
52    ggml_tensor * inp_out_ids = build_inp_out_ids();
53
54    for (int il = 0; il < n_layer; ++il) {
55        ggml_tensor * cur = inpL;
56
57        cur = build_norm(inpL,
58                model.layers[il].attn_norm, NULL,
59                LLM_NORM_RMS, il);
60
61        {
62            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,
63                    n_embd_head, n_head, n_head_kv, il);
64
65            Qcur = ggml_rope_ext(
66                    ctx0, Qcur, inp_pos, nullptr,
67                    n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
68                    ext_factor, attn_factor, beta_fast, beta_slow
69                    );
70
71            Kcur = ggml_rope_ext(
72                    ctx0, Kcur, inp_pos, nullptr,
73                    n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
74                    ext_factor, attn_factor, beta_fast, beta_slow
75                    );
76
77            cb(Qcur, "Qcur", il);
78            cb(Kcur, "Kcur", il);
79            cb(Vcur, "Vcur", il);
80
81            cur = build_attn(inp_attn,
82                    model.layers[il].wo, nullptr, model.layers[il].wo_s,
83                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
84            cb(cur, "kqv_out", il);
85        }
86
87        if (il == n_layer - 1 && inp_out_ids) {
88            cur  = ggml_get_rows(ctx0,  cur, inp_out_ids);
89            inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
90        }
91
92        cur = ggml_add(ctx0, cur, inpL);
93
94        ggml_tensor * ffn_inp = cur;
95        cb(ffn_inp, "ffn_inp", il);
96
97        cur = build_norm(ffn_inp,
98                model.layers[il].ffn_norm, NULL,
99                LLM_NORM_RMS, il);
100        cb(cur, "ffn_norm", il);
101
102        cur = build_ffn(cur,
103                model.layers[il].ffn_up, NULL, NULL,
104                model.layers[il].ffn_gate, NULL, NULL,
105                model.layers[il].ffn_down, NULL, NULL,
106                NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
107        cb(cur, "ffn_out", il);
108
109        cur = ggml_add(ctx0, cur, ffn_inp);
110
111        // input for next layer
112        inpL = cur;
113    }
114    cur = inpL;
115
116    cur = build_norm(cur,
117            model.output_norm, NULL,
118            LLM_NORM_RMS, -1);
119
120    cb(cur, "result_embd", -1);
121    res->t_embd = cur;
122
123    ggml_build_forward_expand(gf, cur);
124}