/src/llama.cpp/src/models/qwen3vl.cpp
Line | Count | Source |
1 | | #include "models.h" |
2 | | |
3 | 0 | llm_build_qwen3vl::llm_build_qwen3vl(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) { |
4 | 0 | const size_t n_deepstack_layers = hparams.n_deepstack_layers; |
5 | 0 | const int64_t n_embd = hparams.n_embd; |
6 | 0 | const int64_t n_embd_head = hparams.n_embd_head_v; |
7 | |
|
8 | 0 | GGML_ASSERT(n_embd_head == hparams.n_embd_head_k); |
9 | 0 | GGML_ASSERT(n_embd_head == hparams.n_rot); |
10 | |
|
11 | 0 | ggml_tensor * cur; |
12 | 0 | ggml_tensor * inpL; |
13 | |
|
14 | 0 | inpL = build_inp_embd(model.tok_embd); |
15 | |
|
16 | 0 | int sections[4]; |
17 | 0 | std::copy(std::begin(hparams.rope_sections), std::begin(hparams.rope_sections) + 4, sections); |
18 | |
|
19 | 0 | std::vector<ggml_tensor *> deepstack_features(n_deepstack_layers, nullptr); |
20 | |
|
21 | 0 | if (ubatch.embd) { |
22 | | // Image input: split main embd and deepstack embds |
23 | 0 | ggml_tensor * inpL_main = ggml_view_2d(ctx0, inpL, n_embd, n_tokens, inpL->nb[1], 0); |
24 | 0 | for (size_t i = 0; i < n_deepstack_layers; i++) { |
25 | 0 | deepstack_features[i] = ggml_view_2d(ctx0, inpL, n_embd, n_tokens, inpL->nb[1], (i + 1) * n_embd * sizeof(float)); |
26 | 0 | } |
27 | 0 | inpL = inpL_main; |
28 | 0 | } |
29 | | |
30 | | // inp_pos - contains the positions |
31 | 0 | ggml_tensor * inp_pos = build_inp_pos(); |
32 | |
|
33 | 0 | auto * inp_attn = build_attn_inp_kv(); |
34 | |
|
35 | 0 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
36 | |
|
37 | 0 | for (int il = 0; il < n_layer; ++il) { |
38 | 0 | ggml_tensor * inpSA = inpL; |
39 | | |
40 | | // norm |
41 | 0 | cur = build_norm(inpL, |
42 | 0 | model.layers[il].attn_norm, NULL, |
43 | 0 | LLM_NORM_RMS, il); |
44 | 0 | cb(cur, "attn_norm", il); |
45 | | |
46 | | // self-attention |
47 | 0 | { |
48 | | // compute Q and K and RoPE them |
49 | 0 | ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur); |
50 | 0 | cb(Qcur, "Qcur", il); |
51 | |
|
52 | 0 | ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur); |
53 | 0 | cb(Kcur, "Kcur", il); |
54 | |
|
55 | 0 | ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur); |
56 | 0 | cb(Vcur, "Vcur", il); |
57 | |
|
58 | 0 | Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens); |
59 | 0 | Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens); |
60 | 0 | Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens); |
61 | |
|
62 | 0 | Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il); |
63 | 0 | cb(Qcur, "Qcur_normed", il); |
64 | |
|
65 | 0 | Qcur = ggml_rope_multi( |
66 | 0 | ctx0, Qcur, inp_pos, nullptr, |
67 | 0 | n_rot, sections, rope_type, n_ctx_orig, freq_base, freq_scale, |
68 | 0 | ext_factor, attn_factor, beta_fast, beta_slow |
69 | 0 | ); |
70 | |
|
71 | 0 | Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il); |
72 | 0 | cb(Kcur, "Kcur_normed", il); |
73 | |
|
74 | 0 | Kcur = ggml_rope_multi( |
75 | 0 | ctx0, Kcur, inp_pos, nullptr, |
76 | 0 | n_rot, sections, rope_type, n_ctx_orig, freq_base, freq_scale, |
77 | 0 | ext_factor, attn_factor, beta_fast, beta_slow |
78 | 0 | ); |
79 | |
|
80 | 0 | cb(Qcur, "Qcur", il); |
81 | 0 | cb(Kcur, "Kcur", il); |
82 | 0 | cb(Vcur, "Vcur", il); |
83 | |
|
84 | 0 | cur = build_attn(inp_attn, |
85 | 0 | model.layers[il].wo, model.layers[il].bo, |
86 | 0 | Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il); |
87 | 0 | } |
88 | |
|
89 | 0 | if (il == n_layer - 1 && inp_out_ids) { |
90 | 0 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
91 | 0 | inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids); |
92 | 0 | } |
93 | |
|
94 | 0 | ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA); |
95 | 0 | cb(ffn_inp, "ffn_inp", il); |
96 | | |
97 | | // feed-forward network |
98 | 0 | cur = build_norm(ffn_inp, |
99 | 0 | model.layers[il].ffn_norm, NULL, |
100 | 0 | LLM_NORM_RMS, il); |
101 | 0 | cb(cur, "ffn_norm", il); |
102 | |
|
103 | 0 | cur = build_ffn(cur, |
104 | 0 | model.layers[il].ffn_up, NULL, NULL, |
105 | 0 | model.layers[il].ffn_gate, NULL, NULL, |
106 | 0 | model.layers[il].ffn_down, NULL, NULL, |
107 | 0 | NULL, |
108 | 0 | LLM_FFN_SILU, LLM_FFN_PAR, il); |
109 | 0 | cb(cur, "ffn_out", il); |
110 | |
|
111 | 0 | cur = ggml_add(ctx0, cur, ffn_inp); |
112 | |
|
113 | 0 | cur = build_cvec(cur, il); |
114 | 0 | cb(cur, "l_out", il); |
115 | |
|
116 | 0 | if (ubatch.embd && (size_t)il < n_deepstack_layers) { |
117 | 0 | cur = ggml_add(ctx0, cur, deepstack_features[il]); |
118 | 0 | cb(cur, "deepstack_out", il); |
119 | 0 | } |
120 | | |
121 | | // input for next layer |
122 | 0 | inpL = cur; |
123 | 0 | } |
124 | |
|
125 | 0 | cur = inpL; |
126 | |
|
127 | 0 | cur = build_norm(cur, |
128 | 0 | model.output_norm, NULL, |
129 | 0 | LLM_NORM_RMS, -1); |
130 | |
|
131 | 0 | cb(cur, "result_norm", -1); |
132 | 0 | res->t_embd = cur; |
133 | | |
134 | | // lm_head |
135 | 0 | cur = build_lora_mm(model.output, cur); |
136 | |
|
137 | 0 | cb(cur, "result_output", -1); |
138 | 0 | res->t_logits = cur; |
139 | |
|
140 | 0 | ggml_build_forward_expand(gf, cur); |
141 | 0 | } |