1
0

clip.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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_w, int img_h);
  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. GGML_DEPRECATED(CLIP_API int clip_n_patches(const struct clip_ctx * ctx),
  48. "use clip_n_output_tokens instead");
  49. GGML_DEPRECATED(CLIP_API int clip_n_patches_by_img(const struct clip_ctx * ctx, struct clip_image_f32 * img),
  50. "use clip_n_output_tokens instead");
  51. CLIP_API int clip_n_output_tokens(const struct clip_ctx * ctx, struct clip_image_f32 * img);
  52. // for M-RoPE, this will be the number of token positions in X and Y directions
  53. // for other models, X will be the total number of tokens and Y will be 1
  54. CLIP_API int clip_n_output_tokens_x(const struct clip_ctx * ctx, struct clip_image_f32 * img);
  55. CLIP_API int clip_n_output_tokens_y(const struct clip_ctx * ctx, struct clip_image_f32 * img);
  56. // this should be equal to the embedding dimension of the text model
  57. CLIP_API int clip_n_mmproj_embd(const struct clip_ctx * ctx);
  58. CLIP_API int clip_uhd_num_image_embeds_col(struct clip_ctx * ctx_clip);
  59. CLIP_API void clip_add_load_image_size(struct clip_ctx * ctx_clip, struct clip_image_size * load_image_size);
  60. CLIP_API struct clip_image_size * clip_get_load_image_size(struct clip_ctx * ctx_clip);
  61. CLIP_API struct clip_image_size * clip_image_size_init(void);
  62. CLIP_API struct clip_image_u8 * clip_image_u8_init (void);
  63. CLIP_API struct clip_image_f32 * clip_image_f32_init(void);
  64. CLIP_API struct clip_image_f32_batch * clip_image_f32_batch_init(void); // only used by libllava
  65. // nx, ny are the output image dimensions
  66. CLIP_API unsigned char * clip_image_u8_get_data(struct clip_image_u8 * img, uint32_t * nx, uint32_t * ny);
  67. CLIP_API void clip_image_size_free (struct clip_image_size * img_size);
  68. CLIP_API void clip_image_u8_free (struct clip_image_u8 * img);
  69. CLIP_API void clip_image_f32_free(struct clip_image_f32 * img);
  70. CLIP_API void clip_image_u8_batch_free (struct clip_image_u8_batch * batch);
  71. CLIP_API void clip_image_f32_batch_free(struct clip_image_f32_batch * batch);
  72. // use for accessing underlay data of clip_image_f32_batch
  73. CLIP_API size_t clip_image_f32_batch_n_images(const struct clip_image_f32_batch * batch); // equivalent to batch->size()
  74. CLIP_API size_t clip_image_f32_batch_nx(const struct clip_image_f32_batch * batch, int idx); // equivalent to batch[idx]->nx
  75. CLIP_API size_t clip_image_f32_batch_ny(const struct clip_image_f32_batch * batch, int idx); // equivalent to batch[idx]->ny
  76. 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
  77. /**
  78. * Build image from pixels decoded by other libraries instead of stb_image.h for better performance.
  79. * The memory layout is RGBRGBRGB..., input buffer length must be 3*nx*ny bytes
  80. */
  81. CLIP_API void clip_build_img_from_pixels(const unsigned char * rgb_pixels, int nx, int ny, struct clip_image_u8 * img);
  82. CLIP_API bool clip_image_load_from_file(const char * fname, struct clip_image_u8 * img);
  83. /** interpret bytes as an image file with length bytes_length, and use the result to populate img */
  84. CLIP_API bool clip_image_load_from_bytes(const unsigned char * bytes, size_t bytes_length, struct clip_image_u8 * img);
  85. /** preprocess img and store the result in res_imgs, pad_to_square may be overridden to false depending on model configuration */
  86. CLIP_API bool clip_image_preprocess(struct clip_ctx * ctx, const struct clip_image_u8 * img, struct clip_image_f32_batch * res_imgs );
  87. CLIP_API struct ggml_tensor * clip_get_newline_tensor(const struct clip_ctx * ctx);
  88. CLIP_API bool clip_image_encode (struct clip_ctx * ctx, int n_threads, struct clip_image_f32 * img, float * vec);
  89. CLIP_API bool clip_image_batch_encode(struct clip_ctx * ctx, int n_threads, const struct clip_image_f32_batch * imgs, float * vec);
  90. CLIP_API bool clip_model_quantize(const char * fname_inp, const char * fname_out, int itype);
  91. CLIP_API int clip_is_minicpmv(const struct clip_ctx * ctx);
  92. CLIP_API bool clip_is_glm(const struct clip_ctx * ctx);
  93. CLIP_API bool clip_is_qwen2vl(const struct clip_ctx * ctx);
  94. CLIP_API bool clip_is_llava(const struct clip_ctx * ctx);
  95. CLIP_API bool clip_is_gemma3(const struct clip_ctx * ctx);
  96. CLIP_API bool clip_encode_float_image (struct clip_ctx * ctx, int n_threads, float * img, int h, int w, float * vec);
  97. #ifdef __cplusplus
  98. }
  99. #endif
  100. #endif // CLIP_H