Team Ai
Datasetpublic

Brunobkr/llama.cpp_AlgMor24_github

ΩFFFΣLLIa • llama.cpp • AlgMor24 ██████╗ ███████╗███████╗███████╗██╗ ██╗ ██╗ █████╗ ██╔═══██╗██╔════╝██╔════╝██╔════╝██║ ██║ ██║██╔══██╗ ██║ ██║█████╗ █████╗ █████╗ ██║ ██║ ██║███████║ ██║ ██║██╔══╝ ██╔══╝ ██╔══╝ ██║ ██║ ██║██╔══██║ ╚██████╔╝██║ ██║ ███████╗███████╗███████╗██║██║ ██║ ╚═════╝ ╚═╝ ╚═╝ ╚══════╝╚══════╝╚══════╝╚═╝╚═╝ ╚═╝ High-Performance LLM / VLM Inference & Autonomous Agentic Ecosystem… See the full description on the dataset page: https://huggingface.co/datasets/Brunobkr/llama.cpp_AlgMor24_github.

sourceHugging Faceupdated 2mo agoView on Hugging Face
0likes3.1kdownloads
glm4.cpp191 linesDownload Raw Back to models
1#include "models.h"2 3void llama_model_glm4::load_arch_hparams(llama_model_loader & ml) {4    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS,    hparams.f_norm_rms_eps);5    ml.get_key_or_arr(LLM_KV_ROPE_DIMENSION_SECTIONS, hparams.rope_sections, 4, false);6 7    // NextN/MTP parameters (GLM-OCR)8    ml.get_key(LLM_KV_NEXTN_PREDICT_LAYERS, hparams.n_layer_nextn, false);9    GGML_ASSERT(hparams.n_layer_nextn < hparams.n_layer_all && "n_layer_nextn must be < n_layer_impl");10 11    switch (hparams.n_layer()) {12        case 17: type = LLM_TYPE_1B; break; // GLM-OCR13        case 40: type = LLM_TYPE_9B; break;14        case 61: type = LLM_TYPE_32B; break;15        default: type = LLM_TYPE_UNKNOWN;16    }17}18 19void llama_model_glm4::load_arch_tensors(llama_model_loader &) {20    LLAMA_LOAD_LOCALS;21 22    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);23 24    // output25    output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);26    output      = create_tensor(tn(LLM_TENSOR_OUTPUT,      "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);27    // if output is NULL, init from the input tok embed28    if (output == NULL) {29        output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);30    }31 32    for (int i = 0; i < n_layer_all; ++i) {33        int flags = 0;34        if (i >= n_layer) {35            // skip all tensors in the NextN layers36            flags |= TENSOR_SKIP;37        }38 39        auto & layer = layers[i];40 41        layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, flags);42        create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head, n_embd_k_gqa, n_embd_v_gqa, flags);43 44        layer.wo   = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, flags);45 46        layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, flags);47 48        layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, flags);49        layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {  n_ff, n_embd}, flags);50        layer.ffn_up   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd, n_ff * 2}, flags);51 52        layer.ffn_post_norm  = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, flags);53 54        // NextN/MTP tensors (preserved but unused) - conditionally load for last nextn_predict_layers55        if (i >= n_layer) {56            layer.nextn.eh_proj          = create_tensor(tn(LLM_TENSOR_NEXTN_EH_PROJ, "weight", i), { 2 * n_embd, n_embd }, flags);57            layer.nextn.enorm            = create_tensor(tn(LLM_TENSOR_NEXTN_ENORM, "weight", i), { n_embd }, flags);58            layer.nextn.hnorm            = create_tensor(tn(LLM_TENSOR_NEXTN_HNORM, "weight", i), { n_embd }, flags);59 60            // Optional tensors61            layer.nextn.embed_tokens     = create_tensor(tn(LLM_TENSOR_NEXTN_EMBED_TOKENS, "weight", i), { n_embd, n_vocab }, flags | TENSOR_NOT_REQUIRED);62            layer.nextn.shared_head_head = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_HEAD, "weight", i), { n_embd, n_vocab }, flags | TENSOR_NOT_REQUIRED);63            layer.nextn.shared_head_norm = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_NORM, "weight", i), { n_embd }, flags | TENSOR_NOT_REQUIRED);64        }65    }66}67 68std::unique_ptr<llm_graph_context> llama_model_glm4::build_arch_graph(const llm_graph_params & params) const {69    return std::make_unique<graph>(*this, params);70}71 72llama_model_glm4::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    int sections[4];78    std::copy(std::begin(hparams.rope_sections), std::begin(hparams.rope_sections) + 4, sections);79 80    ggml_tensor * cur;81    ggml_tensor * inpL;82 83    inpL = build_inp_embd(model.tok_embd);84 85    bool use_mrope = hparams.use_mrope();86    if (ubatch.embd && !use_mrope) {87        // unfortunately, we need to forcefully stop here, to avoid users complaining about wrong results88        GGML_ABORT("This GGUF does not support multimodal. Please reconvert it.");89    }90 91    // inp_pos - contains the positions92    ggml_tensor * inp_pos = build_inp_pos();93 94    auto * inp_attn = build_attn_inp_kv();95 96    ggml_tensor * inp_out_ids = build_inp_out_ids();97 98    // Only process up to last layer (skip final NextN layer)99    // Final layer tensors are loaded but not processed in forward pass100    for (int il = 0; il < n_layer; ++il) {101        ggml_tensor * inpSA = inpL;102 103        // Pre-attention norm104        cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);105        cb(cur, "attn_norm", il);106 107        // self-attention108        {109            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,110                    n_embd_head, n_head, n_head_kv, il);111 112            if (use_mrope) {113                Qcur = ggml_rope_multi(ctx0, Qcur, inp_pos, nullptr,114                            n_rot, sections, rope_type, n_ctx_orig, freq_base, freq_scale,115                            ext_factor, attn_factor, beta_fast, beta_slow);116 117                Kcur = ggml_rope_multi(ctx0, Kcur, inp_pos, nullptr,118                            n_rot, sections, rope_type, n_ctx_orig, freq_base, freq_scale,119                            ext_factor, attn_factor, beta_fast, beta_slow);120            } else {121                // Normal RoPE122                Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot,123                                    rope_type, n_ctx_orig, freq_base, freq_scale,124                                    ext_factor, attn_factor, beta_fast, beta_slow);125 126                Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot,127                                    rope_type, n_ctx_orig, freq_base, freq_scale,128                                    ext_factor, attn_factor, beta_fast, beta_slow);129            }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, NULL, model.layers[il].wo_s,137                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), 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        // Post-attention norm (new!)144        cur = build_norm(cur, model.layers[il].attn_post_norm, NULL, LLM_NORM_RMS, il);145        cb(cur, "post_attn_norm", il);146 147        // Add the input (residual connection after post-attention norm)148        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);149        cb(ffn_inp, "ffn_inp", il);150 151        // FF152        {153            // Pre-MLP norm154            cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);155            cb(cur, "ffn_norm", il);156 157            // MLP158            cur = build_ffn(cur,159                    model.layers[il].ffn_up, NULL, NULL,160                    NULL, NULL, NULL,161                    model.layers[il].ffn_down, NULL, NULL,162                    NULL, LLM_FFN_SWIGLU, LLM_FFN_SEQ, il);163            cb(cur, "ffn_out", il);164 165            // Post-MLP norm166            cur = build_norm(cur, model.layers[il].ffn_post_norm, NULL, LLM_NORM_RMS, il);167            cb(cur, "post_mlp_norm", il);168        }169        cur = ggml_add(ctx0, cur, ffn_inp);170 171        cur = build_cvec(cur, il);172        cb(cur, "l_out", il);173 174        // input for next layer175        inpL = cur;176    }177    // Final norm178    cur = build_norm(inpL, model.output_norm, NULL, LLM_NORM_RMS, -1);179 180    cb(cur, "result_norm", -1);181    res->t_embd = cur;182 183    // Output projection184    cur = build_lora_mm(model.output, cur, model.output_s);185 186    cb(cur, "result_output", -1);187    res->t_logits = cur;188 189    ggml_build_forward_expand(gf, cur);190}191 
Brunobkr/llama.cpp_AlgMor24_github · Team Ai