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_qwen3vl::load_arch_hparams(llama_model_loader & ml) {4 ml.get_key(LLM_KV_NUM_DEEPSTACK_LAYERS, hparams.n_deepstack_layers, false);5 ml.get_key_or_arr(LLM_KV_ROPE_DIMENSION_SECTIONS, hparams.rope_sections, 4, true);6 ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);7 8 switch (hparams.n_layer()) {9 case 28: type = LLM_TYPE_1_7B; break;10 case 36: type = hparams.n_embd == 2560 ? LLM_TYPE_4B : LLM_TYPE_8B; break;11 case 64: type = LLM_TYPE_32B; break;12 default: type = LLM_TYPE_UNKNOWN;13 }14}15 16void llama_model_qwen3vl::load_arch_tensors(llama_model_loader &) {17 LLAMA_LOAD_LOCALS;18 19 int64_t n_vocab_out = n_vocab;20 if (arch == LLM_ARCH_QWEN3TTS) {21 n_vocab_out = 3072;22 }23 24 tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);25 26 // output27 output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);28 output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab_out}, TENSOR_NOT_REQUIRED);29 // if output is NULL, init from the input tok embed30 if (output == NULL) {31 output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);32 }33 34 // output rerank head35 cls_out = create_tensor(tn(LLM_TENSOR_CLS_OUT, "weight"), {n_embd, hparams.n_cls_out}, TENSOR_NOT_REQUIRED);36 37 for (int i = 0; i < n_layer; ++i) {38 auto & layer = layers[i];39 40 layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);41 42 create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head, n_embd_gqa, n_embd_gqa, 0);43 layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);44 45 layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);46 layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);47 48 layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);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}54 55std::unique_ptr<llm_graph_context> llama_model_qwen3vl::build_arch_graph(const llm_graph_params & params) const {56 return std::make_unique<graph>(*this, params);57}58 59llama_model_qwen3vl::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {60 const size_t n_deepstack_layers = hparams.n_deepstack_layers;61 62 const int64_t n_embd = hparams.n_embd;63 const int64_t n_embd_head = hparams.n_embd_head_v();64 65 GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());66 GGML_ASSERT(n_embd_head == n_rot);67 68 ggml_tensor * cur;69 ggml_tensor * inpL;70 71 inpL = build_inp_embd(model.tok_embd);72 73 int sections[4];74 std::copy(std::begin(hparams.rope_sections), std::begin(hparams.rope_sections) + 4, sections);75 76 // inp_pos - contains the positions77 ggml_tensor * inp_pos = build_inp_pos();78 79 auto * inp_attn = build_attn_inp_kv();80 81 ggml_tensor * inp_out_ids = build_inp_out_ids();82 83 for (int il = 0; il < n_layer; ++il) {84 ggml_tensor * inpSA = inpL;85 86 // norm87 cur = build_norm(inpL,88 model.layers[il].attn_norm, NULL,89 LLM_NORM_RMS, il);90 cb(cur, "attn_norm", il);91 92 // self-attention93 {94 // compute Q and K and RoPE them95 auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,96 n_embd_head, n_head, n_head_kv, il);97 98 Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);99 cb(Qcur, "Qcur_normed", il);100 101 Qcur = ggml_rope_multi(102 ctx0, Qcur, inp_pos, nullptr,103 n_rot, sections, rope_type, n_ctx_orig, freq_base, freq_scale,104 ext_factor, attn_factor, beta_fast, beta_slow105 );106 107 Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);108 cb(Kcur, "Kcur_normed", il);109 110 Kcur = ggml_rope_multi(111 ctx0, Kcur, inp_pos, nullptr,112 n_rot, sections, rope_type, n_ctx_orig, freq_base, freq_scale,113 ext_factor, attn_factor, beta_fast, beta_slow114 );115 116 cb(Qcur, "Qcur", il);117 cb(Kcur, "Kcur", il);118 cb(Vcur, "Vcur", il);119 120 cur = build_attn(inp_attn,121 model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,122 Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);123 }124 125 if (il == n_layer - 1 && inp_out_ids) {126 cur = ggml_get_rows(ctx0, cur, inp_out_ids);127 inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);128 }129 130 ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);131 cb(ffn_inp, "ffn_inp", il);132 133 // feed-forward network134 cur = build_norm(ffn_inp,135 model.layers[il].ffn_norm, NULL,136 LLM_NORM_RMS, il);137 cb(cur, "ffn_norm", il);138 139 cur = build_ffn(cur,140 model.layers[il].ffn_up, NULL, NULL,141 model.layers[il].ffn_gate, NULL, NULL,142 model.layers[il].ffn_down, NULL, NULL,143 NULL,144 LLM_FFN_SILU, LLM_FFN_PAR, il);145 cb(cur, "ffn_out", il);146 147 cur = ggml_add(ctx0, cur, ffn_inp);148 149 cur = build_cvec(cur, il);150 cb(cur, "l_out", il);151 152 if (il < (int) n_deepstack_layers) {153 ggml_tensor * ds = ggml_view_2d(ctx0, res->t_inp_embd, n_embd, n_tokens, res->t_inp_embd->nb[1], (il + 1) * n_embd * sizeof(float));154 cur = ggml_add(ctx0, cur, ds);155 cb(cur, "deepstack_out", il);156 }157 158 // input for next layer159 inpL = cur;160 }161 162 cur = inpL;163 164 cur = build_norm(cur,165 model.output_norm, NULL,166 LLM_NORM_RMS, -1);167 168 cb(cur, "result_norm", -1);169 res->t_embd = cur;170 171 // lm_head172 cur = build_lora_mm(model.output, cur, model.output_s);173 174 int64_t n_vocab_in = model.tok_embd->ne[1];175 int64_t n_vocab_out = model.output->ne[1];176 if (n_vocab_in > n_vocab_out) {177 // case: Qwen3TTS model with codec_head as output178 GGML_ASSERT(model.output_norm);179 int64_t pad = n_vocab_in - n_vocab_out;180 181 // using this trick to get a scalar -inf tensor to pad the output182 ggml_tensor * neg_inf = ggml_scale_bias(ctx0,183 ggml_view_1d(ctx0, model.output_norm, 1, 0),184 0.0f, -INFINITY);185 neg_inf = ggml_repeat_4d(ctx0, neg_inf, pad, cur->ne[1], 1, 1);186 cur = ggml_concat(ctx0, neg_inf, cur, 0); // [padded .. n_vocab_out, n_stream]187 188 } else if (n_vocab_in < n_vocab_out) {189 GGML_ABORT("invalid case");190 }191 192 cb(cur, "result_output", -1);193 res->t_logits = cur;194 195 ggml_build_forward_expand(gf, cur);196}197 