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.9 KiB132 linesraw
1#include "models.h"
2
3void llama_model_jais::load_arch_hparams(llama_model_loader & ml) {
4    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
5    ml.get_key(LLM_KV_ATTENTION_MAX_ALIBI_BIAS, hparams.f_max_alibi_bias, false);
6
7    switch (hparams.n_layer()) {
8        case 24: type = LLM_TYPE_1_3B; break;
9        case 40: type = LLM_TYPE_13B; break;
10        /* TODO: add variants */
11        default: type = LLM_TYPE_UNKNOWN;
12    }
13}
14
15void llama_model_jais::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
20    // output
21    output_norm   = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
22    output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"),   {n_embd}, 0);
23    output        = create_tensor(tn(LLM_TENSOR_OUTPUT,      "weight"), {n_embd, n_vocab}, 0);
24
25    for (int i = 0; i < n_layer; ++i) {
26        auto & layer = layers[i];
27
28        layer.attn_norm   = create_tensor(tn(LLM_TENSOR_ATTN_NORM,   "weight", i), {n_embd}, 0);
29        layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM,   "bias", i),   {n_embd}, 0);
30
31        layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
32        layer.wqkv_b = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, 0);
33
34        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
35        layer.wo_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
36
37        layer.ffn_norm   = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
38        layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i),   {n_embd}, 0);
39
40        layer.ffn_down   = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
41        layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i),   {n_embd}, 0);
42
43        layer.ffn_gate   = create_tensor(tn(LLM_TENSOR_FFN_GATE,   "weight", i), {n_embd, n_ff}, 0);
44        layer.ffn_gate_b = create_tensor(tn(LLM_TENSOR_FFN_GATE,   "bias", i),   {n_ff}, 0);
45
46        layer.ffn_up     = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd, n_ff}, 0);
47        layer.ffn_up_b   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "bias", i),   {n_ff}, 0);
48    }
49}
50
51std::unique_ptr<llm_graph_context> llama_model_jais::build_arch_graph(const llm_graph_params & params) const {
52    return std::make_unique<graph>(*this, params);
53}
54
55llama_model_jais::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
56    const int64_t n_embd_head = hparams.n_embd_head_v();
57
58    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());
59
60    ggml_tensor * cur;
61    ggml_tensor * inpL;
62
63    inpL = build_inp_embd(model.tok_embd);
64
65    auto * inp_attn = build_attn_inp_kv();
66
67    ggml_tensor * inp_out_ids = build_inp_out_ids();
68
69    for (int il = 0; il < n_layer; ++il) {
70        cur = build_norm(inpL,
71                model.layers[il].attn_norm,
72                model.layers[il].attn_norm_b,
73                LLM_NORM, il);
74        cb(cur, "attn_norm", il);
75
76        // self-attention
77        {
78            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,
79                    n_embd_head, n_head, n_head_kv, il);
80
81            cur = build_attn(inp_attn,
82                    model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
83                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/float(n_embd_head), il);
84        }
85        if (il == n_layer - 1 && inp_out_ids) {
86            cur  = ggml_get_rows(ctx0,  cur, inp_out_ids);
87            inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
88        }
89        // add the input
90        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
91        cb(ffn_inp, "ffn_inp", il);
92
93        // FF
94        {
95            cur = build_norm(ffn_inp,
96                    model.layers[il].ffn_norm,
97                    model.layers[il].ffn_norm_b,
98                    LLM_NORM, il);
99            cb(cur, "ffn_norm", il);
100
101            cur = build_ffn(cur,
102                    model.layers[il].ffn_up,   model.layers[il].ffn_up_b,   NULL,
103                    model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
104                    model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
105                    NULL,
106                    LLM_FFN_SILU, LLM_FFN_PAR, il);
107            cb(cur, "ffn_out", il);
108        }
109
110        cur = ggml_add(ctx0, cur, ffn_inp);
111
112        cur = build_cvec(cur, il);
113        cb(cur, "l_out", il);
114
115        // input for next layer
116        inpL = cur;
117    }
118    cur = build_norm(inpL,
119            model.output_norm,
120            model.output_norm_b,
121            LLM_NORM, -1);
122
123    cb(cur, "result_norm", -1);
124    res->t_embd = cur;
125
126    cur = build_lora_mm(model.output, cur, model.output_s);
127
128    cb(cur, "result_output", -1);
129    res->t_logits = cur;
130
131    ggml_build_forward_expand(gf, cur);
132}