CoolFace
Modelpublic

Felipe97/llama-cpp-compiled

sourceHugging Faceupdated 2d agoView on Hugging Face
0likes1.1kdownloads
starcoder.cpp146 linesDownload Raw Back to models
1#include "models.h"2 3void llama_model_starcoder::load_arch_hparams(llama_model_loader & ml) {4    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);5 6    switch (hparams.n_layer()) {7        case 24: type = LLM_TYPE_1B; break;8        case 36: type = LLM_TYPE_3B; break;9        case 42: type = LLM_TYPE_7B; break;10        case 40: type = LLM_TYPE_15B; break;11        default: type = LLM_TYPE_UNKNOWN;12    }13}14 15void llama_model_starcoder::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    pos_embd = create_tensor(tn(LLM_TENSOR_POS_EMBD,   "weight"), {n_embd, n_ctx_train}, 0);20 21    // output22    {23        output_norm   = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);24        output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"),   {n_embd}, 0);25        output        = create_tensor(tn(LLM_TENSOR_OUTPUT,      "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);26        if (!output) {27            // needs to be on GPU28            output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);29        }30 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_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i),   {n_embd}, 0);38 39        layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);40        layer.wqkv_b = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, 0);41 42        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);43        layer.wo_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);44 45        layer.ffn_norm   = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);46        layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i),   {n_embd}, 0);47 48        layer.ffn_down   = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);49        layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i),   {n_embd}, 0);50 51        layer.ffn_up   = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i),   {n_embd, n_ff}, 0);52        layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i),     {n_ff}, 0);53    }54}55 56std::unique_ptr<llm_graph_context> llama_model_starcoder::build_arch_graph(const llm_graph_params & params) const {57    return std::make_unique<graph>(*this, params);58}59 60llama_model_starcoder::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 65    ggml_tensor * cur;66    ggml_tensor * inpL;67 68    inpL = build_inp_embd(model.tok_embd);69 70    // inp_pos - contains the positions71    ggml_tensor * inp_pos = build_inp_pos();72 73    auto * inp_attn = build_attn_inp_kv();74 75    ggml_tensor * pos = ggml_get_rows(ctx0, model.pos_embd, inp_pos);76    cb(pos, "pos_embd", -1);77 78    inpL = ggml_add(ctx0, inpL, pos);79    cb(inpL, "inpL", -1);80 81    ggml_tensor * inp_out_ids = build_inp_out_ids();82 83    for (int il = 0; il < n_layer; ++il) {84        cur = build_norm(inpL,85                model.layers[il].attn_norm,86                model.layers[il].attn_norm_b,87                LLM_NORM, il);88        cb(cur, "attn_norm", il);89 90        // self-attention91        {92            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,93                    n_embd_head, n_head, n_head_kv, il);94 95            cur = build_attn(inp_attn,96                    model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,97                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);98        }99        if (il == n_layer - 1 && inp_out_ids) {100            cur  = ggml_get_rows(ctx0,  cur, inp_out_ids);101            inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);102        }103        // add the input104        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);105        cb(ffn_inp, "ffn_inp", il);106 107        // FF108        {109            cur = build_norm(ffn_inp,110                    model.layers[il].ffn_norm,111                    model.layers[il].ffn_norm_b,112                    LLM_NORM, il);113            cb(cur, "ffn_norm", il);114 115            cur = build_ffn(cur,116                    model.layers[il].ffn_up,   model.layers[il].ffn_up_b,   NULL,117                    NULL,                      NULL,                        NULL,118                    model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,119                    NULL,120                    LLM_FFN_GELU, LLM_FFN_SEQ, il);121            cb(cur, "ffn_out", il);122        }123        cur = ggml_add(ctx0, cur, ffn_inp);124 125        cur = build_cvec(cur, il);126        cb(cur, "l_out", il);127 128        // input for next layer129        inpL = cur;130    }131    cur = build_norm(inpL,132            model.output_norm,133            model.output_norm_b,134            LLM_NORM, -1);135 136    cb(cur, "result_norm", -1);137    res->t_embd = cur;138 139    cur = build_lora_mm(model.output, cur, model.output_s);140 141    cb(cur, "result_output", -1);142    res->t_logits = cur;143 144    ggml_build_forward_expand(gf, cur);145}146