CoolFace
Modelpublic

Felipe97/llama-cpp-compiled

sourceHugging Faceupdated 2d agoView on Hugging Face
0likes1.1kdownloads
apertus.cpp171 linesDownload Raw Back to models
1#include "models.h"2 3void llama_model_apertus::load_arch_hparams(llama_model_loader & ml) {4    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);5 6    ml.get_key_or_arr(LLM_KV_XIELU_ALPHA_N, hparams.xielu_alpha_n, hparams.n_layer());7    ml.get_key_or_arr(LLM_KV_XIELU_ALPHA_P, hparams.xielu_alpha_p, hparams.n_layer());8    ml.get_key_or_arr(LLM_KV_XIELU_BETA,    hparams.xielu_beta,    hparams.n_layer());9    ml.get_key_or_arr(LLM_KV_XIELU_EPS,     hparams.xielu_eps,     hparams.n_layer());10 11    switch (hparams.n_layer()) {12        case 32: type = LLM_TYPE_8B; break;13        default: type = LLM_TYPE_UNKNOWN;14    }15}16 17void llama_model_apertus::load_arch_tensors(llama_model_loader &) {18    LLAMA_LOAD_LOCALS;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        if (hparams.rope_scaling_type_train == LLAMA_ROPE_SCALING_TYPE_LONGROPE) {32            layer.rope_long  = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_LONG,  "weight", i), { n_rot/2 }, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));33            layer.rope_short = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_SHORT, "weight", i), { n_rot/2 }, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));34        } else {35            layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), { n_rot/2 }, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));36        }37 38        create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head, n_embd_gqa, n_embd_gqa, 0);39        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd_head_k * n_head, n_embd }, 0);40 41        // optional bias tensors42        layer.wo_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), { n_embd }, TENSOR_NOT_REQUIRED);43 44        layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), { n_embd }, 0);45        layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd }, 0);46        layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), { n_embd, n_ff }, 0);47 48        // Q and K layernorms for Apertus49        layer.attn_q_norm   = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), { n_embd_head_k }, 0);50        layer.attn_q_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "bias",   i), { n_embd_head_k }, TENSOR_NOT_REQUIRED);51        layer.attn_k_norm   = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), { n_embd_head_k }, 0);52        layer.attn_k_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "bias",   i), { n_embd_head_k }, TENSOR_NOT_REQUIRED);53    }54}55 56std::unique_ptr<llm_graph_context> llama_model_apertus::build_arch_graph(const llm_graph_params & params) const {57    return std::make_unique<graph>(*this, params);58}59 60llama_model_apertus::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    GGML_ASSERT(n_embd_head == n_rot);65 66    ggml_tensor * cur;67    ggml_tensor * inpL;68 69    inpL = build_inp_embd(model.tok_embd);70 71    ggml_tensor * inp_pos  = build_inp_pos();72    auto *        inp_attn = build_attn_inp_kv();73 74    const float kq_scale =75        hparams.f_attention_scale == 0.0f ? 1.0f / sqrtf(float(n_embd_head)) : hparams.f_attention_scale;76 77    ggml_tensor * inp_out_ids = build_inp_out_ids();78 79    for (int il = 0; il < n_layer; ++il) {80        ggml_tensor * inpSA = inpL;81 82        cur = build_norm(inpL, model.layers[il].attn_norm, nullptr, LLM_NORM_RMS, il);83        cb(cur, "attn_norm", il);84 85        // self-attention86        {87            ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);88 89            // compute Q and K and RoPE them90            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,91                    n_embd_head, n_head, n_head_kv, il);92 93            Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);94            cb(Qcur, "Qcur_normed", il);95 96            Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);97            cb(Kcur, "Kcur_normed", il);98 99            Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, rope_factors, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,100                                 ext_factor, attn_factor, beta_fast, beta_slow);101 102            Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, rope_factors, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,103                                 ext_factor, attn_factor, beta_fast, beta_slow);104 105            cb(Qcur, "Qcur_pos", il);106            cb(Kcur, "Kcur_pos", il);107            cb(Vcur, "Vcur_pos", il);108 109            cur = build_attn(inp_attn,110                    model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,111                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);112            cb(cur, "attn_out", il);113        }114 115        if (il == n_layer - 1 && inp_out_ids) {116            cur   = ggml_get_rows(ctx0, cur, inp_out_ids);117            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);118        }119 120        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);121        cb(ffn_inp, "ffn_inp", il);122 123        // feed-forward network with xIELU activation124        {125            cur = build_norm(ffn_inp, model.layers[il].ffn_norm, nullptr, LLM_NORM_RMS, il);126            cb(cur, "ffn_norm", il);127 128            // Up projection129            ggml_tensor * up = build_lora_mm(model.layers[il].ffn_up, cur);130            cb(up, "ffn_up", il);131 132            float alpha_n_val = hparams.xielu_alpha_n[il];133            float alpha_p_val = hparams.xielu_alpha_p[il];134            float beta_val    = hparams.xielu_beta[il];135            float eps_val     = hparams.xielu_eps[il];136 137            // Apply xIELU activation138            ggml_tensor * activated = ggml_xielu(ctx0, up, alpha_n_val, alpha_p_val, beta_val, eps_val);139            cb(activated, "ffn_xielu", il);140 141            // Down projection142            cur = build_lora_mm(model.layers[il].ffn_down, activated);143            cb(cur, "ffn_down", il);144        }145 146        cur = ggml_add(ctx0, cur, ffn_inp);147        cb(cur, "ffn_out", il);148 149        cur = build_cvec(cur, il);150        cb(cur, "l_out", il);151 152        // input for next layer153        inpL = cur;154    }155 156    cur = inpL;157 158    cur = build_norm(cur, model.output_norm, nullptr, LLM_NORM_RMS, -1);159 160    cb(cur, "result_norm", -1);161    res->t_embd = cur;162 163    // lm_head164    cur = build_lora_mm(model.output, cur, model.output_s);165 166    cb(cur, "result_output", -1);167    res->t_logits = cur;168 169    ggml_build_forward_expand(gf, cur);170}171