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
5.2 KiB161 linesraw
1#include "models.h"
2
3void llama_model_chatglm::load_arch_hparams(llama_model_loader & ml) {
4    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
5
6    switch (hparams.n_layer()) {
7        case 28: {
8            if (hparams.n_head(0) == 16) {
9                type = LLM_TYPE_1_5B;
10            } else {
11                type = LLM_TYPE_6B;
12            }
13        } break;
14        case 40: {
15            if (hparams.n_head(0) == 24) {
16                type = LLM_TYPE_4B;
17            } else {
18                type = LLM_TYPE_9B;
19            }
20        } break;
21        default: type = LLM_TYPE_UNKNOWN;
22    }
23}
24
25void llama_model_chatglm::load_arch_tensors(llama_model_loader &) {
26    LLAMA_LOAD_LOCALS;
27
28    tok_embd   = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD,      "weight"), {n_embd, n_vocab}, 0);
29
30    // output
31    output_norm   = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
32    output        = create_tensor(tn(LLM_TENSOR_OUTPUT,      "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
33    // if output is NULL, init from the input tok embed
34    if (output == NULL) {
35        output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
36    }
37
38    for (int i = 0; i < n_layer; ++i) {
39        auto & layer = layers[i];
40
41        layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
42        create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head, n_embd_k_gqa, n_embd_v_gqa, 0);
43
44        layer.wo   = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
45
46        layer.ffn_norm   = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
47
48        layer.ffn_up     = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd, n_ff * 2}, 0);
49
50        layer.ffn_down   = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
51    }
52}
53
54std::unique_ptr<llm_graph_context> llama_model_chatglm::build_arch_graph(const llm_graph_params & params) const {
55    return std::make_unique<graph>(*this, params);
56}
57
58llama_model_chatglm::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
59    const int64_t n_embd_head = hparams.n_embd_head_v();
60
61    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());
62
63    ggml_tensor * cur;
64    ggml_tensor * inpL;
65
66    inpL = build_inp_embd(model.tok_embd);
67
68    // inp_pos - contains the positions
69    ggml_tensor * inp_pos = build_inp_pos();
70
71    auto * inp_attn = build_attn_inp_kv();
72
73    ggml_tensor * inp_out_ids = build_inp_out_ids();
74
75    for (int il = 0; il < n_layer; ++il) {
76        ggml_tensor * inpSA = inpL;
77
78        cur = build_norm(inpL,
79                model.layers[il].attn_norm,
80                NULL,
81                LLM_NORM_RMS, il);
82        cb(cur, "attn_norm", il);
83
84        // self-attention
85        {
86            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,
87                    n_embd_head, n_head, n_head_kv, il);
88
89            //printf("freq_base: %f freq_scale: %f ext_factor: %f attn_factor: %f\n", freq_base, freq_scale, ext_factor, attn_factor);
90            Qcur = ggml_rope_ext(
91                    ctx0, Qcur, inp_pos, nullptr,
92                    n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
93                    ext_factor, attn_factor, beta_fast, beta_slow
94                    );
95
96            Kcur = ggml_rope_ext(
97                    ctx0, Kcur, inp_pos, nullptr,
98                    n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
99                    ext_factor, attn_factor, beta_fast, beta_slow
100                    );
101
102            cb(Qcur, "Qcur", il);
103            cb(Kcur, "Kcur", il);
104            cb(Vcur, "Vcur", il);
105
106            cur = build_attn(inp_attn,
107                    model.layers[il].wo, NULL, model.layers[il].wo_s,
108                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
109        }
110
111        if (il == n_layer - 1 && inp_out_ids) {
112            cur   = ggml_get_rows(ctx0,   cur, inp_out_ids);
113            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
114        }
115
116        // Add the input
117        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
118        cb(ffn_inp, "ffn_inp", il);
119
120        // FF
121        {
122            cur = build_norm(ffn_inp,
123                    model.layers[il].ffn_norm,
124                    NULL,
125                    LLM_NORM_RMS, il);
126            cb(cur, "ffn_norm", il);
127
128            cur = build_ffn(cur,
129                    model.layers[il].ffn_up,   NULL, NULL,
130                    NULL,                      NULL, NULL,
131                    model.layers[il].ffn_down, NULL, NULL,
132                    NULL,
133                    LLM_FFN_SWIGLU, LLM_FFN_SEQ, il);
134            cb(cur, "ffn_out", il);
135
136        }
137
138        cur = ggml_add(ctx0, cur, ffn_inp);
139
140        cur = build_cvec(cur, il);
141        cb(cur, "l_out", il);
142
143        // input for next layer
144        inpL = cur;
145    }
146
147    cur = build_norm(inpL,
148            model.output_norm,
149            NULL,
150            LLM_NORM_RMS, -1);
151
152    cb(cur, "result_norm", -1);
153    res->t_embd = cur;
154
155    cur = build_lora_mm(model.output, cur, model.output_s);
156
157    cb(cur, "result_output", -1);
158    res->t_logits = cur;
159
160    ggml_build_forward_expand(gf, cur);
161}