Team Ai
Datasetpublic

echodict/llama.cpp

version https://git-lfs.github.com/spec/v1 oid sha256:cfc44b7ba25614df70e6b65e3341cae0310163bd32fd31a6b928a542df433faf size 30786

sourceHugging Faceupdated 6mo agoView on Hugging Face
0likes610downloads
llada.cpp91 linesDownload Raw Back to models
1#include "models.h"2 3llm_build_llada::llm_build_llada(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {4    // LLaDA is similar to LLaMA but uses non-causal attention for diffusion5    const int64_t n_embd_head = hparams.n_embd_head_v();6 7    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());8    GGML_ASSERT(n_embd_head == n_rot);9 10    ggml_tensor * cur;11    ggml_tensor * inpL;12 13    inpL = build_inp_embd(model.tok_embd);14 15    // inp_pos - contains the positions16    ggml_tensor * inp_pos = build_inp_pos();17 18    // Non-causal attention for diffusion19    auto * inp_attn = build_attn_inp_no_cache();20 21    ggml_tensor * inp_out_ids = build_inp_out_ids();22 23    for (int il = 0; il < n_layer; ++il) {24        ggml_tensor * inpSA = inpL;25 26        // norm27        cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);28        cb(cur, "attn_norm", il);29 30        // self-attention31        {32            // compute separate Q, K, V projections without bias, matching LLaDALlamaBlock33            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,34                    n_embd_head, n_head, n_head_kv, il);35 36            Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,37                                    ext_factor, attn_factor, beta_fast, beta_slow);38 39            Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,40                                    ext_factor, attn_factor, beta_fast, beta_slow);41 42            cb(Qcur, "Qcur", il);43            cb(Kcur, "Kcur", il);44            cb(Vcur, "Vcur", il);45 46            cur = build_attn(inp_attn,47                    model.layers[il].wo, NULL, model.layers[il].wo_s,48                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);49        }50        if (il == n_layer - 1 && inp_out_ids) {51            cur   = ggml_get_rows(ctx0, cur, inp_out_ids);52            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);53        }54        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);55        cb(ffn_inp, "ffn_inp", il);56 57        // feed-forward network58        cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);59        cb(cur, "ffn_norm", il);60 61        cur = build_ffn(cur,62                model.layers[il].ffn_up, NULL, NULL,63                model.layers[il].ffn_gate, NULL, NULL,64                model.layers[il].ffn_down, NULL, NULL,65                NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);66        cb(cur, "ffn_out", il);67 68        cur = ggml_add(ctx0, cur, ffn_inp);69 70        cur = build_cvec(cur, il);71        cb(cur, "l_out", il);72 73        // input for next layer74        inpL = cur;75    }76    cur = inpL;77 78    cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);79 80    cb(cur, "result_norm", -1);81    res->t_embd = cur;82 83    // lm_head84    cur = build_lora_mm(model.output, cur);85 86    cb(cur, "result_output", -1);87    res->t_logits = cur;88 89    ggml_build_forward_expand(gf, cur);90}91