clip-graph.h 3.7 KB

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