| 1 | #include "models.h" |
| 2 | |
| 3 | llm_build_qwen3vlmoe::llm_build_qwen3vlmoe(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) { |
| 4 | const size_t n_deepstack_layers = hparams.n_deepstack_layers; |
| 5 | const int64_t n_embd = hparams.n_embd; |
| 6 | const int64_t n_embd_head = hparams.n_embd_head_v; |
| 7 | |
| 8 | GGML_ASSERT(n_embd_head == hparams.n_embd_head_k); |
| 9 | GGML_ASSERT(n_embd_head == hparams.n_rot); |
| 10 | |
| 11 | ggml_tensor * cur; |
| 12 | ggml_tensor * inpL; |
| 13 | |
| 14 | inpL = build_inp_embd(tok_embd: model.tok_embd); |
| 15 | |
| 16 | int sections[4]; |
| 17 | std::copy(first: std::begin(cont: hparams.rope_sections), last: std::begin(cont: hparams.rope_sections) + 4, result: sections); |
| 18 | |
| 19 | std::vector<ggml_tensor *> deepstack_features(n_deepstack_layers, nullptr); |
| 20 | |
| 21 | if (ubatch.embd) { |
| 22 | // Image input: split main embd and deepstack embds |
| 23 | ggml_tensor * inpL_main = ggml_view_2d(ctx: ctx0, a: inpL, ne0: n_embd, ne1: n_tokens, nb1: inpL->nb[1], offset: 0); |
| 24 | for (size_t i = 0; i < n_deepstack_layers; i++) { |
| 25 | deepstack_features[i] = ggml_view_2d(ctx: ctx0, a: inpL, ne0: n_embd, ne1: n_tokens, nb1: inpL->nb[1], offset: (i + 1) * n_embd * sizeof(float)); |
| 26 | } |
| 27 | inpL = inpL_main; |
| 28 | } |
| 29 | |
| 30 | // inp_pos - contains the positions |
| 31 | ggml_tensor * inp_pos = build_inp_pos(); |
| 32 | |
| 33 | auto * inp_attn = build_attn_inp_kv(); |
| 34 | |
| 35 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
| 36 | |
| 37 | for (int il = 0; il < n_layer; ++il) { |
| 38 | ggml_tensor * inpSA = inpL; |
| 39 | |
| 40 | // norm |
| 41 | cur = build_norm(cur: inpL, |
| 42 | mw: model.layers[il].attn_norm, NULL, |
| 43 | type: LLM_NORM_RMS, il); |
| 44 | cb(cur, name: "attn_norm" , il); |
| 45 | |
| 46 | // self_attention |
| 47 | { |
| 48 | // compute Q and K and RoPE them |
| 49 | ggml_tensor * Qcur = build_lora_mm(w: model.layers[il].wq, cur); |
| 50 | cb(cur: Qcur, name: "Qcur" , il); |
| 51 | |
| 52 | ggml_tensor * Kcur = build_lora_mm(w: model.layers[il].wk, cur); |
| 53 | cb(cur: Kcur, name: "Kcur" , il); |
| 54 | |
| 55 | ggml_tensor * Vcur = build_lora_mm(w: model.layers[il].wv, cur); |
| 56 | cb(cur: Vcur, name: "Vcur" , il); |
| 57 | |
| 58 | Qcur = ggml_reshape_3d(ctx: ctx0, a: Qcur, ne0: n_embd_head, ne1: n_head, ne2: n_tokens); |
| 59 | Kcur = ggml_reshape_3d(ctx: ctx0, a: Kcur, ne0: n_embd_head, ne1: n_head_kv, ne2: n_tokens); |
| 60 | Vcur = ggml_reshape_3d(ctx: ctx0, a: Vcur, ne0: n_embd_head, ne1: n_head_kv, ne2: n_tokens); |
| 61 | |
| 62 | Qcur = build_norm(cur: Qcur, mw: model.layers[il].attn_q_norm, NULL, type: LLM_NORM_RMS, il); |
| 63 | cb(cur: Qcur, name: "Qcur_normed" , il); |
| 64 | |
| 65 | Qcur = ggml_rope_multi( |
| 66 | ctx: ctx0, a: Qcur, b: inp_pos, c: nullptr, |
| 67 | n_dims: n_rot, sections, mode: rope_type, n_ctx_orig, freq_base, freq_scale, |
| 68 | ext_factor, attn_factor, beta_fast, beta_slow |
| 69 | ); |
| 70 | |
| 71 | Kcur = build_norm(cur: Kcur, mw: model.layers[il].attn_k_norm, NULL, type: LLM_NORM_RMS, il); |
| 72 | cb(cur: Kcur, name: "Kcur_normed" , il); |
| 73 | |
| 74 | Kcur = ggml_rope_multi( |
| 75 | ctx: ctx0, a: Kcur, b: inp_pos, c: nullptr, |
| 76 | n_dims: n_rot, sections, mode: rope_type, n_ctx_orig, freq_base, freq_scale, |
| 77 | ext_factor, attn_factor, beta_fast, beta_slow |
| 78 | ); |
| 79 | |
| 80 | cb(cur: Qcur, name: "Qcur" , il); |
| 81 | cb(cur: Kcur, name: "Kcur" , il); |
| 82 | cb(cur: Vcur, name: "Vcur" , il); |
| 83 | |
| 84 | cur = build_attn(inp: inp_attn, |
| 85 | wo: model.layers[il].wo, wo_b: model.layers[il].bo, |
| 86 | q_cur: Qcur, k_cur: Kcur, v_cur: Vcur, kq_b: nullptr, sinks: nullptr, v_mla: nullptr, kq_scale: 1.0f/sqrtf(x: float(n_embd_head)), il); |
| 87 | } |
| 88 | |
| 89 | if (il == n_layer - 1 && inp_out_ids) { |
| 90 | cur = ggml_get_rows(ctx: ctx0, a: cur, b: inp_out_ids); |
| 91 | inpSA = ggml_get_rows(ctx: ctx0, a: inpSA, b: inp_out_ids); |
| 92 | } |
| 93 | |
| 94 | ggml_tensor * ffn_inp = ggml_add(ctx: ctx0, a: cur, b: inpSA); |
| 95 | cb(cur: ffn_inp, name: "ffn_inp" , il); |
| 96 | |
| 97 | // MoE branch |
| 98 | cur = build_norm(cur: ffn_inp, |
| 99 | mw: model.layers[il].ffn_norm, NULL, |
| 100 | type: LLM_NORM_RMS, il); |
| 101 | cb(cur, name: "ffn_norm" , il); |
| 102 | |
| 103 | ggml_tensor * moe_out = |
| 104 | build_moe_ffn(cur, |
| 105 | gate_inp: model.layers[il].ffn_gate_inp, |
| 106 | up_exps: model.layers[il].ffn_up_exps, |
| 107 | gate_exps: model.layers[il].ffn_gate_exps, |
| 108 | down_exps: model.layers[il].ffn_down_exps, |
| 109 | exp_probs_b: nullptr, |
| 110 | n_expert, n_expert_used, |
| 111 | type_op: LLM_FFN_SILU, norm_w: true, |
| 112 | scale_w: false, w_scale: 0.0, |
| 113 | gating_op: LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX, |
| 114 | il); |
| 115 | cb(cur: moe_out, name: "ffn_moe_out" , il); |
| 116 | cur = moe_out; |
| 117 | |
| 118 | cur = ggml_add(ctx: ctx0, a: cur, b: ffn_inp); |
| 119 | |
| 120 | cur = build_cvec(cur, il); |
| 121 | cb(cur, name: "l_out" , il); |
| 122 | |
| 123 | if (ubatch.embd && (size_t)il < n_deepstack_layers) { |
| 124 | cur = ggml_add(ctx: ctx0, a: cur, b: deepstack_features[il]); |
| 125 | cb(cur, name: "deepstack_out" , il); |
| 126 | } |
| 127 | |
| 128 | // input for next layer |
| 129 | inpL = cur; |
| 130 | } |
| 131 | |
| 132 | cur = inpL; |
| 133 | |
| 134 | cur = build_norm(cur, |
| 135 | mw: model.output_norm, NULL, |
| 136 | type: LLM_NORM_RMS, il: -1); |
| 137 | |
| 138 | cb(cur, name: "result_norm" , il: -1); |
| 139 | res->t_embd = cur; |
| 140 | |
| 141 | // lm_head |
| 142 | cur = build_lora_mm(w: model.output, cur); |
| 143 | |
| 144 | cb(cur, name: "result_output" , il: -1); |
| 145 | res->t_logits = cur; |
| 146 | |
| 147 | ggml_build_forward_expand(cgraph: gf, tensor: cur); |
| 148 | } |
| 149 | |
| 150 | |