Felipe97/llama-cpp-compiled
01.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 // [TAG_LLAMA_N_VOCAB_OUT]22 n_vocab_out = 3072;23 }24 25 tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);26 27 // output28 output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);29 output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab_out}, TENSOR_NOT_REQUIRED);30 // if output is NULL, init from the input tok embed31 if (output == NULL) {32 output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);33 }34 35 // output rerank head36 cls_out = create_tensor(tn(LLM_TENSOR_CLS_OUT, "weight"), {n_embd, hparams.n_cls_out}, TENSOR_NOT_REQUIRED);37 38 for (int i = 0; i < n_layer; ++i) {39 auto & layer = layers[i];40 41 layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);42 43 create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head, n_embd_gqa, n_embd_gqa, 0);44 layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);45 46 layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);47 layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);48 49 layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);50 layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);51 layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0);52 layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0);53 }54}55 56std::unique_ptr<llm_graph_context> llama_model_qwen3vl::build_arch_graph(const llm_graph_params & params) const {57 return std::make_unique<graph>(*this, params);58}59 60llama_model_qwen3vl::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {61 const size_t n_deepstack_layers = hparams.n_deepstack_layers;62 63 const int64_t n_embd = hparams.n_embd;64 const int64_t n_embd_head = hparams.n_embd_head_v();65 66 GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());67 GGML_ASSERT(n_embd_head == n_rot);68 69 ggml_tensor * cur;70 ggml_tensor * inpL;71 72 inpL = build_inp_embd(model.tok_embd);73 74 int sections[4];75 std::copy(std::begin(hparams.rope_sections), std::begin(hparams.rope_sections) + 4, sections);76 77 // inp_pos - contains the positions78 ggml_tensor * inp_pos = build_inp_pos();79 80 auto * inp_attn = build_attn_inp_kv();81 82 ggml_tensor * inp_out_ids = build_inp_out_ids();83 84 for (int il = 0; il < n_layer; ++il) {85 ggml_tensor * inpSA = inpL;86 87 // norm88 cur = build_norm(inpL,89 model.layers[il].attn_norm, NULL,90 LLM_NORM_RMS, il);91 cb(cur, "attn_norm", il);92 93 // self-attention94 {95 // compute Q and K and RoPE them96 auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,97 n_embd_head, n_head, n_head_kv, il);98 99 Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);100 cb(Qcur, "Qcur_normed", il);101 102 Qcur = ggml_rope_multi(103 ctx0, Qcur, inp_pos, nullptr,104 n_rot, sections, rope_type, n_ctx_orig, freq_base, freq_scale,105 ext_factor, attn_factor, beta_fast, beta_slow106 );107 108 Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);109 cb(Kcur, "Kcur_normed", il);110 111 Kcur = ggml_rope_multi(112 ctx0, Kcur, inp_pos, nullptr,113 n_rot, sections, rope_type, n_ctx_orig, freq_base, freq_scale,114 ext_factor, attn_factor, beta_fast, beta_slow115 );116 117 cb(Qcur, "Qcur", il);118 cb(Kcur, "Kcur", il);119 cb(Vcur, "Vcur", il);120 121 cur = build_attn(inp_attn,122 model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,123 Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);124 }125 126 if (il == n_layer - 1 && inp_out_ids) {127 cur = ggml_get_rows(ctx0, cur, inp_out_ids);128 inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);129 }130 131 ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);132 cb(ffn_inp, "ffn_inp", il);133 134 // feed-forward network135 cur = build_norm(ffn_inp,136 model.layers[il].ffn_norm, NULL,137 LLM_NORM_RMS, il);138 cb(cur, "ffn_norm", il);139 140 cur = build_ffn(cur,141 model.layers[il].ffn_up, NULL, NULL,142 model.layers[il].ffn_gate, NULL, NULL,143 model.layers[il].ffn_down, NULL, NULL,144 NULL,145 LLM_FFN_SILU, LLM_FFN_PAR, il);146 cb(cur, "ffn_out", il);147 148 cur = ggml_add(ctx0, cur, ffn_inp);149 150 cur = build_cvec(cur, il);151 cb(cur, "l_out", il);152 153 if (il < (int) n_deepstack_layers) {154 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));155 cur = ggml_add(ctx0, cur, ds);156 cb(cur, "deepstack_out", il);157 }158 159 // input for next layer160 inpL = cur;161 }162 163 cur = inpL;164 165 cur = build_norm(cur,166 model.output_norm, NULL,167 LLM_NORM_RMS, -1);168 169 cb(cur, "result_norm", -1);170 res->t_embd = cur;171 172 // lm_head173 cur = build_lora_mm(model.output, cur, model.output_s);174 175 int64_t n_vocab_in = model.tok_embd->ne[1];176 int64_t n_vocab_out = model.output->ne[1];177 if (n_vocab_in > n_vocab_out) {178 // case: Qwen3TTS model with codec_head as output179 GGML_ASSERT(model.output_norm);180 int64_t pad = n_vocab_in - n_vocab_out;181 182 // using this trick to get a scalar -inf tensor to pad the output183 ggml_tensor * neg_inf = ggml_scale_bias(ctx0,184 ggml_view_1d(ctx0, model.output_norm, 1, 0),185 0.0f, -INFINITY);186 neg_inf = ggml_repeat_4d(ctx0, neg_inf, pad, cur->ne[1], 1, 1);187 cur = ggml_concat(ctx0, neg_inf, cur, 0); // [padded .. n_vocab_out, n_stream]188 189 } else if (n_vocab_in < n_vocab_out) {190 GGML_ABORT("invalid case");191 }192 193 cb(cur, "result_output", -1);194 res->t_logits = cur;195 196 ggml_build_forward_expand(gf, cur);197}198 