CoolFace
Modelpublic

Felipe97/llama-cpp-compiled

sourceHugging Faceupdated 2d agoView on Hugging Face
0likes1.1kdownloads
llada.cpp154 linesDownload Raw Back to models
1#include "models.h"2 3void llama_model_llada::load_arch_hparams(llama_model_loader & ml) {4    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);5 6    // LLaDA-8B has 32 layers, similar to LLaMA but for diffusion7    switch (hparams.n_layer()) {8        case 32:9            type = LLM_TYPE_8B;10            break;11        default:12            type = LLM_TYPE_UNKNOWN;13    }14 15    // Set non-causal attention for diffusion models16    hparams.causal_attn = false;17}18 19void llama_model_llada::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 28    // if output is NULL, init from the input tok embed29    if (output == NULL) {30        output =31            create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, TENSOR_DUPLICATED);32    }33 34    for (int i = 0; i < n_layer; ++i) {35        auto & layer = layers[i];36 37        layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, 0);38 39        create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head, n_embd_k_gqa, n_embd_v_gqa, 0);40        layer.wo =41            create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd_head_k * n_head, n_embd }, 0);42        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 46        layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), { n_rot / 2 },47                                         TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));48 49        layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), { n_embd, n_ff }, 0);50        layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd }, 0);51        layer.ffn_up   = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), { n_embd, n_ff }, 0);52 53        // optional MLP bias54        layer.ffn_gate_b =55            create_tensor(tn(LLM_TENSOR_FFN_GATE, "bias", i), { n_ff }, TENSOR_NOT_REQUIRED);56        layer.ffn_down_b =57            create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), { n_embd }, TENSOR_NOT_REQUIRED);58        layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), { n_ff }, TENSOR_NOT_REQUIRED);59    }60}61 62std::unique_ptr<llm_graph_context> llama_model_llada::build_arch_graph(const llm_graph_params & params) const {63    return std::make_unique<graph>(*this, params);64}65 66llama_model_llada::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {67    // LLaDA is similar to LLaMA but uses non-causal attention for diffusion68    const int64_t n_embd_head = hparams.n_embd_head_v();69 70    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());71    GGML_ASSERT(n_embd_head == n_rot);72 73    ggml_tensor * cur;74    ggml_tensor * inpL;75 76    inpL = build_inp_embd(model.tok_embd);77 78    // inp_pos - contains the positions79    ggml_tensor * inp_pos = build_inp_pos();80 81    // Non-causal attention for diffusion82    auto * inp_attn = build_attn_inp_no_cache();83 84    ggml_tensor * inp_out_ids = build_inp_out_ids();85 86    for (int il = 0; il < n_layer; ++il) {87        ggml_tensor * inpSA = inpL;88 89        // norm90        cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);91        cb(cur, "attn_norm", il);92 93        // self-attention94        {95            // compute separate Q, K, V projections without bias, matching LLaDALlamaBlock96            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,97                    n_embd_head, n_head, n_head_kv, il);98 99            Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, 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, nullptr, 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", il);106            cb(Kcur, "Kcur", il);107            cb(Vcur, "Vcur", il);108 109            cur = build_attn(inp_attn,110                    model.layers[il].wo, NULL, model.layers[il].wo_s,111                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);112        }113        if (il == n_layer - 1 && inp_out_ids) {114            cur   = ggml_get_rows(ctx0, cur, inp_out_ids);115            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);116        }117        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);118        cb(ffn_inp, "ffn_inp", il);119 120        // feed-forward network121        cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);122        cb(cur, "ffn_norm", il);123 124        cur = build_ffn(cur,125                model.layers[il].ffn_up, NULL, NULL,126                model.layers[il].ffn_gate, NULL, NULL,127                model.layers[il].ffn_down, NULL, NULL,128                NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);129        cb(cur, "ffn_out", il);130 131        cur = ggml_add(ctx0, cur, ffn_inp);132 133        cur = build_cvec(cur, il);134        cb(cur, "l_out", il);135 136        // input for next layer137        inpL = cur;138    }139    cur = inpL;140 141    cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);142 143    cb(cur, "result_norm", -1);144    res->t_embd = cur;145 146    // lm_head147    cur = build_lora_mm(model.output, cur, model.output_s);148 149    cb(cur, "result_output", -1);150    res->t_logits = cur;151 152    ggml_build_forward_expand(gf, cur);153}154