Team Ai
Datasetpublic

Brunobkr/llama.cpp_AlgMor24_github

ΩFFFΣLLIa • llama.cpp • AlgMor24 ██████╗ ███████╗███████╗███████╗██╗ ██╗ ██╗ █████╗ ██╔═══██╗██╔════╝██╔════╝██╔════╝██║ ██║ ██║██╔══██╗ ██║ ██║█████╗ █████╗ █████╗ ██║ ██║ ██║███████║ ██║ ██║██╔══╝ ██╔══╝ ██╔══╝ ██║ ██║ ██║██╔══██║ ╚██████╔╝██║ ██║ ███████╗███████╗███████╗██║██║ ██║ ╚═════╝ ╚═╝ ╚═╝ ╚══════╝╚══════╝╚══════╝╚═╝╚═╝ ╚═╝ High-Performance LLM / VLM Inference & Autonomous Agentic Ecosystem… See the full description on the dataset page: https://huggingface.co/datasets/Brunobkr/llama.cpp_AlgMor24_github.

sourceHugging Faceupdated 2mo agoView on Hugging Face
0likes3.1kdownloads
bert.cpp234 linesDownload Raw Back to models
1#include "models.h"2 3void llama_model_bert::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 3:8            type = LLM_TYPE_17M; break; // bge-micro9        case 6:10            type = LLM_TYPE_22M; break; // MiniLM-L611        case 12:12            switch (hparams.n_embd) {13                case 384: type = LLM_TYPE_33M; break; // MiniLM-L12, bge-small14                case 768: type = LLM_TYPE_109M; break; // bge-base15                default: type = LLM_TYPE_UNKNOWN;16            } break;17        case 24:18            type = LLM_TYPE_335M; break; // bge-large19        default: type = LLM_TYPE_UNKNOWN;20    }21}22 23void llama_model_bert::load_arch_tensors(llama_model_loader &) {24    LLAMA_LOAD_LOCALS;25 26    if (n_token_types == 0) {27        throw std::runtime_error(arch_name() + " model needs to define token type count");28    }29    tok_embd     = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD,  "weight"), {n_embd, n_vocab}, 0);30    type_embd    = create_tensor(tn(LLM_TENSOR_TOKEN_TYPES, "weight"), {n_embd, n_token_types}, TENSOR_NOT_REQUIRED);31 32    if (arch == LLM_ARCH_BERT) {33        pos_embd = create_tensor(tn(LLM_TENSOR_POS_EMBD,    "weight"), {n_embd, n_ctx_train}, 0);34 35        cls   = create_tensor(tn(LLM_TENSOR_CLS, "weight"), {n_embd, n_embd}, TENSOR_NOT_REQUIRED);36        cls_b = create_tensor(tn(LLM_TENSOR_CLS, "bias"),   {n_embd},         TENSOR_NOT_REQUIRED);37 38        cls_out   = create_tensor(tn(LLM_TENSOR_CLS_OUT, "weight"), {n_embd, hparams.n_cls_out}, TENSOR_NOT_REQUIRED);39        cls_out_b = create_tensor(tn(LLM_TENSOR_CLS_OUT, "bias"),   {hparams.n_cls_out},         TENSOR_NOT_REQUIRED);40    }41 42    tok_norm   = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight", 0), {n_embd}, 0);43    tok_norm_b = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "bias",   0), {n_embd}, 0);44 45    for (int i = 0; i < n_layer; ++i) {46        auto & layer = layers[i];47 48        create_tensor_qkv(layer, i, n_embd, n_embd, n_embd_gqa, n_embd_gqa, 0);49 50        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);51        layer.wo_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);52 53        layer.attn_out_norm   = create_tensor(tn(LLM_TENSOR_ATTN_OUT_NORM, "weight", i), {n_embd}, 0);54        layer.attn_out_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT_NORM, "bias", i),   {n_embd}, 0);55 56        if (hparams.moe_every_n_layers > 0 && i % hparams.moe_every_n_layers == 1) {57            layer.ffn_up_exps   = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS,   "weight", i), {  n_embd, n_ff,   n_expert}, 0);58            layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {  n_ff,   n_embd, n_expert}, 0);59            layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP,   "weight", i), {n_embd, n_expert}, 0);60        } else {61            layer.ffn_up     = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd, n_ff}, 0);62            layer.ffn_up_b   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "bias", i),   {n_ff}, TENSOR_NOT_REQUIRED);63            layer.ffn_down   = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);64            layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i),   {n_embd}, TENSOR_NOT_REQUIRED);65 66            if (arch == LLM_ARCH_NOMIC_BERT) {67                layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);68            }69        }70 71        layer.layer_out_norm   = create_tensor(tn(LLM_TENSOR_LAYER_OUT_NORM, "weight", i), {n_embd}, 0);72        layer.layer_out_norm_b = create_tensor(tn(LLM_TENSOR_LAYER_OUT_NORM, "bias", i),   {n_embd}, 0);73    }74}75 76std::unique_ptr<llm_graph_context> llama_model_bert::build_arch_graph(const llm_graph_params & params) const {77    return std::make_unique<graph>(*this, params);78}79 80llama_model_bert::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {81    const int64_t n_embd_head = hparams.n_embd_head_v();82 83    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());84 85    ggml_tensor * cur;86    ggml_tensor * inpL;87    ggml_tensor * inp_pos = nullptr;88 89    if (model.arch != LLM_ARCH_JINA_BERT_V2) {90        inp_pos = build_inp_pos();91    }92 93    // construct input embeddings (token, type, position)94    inpL = build_inp_embd(model.tok_embd);95 96    // token types are hardcoded to zero ("Sentence A")97    if (model.type_embd) {98        ggml_tensor * type_row0 = ggml_view_1d(ctx0, model.type_embd, n_embd, 0);99        inpL                    = ggml_add(ctx0, inpL, type_row0);100    }101    if (model.arch == LLM_ARCH_BERT) {102        inpL = ggml_add(ctx0, ggml_get_rows(ctx0, model.pos_embd, inp_pos), inpL);103    }104    cb(inpL, "inp_embd", -1);105 106    // embed layer norm107    inpL = build_norm(inpL, model.tok_norm, model.tok_norm_b, LLM_NORM, 0);108    cb(inpL, "inp_norm", 0);109 110    auto * inp_attn = build_attn_inp_no_cache();111 112    ggml_tensor * inp_out_ids = build_inp_out_ids();113 114    for (int il = 0; il < n_layer; ++il) {115        ggml_tensor * cur = inpL;116 117        {118            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,119                    n_embd_head, n_head, n_head_kv, il);120 121            if (model.layers[il].attn_q_norm) {122                Qcur = ggml_reshape_2d(ctx0, Qcur, n_embd_head * n_head, n_tokens);123 124                Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, model.layers[il].attn_q_norm_b, LLM_NORM, il);125 126                Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);127            }128 129            if (model.layers[il].attn_k_norm) {130                Kcur = ggml_reshape_2d(ctx0, Kcur, n_embd_head * n_head_kv, n_tokens);131 132                Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, model.layers[il].attn_k_norm_b, LLM_NORM, il);133 134                Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);135            }136 137            // RoPE138            if (model.arch == LLM_ARCH_NOMIC_BERT || model.arch == LLM_ARCH_NOMIC_BERT_MOE ||139                model.arch == LLM_ARCH_JINA_BERT_V3) {140                Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,141                                     ext_factor, attn_factor, beta_fast, beta_slow);142 143                Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,144                                     ext_factor, attn_factor, beta_fast, beta_slow);145            }146 147            cb(Qcur, "Qcur", il);148            cb(Kcur, "Kcur", il);149            cb(Vcur, "Vcur", il);150 151            cur = build_attn(inp_attn,152                    model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,153                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);154            cb(cur, "kqv_out", il);155        }156 157        if (il == n_layer - 1 && inp_out_ids) {158            cur  = ggml_get_rows(ctx0, cur, inp_out_ids);159            inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);160        }161 162        // re-add the layer input163        cur = ggml_add(ctx0, cur, inpL);164 165        // attention layer norm166        cur = build_norm(cur, model.layers[il].attn_out_norm, model.layers[il].attn_out_norm_b, LLM_NORM, il);167 168        if (model.layers[il].attn_norm_2 != nullptr) {169            cur = ggml_add(ctx0, cur, inpL);  // re-add the layer input170            cur = build_norm(cur, model.layers[il].attn_norm_2, model.layers[il].attn_norm_2_b, LLM_NORM, il);171        }172 173        ggml_tensor * ffn_inp = cur;174        cb(ffn_inp, "ffn_inp", il);175 176        // feed-forward network177        if (hparams.moe_every_n_layers > 0 && il % hparams.moe_every_n_layers == 1) {178            // MoE branch179            cur = build_moe_ffn(cur,180                    model.layers[il].ffn_gate_inp,181                    model.layers[il].ffn_up_exps,182                    nullptr,183                    model.layers[il].ffn_down_exps,184                    nullptr,185                    hparams.n_expert, hparams.n_expert_used,186                    LLM_FFN_GELU, false,187                    hparams.expert_weights_scale,188                    LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,189                    il);190            cb(cur, "ffn_moe_out", il);191        } else if (model.arch == LLM_ARCH_BERT || model.arch == LLM_ARCH_NOMIC_BERT_MOE ||192                   model.arch == LLM_ARCH_JINA_BERT_V3) {193            cur = build_ffn(cur,194                    model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,195                    NULL, NULL, NULL,196                    model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL, NULL,197                    LLM_FFN_GELU, LLM_FFN_SEQ, il);198            cb(cur, "ffn_out", il);199        } else if (model.arch == LLM_ARCH_JINA_BERT_V2) {200            const bool up_contains_gate = !model.layers[il].ffn_gate && model.layers[il].ffn_up->ne[1] != hparams.n_ff();201            auto type_op = up_contains_gate ? LLM_FFN_GEGLU : LLM_FFN_GELU;202            cur = build_ffn(cur,203                    model.layers[il].ffn_up, model.layers[il].ffn_up_b, NULL,204                    model.layers[il].ffn_gate, NULL, NULL,205                    model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL, NULL,206                    type_op, LLM_FFN_PAR, il);207            cb(cur, "ffn_out", il);208        } else {209            cur = build_ffn(cur,210                model.layers[il].ffn_up, NULL, NULL,211                model.layers[il].ffn_gate, NULL, NULL,212                model.layers[il].ffn_down, NULL, NULL,213                NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);214            cb(cur, "ffn_out", il);215        }216 217        // attentions bypass the intermediate layer218        cur = ggml_add(ctx0, cur, ffn_inp);219 220        // output layer norm221        cur = build_norm(cur, model.layers[il].layer_out_norm, model.layers[il].layer_out_norm_b, LLM_NORM, il);222 223        // input for next layer224        inpL = cur;225    }226 227    cur = inpL;228 229    cb(cur, "result_embd", -1);230    res->t_embd = cur;231 232    ggml_build_forward_expand(gf, cur);233}234 
Brunobkr/llama.cpp_AlgMor24_github · Team Ai