char-fork/llama.cpp

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

ynankanimodel : NvFP4 quantized LM head support (#23046)42928bc14

char/aliceai-t5-moe
7.1 KiB189 linesraw
1#include "models.h"
2
3void llama_model_hunyuan_vl::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_or_arr(LLM_KV_ROPE_DIMENSION_SECTIONS, hparams.rope_sections, 4, false);
6
7    // XDRoPE / NTK-aware scaling: base = rope_theta * alpha^(dim / (dim - 2))
8    if (hparams.rope_scaling_alpha > 0.0f) {
9        const int dim = hparams.n_embd_head_k();
10        hparams.rope_freq_base_train = hparams.rope_freq_base_train
11            * powf(hparams.rope_scaling_alpha, (float)dim / (float)(dim - 2));
12    }
13
14    switch (hparams.n_embd) {
15        case 1024: type = LLM_TYPE_0_5B; break;
16        case 2048: type = LLM_TYPE_1_8B; break;
17        case 3072: type = LLM_TYPE_4B; break;
18        case 4096: type = LLM_TYPE_7B; break;
19        default: type = LLM_TYPE_UNKNOWN;
20    }
21}
22
23void llama_model_hunyuan_vl::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    output      = create_tensor(tn(LLM_TENSOR_OUTPUT,      "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
31    // if output is NULL, init from the input tok embed
32    if (output == NULL) {
33        output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
34    }
35
36    for (int i = 0; i < n_layer; ++i) {
37        auto & layer = layers[i];
38
39        layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
40
41        create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head, n_embd_k_gqa, n_embd_v_gqa, 0);
42        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
43
44        layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
45        layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
46
47        layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
48
49        layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd,   n_ff}, 0);
50        layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {  n_ff, n_embd}, 0);
51        layer.ffn_up   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd,   n_ff}, 0);
52
53    }
54}
55
56std::unique_ptr<llm_graph_context> llama_model_hunyuan_vl::build_arch_graph(const llm_graph_params & params) const {
57    return std::make_unique<graph>(*this, params);
58}
59
60llama_model_hunyuan_vl::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
61    const int64_t n_embd_head = hparams.n_embd_head_v();
62
63    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());
64    GGML_ASSERT(n_embd_head == n_rot);
65
66    const bool use_mrope = hparams.use_mrope();
67
68    int sections[4];
69    std::copy(std::begin(hparams.rope_sections), std::begin(hparams.rope_sections) + 4, sections);
70
71    ggml_tensor * cur;
72    ggml_tensor * inpL;
73
74    inpL = build_inp_embd(model.tok_embd);
75
76    // inp_pos - contains the positions
77    ggml_tensor * inp_pos = build_inp_pos();
78
79    auto * inp_attn = build_attn_inp_kv();
80
81    const float kq_scale = 1.0f / sqrtf(float(n_embd_head));
82
83    ggml_tensor * inp_out_ids = build_inp_out_ids();
84
85    for (int il = 0; il < n_layer; ++il) {
86        ggml_tensor * inpSA = inpL;
87
88        // norm
89        cur = build_norm(inpL,
90                model.layers[il].attn_norm, NULL,
91                LLM_NORM_RMS, il);
92        cb(cur, "attn_norm", il);
93        // self-attention
94        {
95            // rope freq factors for llama3; may return nullptr for llama2 and other models
96            ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
97
98            // compute Q and K and RoPE them
99            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,
100                    n_embd_head, n_head, n_head_kv, il);
101
102            if (use_mrope) {
103                Qcur = ggml_rope_multi(
104                            ctx0, Qcur, inp_pos, rope_factors,
105                            n_rot, sections, rope_type, n_ctx_orig, freq_base, freq_scale,
106                            ext_factor, attn_factor, beta_fast, beta_slow
107                            );
108
109                Kcur = ggml_rope_multi(
110                            ctx0, Kcur, inp_pos, rope_factors,
111                            n_rot, sections, rope_type, n_ctx_orig, freq_base, freq_scale,
112                            ext_factor, attn_factor, beta_fast, beta_slow
113                            );
114            } else {
115                Qcur = ggml_rope_ext(
116                            ctx0, Qcur, inp_pos, rope_factors,
117                            n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
118                            ext_factor, attn_factor, beta_fast, beta_slow
119                            );
120
121                Kcur = ggml_rope_ext(
122                            ctx0, Kcur, inp_pos, rope_factors,
123                            n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
124                            ext_factor, attn_factor, beta_fast, beta_slow
125                            );
126            }
127
128            cb(Qcur, "Qcur", il);
129            cb(Kcur, "Kcur", il);
130            cb(Vcur, "Vcur", il);
131
132            Kcur = build_norm(Kcur,
133                        model.layers[il].attn_k_norm, nullptr,
134                        LLM_NORM_RMS, il);
135            cb(Kcur, "Kcur_norm", il);
136
137            Qcur = build_norm(Qcur,
138                        model.layers[il].attn_q_norm, nullptr,
139                        LLM_NORM_RMS, il);
140            cb(Qcur, "Qcur_norm", il);
141
142            cur = build_attn(inp_attn,
143                    model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
144                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
145            cb(cur, "attn_out", il);
146        }
147        if (il == n_layer - 1 && inp_out_ids) {
148            cur   = ggml_get_rows(ctx0,   cur, inp_out_ids);
149            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
150        }
151        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
152        cb(ffn_inp, "ffn_inp", il);
153
154        cur = build_norm(ffn_inp,
155                model.layers[il].ffn_norm, NULL,
156                LLM_NORM_RMS, il);
157        cb(cur, "ffn_norm", il);
158        // feed-forward network (non-MoE)
159        ggml_tensor * cur_mlp = build_ffn(cur,
160                    model.layers[il].ffn_up,   NULL, NULL,
161                    model.layers[il].ffn_gate, NULL, NULL,
162                    model.layers[il].ffn_down, NULL, NULL,
163                    NULL,
164                    LLM_FFN_SILU, LLM_FFN_PAR, il);
165        cb(cur_mlp, "ffn_out", il);
166
167        cur = ggml_add(ctx0, cur_mlp, ffn_inp);
168
169        cur = build_cvec(cur, il);
170        cb(cur, "l_out", il);
171
172        // input for next layer
173        inpL = cur;
174    }
175    cur = inpL;
176
177    cur = build_norm(cur,
178            model.output_norm, NULL,
179            LLM_NORM_RMS, -1);
180
181    cb(cur, "result_norm", -1);
182    res->t_embd = cur;
183    // lm_head
184    cur = build_lora_mm(model.output, cur, model.output_s);
185    cb(cur, "result_output", -1);
186    res->t_logits = cur;
187
188    ggml_build_forward_expand(gf, cur);
189}