CoolFace
Modelpublic

Felipe97/llama-cpp-compiled

sourceHugging Faceupdated 2d agoView on Hugging Face
0likes1.1kdownloads
command-r.cpp145 linesDownload Raw Back to models
1#include "models.h"2 3void llama_model_command_r::load_arch_hparams(llama_model_loader & ml) {4    ml.get_key(LLM_KV_LOGIT_SCALE,             hparams.f_logit_scale, false);5    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);6 7    switch (hparams.n_layer()) {8        case 40: type = LLM_TYPE_35B; break;9        default: type = LLM_TYPE_UNKNOWN;10    }11}12 13void llama_model_command_r::load_arch_tensors(llama_model_loader &) {14    LLAMA_LOAD_LOCALS;15 16    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);17 18    // output19    output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);20    // init output from the input tok embed21    output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);22 23    for (int i = 0; i < n_layer; ++i) {24        auto & layer = layers[i];25 26        layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);27 28        if (n_layer >= 64){29            layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k, n_head}, 0);30            layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k, n_head_kv}, 0);31        }32 33        create_tensor_qkv(layer, i, n_embd, n_embd, n_embd_gqa, n_embd_gqa, 0);34        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);35 36        layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd,   n_ff}, 0);37        layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {  n_ff, n_embd}, 0);38        layer.ffn_up   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd,   n_ff}, 0);39    }40}41 42std::unique_ptr<llm_graph_context> llama_model_command_r::build_arch_graph(const llm_graph_params & params) const {43    return std::make_unique<graph>(*this, params);44}45 46llama_model_command_r::graph::graph(const llama_model & model, const llm_graph_params & params) :47    llm_graph_context(params) {48    const int64_t n_embd_head = hparams.n_embd_head_v();49 50    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());51 52    const float f_logit_scale = hparams.f_logit_scale;53 54    ggml_tensor * cur;55    ggml_tensor * inpL;56 57    inpL = build_inp_embd(model.tok_embd);58 59    // inp_pos - contains the positions60    ggml_tensor * inp_pos = build_inp_pos();61 62    auto * inp_attn = build_attn_inp_kv();63 64    ggml_tensor * inp_out_ids = build_inp_out_ids();65 66    for (int il = 0; il < n_layer; ++il) {67        // norm68        cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM, il);69        cb(cur, "attn_norm", il);70 71        ggml_tensor * ffn_inp = cur;72 73        // self-attention74        {75            // compute Q and K and RoPE them76            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,77                    n_embd_head, n_head, n_head_kv, il);78 79            if (model.layers[il].attn_q_norm) {80                Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM, il);81                cb(Qcur, "Qcur", il);82            }83            Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,84                                 ext_factor, attn_factor, beta_fast, beta_slow);85 86            if (model.layers[il].attn_k_norm) {87                Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM, il);88                cb(Kcur, "Kcur", il);89            }90            Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,91                                 ext_factor, attn_factor, beta_fast, beta_slow);92 93            cb(Qcur, "Qcur", il);94            cb(Kcur, "Kcur", il);95            cb(Vcur, "Vcur", il);96 97            cur = build_attn(inp_attn,98                    model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,99                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);100        }101        if (il == n_layer - 1 && inp_out_ids) {102            cur     = ggml_get_rows(ctx0, cur, inp_out_ids);103            inpL    = ggml_get_rows(ctx0, inpL, inp_out_ids);104            ffn_inp = ggml_get_rows(ctx0, ffn_inp, inp_out_ids);105        }106        ggml_tensor * attn_out = cur;107 108        // feed-forward network109        {110            cur = build_ffn(ffn_inp,111                    model.layers[il].ffn_up, NULL, NULL,112                    model.layers[il].ffn_gate, NULL, NULL,113                    model.layers[il].ffn_down, NULL, NULL,114                    NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);115            cb(cur, "ffn_out", il);116        }117        // add together residual + FFN + self-attention118        cur = ggml_add(ctx0, cur, inpL);119        cur = ggml_add(ctx0, cur, attn_out);120 121        cur = build_cvec(cur, il);122        cb(cur, "l_out", il);123 124        // input for next layer125        inpL = cur;126    }127    cur = inpL;128 129    cur = build_norm(cur, model.output_norm, NULL, LLM_NORM, -1);130 131    cb(cur, "result_norm", -1);132    res->t_embd = cur;133 134    // lm_head135    cur = build_lora_mm(model.output, cur, model.output_s);136 137    if (f_logit_scale) {138        cur = ggml_scale(ctx0, cur, f_logit_scale);139    }140    cb(cur, "result_output", -1);141    res->t_logits = cur;142 143    ggml_build_forward_expand(gf, cur);144}145