1
0

clip.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #ifndef CLIP_H
  2. #define CLIP_H
  3. #include <stddef.h>
  4. #include <stdint.h>
  5. #ifdef LLAMA_SHARED
  6. # if defined(_WIN32) && !defined(__MINGW32__)
  7. # ifdef LLAMA_BUILD
  8. # define CLIP_API __declspec(dllexport)
  9. # else
  10. # define CLIP_API __declspec(dllimport)
  11. # endif
  12. # else
  13. # define CLIP_API __attribute__ ((visibility ("default")))
  14. # endif
  15. #else
  16. # define CLIP_API
  17. #endif
  18. struct clip_ctx;
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. struct clip_vision_hparams {
  23. int32_t image_size;
  24. int32_t patch_size;
  25. int32_t hidden_size;
  26. int32_t n_intermediate;
  27. int32_t projection_dim;
  28. int32_t n_head;
  29. int32_t n_layer;
  30. float eps;
  31. };
  32. /** load mmproj model */
  33. CLIP_API struct clip_ctx * clip_model_load(const char * fname, const int verbosity);
  34. /** free mmproj model */
  35. CLIP_API void clip_free(struct clip_ctx * ctx);
  36. size_t clip_embd_nbytes(const struct clip_ctx * ctx);
  37. int clip_n_patches(const struct clip_ctx * ctx);
  38. int clip_n_mmproj_embd(const struct clip_ctx * ctx);
  39. // RGB uint8 image
  40. struct clip_image_u8 {
  41. int nx;
  42. int ny;
  43. uint8_t * data = NULL;
  44. size_t size;
  45. };
  46. // RGB float32 image (NHWC)
  47. // Memory layout: RGBRGBRGB...
  48. struct clip_image_f32 {
  49. int nx;
  50. int ny;
  51. float * data = NULL;
  52. size_t size;
  53. };
  54. struct clip_image_u8_batch {
  55. struct clip_image_u8 * data;
  56. size_t size;
  57. };
  58. struct clip_image_f32_batch {
  59. struct clip_image_f32 * data;
  60. size_t size;
  61. };
  62. struct clip_image_u8 * make_clip_image_u8();
  63. struct clip_image_f32 * make_clip_image_f32();
  64. CLIP_API void clip_image_u8_free(clip_image_u8 * img);
  65. CLIP_API void clip_image_f32_free(clip_image_f32 * img);
  66. CLIP_API bool clip_image_load_from_file(const char * fname, struct clip_image_u8 * img);
  67. /** interpret bytes as an image file with length bytes_length, and use the result to populate img */
  68. CLIP_API bool clip_image_load_from_bytes(const unsigned char * bytes, size_t bytes_length, struct clip_image_u8 * img);
  69. bool clip_image_preprocess(const struct clip_ctx * ctx, const struct clip_image_u8 * img, struct clip_image_f32 * res, const bool pad2square);
  70. bool clip_image_encode(const struct clip_ctx * ctx, const int n_threads, struct clip_image_f32 * img, float * vec);
  71. bool clip_image_batch_encode(const struct clip_ctx * ctx, const int n_threads, const struct clip_image_f32_batch * imgs,
  72. float * vec);
  73. bool clip_model_quantize(const char * fname_inp, const char * fname_out, const int itype);
  74. #ifdef __cplusplus
  75. }
  76. #endif
  77. #endif // CLIP_H