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.2 KiB158 linesraw
1#include "models.h"
2
3void llama_model_cogvlm::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 32: type = LLM_TYPE_13B; break;
8        default: type = LLM_TYPE_UNKNOWN;
9    }
10}
11
12void llama_model_cogvlm::load_arch_tensors(llama_model_loader &) {
13    LLAMA_LOAD_LOCALS;
14
15    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
16
17    // output
18    output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
19    output      = create_tensor(tn(LLM_TENSOR_OUTPUT,      "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
20
21    // if output is NULL, init from the input tok embed
22    if (output == NULL) {
23        output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
24    }
25
26    for (int i = 0; i < n_layer; ++i) {
27        auto & layer = layers[i];
28
29        layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
30        layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd_head_k * n_head * 3}, 0);
31        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
32
33        layer.visexp_attn_wqkv = create_tensor(tn(LLM_TENSOR_VISEXP_ATTN_QKV, "weight", i), {n_embd, n_embd_head_k * n_head * 3}, 0);
34        layer.visexp_attn_wo = create_tensor(tn(LLM_TENSOR_VISEXP_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
35
36        layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
37
38        layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
39        layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd,   n_ff}, 0);
40        layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {  n_ff, n_embd}, 0);
41        layer.ffn_up   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd,   n_ff}, 0);
42
43        layer.visexp_ffn_gate = create_tensor(tn(LLM_TENSOR_VISEXP_FFN_GATE, "weight", i), {n_embd,   n_ff}, 0);
44        layer.visexp_ffn_down = create_tensor(tn(LLM_TENSOR_VISEXP_FFN_DOWN, "weight", i), {  n_ff, n_embd}, 0);
45        layer.visexp_ffn_up   = create_tensor(tn(LLM_TENSOR_VISEXP_FFN_UP,   "weight", i), {n_embd,   n_ff}, 0);
46    }
47}
48
49std::unique_ptr<llm_graph_context> llama_model_cogvlm::build_arch_graph(const llm_graph_params & params) const {
50    return std::make_unique<graph>(*this, params);
51}
52
53llama_model_cogvlm::graph::graph(const llama_model & model, const llm_graph_params & params) :
54    llm_graph_context(params) {
55    const int64_t n_embd_head = hparams.n_embd_head_v();
56    const float   kq_scale    = 1.0f / sqrtf(float(n_embd_head));
57
58    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());
59    GGML_ASSERT(n_embd_head == n_rot);
60
61    ggml_tensor * inpL;
62    ggml_tensor * cur;
63
64    inpL = build_inp_embd(model.tok_embd);
65
66    ggml_tensor * inp_pos = build_inp_pos();
67
68    auto * inp_attn = build_attn_inp_kv();
69
70    // check ubatch to see if we have input tokens (text)
71    // or an input embedding vector (image)
72    bool is_text;
73    if (ubatch.token) {
74        is_text = true;
75    } else {
76        is_text = false;
77    }
78
79    for (int il = 0; il < n_layer; ++il) {
80        // get either the text or image weight tensors
81        ggml_tensor *wqkv, *wo, *wo_s;
82        ggml_tensor *ffn_gate, *ffn_down, *ffn_up;
83
84        if (is_text) {
85            wqkv     = model.layers[il].wqkv;
86            wo       = model.layers[il].wo;
87            wo_s     = model.layers[il].wo_s;
88            ffn_gate = model.layers[il].ffn_gate;
89            ffn_down = model.layers[il].ffn_down;
90            ffn_up   = model.layers[il].ffn_up;
91        } else {
92            wqkv     = model.layers[il].visexp_attn_wqkv;
93            wo       = model.layers[il].visexp_attn_wo;
94            wo_s     = nullptr;
95            ffn_gate = model.layers[il].visexp_ffn_gate;
96            ffn_down = model.layers[il].visexp_ffn_down;
97            ffn_up   = model.layers[il].visexp_ffn_up;
98        }
99
100        ggml_tensor * inpSA = inpL;
101        cur = build_norm(inpSA, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
102
103        // build self attention
104        {
105            ggml_tensor * qkv = build_lora_mm(wqkv, cur);
106
107            // split qkv into Q, K, V along the first dimension
108            ggml_tensor * Qcur =
109                ggml_view_3d(ctx0, qkv, n_embd_head, n_head, n_tokens, n_embd_head * sizeof(float), qkv->nb[1], 0);
110            ggml_tensor * Kcur = ggml_view_3d(ctx0, qkv, n_embd_head, n_head_kv, n_tokens, n_embd_head * sizeof(float),
111                                              qkv->nb[1], n_embd * ggml_element_size(qkv));
112            ggml_tensor * Vcur = ggml_view_3d(ctx0, qkv, n_embd_head, n_head_kv, n_tokens, n_embd_head * sizeof(float),
113                                              qkv->nb[1], 2 * n_embd * ggml_element_size(qkv));
114
115            Qcur = ggml_rope(ctx0, Qcur, inp_pos, n_embd_head, rope_type);
116            Kcur = ggml_rope(ctx0, Kcur, inp_pos, n_embd_head, rope_type);
117
118            cur = build_attn(inp_attn,
119                wo, nullptr, wo_s,
120                Qcur, Kcur, Vcur,
121                nullptr, nullptr, nullptr,
122                kq_scale, il);
123            cb(cur, "attn_out", il);
124        }
125
126        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
127        cb(ffn_inp, "ffn_inp", il);
128
129        cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
130        cb(cur, "ffn_norm", il);
131
132        cur = build_ffn(cur,
133                ffn_up, NULL, NULL,
134                ffn_gate, NULL, NULL,
135                ffn_down, NULL, NULL,
136                NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
137
138        cur = ggml_add(ctx0, cur, ffn_inp);
139        cb(cur, "ffn_out", il);
140
141        cur = build_cvec(cur, il);
142        cb(cur, "l_out", il);
143
144        // input for next layer
145        inpL = cur;
146    }
147
148    cur = inpL;
149
150    cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
151    cb(cur, "result_norm", -1);
152    res->t_embd = cur;
153
154    cur = build_lora_mm(model.output, cur, model.output_s);
155    cb(cur, "result_output", -1);
156    res->t_logits = cur;
157    ggml_build_forward_expand(gf, cur);
158}