clip.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #ifndef CLIP_H
  2. #define CLIP_H
  3. #include "ggml.h"
  4. #include <stddef.h>
  5. #include <stdint.h>
  6. #ifdef LLAMA_SHARED
  7. # if defined(_WIN32) && !defined(__MINGW32__)
  8. # ifdef LLAMA_BUILD
  9. # define CLIP_API __declspec(dllexport)
  10. # else
  11. # define CLIP_API __declspec(dllimport)
  12. # endif
  13. # else
  14. # define CLIP_API __attribute__ ((visibility ("default")))
  15. # endif
  16. #else
  17. # define CLIP_API
  18. #endif
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. struct clip_ctx;
  23. struct clip_image_size {
  24. int width;
  25. int height;
  26. };
  27. struct clip_image_u8_batch {
  28. struct clip_image_u8 * data;
  29. size_t size;
  30. };
  31. struct clip_image_f32_batch {
  32. struct clip_image_f32 * data;
  33. size_t size;
  34. };
  35. struct clip_context_params {
  36. bool use_gpu;
  37. ggml_log_level verbosity;
  38. };
  39. // deprecated, use clip_init
  40. CLIP_API struct clip_ctx * clip_model_load(const char * fname, int verbosity);
  41. CLIP_API struct clip_ctx * clip_init(const char * fname, struct clip_context_params ctx_params);
  42. CLIP_API void clip_free(struct clip_ctx * ctx);
  43. CLIP_API size_t clip_embd_nbytes(const struct clip_ctx * ctx);
  44. CLIP_API size_t clip_embd_nbytes_by_img(const struct clip_ctx * ctx, int img_h, int img_w);
  45. CLIP_API int32_t clip_image_size (const struct clip_ctx * ctx);
  46. CLIP_API int32_t clip_patch_size (const struct clip_ctx * ctx);
  47. CLIP_API int32_t clip_hidden_size(const struct clip_ctx * ctx);
  48. // TODO: should be enum, not string
  49. CLIP_API const char * clip_patch_merge_type(const struct clip_ctx * ctx);
  50. CLIP_API const int32_t * clip_image_grid(const struct clip_ctx * ctx);
  51. CLIP_API size_t get_clip_image_grid_size(const struct clip_ctx * ctx);
  52. CLIP_API int clip_n_patches (const struct clip_ctx * ctx);
  53. CLIP_API int clip_n_patches_by_img (const struct clip_ctx * ctx, struct clip_image_f32 * img);
  54. CLIP_API int clip_n_mmproj_embd (const struct clip_ctx * ctx);
  55. CLIP_API int clip_uhd_num_image_embeds_col(struct clip_ctx * ctx_clip);
  56. CLIP_API void clip_add_load_image_size(struct clip_ctx * ctx_clip, struct clip_image_size * load_image_size);
  57. CLIP_API struct clip_image_size * clip_get_load_image_size(struct clip_ctx * ctx_clip);
  58. CLIP_API struct clip_image_size * clip_image_size_init();
  59. CLIP_API struct clip_image_u8 * clip_image_u8_init ();
  60. CLIP_API struct clip_image_f32 * clip_image_f32_init();
  61. CLIP_API void clip_image_u8_free (struct clip_image_u8 * img);
  62. CLIP_API void clip_image_f32_free(struct clip_image_f32 * img);
  63. CLIP_API void clip_image_u8_batch_free (struct clip_image_u8_batch * batch);
  64. CLIP_API void clip_image_f32_batch_free(struct clip_image_f32_batch * batch);
  65. /**
  66. * Build image from pixels decoded by other libraries instead of stb_image.h for better performance.
  67. * The memory layout is RGBRGBRGB..., input buffer length must be 3*nx*ny bytes
  68. */
  69. CLIP_API void clip_build_img_from_pixels(const unsigned char * rgb_pixels, int nx, int ny, struct clip_image_u8 * img);
  70. CLIP_API bool clip_image_load_from_file(const char * fname, struct clip_image_u8 * img);
  71. /** interpret bytes as an image file with length bytes_length, and use the result to populate img */
  72. CLIP_API bool clip_image_load_from_bytes(const unsigned char * bytes, size_t bytes_length, struct clip_image_u8 * img);
  73. /** preprocess img and store the result in res_imgs, pad_to_square may be overridden to false depending on model configuration */
  74. CLIP_API bool clip_image_preprocess(struct clip_ctx * ctx, const struct clip_image_u8 * img, struct clip_image_f32_batch * res_imgs );
  75. CLIP_API struct ggml_tensor * clip_get_newline_tensor(const struct clip_ctx * ctx);
  76. CLIP_API bool clip_image_encode (struct clip_ctx * ctx, int n_threads, struct clip_image_f32 * img, float * vec);
  77. CLIP_API bool clip_image_batch_encode(struct clip_ctx * ctx, int n_threads, const struct clip_image_f32_batch * imgs, float * vec);
  78. CLIP_API bool clip_model_quantize(const char * fname_inp, const char * fname_out, int itype);
  79. CLIP_API int clip_is_minicpmv(const struct clip_ctx * ctx);
  80. CLIP_API bool clip_is_glm(const struct clip_ctx * ctx);
  81. CLIP_API bool clip_is_qwen2vl(const struct clip_ctx * ctx);
  82. CLIP_API int get_deepest_feature_layer(const struct clip_ctx * ctx);
  83. CLIP_API bool clip_encode_float_image (struct clip_ctx * ctx, int n_threads, float * img, int h, int w, float * vec);
  84. #ifdef __cplusplus
  85. }
  86. #endif
  87. #endif // CLIP_H