CoolFace
Modelpublic

Felipe97/llama-cpp-compiled

sourceHugging Faceupdated 2d agoView on Hugging Face
0likes1.1kdownloads
dbrx.cpp155 linesDownload Raw Back to models
1#include "models.h"2 3void llama_model_dbrx::load_arch_hparams(llama_model_loader & ml) {4    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);5    ml.get_key(LLM_KV_ATTENTION_CLAMP_KQV,     hparams.f_clamp_kqv);6 7    switch (hparams.n_layer()) {8        case 40: type = LLM_TYPE_16x12B; break;9        default: type = LLM_TYPE_UNKNOWN;10    }11}12 13void llama_model_dbrx::load_arch_tensors(llama_model_loader &) {14    LLAMA_LOAD_LOCALS;15 16    if (n_expert == 0) {17        throw std::runtime_error("DBRX model cannot have zero experts");18    }19 20    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);21 22    // output23    output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);24    output      = create_tensor(tn(LLM_TENSOR_OUTPUT,      "weight"), {n_embd, n_vocab}, 0);25 26    for (int i = 0; i < n_layer; ++i) {27        auto & layer = layers[i];28 29        layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);30 31        layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);32        layer.wo   = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);33 34        layer.attn_out_norm = create_tensor(tn(LLM_TENSOR_ATTN_OUT_NORM, "weight", i), {n_embd}, 0);35 36        layer.ffn_gate_inp  = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP,  "weight", i), {n_embd, n_expert}, 0);37        layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff,   n_expert}, 0);38        layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff,   n_embd, n_expert}, 0);39        layer.ffn_up_exps   = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS,   "weight", i), {n_embd, n_ff,   n_expert}, 0);40    }41}42 43std::unique_ptr<llm_graph_context> llama_model_dbrx::build_arch_graph(const llm_graph_params & params) const {44    return std::make_unique<graph>(*this, params);45}46 47llama_model_dbrx::graph::graph(const llama_model & model, const llm_graph_params & params) : 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    GGML_ASSERT(n_embd_head == n_rot);52 53    ggml_tensor * cur;54    ggml_tensor * inpL;55 56    inpL = build_inp_embd(model.tok_embd);57 58    // inp_pos - contains the positions59    ggml_tensor * inp_pos = build_inp_pos();60 61    auto * inp_attn = build_attn_inp_kv();62 63    ggml_tensor * inp_out_ids = build_inp_out_ids();64 65    for (int il = 0; il < n_layer; ++il) {66        ggml_tensor * inpSA = inpL;67 68        // norm69        cur = build_norm(inpL,70                model.layers[il].attn_norm, NULL,71                LLM_NORM, il);72        cb(cur, "attn_norm", il);73 74        // self-attention75        {76            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,77                    n_embd_head, n_head, n_head_kv, il);78 79            Qcur = ggml_rope_ext(80                    ctx0, Qcur, inp_pos, nullptr,81                    n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,82                    ext_factor, attn_factor, beta_fast, beta_slow83                    );84 85            Kcur = ggml_rope_ext(86                    ctx0, Kcur, inp_pos, nullptr,87                    n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,88                    ext_factor, attn_factor, beta_fast, beta_slow89                    );90 91            cb(Qcur, "Qcur", il);92            cb(Kcur, "Kcur", il);93            cb(Vcur, "Vcur", il);94 95            cur = build_attn(inp_attn,96                    model.layers[il].wo, NULL, model.layers[il].wo_s,97                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);98        }99 100        if (il == n_layer - 1 && inp_out_ids) {101            cur   = ggml_get_rows(ctx0,   cur, inp_out_ids);102            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);103        }104 105        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);106        cb(ffn_inp, "ffn_inp", il);107 108        // feed-forward network109        // MoE branch110        cur = build_norm(ffn_inp,111                model.layers[il].attn_out_norm, NULL,112                LLM_NORM, il);113        cb(cur, "attn_out_norm", il);114 115        cur = build_moe_ffn(cur,116                model.layers[il].ffn_gate_inp,117                model.layers[il].ffn_up_exps,118                model.layers[il].ffn_gate_exps,119                model.layers[il].ffn_down_exps,120                nullptr,121                n_expert, n_expert_used,122                LLM_FFN_SILU, true,123                hparams.expert_weights_scale,124                LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,125                il);126        cb(cur, "ffn_moe_out", il);127 128        cur = ggml_add(ctx0, cur, ffn_inp);129        cb(cur, "ffn_out", il);130 131        cur = build_cvec(cur, il);132        cb(cur, "l_out", il);133 134        // input for next layer135        inpL = cur;136    }137 138    cur = inpL;139 140    cur = build_norm(cur,141            model.output_norm, NULL,142            LLM_NORM, -1);143 144    cb(cur, "result_norm", -1);145    res->t_embd = cur;146 147    // lm_head148    cur = build_lora_mm(model.output, cur, model.output_s);149 150    cb(cur, "result_output", -1);151    res->t_logits = cur;152 153    ggml_build_forward_expand(gf, cur);154}155