char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Michael WandAdd arch support for cohere2-MoE (#24260)4988f6e86

char/aliceai-t5-moe
5.7 KiB161 linesraw
1#include "models.h"
2
3void llama_model_cohere2::load_arch_hparams(llama_model_loader & ml) {
4    hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
5    uint32_t swa_period = 4;
6    ml.get_key_or_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, swa_period, false);
7    hparams.set_swa_pattern(swa_period);
8
9    hparams.rope_freq_base_train_swa  = hparams.rope_freq_base_train;
10    hparams.rope_freq_scale_train_swa = hparams.rope_freq_scale_train;
11
12    ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA,       hparams.rope_freq_base_train_swa, false);
13    ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa);
14    ml.get_key(LLM_KV_LOGIT_SCALE,              hparams.f_logit_scale);
15    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS,  hparams.f_norm_eps);
16
17    switch (hparams.n_layer()) {
18        case 32: type = LLM_TYPE_8B; break;
19        default: type = LLM_TYPE_UNKNOWN;
20    }
21}
22
23void llama_model_cohere2::load_arch_tensors(llama_model_loader &) {
24    LLAMA_LOAD_LOCALS;
25
26    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0);
27
28    // output
29    output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0);
30    // init output from the input tok embed
31    output      = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab },
32                                      TENSOR_DUPLICATED);
33
34    for (int i = 0; i < n_layer; ++i) {
35        auto & layer = layers[i];
36
37        layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, 0);
38
39        create_tensor_qkv(layer, i, n_embd, n_embd, n_embd_gqa, n_embd_gqa, 0);
40        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd, 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_cohere2::build_arch_graph(const llm_graph_params & params) const {
49    return std::make_unique<graph>(*this, params);
50}
51
52llama_model_cohere2::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
53    const int64_t n_embd_head = hparams.n_embd_head_v();
54
55    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());
56
57    const float f_logit_scale = hparams.f_logit_scale;
58
59    ggml_tensor * cur;
60    ggml_tensor * inpL;
61
62    inpL = build_inp_embd(model.tok_embd);
63
64    // inp_pos - contains the positions
65    ggml_tensor * inp_pos = build_inp_pos();
66
67    auto * inp_attn = build_attn_inp_kv_iswa();
68
69    ggml_tensor * inp_out_ids = build_inp_out_ids();
70
71    for (int il = 0; il < n_layer; ++il) {
72        const bool is_swa = hparams.is_swa(il);
73        // UNUSED:
74        // const float freq_base_l  = model.get_rope_freq_base (cparams, il);
75        // const float freq_scale_l = model.get_rope_freq_scale(cparams, il);
76
77        // norm
78        cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM, il);
79        cb(cur, "attn_norm", il);
80        ggml_tensor * ffn_inp = cur;
81
82        // self-attention
83        {
84            // rope freq factors for 128k context
85            ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
86
87            // compute Q and K and RoPE them
88            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,
89                    n_embd_head, n_head, n_head_kv, il);
90
91            if (is_swa) {
92                Qcur = ggml_rope_ext(
93                        ctx0, Qcur, inp_pos, rope_factors,
94                        n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
95                        ext_factor, attn_factor, beta_fast, beta_slow
96                        );
97
98                Kcur = ggml_rope_ext(
99                        ctx0, Kcur, inp_pos, rope_factors,
100                        n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
101                        ext_factor, attn_factor, beta_fast, beta_slow
102                        );
103            }
104
105            cb(Qcur, "Qcur", il);
106            cb(Kcur, "Kcur", il);
107            cb(Vcur, "Vcur", il);
108
109            cur = build_attn(inp_attn,
110                    model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
111                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
112        }
113
114        if (il == n_layer - 1 && inp_out_ids) {
115            cur     = ggml_get_rows(ctx0, cur, inp_out_ids);
116            inpL    = ggml_get_rows(ctx0, inpL, inp_out_ids);
117            ffn_inp = ggml_get_rows(ctx0, ffn_inp, inp_out_ids);
118        }
119
120        ggml_tensor * attn_out = cur;
121
122        // feed-forward network
123        {
124            cur = build_ffn(ffn_inp,
125                    model.layers[il].ffn_up, NULL, model.layers[il].ffn_up_s,
126                    model.layers[il].ffn_gate, NULL, model.layers[il].ffn_gate_s,
127                    model.layers[il].ffn_down, NULL, model.layers[il].ffn_down_s,
128                    NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
129            cb(cur, "ffn_out", il);
130        }
131
132        // add together residual + FFN + self-attention
133        cur = ggml_add(ctx0, cur, inpL);
134        cur = ggml_add(ctx0, cur, attn_out);
135
136        cur = build_cvec(cur, il);
137        cb(cur, "l_out", il);
138
139        // input for next layer
140        inpL = cur;
141    }
142
143    cur = inpL;
144
145    cur = build_norm(cur, model.output_norm, NULL, LLM_NORM, -1);
146
147    cb(cur, "result_norm", -1);
148    res->t_embd = cur;
149
150    // lm_head
151    cur = build_lora_mm(model.output, cur, model.output_s);
152
153    if (f_logit_scale) {
154        cur = ggml_scale(ctx0, cur, f_logit_scale);
155    }
156
157    cb(cur, "result_output", -1);
158    res->t_logits = cur;
159
160    ggml_build_forward_expand(gf, cur);
161}