CoolFace
Modelpublic

Felipe97/llama-cpp-compiled

sourceHugging Faceupdated 2d agoView on Hugging Face
0likes1.1kdownloads
modern-bert.cpp173 linesDownload Raw Back to models
1#include "models.h"2 3void llama_model_modern_bert::load_arch_hparams(llama_model_loader & ml) {4    const bool found_swa = ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false);5    if (found_swa && hparams.n_swa > 0) {6        hparams.swa_type = LLAMA_SWA_TYPE_SYMMETRIC;7        ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA, hparams.rope_freq_base_train_swa, false);8        load_swa_pattern(ml, 3, true);9    } else {10        hparams.swa_type = LLAMA_SWA_TYPE_NONE;11    }12 13    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);14 15    // Some ModernBert derivatives (e.g. IBM Granite Embedding 97m R2) use16    // SiLU/SwiGLU in the FFN instead of the default GELU/GeGLU.17    hparams.llm_ffn_op = LLM_FFN_GEGLU;18    std::string hidden_act;19    if (ml.get_key(LLM_KV_HIDDEN_ACT, hidden_act, false)) {20        hparams.llm_ffn_op = llm_ffn_op_type_from_string(hidden_act, LLM_FFN_GEGLU);21    }22 23    switch (hparams.n_layer()) {24        case 12:25            type = LLM_TYPE_47M; break; // granite-embedding-small26        case 22:27            type = LLM_TYPE_149M; break; // modern-bert-base28        case 28:29            type = LLM_TYPE_395M; break; // modern-bert-large30        default: type = LLM_TYPE_UNKNOWN;31    }32}33 34void llama_model_modern_bert::load_arch_tensors(llama_model_loader &) {35    LLAMA_LOAD_LOCALS;36 37    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);38    tok_norm = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight", 0), {n_embd}, 0);39 40    output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);41 42    for(int i = 0; i < n_layer; ++i) {43        auto& layer = layers[i];44 45        if ( i != 0 ) {46            layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);47        } else{48            // layer 0 uses identity49            layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);50        }51 52 53        layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, 3 * n_embd }, 0);54        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT,   "weight", i), {n_embd, n_embd}, 0);55 56        layer.ffn_up   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd, 2 * n_ff}, 0);57        layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);58        layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);59    }60 61    cls_out   = create_tensor(tn(LLM_TENSOR_CLS_OUT,  "weight"), {n_embd, hparams.n_cls_out}, TENSOR_NOT_REQUIRED);62    cls_out_b = create_tensor(tn(LLM_TENSOR_CLS_OUT,  "bias"),   {hparams.n_cls_out},         TENSOR_NOT_REQUIRED);63    cls       = create_tensor(tn(LLM_TENSOR_CLS,      "weight"), {n_embd, n_embd},            TENSOR_NOT_REQUIRED);64    cls_norm  = create_tensor(tn(LLM_TENSOR_CLS_NORM, "weight"), {n_embd},                    TENSOR_NOT_REQUIRED);65 66}67 68std::unique_ptr<llm_graph_context> llama_model_modern_bert::build_arch_graph(const llm_graph_params & params) const {69    return std::make_unique<graph>(*this, params);70}71 72llama_model_modern_bert::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {73    const int64_t n_embd_head = hparams.n_embd_head_v();74 75    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());76 77    ggml_tensor * cur;78    ggml_tensor * inpL;79    ggml_tensor * inp_pos = build_inp_pos();80 81    // construct input embeddings (token, type, position)82    inpL = build_inp_embd(model.tok_embd);83    cb(inpL, "inp_embd", -1);84 85    // embed layer norm86    inpL = build_norm(inpL, model.tok_norm, nullptr, LLM_NORM, 0);87    cb(inpL, "inp_norm", 0);88 89    ggml_tensor * inp_out_ids = build_inp_out_ids();90 91    auto * inp_attn = build_attn_inp_no_cache();92 93    for (int il = 0; il < n_layer; ++il) {94        const float freq_base_l  = model.get_rope_freq_base(cparams, il);95        const float freq_scale_l = model.get_rope_freq_scale(cparams, il);96 97        cur = inpL;98 99        // attention layer norm100        if (model.layers[il].attn_norm) {101            cur = build_norm(inpL,102                    model.layers[il].attn_norm, NULL,103                    LLM_NORM, il);104            cb(cur, "attn_norm", il);105        }106 107        // self attention108        auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,109                n_embd_head, n_head, n_head_kv, il);110 111        // RoPE112        Qcur = ggml_rope_ext(113                ctx0, Qcur, inp_pos, nullptr,114                n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,115                ext_factor, attn_factor, beta_fast, beta_slow116                );117 118        Kcur = ggml_rope_ext(119                ctx0, Kcur, inp_pos, nullptr,120                n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,121                ext_factor, attn_factor, beta_fast, beta_slow122                );123 124        cb(Qcur, "Qcur", il);125        cb(Kcur, "Kcur", il);126        cb(Vcur, "Vcur", il);127 128        cur = build_attn(inp_attn,129                    model.layers[il].wo, nullptr, model.layers[il].wo_s,130                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);131        cb(cur, "kqv_out", il);132 133        if (il == n_layer - 1 && inp_out_ids) {134            cur  = ggml_get_rows(ctx0,  cur, inp_out_ids);135            inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);136        }137 138        // re-add the layer input139        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);140        cb(ffn_inp, "ffn_inp", il);141 142        // attention layer norm143        cur = build_norm(ffn_inp,144                model.layers[il].ffn_norm, NULL,145                LLM_NORM, il);146        cb(cur, "ffn_norm", il);147 148        cur = build_ffn(cur,149                model.layers[il].ffn_up,   NULL, NULL,150                NULL,                      NULL, NULL,151                model.layers[il].ffn_down, NULL, NULL,152                NULL,153                hparams.llm_ffn_op,154                LLM_FFN_SEQ, il);155 156        // attentions bypass the intermediate layer157        cur = ggml_add(ctx0, cur, ffn_inp);158 159        // input for next layer160        inpL = cur;161    }162 163    cur = inpL;164 165    cur = build_norm(cur,166            model.output_norm, NULL,167            LLM_NORM, -1);168    cb(cur, "final_norm_out", -1);169 170    res->t_embd = cur;171    ggml_build_forward_expand(gf, cur);172}173