clip-graph.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #pragma once
  2. #include "ggml.h"
  3. #include "ggml-cpp.h"
  4. #include "clip.h"
  5. #include "clip-impl.h"
  6. #include "clip-model.h"
  7. #include <vector>
  8. #include <functional>
  9. struct clip_graph {
  10. const clip_model & model;
  11. const clip_hparams & hparams;
  12. projector_type proj_type;
  13. // we only support single image per batch
  14. const clip_image_f32 & img;
  15. const int patch_size;
  16. const int n_patches_x;
  17. const int n_patches_y;
  18. const int n_patches;
  19. const int n_embd;
  20. const int n_head;
  21. const int d_head;
  22. const int n_layer;
  23. const int n_mmproj_embd;
  24. const float eps;
  25. const float kq_scale;
  26. const clip_flash_attn_type flash_attn_type;
  27. // for debugging
  28. const bool debug_graph;
  29. std::vector<ggml_tensor *> & debug_print_tensors;
  30. ggml_context_ptr ctx0_ptr;
  31. ggml_context * ctx0;
  32. ggml_cgraph * gf;
  33. clip_graph(clip_ctx * ctx, const clip_image_f32 & img);
  34. virtual ~clip_graph() = default;
  35. virtual ggml_cgraph * build() = 0;
  36. //
  37. // utility functions
  38. //
  39. void cb(ggml_tensor * cur0, const char * name, int il) const;
  40. // siglip2 naflex
  41. ggml_tensor * resize_position_embeddings();
  42. // build vision transformer (ViT) cgraph
  43. // this function should cover most of the models
  44. // if your model has specific features, you should probably duplicate this function
  45. ggml_tensor * build_vit(
  46. ggml_tensor * inp,
  47. int64_t n_pos,
  48. norm_type norm_t,
  49. ffn_op_type ffn_t,
  50. ggml_tensor * learned_pos_embd,
  51. std::function<ggml_tensor *(ggml_tensor *, const clip_layer &)> add_pos);
  52. // build the input after conv2d (inp_raw --> patches)
  53. // returns tensor with shape [n_embd, n_patches]
  54. ggml_tensor * build_inp();
  55. ggml_tensor * build_inp_raw(int channels = 3);
  56. ggml_tensor * build_norm(
  57. ggml_tensor * cur,
  58. ggml_tensor * mw,
  59. ggml_tensor * mb,
  60. norm_type type,
  61. float norm_eps,
  62. int il) const;
  63. ggml_tensor * build_ffn(
  64. ggml_tensor * cur,
  65. ggml_tensor * up,
  66. ggml_tensor * up_b,
  67. ggml_tensor * gate,
  68. ggml_tensor * gate_b,
  69. ggml_tensor * down,
  70. ggml_tensor * down_b,
  71. ffn_op_type type_op,
  72. int il) const;
  73. ggml_tensor * build_attn(
  74. ggml_tensor * wo,
  75. ggml_tensor * wo_b,
  76. ggml_tensor * q_cur,
  77. ggml_tensor * k_cur,
  78. ggml_tensor * v_cur,
  79. ggml_tensor * kq_mask,
  80. float kq_scale,
  81. int il) const;
  82. // implementation of the 2D RoPE without adding a new op in ggml
  83. // this is not efficient (use double the memory), but works on all backends
  84. // TODO: there was a more efficient which relies on ggml_view and ggml_rope_ext_inplace, but the rope inplace does not work well with non-contiguous tensors ; we should fix that and revert back to the original implementation in https://github.com/ggml-org/llama.cpp/pull/13065
  85. ggml_tensor * build_rope_2d(
  86. ggml_context * ctx0,
  87. ggml_tensor * cur,
  88. ggml_tensor * pos_a, // first half
  89. ggml_tensor * pos_b, // second half
  90. const float freq_base,
  91. const bool interleave_freq
  92. );
  93. // aka pixel_shuffle / pixel_unshuffle / patch_merger (Kimi-VL)
  94. // support dynamic resolution
  95. ggml_tensor * build_patch_merge_permute(ggml_tensor * cur, int scale_factor);
  96. // Generic function to stack frames for audio processing
  97. // Abstracts out the StackAudioFrames logic used by ultravox
  98. ggml_tensor * build_stack(ggml_tensor * cur, int32_t stack_factor, int32_t n_embed);
  99. };