1
0

mtmd-helper.h 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #ifndef MTMD_HELPER_H
  2. #define MTMD_HELPER_H
  3. #include "ggml.h"
  4. #include "llama.h"
  5. #include "mtmd.h"
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <stdbool.h>
  9. #ifdef __cplusplus
  10. extern "C" {
  11. #endif
  12. //
  13. // libmtmd helper functions
  14. //
  15. // Please note that these helpers are not guaranteed to be stable.
  16. // BREAKING CHANGES are expected.
  17. //
  18. // helper function to construct a mtmd_bitmap from a file
  19. // it calls mtmd_helper_bitmap_init_from_buf() internally
  20. // returns nullptr on failure
  21. // this function is thread-safe
  22. MTMD_API mtmd_bitmap * mtmd_helper_bitmap_init_from_file(mtmd_context * ctx, const char * fname);
  23. // helper function to construct a mtmd_bitmap from a buffer containing a file
  24. // supported formats:
  25. // image: formats supported by stb_image: jpg, png, bmp, gif, etc.
  26. // audio: formats supported by miniaudio: wav, mp3, flac
  27. // note: audio files will be auto-detected based on magic bytes
  28. // returns nullptr on failure
  29. // this function is thread-safe
  30. MTMD_API mtmd_bitmap * mtmd_helper_bitmap_init_from_buf(mtmd_context * ctx, const unsigned char * buf, size_t len);
  31. // helper to count the total number of tokens from a list of chunks, useful to keep track of KV cache
  32. MTMD_API size_t mtmd_helper_get_n_tokens(const mtmd_input_chunks * chunks);
  33. // helper to count the total position of tokens from a list of chunks, useful to keep track of n_past
  34. // normally, n_pos is equal to n_tokens, but for M-RoPE it is different
  35. MTMD_API llama_pos mtmd_helper_get_n_pos(const mtmd_input_chunks * chunks);
  36. // helper function that automatically:
  37. // 1. run llama_decode() on text chunks
  38. // 2. run mtmd_encode() on image chunks, then mtmd_get_output_embd() and then llama_decode()
  39. // if any of the mtmd_encode() or llama_decode() calls return non-zero, stop and forward the error
  40. // otherwise, returns 0 on success
  41. // this function is NOT thread-safe
  42. MTMD_API int32_t mtmd_helper_eval_chunks(mtmd_context * ctx,
  43. struct llama_context * lctx,
  44. const mtmd_input_chunks * chunks,
  45. llama_pos n_past,
  46. llama_seq_id seq_id,
  47. int32_t n_batch,
  48. bool logits_last,
  49. llama_pos * new_n_past);
  50. // works like mtmd_helper_eval_chunks(), but only for a single chunk
  51. // this function is NOT thread-safe
  52. MTMD_API int32_t mtmd_helper_eval_chunk_single(mtmd_context * ctx,
  53. struct llama_context * lctx,
  54. const mtmd_input_chunk * chunk,
  55. llama_pos n_past,
  56. llama_seq_id seq_id,
  57. int32_t n_batch,
  58. bool logits_last,
  59. llama_pos * new_n_past);
  60. // helper function to decode an image whose embeddings have already been calculated
  61. // this helper will handle batching and pre/post decoding setup (for ex. gemma 3 requires non-causal attention)
  62. // ret 0 on success, -1 on chunk not being a valid image chunk, 1 on decode failure
  63. MTMD_API int32_t mtmd_helper_decode_image_chunk(mtmd_context * ctx,
  64. struct llama_context * lctx,
  65. const mtmd_input_chunk * chunk,
  66. float * encoded_embd,
  67. llama_pos n_past,
  68. llama_seq_id seq_id,
  69. int32_t n_batch,
  70. llama_pos * new_n_past);
  71. #ifdef __cplusplus
  72. } // extern "C"
  73. #endif
  74. //
  75. // C++ wrappers
  76. //
  77. #endif