pixtral.cpp 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include "models.h"
  2. ggml_cgraph * clip_graph_pixtral::build() {
  3. const int n_merge = hparams.n_merge;
  4. // 2D input positions
  5. ggml_tensor * pos_h = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, n_patches);
  6. ggml_set_name(pos_h, "pos_h");
  7. ggml_set_input(pos_h);
  8. ggml_tensor * pos_w = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, n_patches);
  9. ggml_set_name(pos_w, "pos_w");
  10. ggml_set_input(pos_w);
  11. auto add_pos = [&](ggml_tensor * cur, const clip_layer &) {
  12. return build_rope_2d(ctx0, cur, pos_h, pos_w, hparams.rope_theta, true);
  13. };
  14. ggml_tensor * inp = build_inp();
  15. ggml_tensor * cur = build_vit(
  16. inp, n_patches,
  17. NORM_TYPE_RMS,
  18. hparams.ffn_op,
  19. nullptr, // no learned pos embd
  20. add_pos);
  21. // mistral small 3.1 patch merger
  22. // ref: https://github.com/huggingface/transformers/blob/7a3e208892c06a5e278144eaf38c8599a42f53e7/src/transformers/models/mistral3/modeling_mistral3.py#L67
  23. if (model.mm_patch_merger_w) {
  24. GGML_ASSERT(hparams.n_merge > 0);
  25. cur = ggml_mul(ctx0, ggml_rms_norm(ctx0, cur, eps), model.mm_input_norm_w);
  26. // reshape image tokens to 2D grid
  27. cur = ggml_reshape_3d(ctx0, cur, n_embd, n_patches_x, n_patches_y);
  28. cur = ggml_permute(ctx0, cur, 2, 0, 1, 3); // [x, y, n_embd]
  29. cur = ggml_cont(ctx0, cur);
  30. // torch.nn.functional.unfold is just an im2col under the hood
  31. // we just need a dummy kernel to make it work
  32. ggml_tensor * kernel = ggml_view_3d(ctx0, cur, n_merge, n_merge, cur->ne[2], 0, 0, 0);
  33. cur = ggml_im2col(ctx0, kernel, cur, n_merge, n_merge, 0, 0, 1, 1, true, inp->type);
  34. // project to n_embd
  35. cur = ggml_reshape_2d(ctx0, cur, cur->ne[0], cur->ne[1] * cur->ne[2]);
  36. cur = ggml_mul_mat(ctx0, model.mm_patch_merger_w, cur);
  37. }
  38. // LlavaMultiModalProjector (always using GELU activation)
  39. {
  40. cur = build_ffn(cur,
  41. model.mm_1_w, model.mm_1_b,
  42. nullptr, nullptr,
  43. model.mm_2_w, model.mm_2_b,
  44. FFN_GELU,
  45. -1);
  46. }
  47. // arrangement of the [IMG_BREAK] token
  48. if (model.token_embd_img_break) {
  49. // not efficient, but works
  50. // the trick is to view the embeddings as a 3D tensor with shape [n_embd, n_patches_per_row, n_rows]
  51. // and then concatenate the [IMG_BREAK] token to the end of each row, aka n_patches_per_row dimension
  52. // after the concatenation, we have a tensor with shape [n_embd, n_patches_per_row + 1, n_rows]
  53. const int p_y = n_merge > 0 ? n_patches_y / n_merge : n_patches_y;
  54. const int p_x = n_merge > 0 ? n_patches_x / n_merge : n_patches_x;
  55. const int p_total = p_x * p_y;
  56. const int n_embd_text = cur->ne[0];
  57. const int n_tokens_output = p_total + p_y - 1; // one [IMG_BREAK] per row, except the last row
  58. ggml_tensor * tmp = ggml_reshape_3d(ctx0, cur, n_embd_text, p_x, p_y);
  59. ggml_tensor * tok = ggml_new_tensor_3d(ctx0, tmp->type, n_embd_text, 1, p_y);
  60. tok = ggml_scale(ctx0, tok, 0.0); // clear the tensor
  61. tok = ggml_add(ctx0, tok, model.token_embd_img_break);
  62. tmp = ggml_concat(ctx0, tmp, tok, 1);
  63. cur = ggml_view_2d(ctx0, tmp,
  64. n_embd_text, n_tokens_output,
  65. ggml_row_size(tmp->type, n_embd_text), 0);
  66. }
  67. // build the graph
  68. ggml_build_forward_expand(gf, cur);
  69. return gf;
  70. }