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.8 KiB138 linesraw
1#include "models.h"
2
3void llama_model_dream::load_arch_hparams(llama_model_loader & ml) {
4    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
5
6    // Dream models are primarily 7B with 28 layers
7    switch (hparams.n_layer()) {
8        case 28:
9            type = LLM_TYPE_7B;
10            break;
11        default:
12            type = LLM_TYPE_UNKNOWN;
13    }
14    // Set non-causal attention for diffusion models
15    hparams.causal_attn = false;
16}
17
18void llama_model_dream::load_arch_tensors(llama_model_loader &) {
19    LLAMA_LOAD_LOCALS;
20
21    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
22
23    // output
24    output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
25    output      = create_tensor(tn(LLM_TENSOR_OUTPUT,      "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
26    output_b    = create_tensor(tn(LLM_TENSOR_OUTPUT,      "bias"),   {n_vocab}, TENSOR_NOT_REQUIRED);
27    // if output is NULL, init from the input tok embed
28    if (output == NULL) {
29        output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
30    }
31
32    for (int i = 0; i < n_layer; ++i) {
33        auto & layer = layers[i];
34
35        layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
36
37        create_tensor_qkv(layer, i, n_embd, n_embd, n_embd_gqa, n_embd_gqa, 0);
38        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
39
40        layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
41
42        layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd,   n_ff}, 0);
43        layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {  n_ff, n_embd}, 0);
44        layer.ffn_up   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd,   n_ff}, 0);
45    }
46}
47
48std::unique_ptr<llm_graph_context> llama_model_dream::build_arch_graph(const llm_graph_params & params) const {
49    return std::make_unique<graph>(*this, params);
50}
51
52llama_model_dream::graph::graph(const llama_model & model, const llm_graph_params & params) :
53    llm_graph_context(params) {
54    //copied from qwen2
55    const int64_t n_embd_head = hparams.n_embd_head_v();
56
57    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());
58    GGML_ASSERT(n_embd_head == n_rot);
59
60    ggml_tensor * cur;
61    ggml_tensor * inpL;
62
63    inpL = build_inp_embd(model.tok_embd);
64
65    // inp_pos - contains the positions
66    ggml_tensor * inp_pos = build_inp_pos();
67
68    auto * inp_attn = build_attn_inp_no_cache();
69
70    ggml_tensor * inp_out_ids = build_inp_out_ids();
71
72    for (int il = 0; il < n_layer; ++il) {
73        ggml_tensor * inpSA = inpL;
74
75        // norm
76        cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
77        cb(cur, "attn_norm", il);
78
79        // self-attention
80        {
81            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,
82                    n_embd_head, n_head, n_head_kv, il);
83
84            Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
85                                 ext_factor, attn_factor, beta_fast, beta_slow);
86
87            Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
88                                 ext_factor, attn_factor, beta_fast, beta_slow);
89
90            cb(Qcur, "Qcur", il);
91            cb(Kcur, "Kcur", il);
92            cb(Vcur, "Vcur", 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        if (il == n_layer - 1 && inp_out_ids) {
99            cur   = ggml_get_rows(ctx0, cur, inp_out_ids);
100            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
101        }
102        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
103        cb(ffn_inp, "ffn_inp", il);
104
105        // feed-forward network
106        cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
107        cb(cur, "ffn_norm", il);
108
109        cur = build_ffn(cur,
110            model.layers[il].ffn_up, NULL, NULL,
111            model.layers[il].ffn_gate, NULL, NULL,
112            model.layers[il].ffn_down, NULL, NULL,
113            NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
114        cb(cur, "ffn_out", il);
115
116        cur = ggml_add(ctx0, cur, ffn_inp);
117
118        cur = build_cvec(cur, il);
119        cb(cur, "l_out", il);
120
121        // input for next layer
122        inpL = cur;
123    }
124    cur = inpL;
125
126    cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
127
128    cb(cur, "result_norm", -1);
129    res->t_embd = cur;
130
131    // lm_head
132    cur = build_lora_mm(model.output, cur, model.output_s);
133
134    cb(cur, "result_output", -1);
135    res->t_logits = cur;
136
137    ggml_build_forward_expand(gf, cur);
138}