mtmd.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. #ifndef MTMD_H
  2. #define MTMD_H
  3. #include "ggml.h"
  4. #include "llama.h"
  5. #include "clip.h"
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <stdbool.h>
  9. #ifdef __cplusplus
  10. #include <string>
  11. #include <vector>
  12. #include <cinttypes>
  13. #include <memory>
  14. #endif
  15. /**
  16. * libmtmd: A library for multimodal support in llama.cpp.
  17. *
  18. * WARNING: This API is experimental and subject to many BREAKING CHANGES.
  19. * Issues related to API usage may receive lower priority support.
  20. *
  21. * For the usage, see an example in mtmd-cli.cpp
  22. */
  23. #ifdef LLAMA_SHARED
  24. # if defined(_WIN32) && !defined(__MINGW32__)
  25. # ifdef LLAMA_BUILD
  26. # define MTMD_API __declspec(dllexport)
  27. # else
  28. # define MTMD_API __declspec(dllimport)
  29. # endif
  30. # else
  31. # define MTMD_API __attribute__ ((visibility ("default")))
  32. # endif
  33. #else
  34. # define MTMD_API
  35. #endif
  36. // deprecated marker, use mtmd_default_marker() instead
  37. #define MTMD_DEFAULT_IMAGE_MARKER "<__image__>"
  38. #ifdef __cplusplus
  39. extern "C" {
  40. #endif
  41. enum mtmd_input_chunk_type {
  42. MTMD_INPUT_CHUNK_TYPE_TEXT,
  43. MTMD_INPUT_CHUNK_TYPE_IMAGE,
  44. MTMD_INPUT_CHUNK_TYPE_AUDIO,
  45. };
  46. // opaque types
  47. struct mtmd_context;
  48. struct mtmd_bitmap;
  49. struct mtmd_image_tokens;
  50. struct mtmd_input_chunk;
  51. struct mtmd_input_chunks;
  52. struct mtmd_input_text {
  53. const char * text;
  54. bool add_special;
  55. bool parse_special;
  56. };
  57. //
  58. // C API
  59. //
  60. typedef struct mtmd_context mtmd_context;
  61. typedef struct mtmd_bitmap mtmd_bitmap;
  62. typedef struct mtmd_image_tokens mtmd_image_tokens;
  63. typedef struct mtmd_input_chunk mtmd_input_chunk;
  64. typedef struct mtmd_input_chunks mtmd_input_chunks;
  65. typedef struct mtmd_input_text mtmd_input_text;
  66. struct mtmd_context_params {
  67. bool use_gpu;
  68. bool print_timings;
  69. int n_threads;
  70. enum ggml_log_level verbosity;
  71. const char * image_marker; // deprecated, use media_marker instead
  72. const char * media_marker;
  73. };
  74. MTMD_API const char * mtmd_default_marker(void);
  75. MTMD_API struct mtmd_context_params mtmd_context_params_default(void);
  76. // initialize the mtmd context
  77. // return nullptr on failure
  78. MTMD_API mtmd_context * mtmd_init_from_file(const char * mmproj_fname,
  79. const struct llama_model * text_model,
  80. const struct mtmd_context_params ctx_params);
  81. MTMD_API void mtmd_free(mtmd_context * ctx);
  82. // whether we need to set non-causal mask before llama_decode
  83. MTMD_API bool mtmd_decode_use_non_causal(mtmd_context * ctx);
  84. // whether the current model use M-RoPE for llama_decode
  85. MTMD_API bool mtmd_decode_use_mrope(mtmd_context * ctx);
  86. // whether the current model supports vision input
  87. MTMD_API bool mtmd_support_vision(mtmd_context * ctx);
  88. // whether the current model supports audio input
  89. MTMD_API bool mtmd_support_audio(mtmd_context * ctx);
  90. // get audio bitrate in Hz, for example 16000 for Whisper
  91. // return -1 if audio is not supported
  92. MTMD_API int mtmd_get_audio_bitrate(mtmd_context * ctx);
  93. // mtmd_bitmap
  94. //
  95. // if bitmap is image:
  96. // length of data must be nx * ny * 3
  97. // the data is in RGBRGBRGB... format
  98. // if bitmap is audio:
  99. // length of data must be n_samples * sizeof(float)
  100. // the data is in float format (PCM F32)
  101. MTMD_API mtmd_bitmap * mtmd_bitmap_init (uint32_t nx, uint32_t ny, const unsigned char * data);
  102. MTMD_API mtmd_bitmap * mtmd_bitmap_init_from_audio(size_t n_samples, const float * data);
  103. MTMD_API uint32_t mtmd_bitmap_get_nx (const mtmd_bitmap * bitmap);
  104. MTMD_API uint32_t mtmd_bitmap_get_ny (const mtmd_bitmap * bitmap);
  105. MTMD_API const unsigned char * mtmd_bitmap_get_data (const mtmd_bitmap * bitmap);
  106. MTMD_API size_t mtmd_bitmap_get_n_bytes(const mtmd_bitmap * bitmap);
  107. MTMD_API bool mtmd_bitmap_is_audio (const mtmd_bitmap * bitmap);
  108. MTMD_API void mtmd_bitmap_free (mtmd_bitmap * bitmap);
  109. // bitmap ID is optional, but useful for KV cache tracking
  110. // these getters/setters are dedicated functions, so you can for example calculate the hash of the image based on mtmd_bitmap_get_data()
  111. MTMD_API const char * mtmd_bitmap_get_id(const mtmd_bitmap * bitmap);
  112. MTMD_API void mtmd_bitmap_set_id(mtmd_bitmap * bitmap, const char * id);
  113. // mtmd_input_chunks
  114. //
  115. // this is simply a list of mtmd_input_chunk
  116. // the elements can only be populated via mtmd_tokenize()
  117. MTMD_API mtmd_input_chunks * mtmd_input_chunks_init(void);
  118. MTMD_API size_t mtmd_input_chunks_size(const mtmd_input_chunks * chunks);
  119. MTMD_API const mtmd_input_chunk * mtmd_input_chunks_get (const mtmd_input_chunks * chunks, size_t idx);
  120. MTMD_API void mtmd_input_chunks_free(mtmd_input_chunks * chunks);
  121. // mtmd_input_chunk
  122. //
  123. // the instance will be constructed via mtmd_tokenize()
  124. // it will be freed along with mtmd_input_chunks
  125. MTMD_API enum mtmd_input_chunk_type mtmd_input_chunk_get_type (const mtmd_input_chunk * chunk);
  126. MTMD_API const llama_token * mtmd_input_chunk_get_tokens_text (const mtmd_input_chunk * chunk, size_t * n_tokens_output);
  127. MTMD_API const mtmd_image_tokens * mtmd_input_chunk_get_tokens_image(const mtmd_input_chunk * chunk);
  128. MTMD_API size_t mtmd_input_chunk_get_n_tokens (const mtmd_input_chunk * chunk);
  129. // returns nullptr for ID on text chunk
  130. MTMD_API const char * mtmd_input_chunk_get_id (const mtmd_input_chunk * chunk);
  131. // number of temporal positions (always 1 for M-RoPE, n_tokens otherwise)
  132. MTMD_API llama_pos mtmd_input_chunk_get_n_pos (const mtmd_input_chunk * chunk);
  133. // in case you want to use custom logic to handle the chunk (i.e. KV cache management)
  134. // you can move the chunk ownership to your own code by copying it
  135. // remember to free the chunk when you are done with it
  136. MTMD_API mtmd_input_chunk * mtmd_input_chunk_copy(const mtmd_input_chunk * chunk);
  137. MTMD_API void mtmd_input_chunk_free(mtmd_input_chunk * chunk);
  138. // mtmd_image_tokens
  139. //
  140. // the instance will be constructed via mtmd_tokenize()
  141. // it will be freed along with mtmd_input_chunk
  142. MTMD_API size_t mtmd_image_tokens_get_n_tokens(const mtmd_image_tokens * image_tokens); // TODO: deprecate
  143. MTMD_API size_t mtmd_image_tokens_get_nx (const mtmd_image_tokens * image_tokens);
  144. MTMD_API size_t mtmd_image_tokens_get_ny (const mtmd_image_tokens * image_tokens);
  145. MTMD_API const char * mtmd_image_tokens_get_id (const mtmd_image_tokens * image_tokens); // TODO: deprecate
  146. // number of temporal positions (always 1 for M-RoPE, n_tokens otherwise)
  147. MTMD_API llama_pos mtmd_image_tokens_get_n_pos (const mtmd_image_tokens * image_tokens); // TODO: deprecate
  148. // tokenize an input text prompt and a list of bitmaps (images/audio)
  149. // the prompt must have the input image marker (default: "<__media__>") in it
  150. // the default marker is defined by mtmd_default_marker()
  151. // the marker will be replaced with the image/audio chunk
  152. // for example:
  153. // "here is an image: <__media__>\ndescribe it in detail."
  154. // this will gives 3 chunks:
  155. // 1. "here is an image: <start_of_image>"
  156. // 2. (image/audio tokens)
  157. // 3. "<end_of_image>\ndescribe it in detail."
  158. // number of bitmaps must be equal to the number of markers in the prompt
  159. // this function is thread-safe (shared ctx)
  160. // return values:
  161. // 0 on success
  162. // 1 on number of bitmaps not matching the number of markers
  163. // 2 on image preprocessing error
  164. MTMD_API int32_t mtmd_tokenize(mtmd_context * ctx,
  165. mtmd_input_chunks * output,
  166. const mtmd_input_text * text,
  167. const mtmd_bitmap ** bitmaps,
  168. size_t n_bitmaps);
  169. // returns 0 on success
  170. // TODO: deprecate
  171. MTMD_API int32_t mtmd_encode(mtmd_context * ctx,
  172. const mtmd_image_tokens * image_tokens);
  173. // returns 0 on success
  174. MTMD_API int32_t mtmd_encode_chunk(mtmd_context * ctx,
  175. const mtmd_input_chunk * chunk);
  176. // get output embeddings from the last encode pass
  177. // the reading size (in bytes) is equal to:
  178. // llama_model_n_embd(model) * mtmd_input_chunk_get_n_tokens(chunk) * sizeof(float)
  179. MTMD_API float * mtmd_get_output_embd(mtmd_context * ctx);
  180. /////////////////////////////////////////
  181. // test function, to be used in test-mtmd-c-api.c
  182. MTMD_API mtmd_input_chunks * mtmd_test_create_input_chunks(void);
  183. #ifdef __cplusplus
  184. } // extern "C"
  185. #endif
  186. //
  187. // C++ wrappers
  188. //
  189. #ifdef __cplusplus
  190. namespace mtmd {
  191. struct mtmd_context_deleter {
  192. void operator()(mtmd_context * val) { mtmd_free(val); }
  193. };
  194. using context_ptr = std::unique_ptr<mtmd_context, mtmd_context_deleter>;
  195. struct mtmd_bitmap_deleter {
  196. void operator()(mtmd_bitmap * val) { mtmd_bitmap_free(val); }
  197. };
  198. using bitmap_ptr = std::unique_ptr<mtmd_bitmap, mtmd_bitmap_deleter>;
  199. struct mtmd_input_chunks_deleter {
  200. void operator()(mtmd_input_chunks * val) { mtmd_input_chunks_free(val); }
  201. };
  202. using input_chunks_ptr = std::unique_ptr<mtmd_input_chunks, mtmd_input_chunks_deleter>;
  203. struct mtmd_input_chunk_deleter {
  204. void operator()(mtmd_input_chunk * val) { mtmd_input_chunk_free(val); }
  205. };
  206. using input_chunk_ptr = std::unique_ptr<mtmd_input_chunk, mtmd_input_chunk_deleter>;
  207. struct bitmap {
  208. bitmap_ptr ptr;
  209. bitmap() : ptr(nullptr) {}
  210. bitmap(mtmd_bitmap * bitmap) : ptr(bitmap) {}
  211. bitmap(bitmap && other) noexcept : ptr(std::move(other.ptr)) {}
  212. bitmap(uint32_t nx, uint32_t ny, const unsigned char * data) {
  213. ptr.reset(mtmd_bitmap_init(nx, ny, data));
  214. }
  215. ~bitmap() = default;
  216. uint32_t nx() { return mtmd_bitmap_get_nx(ptr.get()); }
  217. uint32_t ny() { return mtmd_bitmap_get_ny(ptr.get()); }
  218. const unsigned char * data() { return mtmd_bitmap_get_data(ptr.get()); }
  219. size_t n_bytes() { return mtmd_bitmap_get_n_bytes(ptr.get()); }
  220. std::string id() { return mtmd_bitmap_get_id(ptr.get()); }
  221. void set_id(const char * id) { mtmd_bitmap_set_id(ptr.get(), id); }
  222. };
  223. struct bitmaps {
  224. std::vector<bitmap> entries;
  225. ~bitmaps() = default;
  226. // return list of pointers to mtmd_bitmap
  227. // example:
  228. // auto bitmaps_c_ptr = bitmaps.c_ptr();
  229. // int32_t res = mtmd_tokenize(... bitmaps_c_ptr.data(), bitmaps_c_ptr.size());
  230. std::vector<const mtmd_bitmap *> c_ptr() {
  231. std::vector<const mtmd_bitmap *> res(entries.size());
  232. for (size_t i = 0; i < entries.size(); i++) {
  233. res[i] = entries[i].ptr.get();
  234. }
  235. return res;
  236. }
  237. };
  238. struct input_chunks {
  239. input_chunks_ptr ptr;
  240. input_chunks() = default;
  241. input_chunks(mtmd_input_chunks * chunks) : ptr(chunks) {}
  242. ~input_chunks() = default;
  243. size_t size() { return mtmd_input_chunks_size(ptr.get()); }
  244. const mtmd_input_chunk * operator[](size_t idx) {
  245. return mtmd_input_chunks_get(ptr.get(), idx);
  246. }
  247. };
  248. } // namespace mtmd
  249. #endif
  250. #endif