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