1
0

mtmd-helper.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. // Set callback for all future logging events.
  19. // If this is not called, or NULL is supplied, everything is output on stderr.
  20. // Note: this also call mtmd_log_set() internally
  21. MTMD_API void mtmd_helper_log_set(ggml_log_callback log_callback, void * user_data);
  22. // helper function to construct a mtmd_bitmap from a file
  23. // it calls mtmd_helper_bitmap_init_from_buf() internally
  24. // returns nullptr on failure
  25. // this function is thread-safe
  26. MTMD_API mtmd_bitmap * mtmd_helper_bitmap_init_from_file(mtmd_context * ctx, const char * fname);
  27. // helper function to construct a mtmd_bitmap from a buffer containing a file
  28. // supported formats:
  29. // image: formats supported by stb_image: jpg, png, bmp, gif, etc.
  30. // audio: formats supported by miniaudio: wav, mp3, flac
  31. // note: audio files will be auto-detected based on magic bytes
  32. // returns nullptr on failure
  33. // this function is thread-safe
  34. MTMD_API mtmd_bitmap * mtmd_helper_bitmap_init_from_buf(mtmd_context * ctx, const unsigned char * buf, size_t len);
  35. // helper to count the total number of tokens from a list of chunks, useful to keep track of KV cache
  36. MTMD_API size_t mtmd_helper_get_n_tokens(const mtmd_input_chunks * chunks);
  37. // helper to count the total position of tokens from a list of chunks, useful to keep track of n_past
  38. // normally, n_pos is equal to n_tokens, but for M-RoPE it is different
  39. MTMD_API llama_pos mtmd_helper_get_n_pos(const mtmd_input_chunks * chunks);
  40. // helper function that automatically:
  41. // 1. run llama_decode() on text chunks
  42. // 2. run mtmd_encode() on image chunks, then mtmd_get_output_embd() and then llama_decode()
  43. // if any of the mtmd_encode() or llama_decode() calls return non-zero, stop and forward the error
  44. // otherwise, returns 0 on success
  45. // this function is NOT thread-safe
  46. MTMD_API int32_t mtmd_helper_eval_chunks(mtmd_context * ctx,
  47. struct llama_context * lctx,
  48. const mtmd_input_chunks * chunks,
  49. llama_pos n_past,
  50. llama_seq_id seq_id,
  51. int32_t n_batch,
  52. bool logits_last,
  53. llama_pos * new_n_past);
  54. // works like mtmd_helper_eval_chunks(), but only for a single chunk
  55. // this function is NOT thread-safe
  56. MTMD_API int32_t mtmd_helper_eval_chunk_single(mtmd_context * ctx,
  57. struct llama_context * lctx,
  58. const mtmd_input_chunk * chunk,
  59. llama_pos n_past,
  60. llama_seq_id seq_id,
  61. int32_t n_batch,
  62. bool logits_last,
  63. llama_pos * new_n_past);
  64. // helper function to decode an image whose embeddings have already been calculated
  65. // this helper will handle batching and pre/post decoding setup (for ex. gemma 3 requires non-causal attention)
  66. // ret 0 on success, -1 on chunk not being a valid image chunk, 1 on decode failure
  67. MTMD_API int32_t mtmd_helper_decode_image_chunk(mtmd_context * ctx,
  68. struct llama_context * lctx,
  69. const mtmd_input_chunk * chunk,
  70. float * encoded_embd,
  71. llama_pos n_past,
  72. llama_seq_id seq_id,
  73. int32_t n_batch,
  74. llama_pos * new_n_past);
  75. #ifdef __cplusplus
  76. } // extern "C"
  77. #endif
  78. //
  79. // C++ wrappers
  80. //
  81. #endif