CoolFace
Modelpublic

Felipe97/llama-cpp-compiled

sourceHugging Faceupdated 2d agoView on Hugging Face
0likes1.1kdownloads
grok.cpp224 linesDownload Raw Back to models
1#include "models.h"2 3void llama_model_grok::load_arch_hparams(llama_model_loader & ml) {4    // defaults for old GGUFs5    hparams.yarn_beta_fast = 8.0f;6    hparams.f_logit_scale = 0.5773502691896257f;7    hparams.f_embedding_scale = 78.38367176906169f;8    hparams.f_attn_out_scale = 0.08838834764831845f;9    hparams.f_attn_logit_softcapping = 30.0f;10    hparams.f_router_logit_softcapping = 30.0f;11    // no final_logit_softcapping in grok-112    hparams.f_final_logit_softcapping = 0.0f;13 14    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS,  hparams.f_norm_rms_eps);15    ml.get_key_or_arr(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp_arr, hparams.n_layer_all, false);16    ml.get_key(LLM_KV_LOGIT_SCALE,                  hparams.f_logit_scale, false);17    ml.get_key(LLM_KV_EMBEDDING_SCALE,              hparams.f_embedding_scale, false);18    ml.get_key(LLM_KV_ATTENTION_OUTPUT_SCALE,       hparams.f_attn_out_scale, false);19    ml.get_key(LLM_KV_ATTN_LOGIT_SOFTCAPPING,       hparams.f_attn_logit_softcapping, false);20    ml.get_key(LLM_KV_ROUTER_LOGIT_SOFTCAPPING,     hparams.f_router_logit_softcapping, false);21    ml.get_key(LLM_KV_FINAL_LOGIT_SOFTCAPPING,      hparams.f_final_logit_softcapping, false);22 23    ml.get_key(LLM_KV_ATTENTION_TEMPERATURE_LENGTH,  hparams.attn_temp_length, false);24    ml.get_key(LLM_KV_ROPE_SCALING_YARN_EXT_FACTOR,  hparams.yarn_ext_factor, false);25    ml.get_key(LLM_KV_ROPE_SCALING_YARN_ATTN_FACTOR, hparams.yarn_attn_factor, false);26    ml.get_key(LLM_KV_ROPE_SCALING_YARN_BETA_FAST,   hparams.yarn_beta_fast, false);27    ml.get_key(LLM_KV_ROPE_SCALING_YARN_BETA_SLOW,   hparams.yarn_beta_slow, false);28 29    switch (hparams.n_layer()) {30        case 64: type = LLM_TYPE_314B; break;31        default: type = LLM_TYPE_UNKNOWN;32    }33}34 35void llama_model_grok::load_arch_tensors(llama_model_loader &) {36    LLAMA_LOAD_LOCALS;37 38    if (n_expert == 0) {39        throw std::runtime_error(arch_name() + " model cannot have zero experts");40    }41 42    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);43 44    // output45    output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);46    output      = create_tensor(tn(LLM_TENSOR_OUTPUT,      "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);47 48    // if output is NULL, init from the input tok embed49    if (output == NULL) {50        output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);51    }52 53    const int64_t n_ff_exp = hparams.n_ff_exp() ? hparams.n_ff_exp() : n_ff/* / n_expert_used*/; // grok-1 n_ff_exp == n_ff54    for (int i = 0; i < n_layer; ++i) {55        auto & layer = layers[i];56 57        layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);58 59        create_tensor_qkv(layer, i, n_embd, n_embd, n_embd_gqa, n_embd_gqa, 0);60        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);61 62        layer.attn_out_norm   = create_tensor(tn(LLM_TENSOR_ATTN_OUT_NORM, "weight", i), {n_embd}, 0);63 64        layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);65 66        layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, TENSOR_NOT_REQUIRED);67        layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff,   n_embd}, TENSOR_NOT_REQUIRED);68        layer.ffn_up   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd, n_ff}, TENSOR_NOT_REQUIRED);69 70        layer.ffn_gate_inp  = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP,  "weight", i), {n_embd, n_expert}, 0);71        layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd,   n_ff_exp, n_expert}, TENSOR_NOT_REQUIRED);72        layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd,   n_expert}, 0);73        layer.ffn_up_exps   = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS,   "weight", i), {n_embd,   n_ff_exp, n_expert}, 0);74 75        layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_LAYER_OUT_NORM, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);76        if (!layer.ffn_post_norm) {77            layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0);78        }79    }80}81 82std::unique_ptr<llm_graph_context> llama_model_grok::build_arch_graph(const llm_graph_params & params) const {83    return std::make_unique<graph>(*this, params);84}85 86llama_model_grok::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {87    const int64_t n_embd_head = hparams.n_embd_head_v();88 89    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());90    GGML_ASSERT(n_embd_head == n_rot);91 92    ggml_tensor * cur;93    ggml_tensor * inpL;94 95    inpL = build_inp_embd(model.tok_embd);96 97    // inp_pos - contains the positions98    ggml_tensor * inp_pos = build_inp_pos();99 100    auto * inp_attn = build_attn_inp_kv();101 102    ggml_tensor * inp_out_ids = build_inp_out_ids();103 104    for (int il = 0; il < n_layer; ++il) {105        ggml_tensor * inpSA = inpL;106 107        // norm108        cur = build_norm(inpL,109                model.layers[il].attn_norm, NULL,110                LLM_NORM_RMS, il);111        cb(cur, "attn_norm", il);112 113        // self-attention114        {115            // compute Q and K and RoPE them116            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,117                    n_embd_head, n_head, n_head_kv, il);118 119            Qcur = ggml_rope_ext(120                    ctx0, Qcur, inp_pos, nullptr,121                    n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,122                    ext_factor, attn_factor, beta_fast, beta_slow123                    );124 125            Kcur = ggml_rope_ext(126                    ctx0, Kcur, inp_pos, nullptr,127                    n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,128                    ext_factor, attn_factor, beta_fast, beta_slow129                    );130 131            cb(Qcur, "Qcur", il);132            cb(Kcur, "Kcur", il);133            cb(Vcur, "Vcur", il);134 135            cur = build_attn(inp_attn,136                    model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,137                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f, il);138        }139        if (il == n_layer - 1 && inp_out_ids) {140            cur   = ggml_get_rows(ctx0,   cur, inp_out_ids);141            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);142        }143        cur = build_norm(cur,144                model.layers[il].attn_out_norm, NULL,145                LLM_NORM_RMS, il);146        cb(cur, "attn_out_norm", il);147 148        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);149        cb(ffn_inp, "ffn_inp", il);150 151        // feed-forward network152        cur = build_norm(ffn_inp,153                model.layers[il].ffn_norm, NULL,154                LLM_NORM_RMS, il);155        cb(cur, "ffn_norm", il);156 157        // MoE branch158        ggml_tensor * moe_out = build_moe_ffn(cur,159                model.layers[il].ffn_gate_inp,160                model.layers[il].ffn_up_exps,161                model.layers[il].ffn_gate_exps,162                model.layers[il].ffn_down_exps,163                nullptr,164                n_expert, n_expert_used,165                LLM_FFN_GELU, true,166                hparams.expert_weights_scale,167                LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,168                il);169        cb(moe_out, "ffn_moe_out", il);170 171        if (model.layers[il].ffn_up) {172            ggml_tensor * ffn_out = build_ffn(cur,173                    model.layers[il].ffn_up,   NULL, NULL,174                    model.layers[il].ffn_gate, NULL, NULL,175                    model.layers[il].ffn_down, NULL, NULL,176                    NULL,177                    LLM_FFN_GELU, LLM_FFN_PAR, il);178            cb(ffn_out, "ffn_out", il);179 180            cur = ggml_scale(ctx0, ggml_add(ctx0, ffn_out, moe_out), std::sqrt(2) / 2);181            cb(cur, "ffn_out", il);182        } else {183            cur = moe_out;184        }185        cur = build_norm(cur,186                model.layers[il].ffn_post_norm, NULL,187                LLM_NORM_RMS, il);188        cb(cur, "ffn_post_norm", il);189 190        cur = ggml_add(ctx0, cur, ffn_inp);191        cb(cur, "ffn_out", il);192 193        cur = build_cvec(cur, il);194        cb(cur, "l_out", il);195 196        // input for next layer197        inpL = cur;198    }199    cur = inpL;200 201    cur = build_norm(cur,202            model.output_norm, NULL,203            LLM_NORM_RMS, -1);204 205    cb(cur, "result_norm", -1);206    res->t_embd = cur;207 208    // lm_head209    cur = build_lora_mm(model.output, cur, model.output_s);210 211    cur = ggml_scale(ctx0, cur, hparams.f_logit_scale);212 213    // final logit soft-capping214    if (hparams.f_final_logit_softcapping) {215        cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_final_logit_softcapping);216        cur = ggml_tanh(ctx0, cur);217        cur = ggml_scale(ctx0, cur, hparams.f_final_logit_softcapping);218    }219    cb(cur, "result_output", -1);220    res->t_logits = cur;221 222    ggml_build_forward_expand(gf, cur);223}224