char-fork/llama.cpp

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

KnightYao[Model] Support for Spark2_5ForCausalLM implementation (#27868)3ad1ba733

char/aliceai-t5-moe
6.1 KiB146 linesraw
1#include "models.h"
2
3void llama_model_spark2_5::load_arch_hparams(llama_model_loader & ml) {
4    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
5    ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa);
6
7    hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
8    ml.get_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, hparams.is_swa_impl);
9
10    hparams.rope_freq_base_train_swa = hparams.rope_freq_base_train;
11    hparams.rope_freq_scale_train_swa = hparams.rope_freq_scale_train;
12    ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA, hparams.rope_freq_base_train_swa, false);
13
14    switch (hparams.n_layer()) {
15        case 28: type = LLM_TYPE_1_7B; break;
16        default: type = LLM_TYPE_UNKNOWN;
17    }
18}
19
20void llama_model_spark2_5::load_arch_tensors(llama_model_loader &) {
21    LLAMA_LOAD_LOCALS;
22
23    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
24
25    output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
26    output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
27    if (output == nullptr) {
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        const int64_t n_head_i = hparams.n_head(i);
35        const int64_t n_head_kv_i = hparams.n_head_kv(i);
36        const int64_t n_embd_q = hparams.n_embd_head_k(i) * n_head_i;
37        const int64_t n_embd_k = hparams.n_embd_head_k(i) * n_head_kv_i;
38        const int64_t n_embd_v = hparams.n_embd_head_v(i) * n_head_kv_i;
39
40        layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
41        create_tensor_qkv(layer, i, n_embd, n_embd_q, n_embd_k, n_embd_v, 0);
42        layer.wqkv_gate = create_tensor(tn(LLM_TENSOR_ATTN_GATE, "weight", i), {n_embd, n_head_i}, 0);
43        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_q, n_embd}, 0);
44
45        layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
46        layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
47        layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);
48        layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
49    }
50}
51
52std::unique_ptr<llm_graph_context> llama_model_spark2_5::build_arch_graph(const llm_graph_params & params) const {
53    return std::make_unique<graph>(*this, params);
54}
55
56llama_model_spark2_5::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
57    const int64_t n_embd_head = hparams.n_embd_head_v();
58
59    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());
60    GGML_ASSERT(hparams.swa_type == LLAMA_SWA_TYPE_STANDARD);
61
62    ggml_tensor * inpL = build_inp_embd(model.tok_embd);
63    ggml_tensor * inp_pos = build_inp_pos();
64    auto * inp_attn = build_attn_inp_kv_iswa();
65    ggml_tensor * inp_out_ids = build_inp_out_ids();
66
67    const float kq_scale = 1.0f / sqrtf(float(n_embd_head));
68
69    for (int il = 0; il < n_layer; ++il) {
70        ggml_tensor * inpSA = inpL;
71        ggml_tensor * cur = build_norm(inpL, model.layers[il].attn_norm, nullptr, LLM_NORM_RMS, il);
72        cb(cur, "attn_norm", il);
73
74        const int64_t n_head_i = hparams.n_head(il);
75        const int64_t n_head_kv_i = hparams.n_head_kv(il);
76        const int64_t n_rot_i = hparams.n_rot(il);
77        const float freq_base_i = model.get_rope_freq_base(cparams, il);
78        const float freq_scale_i = model.get_rope_freq_scale(cparams, il);
79
80        ggml_tensor * attn_inp = cur;
81        auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur, n_embd_head, n_head_i, n_head_kv_i, il);
82
83        Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr,
84                n_rot_i, rope_type, n_ctx_orig, freq_base_i, freq_scale_i,
85                ext_factor, attn_factor, beta_fast, beta_slow);
86        Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr,
87                n_rot_i, rope_type, n_ctx_orig, freq_base_i, freq_scale_i,
88                ext_factor, attn_factor, beta_fast, beta_slow);
89        cb(Qcur, "Qcur_rope", il);
90        cb(Kcur, "Kcur_rope", il);
91
92        cur = build_attn(inp_attn,
93                nullptr, nullptr, nullptr,
94                Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
95        cb(cur, "attn_out", il);
96
97        ggml_tensor * gate = build_lora_mm(model.layers[il].wqkv_gate, attn_inp);
98        gate = ggml_sigmoid(ctx0, gate);
99        cb(gate, "attn_gate", il);
100
101        const int64_t n_tokens_i = cur->ne[1];
102        cur = ggml_reshape_3d(ctx0, cur, n_embd_head, n_head_i, n_tokens_i);
103        gate = ggml_reshape_3d(ctx0, gate, 1, n_head_i, n_tokens_i);
104        cur = ggml_mul(ctx0, cur, gate);
105        cur = ggml_reshape_2d(ctx0, cur, n_embd_head * n_head_i, n_tokens_i);
106        cb(cur, "attn_gated", il);
107
108        cur = build_lora_mm(model.layers[il].wo, cur, model.layers[il].wo_s);
109        cb(cur, "attn_out_proj", il);
110
111        if (il == n_layer - 1 && inp_out_ids) {
112            cur = ggml_get_rows(ctx0, cur, inp_out_ids);
113            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
114        }
115
116        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
117        cb(ffn_inp, "ffn_inp", il);
118
119        cur = build_norm(ffn_inp, model.layers[il].ffn_norm, nullptr, LLM_NORM_RMS, il);
120        cb(cur, "ffn_norm", il);
121
122        cur = build_ffn(cur,
123                model.layers[il].ffn_up, nullptr, nullptr,
124                model.layers[il].ffn_gate, nullptr, nullptr,
125                model.layers[il].ffn_down, nullptr, nullptr,
126                nullptr,
127                LLM_FFN_GELU, LLM_FFN_PAR, il);
128        cb(cur, "ffn_out", il);
129
130        cur = ggml_add(ctx0, cur, ffn_inp);
131        cur = build_cvec(cur, il);
132        cb(cur, "l_out", il);
133
134        inpL = cur;
135    }
136
137    ggml_tensor * cur = build_norm(inpL, model.output_norm, nullptr, LLM_NORM_RMS, -1);
138    cb(cur, "result_norm", -1);
139    res->t_embd = cur;
140
141    cur = build_lora_mm(model.output, cur);
142    cb(cur, "result_output", -1);
143    res->t_logits = cur;
144
145    ggml_build_forward_expand(gf, cur);
146}