CoolFace
Modelpublic

Felipe97/llama-cpp-compiled

sourceHugging Faceupdated 2d agoView on Hugging Face
0likes1.1kdownloads
olmo2.cpp209 linesDownload Raw Back to models
1#include "models.h"2 3void llama_model_olmo2::load_arch_hparams(llama_model_loader & ml) {4    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);5 6    const bool found_swa = ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false);7    if (found_swa && hparams.n_swa > 0) {8        hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;9        load_swa_pattern(ml, 4);10 11        hparams.rope_freq_base_train_swa  = hparams.rope_freq_base_train;12        hparams.rope_freq_scale_train_swa = 1.0; // See olmo2.cpp13        ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA, hparams.rope_freq_base_train_swa, false);14    } else {15        hparams.swa_type = LLAMA_SWA_TYPE_NONE;16    }17 18    switch (hparams.n_layer()) {19        case 16: type = LLM_TYPE_1B; break;20        case 32: type = LLM_TYPE_7B; break;21        case 40: type = LLM_TYPE_13B; break;22        case 64: type = LLM_TYPE_32B; break;23        default: type = LLM_TYPE_UNKNOWN;24    }25}26 27void llama_model_olmo2::load_arch_tensors(llama_model_loader &) {28    LLAMA_LOAD_LOCALS;29 30    const int64_t n_embd_head = n_embd / n_head;31 32    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);33 34    // output35    output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);36    output      = create_tensor(tn(LLM_TENSOR_OUTPUT,      "weight"), {n_embd, n_vocab}, 0);37 38    for (int i = 0; i < n_layer; ++i) {39        auto & layer = layers[i];40 41        create_tensor_qkv(layer, i, n_embd, n_embd, n_embd_gqa, n_embd_gqa, 0);42        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);43        layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd}, 0);44        layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_head_kv * n_embd_head}, 0);45        layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0);46 47        layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd,   n_ff}, 0);48        layer.ffn_up   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd,   n_ff}, 0);49        layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {  n_ff, n_embd}, 0);50        layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0);51    }52}53 54std::unique_ptr<llm_graph_context> llama_model_olmo2::build_arch_graph(const llm_graph_params & params) const {55    if (hparams.swa_type == LLAMA_SWA_TYPE_STANDARD) {56        return std::make_unique<graph<true>>(*this, params);57    } else {58        return std::make_unique<graph<false>>(*this, params);59    }60}61 62template <bool iswa>63llama_model_olmo2::graph<iswa>::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {64    const int64_t n_embd_head = hparams.n_embd_head_v();65 66    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());67    GGML_ASSERT(n_embd_head == n_rot);68 69    ggml_tensor * cur;70    ggml_tensor * inpL;71 72    inpL = build_inp_embd(model.tok_embd);73 74    // inp_pos - contains the positions75    ggml_tensor * inp_pos = build_inp_pos();76 77    using inp_attn_type = std::conditional_t<iswa, llm_graph_input_attn_kv_iswa, llm_graph_input_attn_kv>;78    inp_attn_type * inp_attn = nullptr;79 80    if constexpr (iswa) {81        inp_attn = build_attn_inp_kv_iswa();82    } else {83        inp_attn = build_attn_inp_kv();84    }85    ggml_tensor * inp_out_ids = build_inp_out_ids();86 87    for (int il = 0; il < n_layer; ++il) {88        ggml_tensor * inpSA = inpL;89 90        cur = inpL;91 92        // self_attention93        {94            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,95                    n_embd_head, n_head,96                    n_embd_head, n_head_kv,97                    n_embd_head, n_head_kv,98                    il, false);99            cb(Qcur, "Qcur", il);100            cb(Kcur, "Kcur", il);101            cb(Vcur, "Vcur", il);102 103            Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL,104                    LLM_NORM_RMS, il);105            cb(Qcur, "Qcur_normed", il);106 107            Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL,108                    LLM_NORM_RMS, il);109            cb(Kcur, "Kcur_normed", il);110 111            Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head,    n_tokens);112            Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);113            Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);114 115            const bool is_swa = hparams.is_swa(il);116 117            if (is_swa) {118                // For sliding window layers, Olmo3 use regular rope with no yarn rope scaling.119                // This is achieved here by setting freq_scale and attn_factor to 1.120                // We also set ext_factor to 0 to avoid a few unnecessary computations.121                Qcur = ggml_rope_ext(122                    ctx0, Qcur, inp_pos, nullptr,123                    n_rot, rope_type, n_ctx_orig, freq_base, 1.0,124                    0.0, 1.0, beta_fast, beta_slow125                    );126 127                Kcur = ggml_rope_ext(128                    ctx0, Kcur, inp_pos, nullptr,129                    n_rot, rope_type, n_ctx_orig, freq_base, 1.0,130                    0.0, 1.0, beta_fast, beta_slow131                    );132            } else {133                Qcur = ggml_rope_ext(134                    ctx0, Qcur, inp_pos, nullptr,135                    n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,136                    ext_factor, attn_factor, beta_fast, beta_slow137                    );138 139                Kcur = ggml_rope_ext(140                    ctx0, Kcur, inp_pos, nullptr,141                    n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,142                    ext_factor, attn_factor, beta_fast, beta_slow143                    );144            }145            cb(Qcur, "Qcur", il);146            cb(Kcur, "Kcur", il);147            cb(Vcur, "Vcur", il);148 149            cur = build_attn(inp_attn,150                    model.layers[il].wo, NULL, model.layers[il].wo_s,151                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);152        }153        if (il == n_layer - 1 && inp_out_ids) {154            cur   = ggml_get_rows(ctx0,   cur, inp_out_ids);155            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);156        }157        cur = build_norm(cur,158                model.layers[il].attn_post_norm, NULL,159                LLM_NORM_RMS, il);160        cb(cur, "attn_post_norm", il);161 162        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);163        cb(ffn_inp, "ffn_inp", il);164 165        // feed-forward network166        cur = build_ffn(ffn_inp,167                model.layers[il].ffn_up,   NULL, NULL,168                model.layers[il].ffn_gate, NULL, NULL,169                model.layers[il].ffn_down, NULL, NULL,170                NULL,171                LLM_FFN_SILU, LLM_FFN_PAR, il);172        cb(cur, "ffn_out", il);173 174        cur = build_norm(cur,175                model.layers[il].ffn_post_norm, NULL,176                LLM_NORM_RMS, -1);177        cb(cur, "ffn_post_norm", -1);178 179        cur = ggml_add(ctx0, cur, ffn_inp);180        cb(cur, "ffn_out", il);181 182        cur = build_cvec(cur, il);183        cb(cur, "l_out", il);184 185        // input for next layer186        inpL = cur;187    }188    cur = inpL;189 190    cur = build_norm(cur,191            model.output_norm, NULL,192            LLM_NORM_RMS, -1);193 194    cb(cur, "result_norm", -1);195    res->t_embd = cur;196 197    // lm_head198    cur = build_lora_mm(model.output, cur, model.output_s);199 200    cb(cur, "result_output", -1);201    res->t_logits = cur;202 203    ggml_build_forward_expand(gf, cur);204}205 206// Explicit template instantiations207template struct llama_model_olmo2::graph<false>;208template struct llama_model_olmo2::graph<true>;209