qwen2vl.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include "models.h"
  2. ggml_cgraph * clip_graph_qwen2vl::build() {
  3. GGML_ASSERT(model.patch_bias == nullptr);
  4. GGML_ASSERT(model.class_embedding == nullptr);
  5. const int batch_size = 1;
  6. const bool use_window_attn = hparams.n_wa_pattern > 0;
  7. const int n_wa_pattern = hparams.n_wa_pattern;
  8. const int n_pos = n_patches;
  9. const int num_position_ids = n_pos * 4; // m-rope requires 4 dim per position
  10. norm_type norm_t = proj_type == PROJECTOR_TYPE_QWEN25VL
  11. ? NORM_TYPE_RMS // qwen 2.5 vl
  12. : NORM_TYPE_NORMAL; // qwen 2 vl
  13. int mrope_sections[4] = {d_head/4, d_head/4, d_head/4, d_head/4};
  14. ggml_tensor * inp_raw = build_inp_raw();
  15. ggml_tensor * inp = ggml_conv_2d(ctx0, model.patch_embeddings_0, inp_raw, patch_size, patch_size, 0, 0, 1, 1);
  16. GGML_ASSERT(img.nx % (patch_size * 2) == 0);
  17. GGML_ASSERT(img.ny % (patch_size * 2) == 0);
  18. // second conv dimension
  19. {
  20. auto inp_1 = ggml_conv_2d(ctx0, model.patch_embeddings_1, inp_raw, patch_size, patch_size, 0, 0, 1, 1);
  21. inp = ggml_add(ctx0, inp, inp_1);
  22. inp = ggml_permute(ctx0, inp, 1, 2, 0, 3); // [w, h, c, b] -> [c, w, h, b]
  23. inp = ggml_cont_4d(
  24. ctx0, inp,
  25. n_embd * 2, n_patches_x / 2, n_patches_y, batch_size);
  26. inp = ggml_reshape_4d(
  27. ctx0, inp,
  28. n_embd * 2, n_patches_x / 2, 2, batch_size * (n_patches_y / 2));
  29. inp = ggml_permute(ctx0, inp, 0, 2, 1, 3);
  30. inp = ggml_cont_3d(
  31. ctx0, inp,
  32. n_embd, n_patches_x * n_patches_y, batch_size);
  33. }
  34. ggml_tensor * inpL = inp;
  35. ggml_tensor * window_mask = nullptr;
  36. ggml_tensor * window_idx = nullptr;
  37. ggml_tensor * inv_window_idx = nullptr;
  38. ggml_tensor * positions = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, num_position_ids);
  39. ggml_set_name(positions, "positions");
  40. ggml_set_input(positions);
  41. // pre-layernorm
  42. if (model.pre_ln_w) {
  43. inpL = build_norm(inpL, model.pre_ln_w, model.pre_ln_b, norm_t, eps, -1);
  44. }
  45. if (use_window_attn) {
  46. // handle window attention inputs
  47. inv_window_idx = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, n_pos / 4);
  48. ggml_set_name(inv_window_idx, "inv_window_idx");
  49. ggml_set_input(inv_window_idx);
  50. // mask for window attention
  51. window_mask = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_pos, n_pos);
  52. ggml_set_name(window_mask, "window_mask");
  53. ggml_set_input(window_mask);
  54. // if flash attn is used, we need to pad the mask and cast to f16
  55. if (flash_attn_type == CLIP_FLASH_ATTN_TYPE_ENABLED) {
  56. window_mask = ggml_cast(ctx0, window_mask, GGML_TYPE_F16);
  57. }
  58. // inpL shape: [n_embd, n_patches_x * n_patches_y, batch_size]
  59. GGML_ASSERT(batch_size == 1);
  60. inpL = ggml_reshape_2d(ctx0, inpL, n_embd * 4, n_patches_x * n_patches_y * batch_size / 4);
  61. inpL = ggml_get_rows(ctx0, inpL, inv_window_idx);
  62. inpL = ggml_reshape_3d(ctx0, inpL, n_embd, n_patches_x * n_patches_y, batch_size);
  63. }
  64. // loop over layers
  65. for (int il = 0; il < n_layer; il++) {
  66. const auto & layer = model.layers[il];
  67. const bool full_attn = use_window_attn ? (il + 1) % n_wa_pattern == 0 : true;
  68. ggml_tensor * cur = inpL; // inpL = residual, cur = hidden_states
  69. // layernorm1
  70. cur = build_norm(cur, layer.ln_1_w, layer.ln_1_b, norm_t, eps, il);
  71. cb(cur, "ln1", il);
  72. // self-attention
  73. {
  74. ggml_tensor * Qcur = ggml_add(ctx0,
  75. ggml_mul_mat(ctx0, layer.q_w, cur), layer.q_b);
  76. ggml_tensor * Kcur = ggml_add(ctx0,
  77. ggml_mul_mat(ctx0, layer.k_w, cur), layer.k_b);
  78. ggml_tensor * Vcur = ggml_add(ctx0,
  79. ggml_mul_mat(ctx0, layer.v_w, cur), layer.v_b);
  80. Qcur = ggml_reshape_3d(ctx0, Qcur, d_head, n_head, n_patches);
  81. Kcur = ggml_reshape_3d(ctx0, Kcur, d_head, n_head, n_patches);
  82. Vcur = ggml_reshape_3d(ctx0, Vcur, d_head, n_head, n_patches);
  83. cb(Qcur, "Qcur", il);
  84. cb(Kcur, "Kcur", il);
  85. cb(Vcur, "Vcur", il);
  86. // apply M-RoPE
  87. Qcur = ggml_rope_multi(
  88. ctx0, Qcur, positions, nullptr,
  89. d_head/2, mrope_sections, GGML_ROPE_TYPE_VISION, 32768, 10000, 1, 0, 1, 32, 1);
  90. Kcur = ggml_rope_multi(
  91. ctx0, Kcur, positions, nullptr,
  92. d_head/2, mrope_sections, GGML_ROPE_TYPE_VISION, 32768, 10000, 1, 0, 1, 32, 1);
  93. cb(Qcur, "Qcur_rope", il);
  94. cb(Kcur, "Kcur_rope", il);
  95. ggml_tensor * attn_mask = full_attn ? nullptr : window_mask;
  96. cur = build_attn(layer.o_w, layer.o_b,
  97. Qcur, Kcur, Vcur, attn_mask, 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. inpL = cur;
  118. }
  119. // post-layernorm
  120. if (model.post_ln_w) {
  121. inpL = build_norm(inpL, model.post_ln_w, model.post_ln_b, norm_t, eps, n_layer);
  122. }
  123. // multimodal projection
  124. ggml_tensor * embeddings = inpL;
  125. embeddings = ggml_reshape_3d(ctx0, embeddings, n_embd * 4, n_pos / 4, batch_size);
  126. embeddings = build_ffn(embeddings,
  127. model.mm_0_w, model.mm_0_b,
  128. nullptr, nullptr,
  129. model.mm_1_w, model.mm_1_b,
  130. FFN_GELU,
  131. -1);
  132. if (use_window_attn) {
  133. window_idx = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, n_pos / 4);
  134. ggml_set_name(window_idx, "window_idx");
  135. ggml_set_input(window_idx);
  136. // embeddings shape: [n_embd, n_patches_x * n_patches_y, batch_size]
  137. GGML_ASSERT(batch_size == 1);
  138. embeddings = ggml_reshape_2d(ctx0, embeddings, hparams.projection_dim, n_patches_x * n_patches_y / 4);
  139. embeddings = ggml_get_rows(ctx0, embeddings, window_idx);
  140. embeddings = ggml_reshape_3d(ctx0, embeddings, hparams.projection_dim, n_patches_x * n_patches_y / 4, batch_size);
  141. }
  142. // build the graph
  143. ggml_build_forward_expand(gf, embeddings);
  144. return gf;
  145. }