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
0likes3kdownloads
granite.cpp312 linesDownload Raw Back to models
1#include "models.h"2 3#include <sstream>4 5void llama_model_granite::load_arch_hparams(llama_model_loader & ml) {6    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);7    ml.get_key(LLM_KV_LOGIT_SCALE,                 hparams.f_logit_scale);8    ml.get_key(LLM_KV_RESIDUAL_SCALE,              hparams.f_residual_scale, false);9    ml.get_key(LLM_KV_EMBEDDING_SCALE,             hparams.f_embedding_scale, false);10    ml.get_key(LLM_KV_ATTENTION_SCALE,             hparams.f_attention_scale, false);11 12    // Granite4 Vision uses array deepstack_mapping13    ml.get_arr(LLM_KV_DEEPSTACK_MAPPING, hparams.deepstack_mapping_arr, false);14 15    // Count the unique deepstack input indices16    std::unordered_set<uint32_t> unique_deepstack_idxs;17    for (const auto val : hparams.deepstack_mapping_arr) {18        if (val >= 0) {19            unique_deepstack_idxs.insert(val);20        }21    }22    hparams.n_deepstack_layers = unique_deepstack_idxs.size();23 24    // Ensure all values are valid (avoid overflow attacks)25    for (const auto val : unique_deepstack_idxs) {26        if (val > hparams.n_deepstack_layers) {27            std::stringstream ss;28            ss << "Invalid deepstack index: " << val << " > " << hparams.n_deepstack_layers;29            throw std::runtime_error(ss.str());30        }31    }32 33    // Granite uses rope_finetuned as a switch for rope, so default to true34    bool rope_finetuned = true;35    ml.get_key(LLM_KV_ROPE_SCALING_FINETUNED, rope_finetuned, false);36    hparams.rope_finetuned = rope_finetuned;37 38    switch (hparams.n_layer()) {39        case 32: type = LLM_TYPE_3B; break;40        case 40: type = LLM_TYPE_3B; break;41        // Add additional layer/vocab/etc checks here for other model sizes42        default: type = LLM_TYPE_UNKNOWN;43    }44 45    // For Granite MoE Shared46    ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp, /* required */ false);47}48 49void llama_model_granite::load_arch_tensors(llama_model_loader &) {50    LLAMA_LOAD_LOCALS;51 52    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);53 54    // output55    output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);56    output      = create_tensor(tn(LLM_TENSOR_OUTPUT,      "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);57 58    // if output is NULL, init from the input tok embed59    if (output == NULL) {60        output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);61    }62 63    for (int i = 0; i < n_layer; ++i) {64        auto & layer = layers[i];65 66        layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);67 68        create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head, n_embd_k_gqa, n_embd_v_gqa, 0);69        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);70 71        // optional bias tensors72        layer.wo_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);73 74        layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);75 76        if (hparams.rope_scaling_type_train == LLAMA_ROPE_SCALING_TYPE_LONGROPE) {77            layer.rope_long  = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_LONG,  "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));78            layer.rope_short = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_SHORT, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));79        }80        else {81            layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));82        }83 84        if (n_expert == 0) {85            layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd,   n_ff}, 0);86            layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {  n_ff, n_embd}, 0);87            layer.ffn_up   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd,   n_ff}, 0);88 89            // optional MLP bias90            layer.ffn_gate_b = create_tensor(tn(LLM_TENSOR_FFN_GATE, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);91            layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);92            layer.ffn_up_b   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "bias", i), {n_ff}, TENSOR_NOT_REQUIRED);93        } else {94            layer.ffn_gate_inp  = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP,  "weight", i), {n_embd, n_expert}, 0);95            layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd,   n_ff, n_expert}, TENSOR_NOT_REQUIRED);96            layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {  n_ff, n_embd, n_expert}, 0);97            layer.ffn_up_exps   = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS,   "weight", i), {n_embd,   n_ff, n_expert}, 0);98 99            // For Granite MoE Shared100            if (hparams.n_ff_shexp > 0) {101                layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, hparams.n_ff_shexp}, 0);102                layer.ffn_up_shexp   = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP,   "weight", i), {n_embd, hparams.n_ff_shexp}, 0);103                layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {hparams.n_ff_shexp, n_embd}, 0);104            }105        }106    }107}108 109std::unique_ptr<llm_graph_context> llama_model_granite::build_arch_graph(const llm_graph_params & params) const {110    return std::make_unique<graph>(*this, params);111}112 113llama_model_granite::graph::graph(114    const llama_model & model,115    const llm_graph_params & params)116    : llm_graph_context(params) {117 118    const int64_t n_embd_head = hparams.n_embd_head_v();119 120    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());121    GGML_ASSERT(n_embd_head == n_rot);122 123    ggml_tensor * cur;124    ggml_tensor * inpL;125 126    inpL = build_inp_embd(model.tok_embd);127 128    // inp_pos - built only if rope enabled129    ggml_tensor * inp_pos = nullptr;130    if (hparams.rope_finetuned) {131        inp_pos = build_inp_pos();132    }133    auto * inp_attn = build_attn_inp_kv();134 135    ggml_tensor * inp_out_ids = build_inp_out_ids();136 137    for (int il = 0; il < n_layer; ++il) {138 139        // Granite Vision 4.1 deepstack: inject the projector stream that140        // targets decoder layer `il` before the decoder runs.141        // NOTE: skip the first deepstack layer since that's inpL142        const auto & deepstack_emb_idx = hparams.deepstack_mapping_arr[il];143        if (il > 0 && deepstack_emb_idx >= 0) {144            ggml_tensor * ds = ggml_view_2d(ctx0,145                res->t_inp_embd, n_embd, n_tokens,146                res->t_inp_embd->nb[1],147                deepstack_emb_idx * n_embd * sizeof(float));148            inpL = ggml_add(ctx0, inpL, ds);149            cb(inpL, "deepstack_in", il);150        }151 152        ggml_tensor * inpSA = inpL;153 154        // norm155        cur = build_norm(inpL,156                model.layers[il].attn_norm, NULL,157                LLM_NORM_RMS, il);158        cb(cur, "attn_norm", il);159 160        // self-attention161        cur = build_attention_layer(162            cur, inp_pos, inp_attn,163            model, n_embd_head, il);164 165        if (il == n_layer - 1 && inp_out_ids) {166            cur   = ggml_get_rows(ctx0,   cur, inp_out_ids);167            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);168        }169        // ffn170        cur = build_layer_ffn(cur, inpSA, model, il);171 172        // input for next layer173        inpL = cur;174    }175    cur = inpL;176 177    cur = build_norm(cur,178            model.output_norm, NULL,179            LLM_NORM_RMS, -1);180 181    cb(cur, "result_norm", -1);182    res->t_embd = cur;183 184    // lm_head185    cur = build_lora_mm(model.output, cur, model.output_s);186 187    // For Granite architectures - scale logits188    cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_logit_scale);189    cb(cur, "result_output", -1);190    res->t_logits = cur;191 192    ggml_build_forward_expand(gf, cur);193}194 195ggml_tensor * llama_model_granite::graph::build_attention_layer(196          ggml_tensor             * cur,197          ggml_tensor             * inp_pos,198          llm_graph_input_attn_kv * inp_attn,199    const llama_model             & model,200    const int64_t                 n_embd_head,201    const int                     il) {202 203    auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,204            n_embd_head, hparams.n_head(il), hparams.n_head_kv(il), il);205 206    const bool use_rope = hparams.rope_finetuned;207    if (use_rope) {208        ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);209        Qcur = ggml_rope_ext(210                ctx0, Qcur, inp_pos, rope_factors,211                n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,212                ext_factor, attn_factor, beta_fast, beta_slow213                );214 215        Kcur = ggml_rope_ext(216                ctx0, Kcur, inp_pos, rope_factors,217                n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,218                ext_factor, attn_factor, beta_fast, beta_slow219                );220    }221 222    cb(Qcur, "Qcur", il);223    cb(Kcur, "Kcur", il);224    cb(Vcur, "Vcur", il);225 226    const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;227    cur = build_attn(inp_attn,228            model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,229            Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);230            cb(cur, "attn_out", il);231    return cur;232}233 234ggml_tensor * llama_model_granite::graph::build_layer_ffn(235          ggml_tensor       * cur,236          ggml_tensor       * inpSA,237    const llama_model       & model,238    const int                 il) {239 240    // For Granite architectures - scale residual241    if (hparams.f_residual_scale) {242        cur = ggml_scale(ctx0, cur, hparams.f_residual_scale);243    }244    ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);245    cb(ffn_inp, "ffn_inp", il);246 247    // feed-forward network (non-MoE)248    if (model.layers[il].ffn_gate_inp == nullptr) {249 250        cur = build_norm(ffn_inp,251                model.layers[il].ffn_norm, NULL,252                LLM_NORM_RMS, il);253                cb(cur, "ffn_norm", il);254 255        cur = build_ffn(cur,256                model.layers[il].ffn_up,   model.layers[il].ffn_up_b,   NULL,257                model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,258                model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,259                NULL,260                LLM_FFN_SILU, LLM_FFN_PAR, il);261                cb(cur, "ffn_out", il);262 263    } else {264        // MoE branch265        cur = build_norm(ffn_inp,266                model.layers[il].ffn_norm, NULL,267                LLM_NORM_RMS, il);268                cb(cur, "ffn_norm", il);269 270        ggml_tensor * moe_out = build_moe_ffn(cur,271                model.layers[il].ffn_gate_inp,272                model.layers[il].ffn_up_exps,273                model.layers[il].ffn_gate_exps,274                model.layers[il].ffn_down_exps,275                nullptr,276                n_expert, n_expert_used,277                LLM_FFN_SILU, true,278                hparams.expert_weights_scale,279                LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,280                il);281        cb(moe_out, "ffn_moe_out", il);282 283        // For Granite MoE Shared284        if (hparams.n_ff_shexp > 0) {285            ggml_tensor * ffn_shexp = build_ffn(cur,286                model.layers[il].ffn_up_shexp,   NULL, NULL,287                model.layers[il].ffn_gate_shexp, NULL, NULL,288                model.layers[il].ffn_down_shexp, NULL, NULL,289                NULL,290                LLM_FFN_SILU, LLM_FFN_PAR, il);291            cb(ffn_shexp, "ffn_shexp", il);292 293            cur = ggml_add(ctx0, moe_out, ffn_shexp);294            cb(cur, "ffn_out", il);295        } else {296            cur = moe_out;297        }298    }299 300    // For Granite architectures - scale residual301    if (hparams.f_residual_scale) {302        cur = ggml_scale(ctx0, cur, hparams.f_residual_scale);303    }304    cur = ggml_add(ctx0, cur, ffn_inp);305    cb(cur, "ffn_out", il);306 307    cur = build_cvec(cur, il);308    cb(cur, "l_out", il);309 310    return cur;311}312 
Brunobkr/llama.cpp_AlgMor24_github · Team Ai