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 KiB148 linesraw
1#include "models.h"
2
3void llama_model_gpt2::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 12: type = LLM_TYPE_SMALL; break;
8        case 24: type = LLM_TYPE_MEDIUM; break;
9        case 36: type = LLM_TYPE_LARGE; break;
10        case 48: type = LLM_TYPE_XL; break;
11        default: type = LLM_TYPE_UNKNOWN;
12    }
13}
14
15void llama_model_gpt2::load_arch_tensors(llama_model_loader &) {
16    LLAMA_LOAD_LOCALS;
17
18    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
19    pos_embd = create_tensor(tn(LLM_TENSOR_POS_EMBD,   "weight"), {n_embd, n_ctx_train}, 0);
20
21    // output
22    output_norm   = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
23    output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"),   {n_embd}, 0);
24    output        = create_tensor(tn(LLM_TENSOR_OUTPUT,      "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
25
26    // if output is NULL, init from the input tok embed
27    if (output == NULL) {
28        output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
29    }
30
31    for (int i = 0; i < n_layer; ++i) {
32        auto & layer = layers[i];
33
34        layer.attn_norm   = create_tensor(tn(LLM_TENSOR_ATTN_NORM,   "weight", i), {n_embd}, 0);
35        layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM,   "bias", i),   {n_embd}, 0);
36
37        layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
38        layer.wqkv_b = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, 0);
39
40        layer.wo   = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
41        layer.wo_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i),   {n_embd}, 0);
42
43        layer.ffn_norm   = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
44        layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i),   {n_embd}, 0);
45
46        layer.ffn_down   = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
47        layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i),   {n_embd}, 0);
48
49        layer.ffn_up     = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd, n_ff}, 0);
50        layer.ffn_up_b   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "bias", i),   {n_ff}, 0);
51    }
52}
53
54std::unique_ptr<llm_graph_context> llama_model_gpt2::build_arch_graph(const llm_graph_params & params) const {
55    return std::make_unique<graph>(*this, params);
56}
57
58llama_model_gpt2::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 * pos;
65    ggml_tensor * inpL;
66
67    inpL = build_inp_embd(model.tok_embd);
68
69    // inp_pos - contains the positions
70    ggml_tensor * inp_pos = build_inp_pos();
71
72    auto * inp_attn = build_attn_inp_kv();
73
74    pos = ggml_get_rows(ctx0, model.pos_embd, inp_pos);
75    cb(pos, "pos_embd", -1);
76
77    inpL = ggml_add(ctx0, inpL, pos);
78    cb(inpL, "inpL", -1);
79
80    ggml_tensor * inp_out_ids = build_inp_out_ids();
81
82    for (int il = 0; il < n_layer; ++il) {
83        cur = build_norm(inpL,
84                model.layers[il].attn_norm,
85                model.layers[il].attn_norm_b,
86                LLM_NORM, il);
87        cb(cur, "attn_norm", il);
88
89        // self-attention
90        {
91            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,
92                    n_embd_head, n_head, n_head_kv, il);
93
94            cur = build_attn(inp_attn,
95                    model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
96                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
97        }
98
99        if (il == n_layer - 1 && inp_out_ids) {
100            cur  = ggml_get_rows(ctx0,  cur, inp_out_ids);
101            inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
102        }
103
104        // add the input
105        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
106        cb(ffn_inp, "ffn_inp", il);
107
108        // FF
109        {
110            cur = build_norm(ffn_inp,
111                    model.layers[il].ffn_norm,
112                    model.layers[il].ffn_norm_b,
113                    LLM_NORM, il);
114            cb(cur, "ffn_norm", il);
115
116            cur = build_ffn(cur,
117                    model.layers[il].ffn_up,   model.layers[il].ffn_up_b,   NULL,
118                    NULL,                      NULL,                        NULL,
119                    model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
120                    NULL,
121                    LLM_FFN_GELU, LLM_FFN_SEQ, il);
122            cb(cur, "ffn_out", il);
123        }
124
125        cur = ggml_add(ctx0, cur, ffn_inp);
126
127        cur = build_cvec(cur, il);
128        cb(cur, "l_out", il);
129
130        // input for next layer
131        inpL = cur;
132    }
133
134    cur = build_norm(inpL,
135            model.output_norm,
136            model.output_norm_b,
137            LLM_NORM, -1);
138
139    cb(cur, "result_norm", -1);
140    res->t_embd = cur;
141
142    cur = build_lora_mm(model.output, cur, model.output_s);
143
144    cb(cur, "result_output", -1);
145    res->t_logits = cur;
146
147    ggml_build_forward_expand(gf, cur);
148}