1
0

mtmd.h 11 KB

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