CoolFace
Modelpublic

Felipe97/llama-cpp-compiled

sourceHugging Faceupdated 2d agoView on Hugging Face
0likes1.1kdownloads
minicpm3.cpp253 linesDownload Raw Back to models
1#include "models.h"2 3void llama_model_minicpm3::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_Q_LORA_RANK,       hparams.n_lora_q);6    ml.get_key(LLM_KV_ATTENTION_KV_LORA_RANK,      hparams.n_lora_kv);7 8    switch (hparams.n_layer()) {9        case 62: type = LLM_TYPE_4B; break;10        default: type = LLM_TYPE_UNKNOWN;11    }12}13 14void llama_model_minicpm3::load_arch_tensors(llama_model_loader &) {15    LLAMA_LOAD_LOCALS;16 17    const int64_t n_embd_head_qk_rope = hparams.n_rot();18    const int64_t n_embd_head_qk_nope = hparams.n_embd_head_k() - hparams.n_rot();19 20    const int64_t q_lora_rank  = hparams.n_lora_q;21    const int64_t kv_lora_rank = hparams.n_lora_kv;22    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);23 24    // output25    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 28    // if output is NULL, init from the input tok embed29    if (output == NULL) {30        output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);31    }32 33    for (int i = 0; i < n_layer; ++i) {34        auto & layer = layers[i];35 36        layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);37        layer.attn_q_a_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_A_NORM, "weight", i), {q_lora_rank}, 0);38 39        layer.attn_kv_a_norm = create_tensor(tn(LLM_TENSOR_ATTN_KV_A_NORM, "weight", i), {kv_lora_rank}, 0);40 41        layer.wq_a = create_tensor(tn(LLM_TENSOR_ATTN_Q_A, "weight", i), {n_embd, q_lora_rank}, 0);42        layer.wq_b = create_tensor(tn(LLM_TENSOR_ATTN_Q_B, "weight", i), {q_lora_rank, n_head * n_embd_head_k}, 0);43 44        layer.wkv_a_mqa = create_tensor(tn(LLM_TENSOR_ATTN_KV_A_MQA, "weight", i), {n_embd, kv_lora_rank + (n_embd_head_qk_rope)}, 0);45        layer.wkv_b     = create_tensor(tn(LLM_TENSOR_ATTN_KV_B,     "weight", i), {kv_lora_rank, n_head * (n_embd_head_qk_nope + n_embd_head_v)}, 0);46        layer.wo        = create_tensor(tn(LLM_TENSOR_ATTN_OUT,      "weight", i), {              n_head * (                      n_embd_head_v), n_embd}, 0);47 48        layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);49 50        layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd,   n_ff}, 0);51        layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {  n_ff, n_embd}, 0);52        layer.ffn_up   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd,   n_ff}, 0);53 54        layer.rope_long  = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_LONG,  "weight", i), { n_embd_head_qk_rope/2 }, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));55        layer.rope_short = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_SHORT, "weight", i), { n_embd_head_qk_rope/2 }, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));56    }57}58 59std::unique_ptr<llm_graph_context> llama_model_minicpm3::build_arch_graph(const llm_graph_params & params) const {60    return std::make_unique<graph>(*this, params);61}62 63llama_model_minicpm3::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {64    //TODO: if the model varies, these parameters need to be read from the model65    const int64_t n_embd_base = 256;66    const float scale_embd  = 12.0f;67    const float scale_depth = 1.4f;68    const float kq_scale = 1.0f / sqrtf(float(hparams.n_embd_head_k()));69 70    const uint32_t n_embd_head_qk_rope = hparams.n_rot();71    const uint32_t n_embd_head_qk_nope = hparams.n_embd_head_k() - hparams.n_rot();72 73    const uint32_t kv_lora_rank = hparams.n_lora_kv;74 75    ggml_tensor * cur;76    ggml_tensor * inpL;77 78    inpL = build_inp_embd(model.tok_embd);79 80    // scale the input embeddings81    inpL = ggml_scale(ctx0, inpL, scale_embd);82    cb(inpL, "inp_scaled", -1);83 84    // inp_pos - contains the positions85    ggml_tensor * inp_pos = build_inp_pos();86 87    auto * inp_attn = build_attn_inp_kv();88 89    ggml_tensor * inp_out_ids = build_inp_out_ids();90 91    for (int il = 0; il < n_layer; ++il) {92        ggml_tensor * inpSA = inpL;93 94        ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);95 96        // norm97        cur = build_norm(inpL,98                model.layers[il].attn_norm, NULL,99                LLM_NORM_RMS, il);100        cb(cur, "attn_norm", il);101 102        // self_attention103        {104            ggml_tensor * q = NULL;105            // {n_embd, q_lora_rank} * {n_embd, n_tokens} -> {q_lora_rank, n_tokens}106            q = ggml_mul_mat(ctx0, model.layers[il].wq_a, cur);107            cb(q, "q", il);108 109            q = build_norm(q,110                    model.layers[il].attn_q_a_norm, NULL,111                    LLM_NORM_RMS, il);112            cb(q, "q", il);113 114            // {q_lora_rank, n_head * hparams.n_embd_head_k()} * {q_lora_rank, n_tokens} -> {n_head * hparams.n_embd_head_k(), n_tokens}115            q = ggml_mul_mat(ctx0, model.layers[il].wq_b, q);116            cb(q, "q", il);117 118            // {n_embd_head_k, n_head, n_tokens}, RoPE is applied to the trailing dims only119            q = ggml_reshape_3d(ctx0, q, hparams.n_embd_head_k(), n_head, n_tokens);120            cb(q, "q", il);121 122            // {n_embd, kv_lora_rank + n_embd_head_qk_rope} * {n_embd, n_tokens} -> {kv_lora_rank + n_embd_head_qk_rope, n_tokens}123            ggml_tensor * kv_pe_compresseed = ggml_mul_mat(ctx0, model.layers[il].wkv_a_mqa, cur);124            cb(kv_pe_compresseed, "kv_pe_compresseed", il);125 126            // split into {kv_lora_rank, n_tokens}127            ggml_tensor * kv_compressed = ggml_view_2d(ctx0, kv_pe_compresseed, kv_lora_rank, n_tokens,128                    kv_pe_compresseed->nb[1],129                    0);130            cb(kv_compressed, "kv_compressed", il);131 132            // and {n_embd_head_qk_rope, n_tokens}133            ggml_tensor * k_pe = ggml_view_3d(ctx0, kv_pe_compresseed, n_embd_head_qk_rope, 1, n_tokens,134                    kv_pe_compresseed->nb[1],135                    kv_pe_compresseed->nb[1],136                    ggml_row_size(kv_pe_compresseed->type, kv_lora_rank));137            cb(k_pe, "k_pe", il);138 139            kv_compressed = build_norm(kv_compressed,140                    model.layers[il].attn_kv_a_norm, NULL,141                    LLM_NORM_RMS, il);142            cb(kv_compressed, "kv_compressed", il);143 144            // {kv_lora_rank, n_head * (n_embd_head_qk_nope + n_embd_head_v)} * {kv_lora_rank, n_tokens} -> {n_head * (n_embd_head_qk_nope + n_embd_head_v), n_tokens}145            ggml_tensor * kv = ggml_mul_mat(ctx0, model.layers[il].wkv_b, kv_compressed);146            cb(kv, "kv", il);147 148            // split into {n_head * n_embd_head_qk_nope, n_tokens}149            ggml_tensor * k_nope = ggml_view_3d(ctx0, kv, n_embd_head_qk_nope, n_head, n_tokens,150                    ggml_row_size(kv->type, n_embd_head_qk_nope + hparams.n_embd_head_v()),151                    ggml_row_size(kv->type, n_head * (n_embd_head_qk_nope + hparams.n_embd_head_v())),152                    0);153            cb(k_nope, "k_nope", il);154 155            // and {n_head * n_embd_head_v, n_tokens}156            ggml_tensor * v_states = ggml_view_3d(ctx0, kv, hparams.n_embd_head_v(), n_head, n_tokens,157                    ggml_row_size(kv->type, (n_embd_head_qk_nope + hparams.n_embd_head_v())),158                    ggml_row_size(kv->type, (n_embd_head_qk_nope + hparams.n_embd_head_v())*n_head),159                    ggml_row_size(kv->type, (n_embd_head_qk_nope)));160            cb(v_states, "v_states", il);161 162            v_states = ggml_cont(ctx0, v_states);163            cb(v_states, "v_states", il);164 165            q = ggml_rope_ext(166                    ctx0, q, inp_pos, rope_factors,167                    n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,168                    ext_factor, attn_factor, beta_fast, beta_slow169                    );170            q = ggml_rope_set_offset(q, n_embd_head_qk_nope);171            cb(q, "q_rope", il);172 173            // shared RoPE key174            k_pe = ggml_rope_ext(175                    ctx0, k_pe, inp_pos, rope_factors,176                    n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,177                    ext_factor, attn_factor, beta_fast, beta_slow178                    );179            cb(k_pe, "k_pe", il);180 181            ggml_tensor * q_states = q;182            cb(q_states, "q_states", il);183 184            ggml_tensor * k_states = ggml_concat(ctx0, k_nope,185                    ggml_repeat_4d(ctx0, k_pe, n_embd_head_qk_rope, n_head, n_tokens, 1), 0);186            cb(k_states, "k_states", il);187 188            cur = build_attn(inp_attn,189                    model.layers[il].wo, NULL, model.layers[il].wo_s,190                    q_states, k_states, v_states, nullptr, nullptr, nullptr, kq_scale, il);191        }192        if (il == n_layer - 1 && inp_out_ids) {193            cur   = ggml_get_rows(ctx0,   cur, inp_out_ids);194            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);195        }196        // scale_res - scale the hidden states for residual connection197        const float scale_res = scale_depth/sqrtf(float(n_layer)); // TODO: is this correct?198        cur = ggml_scale(ctx0, cur, scale_res);199        cb(cur, "hidden_scaled", il);200 201        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);202        cb(ffn_inp, "ffn_inp", il);203 204        // feed-forward network205        {206            cur = build_norm(ffn_inp,207                    model.layers[il].ffn_norm, NULL,208                    LLM_NORM_RMS, il);209            cb(cur, "ffn_norm", il);210 211            cur = build_ffn(cur,212                    model.layers[il].ffn_up,   NULL, NULL,213                    model.layers[il].ffn_gate, NULL, NULL,214                    model.layers[il].ffn_down, NULL, NULL,215                    NULL,216                    LLM_FFN_SILU, LLM_FFN_PAR, il);217            cb(cur, "ffn_out", il);218        }219        // scale the hidden states for residual connection220        cur = ggml_scale(ctx0, cur, scale_res);221        cb(cur, "hidden_scaled_ffn", il);222 223        cur = ggml_add(ctx0, cur, ffn_inp);224 225        cur = build_cvec(cur, il);226        cb(cur, "l_out", il);227 228        // input for next layer229        inpL = cur;230    }231    cur = inpL;232 233    cur = build_norm(cur,234            model.output_norm, NULL,235            LLM_NORM_RMS, -1);236 237    cb(cur, "result_norm", -1);238    res->t_embd = cur;239 240    // lm_head scaling241    const float scale_lmhead = float(n_embd_base)/float(n_embd);242    cur = ggml_scale(ctx0, cur, scale_lmhead);243    cb(cur, "lmhead_scaling", -1);244 245    // lm_head246    cur = build_lora_mm(model.output, cur, model.output_s);247 248    cb(cur, "result_output", -1);249    res->t_logits = cur;250 251    ggml_build_forward_expand(gf, cur);252}253