clip.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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_f32;
  28. struct clip_image_u8_batch;
  29. struct clip_image_f32_batch;
  30. struct clip_context_params {
  31. bool use_gpu;
  32. enum ggml_log_level verbosity;
  33. };
  34. // deprecated, use clip_init
  35. CLIP_API struct clip_ctx * clip_model_load(const char * fname, int verbosity);
  36. CLIP_API struct clip_ctx * clip_init(const char * fname, struct clip_context_params ctx_params);
  37. CLIP_API void clip_free(struct clip_ctx * ctx);
  38. CLIP_API size_t clip_embd_nbytes(const struct clip_ctx * ctx);
  39. CLIP_API size_t clip_embd_nbytes_by_img(const struct clip_ctx * ctx, int img_h, int img_w);
  40. CLIP_API int32_t clip_get_image_size (const struct clip_ctx * ctx);
  41. CLIP_API int32_t clip_get_patch_size (const struct clip_ctx * ctx);
  42. CLIP_API int32_t clip_get_hidden_size(const struct clip_ctx * ctx);
  43. // TODO: should be enum, not string
  44. CLIP_API const char * clip_patch_merge_type(const struct clip_ctx * ctx);
  45. CLIP_API const int32_t * clip_image_grid(const struct clip_ctx * ctx);
  46. CLIP_API size_t get_clip_image_grid_size(const struct clip_ctx * ctx);
  47. CLIP_API int clip_n_patches (const struct clip_ctx * ctx);
  48. CLIP_API int clip_n_patches_by_img (const struct clip_ctx * ctx, struct clip_image_f32 * img);
  49. CLIP_API int clip_n_mmproj_embd (const struct clip_ctx * ctx);
  50. CLIP_API int clip_uhd_num_image_embeds_col(struct clip_ctx * ctx_clip);
  51. CLIP_API void clip_add_load_image_size(struct clip_ctx * ctx_clip, struct clip_image_size * load_image_size);
  52. CLIP_API struct clip_image_size * clip_get_load_image_size(struct clip_ctx * ctx_clip);
  53. CLIP_API struct clip_image_size * clip_image_size_init();
  54. CLIP_API struct clip_image_u8 * clip_image_u8_init ();
  55. CLIP_API struct clip_image_f32 * clip_image_f32_init();
  56. CLIP_API struct clip_image_f32_batch * clip_image_f32_batch_init(); // only used by libllava
  57. // nx, ny are the output image dimensions
  58. CLIP_API unsigned char * clip_image_u8_get_data(struct clip_image_u8 * img, uint32_t * nx, uint32_t * ny);
  59. CLIP_API void clip_image_size_free (struct clip_image_size * img_size);
  60. CLIP_API void clip_image_u8_free (struct clip_image_u8 * img);
  61. CLIP_API void clip_image_f32_free(struct clip_image_f32 * img);
  62. CLIP_API void clip_image_u8_batch_free (struct clip_image_u8_batch * batch);
  63. CLIP_API void clip_image_f32_batch_free(struct clip_image_f32_batch * batch);
  64. // use for accessing underlay data of clip_image_f32_batch
  65. CLIP_API size_t clip_image_f32_batch_n_images(const struct clip_image_f32_batch * batch); // equivalent to batch->size()
  66. CLIP_API size_t clip_image_f32_batch_nx(const struct clip_image_f32_batch * batch, int idx); // equivalent to batch[idx]->nx
  67. CLIP_API size_t clip_image_f32_batch_ny(const struct clip_image_f32_batch * batch, int idx); // equivalent to batch[idx]->ny
  68. CLIP_API struct clip_image_f32 * clip_image_f32_get_img(const struct clip_image_f32_batch * batch, int idx); // equivalent to batch[idx]->data
  69. /**
  70. * Build image from pixels decoded by other libraries instead of stb_image.h for better performance.
  71. * The memory layout is RGBRGBRGB..., input buffer length must be 3*nx*ny bytes
  72. */
  73. CLIP_API void clip_build_img_from_pixels(const unsigned char * rgb_pixels, int nx, int ny, struct clip_image_u8 * img);
  74. CLIP_API bool clip_image_load_from_file(const char * fname, struct clip_image_u8 * img);
  75. /** interpret bytes as an image file with length bytes_length, and use the result to populate img */
  76. CLIP_API bool clip_image_load_from_bytes(const unsigned char * bytes, size_t bytes_length, struct clip_image_u8 * img);
  77. /** preprocess img and store the result in res_imgs, pad_to_square may be overridden to false depending on model configuration */
  78. CLIP_API bool clip_image_preprocess(struct clip_ctx * ctx, const struct clip_image_u8 * img, struct clip_image_f32_batch * res_imgs );
  79. CLIP_API struct ggml_tensor * clip_get_newline_tensor(const struct clip_ctx * ctx);
  80. CLIP_API bool clip_image_encode (struct clip_ctx * ctx, int n_threads, struct clip_image_f32 * img, float * vec);
  81. CLIP_API bool clip_image_batch_encode(struct clip_ctx * ctx, int n_threads, const struct clip_image_f32_batch * imgs, float * vec);
  82. CLIP_API bool clip_model_quantize(const char * fname_inp, const char * fname_out, int itype);
  83. CLIP_API int clip_is_minicpmv(const struct clip_ctx * ctx);
  84. CLIP_API bool clip_is_glm(const struct clip_ctx * ctx);
  85. CLIP_API bool clip_is_qwen2vl(const struct clip_ctx * ctx);
  86. CLIP_API bool clip_is_llava(const struct clip_ctx * ctx);
  87. CLIP_API bool clip_is_gemma3(const struct clip_ctx * ctx);
  88. CLIP_API int get_deepest_feature_layer(const struct clip_ctx * ctx);
  89. CLIP_API bool clip_encode_float_image (struct clip_ctx * ctx, int n_threads, float * img, int h, int w, float * vec);
  90. #ifdef __cplusplus
  91. }
  92. #endif
  93. #endif // CLIP_H