llama4.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include "models.h"
  2. ggml_cgraph * clip_graph_llama4::build() {
  3. GGML_ASSERT(model.class_embedding != nullptr);
  4. GGML_ASSERT(model.position_embeddings != nullptr);
  5. const int n_pos = n_patches + 1; // +1 for [CLS]
  6. // 2D input positions
  7. ggml_tensor * pos_h = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, n_pos);
  8. ggml_set_name(pos_h, "pos_h");
  9. ggml_set_input(pos_h);
  10. ggml_tensor * pos_w = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, n_pos);
  11. ggml_set_name(pos_w, "pos_w");
  12. ggml_set_input(pos_w);
  13. ggml_tensor * inp = build_inp_raw();
  14. // Llama4UnfoldConvolution
  15. {
  16. ggml_tensor * kernel = ggml_reshape_4d(ctx0, model.patch_embeddings_0,
  17. patch_size, patch_size, 3, n_embd);
  18. inp = ggml_im2col(ctx0, kernel, inp, patch_size, patch_size, 0, 0, 1, 1, true, inp->type);
  19. inp = ggml_mul_mat(ctx0, model.patch_embeddings_0, inp);
  20. inp = ggml_reshape_2d(ctx0, inp, n_embd, n_patches);
  21. cb(inp, "patch_conv", -1);
  22. }
  23. // add CLS token
  24. inp = ggml_concat(ctx0, inp, model.class_embedding, 1);
  25. // build ViT with 2D position embeddings
  26. auto add_pos = [&](ggml_tensor * cur, const clip_layer &) {
  27. // first half is X axis and second half is Y axis
  28. // ref: https://github.com/huggingface/transformers/blob/40a493c7ed4f19f08eadb0639cf26d49bfa5e180/src/transformers/models/llama4/modeling_llama4.py#L1312
  29. // ref: https://github.com/Blaizzy/mlx-vlm/blob/a57156aa87b33cca6e5ee6cfc14dd4ef8f611be6/mlx_vlm/models/llama4/vision.py#L441
  30. return build_rope_2d(ctx0, cur, pos_w, pos_h, hparams.rope_theta, false);
  31. };
  32. ggml_tensor * cur = build_vit(
  33. inp, n_pos,
  34. NORM_TYPE_NORMAL,
  35. hparams.ffn_op,
  36. model.position_embeddings,
  37. add_pos);
  38. // remove CLS token
  39. cur = ggml_view_2d(ctx0, cur,
  40. n_embd, n_patches,
  41. ggml_row_size(cur->type, n_embd), 0);
  42. // pixel shuffle
  43. // based on Llama4VisionPixelShuffleMLP
  44. // https://github.com/huggingface/transformers/blob/2932f318a20d9e54cc7aea052e040164d85de7d6/src/transformers/models/llama4/modeling_llama4.py#L1151
  45. {
  46. const int scale_factor = model.hparams.n_merge;
  47. const int bsz = 1; // batch size, always 1 for now since we don't support batching
  48. GGML_ASSERT(scale_factor > 0);
  49. GGML_ASSERT(n_patches_x == n_patches_y); // llama4 only supports square images
  50. cur = ggml_reshape_4d(ctx0, cur,
  51. n_embd * scale_factor,
  52. n_patches_x / scale_factor,
  53. n_patches_y,
  54. bsz);
  55. cur = ggml_permute(ctx0, cur, 0, 2, 1, 3);
  56. cur = ggml_cont_4d(ctx0, cur,
  57. n_embd * scale_factor * scale_factor,
  58. n_patches_x / scale_factor,
  59. n_patches_y / scale_factor,
  60. bsz);
  61. //cur = ggml_permute(ctx0, cur, 0, 2, 1, 3);
  62. // flatten to 2D
  63. cur = ggml_cont_2d(ctx0, cur,
  64. n_embd * scale_factor * scale_factor,
  65. n_patches / scale_factor / scale_factor);
  66. cb(cur, "pixel_shuffle", -1);
  67. }
  68. // based on Llama4VisionMLP2 (always uses GELU activation, no bias)
  69. {
  70. cur = ggml_mul_mat(ctx0, model.mm_model_mlp_1_w, cur);
  71. cur = ggml_gelu(ctx0, cur);
  72. cur = ggml_mul_mat(ctx0, model.mm_model_mlp_2_w, cur);
  73. cur = ggml_gelu(ctx0, cur);
  74. cb(cur, "adapter_mlp", -1);
  75. }
  76. // Llama4MultiModalProjector
  77. cur = ggml_mul_mat(ctx0, model.mm_model_proj, cur);
  78. cb(cur, "projected", -1);
  79. // build the graph
  80. ggml_build_forward_expand(gf, cur);
  81. return gf;
  82. }