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
plm.cpp215 linesDownload Raw Back to models
1#include "models.h"2 3void llama_model_plm::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(LLM_KV_ATTENTION_KV_LORA_RANK, hparams.n_lora_kv);6 7    switch (hparams.n_layer()) {8        case 32: type = LLM_TYPE_1_8B; break;9        default: type = LLM_TYPE_UNKNOWN;10    }11}12 13void llama_model_plm::load_arch_tensors(llama_model_loader &) {14    LLAMA_LOAD_LOCALS;15 16    const int64_t n_embd_head_qk_rope = hparams.n_rot();17    const int64_t n_embd_head_qk_nope = hparams.n_embd_head_k() - hparams.n_rot();18    const int64_t kv_lora_rank = hparams.n_lora_kv;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    output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);26 27    for (int i = 0; i < n_layer; ++i) {28        auto & layer = layers[i];29 30        layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);31 32        layer.wq        = create_tensor(tn(LLM_TENSOR_ATTN_Q,   "weight", i), {n_embd, n_embd_head_k * n_head}, 0);33        layer.wkv_a_mqa = create_tensor(tn(LLM_TENSOR_ATTN_KV_A_MQA, "weight", i), {n_embd, kv_lora_rank + (n_embd_head_qk_rope)}, 0);34        layer.attn_kv_a_norm = create_tensor(tn(LLM_TENSOR_ATTN_KV_A_NORM, "weight", i), {kv_lora_rank}, 0);35        layer.wkv_b     = create_tensor(tn(LLM_TENSOR_ATTN_KV_B,     "weight", i), {kv_lora_rank, n_head * (n_embd_head_qk_nope + n_embd_head_v)}, 0);36        layer.wo        = create_tensor(tn(LLM_TENSOR_ATTN_OUT,      "weight", i), {              n_head * (                      n_embd_head_v), n_embd}, 0);37 38        layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);39        layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {  n_ff, n_embd}, 0);40        layer.ffn_up   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd,   n_ff}, 0);41    }42}43 44std::unique_ptr<llm_graph_context> llama_model_plm::build_arch_graph(const llm_graph_params & params) const {45    return std::make_unique<graph>(*this, params);46}47 48llama_model_plm::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {49    const float kq_scale = 1.0f/sqrtf(float(hparams.n_embd_head_k()));50 51    const uint32_t n_embd_head_qk_rope = hparams.n_rot();52    const uint32_t n_embd_head_qk_nope = hparams.n_embd_head_k() - hparams.n_rot();53 54    const uint32_t kv_lora_rank = hparams.n_lora_kv;55 56    ggml_tensor * cur;57    ggml_tensor * inpL;58 59    // {n_embd, n_tokens}60    inpL = build_inp_embd(model.tok_embd);61 62    // inp_pos - contains the positions63    ggml_tensor * inp_pos = build_inp_pos();64 65    auto * inp_attn = build_attn_inp_kv();66 67    ggml_tensor * inp_out_ids = build_inp_out_ids();68 69    for (int il = 0; il < n_layer; ++il) {70        ggml_tensor * inpSA = inpL;71 72        // norm73        cur = build_norm(inpL,74                model.layers[il].attn_norm, NULL,75                LLM_NORM_RMS, il);76        cb(cur, "attn_norm", il);77 78        // self_attention79        {80            ggml_tensor * q = NULL;81            q = ggml_mul_mat(ctx0, model.layers[il].wq, cur);82            cb(q, "q", il);83 84            // split into {n_head * n_embd_head_qk_nope, n_tokens}85            ggml_tensor * q_nope = ggml_view_3d(ctx0, q, n_embd_head_qk_nope, n_head, n_tokens,86                    ggml_row_size(q->type, hparams.n_embd_head_k()),87                    ggml_row_size(q->type, hparams.n_embd_head_k() * n_head),88                    0);89            cb(q_nope, "q_nope", il);90 91            // and {n_head * n_embd_head_qk_rope, n_tokens}92            ggml_tensor * q_pe = ggml_view_3d(ctx0, q, n_embd_head_qk_rope, n_head, n_tokens,93                    ggml_row_size(q->type, hparams.n_embd_head_k()),94                    ggml_row_size(q->type, hparams.n_embd_head_k() * n_head),95                    ggml_row_size(q->type, n_embd_head_qk_nope));96            cb(q_pe, "q_pe", il);97 98            // {n_embd, kv_lora_rank + n_embd_head_qk_rope} * {n_embd, n_tokens} -> {kv_lora_rank + n_embd_head_qk_rope, n_tokens}99            ggml_tensor * kv_pe_compresseed = ggml_mul_mat(ctx0, model.layers[il].wkv_a_mqa, cur);100            cb(kv_pe_compresseed, "kv_pe_compresseed", il);101 102            // split into {kv_lora_rank, n_tokens}103            ggml_tensor * kv_compressed = ggml_view_2d(ctx0, kv_pe_compresseed, kv_lora_rank, n_tokens,104                    kv_pe_compresseed->nb[1],105                    0);106            cb(kv_compressed, "kv_compressed", il);107 108            // and {n_embd_head_qk_rope, n_tokens}109            ggml_tensor * k_pe = ggml_view_3d(ctx0, kv_pe_compresseed, n_embd_head_qk_rope, 1, n_tokens,110                    kv_pe_compresseed->nb[1],111                    kv_pe_compresseed->nb[1],112                    ggml_row_size(kv_pe_compresseed->type, kv_lora_rank));113            cb(k_pe, "k_pe", il);114 115            kv_compressed = build_norm(kv_compressed,116                    model.layers[il].attn_kv_a_norm, NULL,117                    LLM_NORM_RMS, il);118            cb(kv_compressed, "kv_compressed", il);119 120            // {kv_lora_rank, n_head * (n_embd_head_qk_nope + n_embd_head_v)} * {kv_lora_rank, n_tokens} -> {n_head * (n_embd_head_qk_nope + n_embd_head_v), n_tokens}121            ggml_tensor * kv = ggml_mul_mat(ctx0, model.layers[il].wkv_b, kv_compressed);122            cb(kv, "kv", il);123 124            // split into {n_head * n_embd_head_qk_nope, n_tokens}125            ggml_tensor * k_nope = ggml_view_3d(ctx0, kv, n_embd_head_qk_nope, n_head, n_tokens,126                    ggml_row_size(kv->type, n_embd_head_qk_nope + hparams.n_embd_head_v()),127                    ggml_row_size(kv->type, n_head * (n_embd_head_qk_nope + hparams.n_embd_head_v())),128                    0);129            cb(k_nope, "k_nope", il);130 131            // and {n_head * n_embd_head_v, n_tokens}132            ggml_tensor * v_states = ggml_view_3d(ctx0, kv, hparams.n_embd_head_v(), n_head, n_tokens,133                    ggml_row_size(kv->type, (n_embd_head_qk_nope + hparams.n_embd_head_v())),134                    ggml_row_size(kv->type, (n_embd_head_qk_nope + hparams.n_embd_head_v())*n_head),135                    ggml_row_size(kv->type, (n_embd_head_qk_nope)));136            cb(v_states, "v_states", il);137 138            v_states = ggml_cont(ctx0, v_states);139            cb(v_states, "v_states", il);140 141            v_states = ggml_view_2d(ctx0, v_states, hparams.n_embd_head_v() * n_head, n_tokens,142                    ggml_row_size(kv->type, hparams.n_embd_head_v() * n_head),143                    0);144            cb(v_states, "v_states", il);145 146            q_pe = ggml_rope_ext(147                    ctx0, q_pe, inp_pos, nullptr,148                    n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,149                    ext_factor, attn_factor, beta_fast, beta_slow150                    );151            cb(q_pe, "q_pe", il);152 153            // shared RoPE key154            k_pe = ggml_rope_ext(155                    ctx0, k_pe, inp_pos, nullptr,156                    n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,157                    ext_factor, attn_factor, beta_fast, beta_slow158                    );159            cb(k_pe, "k_pe", il);160 161            ggml_tensor * q_states = ggml_concat(ctx0, q_nope, q_pe, 0);162            cb(q_states, "q_states", il);163 164            ggml_tensor * k_states = ggml_concat(ctx0, k_nope, ggml_repeat(ctx0, k_pe, q_pe), 0);165            cb(k_states, "k_states", il);166 167            cur = build_attn(inp_attn,168                    model.layers[il].wo, NULL, model.layers[il].wo_s,169                    q_states, k_states, v_states, nullptr, nullptr, nullptr, kq_scale, il);170        }171        if (il == n_layer - 1 && inp_out_ids) {172            cur   = ggml_get_rows(ctx0,   cur, inp_out_ids);173            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);174        }175        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);176        cb(ffn_inp, "ffn_inp", il);177 178        cur = build_norm(ffn_inp,179                model.layers[il].ffn_norm, NULL,180                LLM_NORM_RMS, il);181        cb(cur, "ffn_norm", il);182 183        cur = build_ffn(cur,184                model.layers[il].ffn_up,   NULL, NULL,185                NULL, NULL, NULL,186                model.layers[il].ffn_down, NULL, NULL,187                NULL,188                LLM_FFN_RELU_SQR, LLM_FFN_SEQ, il);189        cb(cur, "ffn_out", il);190 191        cur = ggml_add(ctx0, cur, ffn_inp);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    cur = build_lora_mm(model.output, cur, model.output_s);209 210    cb(cur, "result_output", -1);211    res->t_logits = cur;212 213    ggml_build_forward_expand(gf, cur);214}215 
Brunobkr/llama.cpp_AlgMor24_github · Team Ai