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.
03.1k
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 // Use separate Q, K, V projections without bias, matching LLaDALlamaBlock40 layer.wq =41 create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), { n_embd, n_embd_head_k * n_head }, 0);42 layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), { n_embd, n_embd_k_gqa }, 0);43 layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), { n_embd, n_embd_v_gqa }, 0);44 // No bias for QKV projections as per config: include_bias=false, include_qkv_bias=false45 layer.wo =46 create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd_head_k * n_head, n_embd }, 0);47 layer.wo_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), { n_embd }, TENSOR_NOT_REQUIRED);48 49 layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), { n_embd }, 0);50 51 layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), { n_rot / 2 },52 TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));53 54 layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), { n_embd, n_ff }, 0);55 layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd }, 0);56 layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), { n_embd, n_ff }, 0);57 58 // optional MLP bias59 layer.ffn_gate_b =60 create_tensor(tn(LLM_TENSOR_FFN_GATE, "bias", i), { n_ff }, TENSOR_NOT_REQUIRED);61 layer.ffn_down_b =62 create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), { n_embd }, TENSOR_NOT_REQUIRED);63 layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), { n_ff }, TENSOR_NOT_REQUIRED);64 }65}66 67std::unique_ptr<llm_graph_context> llama_model_llada::build_arch_graph(const llm_graph_params & params) const {68 return std::make_unique<graph>(*this, params);69}70 71llama_model_llada::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {72 // LLaDA is similar to LLaMA but uses non-causal attention for diffusion73 const int64_t n_embd_head = hparams.n_embd_head_v();74 75 GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());76 GGML_ASSERT(n_embd_head == n_rot);77 78 ggml_tensor * cur;79 ggml_tensor * inpL;80 81 inpL = build_inp_embd(model.tok_embd);82 83 // inp_pos - contains the positions84 ggml_tensor * inp_pos = build_inp_pos();85 86 // Non-causal attention for diffusion87 auto * inp_attn = build_attn_inp_no_cache();88 89 ggml_tensor * inp_out_ids = build_inp_out_ids();90 91 for (int il = 0; il < n_layer; ++il) {92 ggml_tensor * inpSA = inpL;93 94 // norm95 cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);96 cb(cur, "attn_norm", il);97 98 // self-attention99 {100 // compute separate Q, K, V projections without bias, matching LLaDALlamaBlock101 auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,102 n_embd_head, n_head, n_head_kv, il);103 104 Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,105 ext_factor, attn_factor, beta_fast, beta_slow);106 107 Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,108 ext_factor, attn_factor, beta_fast, beta_slow);109 110 cb(Qcur, "Qcur", il);111 cb(Kcur, "Kcur", il);112 cb(Vcur, "Vcur", il);113 114 cur = build_attn(inp_attn,115 model.layers[il].wo, NULL, model.layers[il].wo_s,116 Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);117 }118 if (il == n_layer - 1 && inp_out_ids) {119 cur = ggml_get_rows(ctx0, cur, inp_out_ids);120 inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);121 }122 ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);123 cb(ffn_inp, "ffn_inp", il);124 125 // feed-forward network126 cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);127 cb(cur, "ffn_norm", il);128 129 cur = build_ffn(cur,130 model.layers[il].ffn_up, NULL, NULL,131 model.layers[il].ffn_gate, NULL, NULL,132 model.layers[il].ffn_down, NULL, NULL,133 NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);134 cb(cur, "ffn_out", il);135 136 cur = ggml_add(ctx0, cur, ffn_inp);137 138 cur = build_cvec(cur, il);139 cb(cur, "l_out", il);140 141 // input for next layer142 inpL = cur;143 }144 cur = inpL;145 146 cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);147 148 cb(cur, "result_norm", -1);149 res->t_embd = cur;150 151 // lm_head152 cur = build_lora_mm(model.output, cur, model.output_s);153 154 cb(cur, "result_output", -1);155 res->t_logits = cur;156 157 ggml_build_forward_expand(gf, cur);158}159 