1
0

qwen3vl.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #include "models.h"
  2. ggml_cgraph * clip_graph_qwen3vl::build() {
  3. GGML_ASSERT(model.patch_bias != nullptr);
  4. GGML_ASSERT(model.position_embeddings != nullptr);
  5. GGML_ASSERT(model.class_embedding == nullptr);
  6. const int batch_size = 1;
  7. const int n_pos = n_patches;
  8. const int num_position_ids = n_pos * 4; // m-rope requires 4 dim per position
  9. norm_type norm_t = NORM_TYPE_NORMAL;
  10. int mrope_sections[4] = {d_head/4, d_head/4, d_head/4, d_head/4};
  11. ggml_tensor * inp_raw = build_inp_raw();
  12. ggml_tensor * inp = ggml_conv_2d(ctx0, model.patch_embeddings_0, inp_raw, patch_size, patch_size, 0, 0, 1, 1);
  13. GGML_ASSERT(img.nx % (patch_size * 2) == 0);
  14. GGML_ASSERT(img.ny % (patch_size * 2) == 0);
  15. // second conv dimension
  16. {
  17. auto inp_1 = ggml_conv_2d(ctx0, model.patch_embeddings_1, inp_raw, patch_size, patch_size, 0, 0, 1, 1);
  18. inp = ggml_add(ctx0, inp, inp_1);
  19. inp = ggml_permute(ctx0, inp, 1, 2, 0, 3); // [w, h, c, b] -> [c, w, h, b]
  20. inp = ggml_cont_4d(
  21. ctx0, inp,
  22. n_embd * 2, n_patches_x / 2, n_patches_y, batch_size);
  23. inp = ggml_reshape_4d(
  24. ctx0, inp,
  25. n_embd * 2, n_patches_x / 2, 2, batch_size * (n_patches_y / 2));
  26. inp = ggml_permute(ctx0, inp, 0, 2, 1, 3);
  27. inp = ggml_cont_3d(
  28. ctx0, inp,
  29. n_embd, n_patches_x * n_patches_y, batch_size);
  30. }
  31. // add patch bias
  32. if (model.patch_bias != nullptr) {
  33. inp = ggml_add(ctx0, inp, model.patch_bias);
  34. cb(inp, "patch_bias", -1);
  35. }
  36. // calculate absolute position embedding and apply
  37. ggml_tensor * learned_pos_embd = resize_position_embeddings();
  38. learned_pos_embd = ggml_cont_4d(
  39. ctx0, learned_pos_embd,
  40. n_embd * 2, n_patches_x / 2, n_patches_y, batch_size);
  41. learned_pos_embd = ggml_reshape_4d(
  42. ctx0, learned_pos_embd,
  43. n_embd * 2, n_patches_x / 2, 2, batch_size * (n_patches_y / 2));
  44. learned_pos_embd = ggml_permute(ctx0, learned_pos_embd, 0, 2, 1, 3);
  45. learned_pos_embd = ggml_cont_3d(
  46. ctx0, learned_pos_embd,
  47. n_embd, n_patches_x * n_patches_y, batch_size);
  48. inp = ggml_add(ctx0, inp, learned_pos_embd);
  49. cb(inp, "inp_pos_emb", -1);
  50. ggml_tensor * inpL = inp;
  51. ggml_tensor * positions = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, num_position_ids);
  52. ggml_set_name(positions, "positions");
  53. ggml_set_input(positions);
  54. // pre-layernorm
  55. if (model.pre_ln_w) {
  56. inpL = build_norm(inpL, model.pre_ln_w, model.pre_ln_b, norm_t, eps, -1);
  57. }
  58. // deepstack features (stack along the feature dimension), [n_embd * len(deepstack_layers), n_patches_x * n_patches_y, batch_size]
  59. ggml_tensor * deepstack_features = nullptr;
  60. const int merge_factor = hparams.n_merge > 0 ? hparams.n_merge * hparams.n_merge : 4; // default 2x2=4 for qwen3vl
  61. // loop over layers
  62. for (int il = 0; il < n_layer; il++) {
  63. auto & layer = model.layers[il];
  64. ggml_tensor * cur = inpL; // inpL = residual, cur = hidden_states
  65. // layernorm1
  66. cur = build_norm(cur, layer.ln_1_w, layer.ln_1_b, norm_t, eps, il);
  67. cb(cur, "ln1", il);
  68. // self-attention
  69. {
  70. cur = ggml_mul_mat(ctx0, layer.qkv_w, cur);
  71. cur = ggml_add(ctx0, cur, layer.qkv_b);
  72. ggml_tensor * Qcur = ggml_view_3d(ctx0, cur, d_head, n_head, n_pos,
  73. /* nb1 */ ggml_row_size(cur->type, d_head),
  74. /* nb2 */ cur->nb[1],
  75. /* offset */ 0);
  76. ggml_tensor * Kcur = ggml_view_3d(ctx0, cur, d_head, n_head, n_pos,
  77. /* nb1 */ ggml_row_size(cur->type, d_head),
  78. /* nb2 */ cur->nb[1],
  79. /* offset */ ggml_row_size(cur->type, n_embd));
  80. ggml_tensor * Vcur = ggml_view_3d(ctx0, cur, d_head, n_head, n_pos,
  81. /* nb1 */ ggml_row_size(cur->type, d_head),
  82. /* nb2 */ cur->nb[1],
  83. /* offset */ ggml_row_size(cur->type, 2 * n_embd));
  84. cb(Qcur, "Qcur", il);
  85. cb(Kcur, "Kcur", il);
  86. cb(Vcur, "Vcur", il);
  87. // apply M-RoPE
  88. Qcur = ggml_rope_multi(
  89. ctx0, Qcur, positions, nullptr,
  90. d_head/2, mrope_sections, GGML_ROPE_TYPE_VISION, 32768, 10000, 1, 0, 1, 32, 1);
  91. Kcur = ggml_rope_multi(
  92. ctx0, Kcur, positions, nullptr,
  93. d_head/2, mrope_sections, GGML_ROPE_TYPE_VISION, 32768, 10000, 1, 0, 1, 32, 1);
  94. cb(Qcur, "Qcur_rope", il);
  95. cb(Kcur, "Kcur_rope", il);
  96. cur = build_attn(layer.o_w, layer.o_b,
  97. Qcur, Kcur, Vcur, nullptr, kq_scale, il);
  98. cb(cur, "attn_out", il);
  99. }
  100. // re-add the layer input, e.g., residual
  101. cur = ggml_add(ctx0, cur, inpL);
  102. inpL = cur; // inpL = residual, cur = hidden_states
  103. cb(cur, "ffn_inp", il);
  104. // layernorm2
  105. cur = build_norm(cur, layer.ln_2_w, layer.ln_2_b, norm_t, eps, il);
  106. cb(cur, "ffn_inp_normed", il);
  107. // ffn
  108. cur = build_ffn(cur,
  109. layer.ff_up_w, layer.ff_up_b,
  110. layer.ff_gate_w, layer.ff_gate_b,
  111. layer.ff_down_w, layer.ff_down_b,
  112. hparams.ffn_op, il);
  113. cb(cur, "ffn_out", il);
  114. // residual 2
  115. cur = ggml_add(ctx0, inpL, cur);
  116. cb(cur, "layer_out", il);
  117. if (layer.has_deepstack()) {
  118. ggml_tensor * feat = ggml_reshape_3d(ctx0, cur, n_embd * merge_factor, n_pos / merge_factor, batch_size);
  119. feat = build_norm(feat, layer.deepstack_norm_w, layer.deepstack_norm_b, norm_t, eps, il);
  120. feat = build_ffn(feat,
  121. layer.deepstack_fc1_w, layer.deepstack_fc1_b,
  122. nullptr, nullptr,
  123. layer.deepstack_fc2_w, layer.deepstack_fc2_b,
  124. ffn_op_type::FFN_GELU, il);
  125. if(!deepstack_features) {
  126. deepstack_features = feat;
  127. } else {
  128. // concat along the feature dimension
  129. deepstack_features = ggml_concat(ctx0, deepstack_features, feat, 0);
  130. }
  131. }
  132. inpL = cur;
  133. }
  134. // post-layernorm
  135. if (model.post_ln_w) {
  136. inpL = build_norm(inpL, model.post_ln_w, model.post_ln_b, norm_t, eps, n_layer);
  137. }
  138. // multimodal projection
  139. ggml_tensor * embeddings = inpL;
  140. embeddings = ggml_reshape_3d(ctx0, embeddings, n_embd * 4, n_pos / 4, batch_size);
  141. embeddings = build_ffn(embeddings,
  142. model.mm_0_w, model.mm_0_b,
  143. nullptr, nullptr,
  144. model.mm_1_w, model.mm_1_b,
  145. ffn_op_type::FFN_GELU, -1);
  146. embeddings = ggml_concat(ctx0, embeddings, deepstack_features, 0); // concat along the feature dimension
  147. // build the graph
  148. ggml_build_forward_expand(gf, embeddings);
  149. return gf;
  150. }