mtmd.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #ifndef MTMD_H
  2. #define MTMD_H
  3. #include "ggml.h"
  4. #include "llama.h"
  5. #include "clip.h"
  6. #include <vector>
  7. #include <cinttypes>
  8. #include <memory>
  9. #ifdef LLAMA_SHARED
  10. # if defined(_WIN32) && !defined(__MINGW32__)
  11. # ifdef LLAMA_BUILD
  12. # define MTMD_API __declspec(dllexport)
  13. # else
  14. # define MTMD_API __declspec(dllimport)
  15. # endif
  16. # else
  17. # define MTMD_API __attribute__ ((visibility ("default")))
  18. # endif
  19. #else
  20. # define MTMD_API
  21. #endif
  22. #ifdef __cplusplus
  23. enum mtmd_input_chunk_type {
  24. MTMD_INPUT_CHUNK_TYPE_TEXT,
  25. MTMD_INPUT_CHUNK_TYPE_IMAGE,
  26. };
  27. struct mtmd_context;
  28. struct mtmd_image_tokens;
  29. // represents raw image data, layout is RGBRGBRGB...
  30. // length of data must be nx * ny * 3
  31. struct mtmd_bitmap {
  32. uint32_t nx;
  33. uint32_t ny;
  34. std::vector<unsigned char> data;
  35. std::string id; // optional user-defined id, for ex: can be set to image hash, useful for KV cache tracking
  36. };
  37. struct mtmd_image_tokens_deleter {
  38. void operator()(mtmd_image_tokens * val); // forward declaration
  39. };
  40. using mtmd_image_tokens_ptr = std::unique_ptr<mtmd_image_tokens, mtmd_image_tokens_deleter>;
  41. struct mtmd_input_chunk {
  42. mtmd_input_chunk_type type;
  43. std::vector<llama_token> tokens_text;
  44. mtmd_image_tokens_ptr tokens_image;
  45. };
  46. using mtmd_input_chunks = std::vector<mtmd_input_chunk>;
  47. struct mtmd_context_params {
  48. bool use_gpu = true;
  49. bool print_timings = true;
  50. int n_threads = 4;
  51. enum ggml_log_level verbosity = GGML_LOG_LEVEL_INFO;
  52. const char * image_marker = "<__image__>";
  53. };
  54. struct mtmd_input_text {
  55. std::string text;
  56. bool add_special;
  57. bool parse_special;
  58. };
  59. // initialize the mtmd context
  60. // return nullptr on failure
  61. MTMD_API mtmd_context * mtmd_init_from_file(const char * mmproj_fname,
  62. const llama_model * text_model,
  63. const mtmd_context_params ctx_params);
  64. MTMD_API void mtmd_free(mtmd_context * ctx);
  65. // tokenize an input text prompt and an image
  66. // the prompt must have the input image marker (default: "<__image__>") in it
  67. // the marker will be replaced with the image tokens
  68. // for example:
  69. // "here is an image: <__image__>\ndescribe it in detail."
  70. // this will gives 3 chunks:
  71. // 1. "here is an image: <start_of_image>"
  72. // 2. (image tokens)
  73. // 3. "<end_of_image>\ndescribe it in detail."
  74. // number of bitmaps must be equal to the number of image markers in the prompt
  75. // this function is thread-safe (shared ctx)
  76. // return values:
  77. // 0 on success
  78. // 1 on number of images not matching the number of markers
  79. // 2 on image preprocessing error
  80. MTMD_API int32_t mtmd_tokenize(mtmd_context * ctx,
  81. std::vector<mtmd_input_chunk> & output,
  82. const mtmd_input_text & text,
  83. const std::vector<mtmd_bitmap> & bitmaps);
  84. // access mtmd_image_tokens
  85. MTMD_API size_t mtmd_image_tokens_get_n_tokens(const mtmd_image_tokens * image_tokens);
  86. MTMD_API size_t mtmd_image_tokens_get_nx(const mtmd_image_tokens * image_tokens);
  87. MTMD_API size_t mtmd_image_tokens_get_ny(const mtmd_image_tokens * image_tokens);
  88. MTMD_API std::string mtmd_image_tokens_get_id(const mtmd_image_tokens * image_tokens);
  89. MTMD_API void mtmd_image_tokens_free(mtmd_image_tokens * image_tokens);
  90. // returns 0 on success
  91. MTMD_API int32_t mtmd_encode(mtmd_context * ctx,
  92. const mtmd_image_tokens * image_tokens);
  93. // get output embeddings from the last encode pass
  94. MTMD_API float * mtmd_get_output_embd(mtmd_context * ctx);
  95. // whether we need to set non-causal mask before llama_decode
  96. MTMD_API bool mtmd_decode_use_non_causal(mtmd_context * ctx);
  97. //
  98. // helper functions (can be implemented based on other functions)
  99. //
  100. // helper to count the total number of tokens from a list of chunks, useful to keep track of n_past
  101. MTMD_API size_t mtmd_helper_get_n_tokens(mtmd_input_chunks & chunks);
  102. // helper function that automatically:
  103. // 1. run llama_decode() on text chunks
  104. // 2. run mtmd_encode() on image chunks, then mtmd_get_output_embd() and then llama_decode()
  105. // if any of the mtmd_encode() or llama_decode() calls return non-zero, stop and forward the error
  106. // otherwise, returns 0 on success
  107. MTMD_API int32_t mtmd_helper_eval(mtmd_context * ctx,
  108. llama_context * lctx,
  109. mtmd_input_chunks & chunks,
  110. llama_pos pos0,
  111. llama_seq_id seq_id,
  112. int32_t n_batch);
  113. // helper function to construct a mtmd_bitmap from a file
  114. // returns 0 on success
  115. // this function is thread-safe
  116. MTMD_API int32_t mtmd_helper_bitmap_init_from_file(const char * fname, mtmd_bitmap & output);
  117. // helper function to construct a mtmd_bitmap from a buffer
  118. // the buffer must be an image in format supported by stb_image (jpg, png, bmp, gif, etc.)
  119. // returns 0 on success
  120. // this function is thread-safe
  121. MTMD_API int32_t mtmd_helper_bitmap_init_from_buf(const unsigned char * buf, size_t len, mtmd_bitmap & output);
  122. // convenient unique_ptr wrappers
  123. struct mtmd_context_deleter {
  124. void operator()(mtmd_context * val) { mtmd_free(val); }
  125. };
  126. using mtmd_context_ptr = std::unique_ptr<mtmd_context, mtmd_context_deleter>;
  127. #else
  128. static_assert(false && "C header is not yet supported by this library");
  129. #endif
  130. #endif