CoolFace
Modelpublic

Felipe97/llama-cpp-compiled

sourceHugging Faceupdated 2d agoView on Hugging Face
0likes1.1kdownloads
refact.cpp161 linesDownload Raw Back to models
1#include "models.h"2 3void llama_model_refact::load_arch_hparams(llama_model_loader & ml) {4    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);5 6    switch (hparams.n_layer()) {7        case 32: type = LLM_TYPE_1B; break;8        default: type = LLM_TYPE_UNKNOWN;9    }10 11    // TODO: become GGUF KV parameter12    hparams.f_max_alibi_bias = 8.0f;13}14 15void llama_model_refact::load_arch_tensors(llama_model_loader &) {16    LLAMA_LOAD_LOCALS;17 18    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);19 20    // output21    output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);22    output      = create_tensor(tn(LLM_TENSOR_OUTPUT,      "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);23 24    // if output is NULL, init from the input tok embed25    if (output == NULL) {26        output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);27    }28 29    for (int i = 0; i < n_layer; ++i) {30        auto & layer = layers[i];31 32        layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);33 34        create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head, n_embd_k_gqa, n_embd_v_gqa, 0);35        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);36 37        // optional bias tensors38        layer.wo_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);39 40        layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);41 42        if (hparams.rope_scaling_type_train == LLAMA_ROPE_SCALING_TYPE_LONGROPE) {43            layer.rope_long  = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_LONG,  "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));44            layer.rope_short = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_SHORT, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));45        }46        else {47            layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));48        }49 50        if (n_expert == 0) {51            layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd,   n_ff}, 0);52            layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {  n_ff, n_embd}, 0);53            layer.ffn_up   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd,   n_ff}, 0);54 55            // optional MLP bias56            layer.ffn_gate_b = create_tensor(tn(LLM_TENSOR_FFN_GATE, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);57            layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);58            layer.ffn_up_b   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);59        } else {60            layer.ffn_gate_inp  = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP,  "weight", i), {n_embd, n_expert}, 0);61            layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd,   n_ff, n_expert}, TENSOR_NOT_REQUIRED);62            layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {  n_ff, n_embd, n_expert}, 0);63            layer.ffn_up_exps   = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS,   "weight", i), {n_embd,   n_ff, n_expert}, 0);64 65            // For Granite MoE Shared66            if (hparams.n_ff_shexp > 0) {67                layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, hparams.n_ff_shexp}, 0);68                layer.ffn_up_shexp   = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP,   "weight", i), {n_embd, hparams.n_ff_shexp}, 0);69                layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {hparams.n_ff_shexp, n_embd}, 0);70            }71        }72    }73}74 75std::unique_ptr<llm_graph_context> llama_model_refact::build_arch_graph(const llm_graph_params & params) const {76    return std::make_unique<graph>(*this, params);77}78 79llama_model_refact::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {80    const int64_t n_embd_head = hparams.n_embd_head_v();81 82    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());83 84    ggml_tensor * cur;85    ggml_tensor * inpL;86 87    inpL = build_inp_embd(model.tok_embd);88 89    auto * inp_attn = build_attn_inp_kv();90 91    ggml_tensor * inp_out_ids = build_inp_out_ids();92 93    for (int il = 0; il < n_layer; ++il) {94        ggml_tensor * inpSA = inpL;95 96        cur = build_norm(inpL,97                model.layers[il].attn_norm, NULL,98                LLM_NORM_RMS, il);99        cb(cur, "attn_norm", il);100 101        // self-attention102        {103            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,104                    n_embd_head, n_head, n_head_kv, il);105 106            cb(Qcur, "Qcur", il);107            cb(Kcur, "Kcur", il);108            cb(Vcur, "Vcur", il);109 110            cur = build_attn(inp_attn,111                    model.layers[il].wo, NULL, model.layers[il].wo_s,112                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);113        }114        if (il == n_layer - 1 && inp_out_ids) {115            cur   = ggml_get_rows(ctx0,   cur, inp_out_ids);116            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);117        }118        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);119        cb(ffn_inp, "ffn_inp", il);120 121        // feed-forward network122        {123            cur = build_norm(ffn_inp,124                    model.layers[il].ffn_norm, NULL,125                    LLM_NORM_RMS, il);126            cb(cur, "ffn_norm", il);127 128            cur = build_ffn(cur,129                    model.layers[il].ffn_up,   NULL, NULL,130                    model.layers[il].ffn_gate, NULL, NULL,131                    model.layers[il].ffn_down, NULL, NULL,132                    NULL,133                    LLM_FFN_SILU, LLM_FFN_PAR, il);134            cb(cur, "ffn_out", il);135        }136        cur = ggml_add(ctx0, cur, ffn_inp);137 138        cur = build_cvec(cur, il);139        cb(cur, "l_out", il);140 141        // input for next layer142        inpL = cur;143    }144    cur = inpL;145 146    cur = build_norm(cur,147            model.output_norm, NULL,148            LLM_NORM_RMS, -1);149 150    cb(cur, "result_norm", -1);151    res->t_embd = cur;152 153    // lm_head154    cur = build_lora_mm(model.output, cur, model.output_s);155 156    cb(cur, "result_output", -1);157    res->t_logits = cur;158 159    ggml_build_forward_expand(gf, cur);160}161