echodict/llama.cpp
version https://git-lfs.github.com/spec/v1 oid sha256:cfc44b7ba25614df70e6b65e3341cae0310163bd32fd31a6b928a542df433faf size 30786
0773
1#ifndef MTMD_HELPER_H2#define MTMD_HELPER_H3 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 __cplusplus13extern "C" {14#endif15 16//17// libmtmd helper functions18//19// Please note that these helpers are not guaranteed to be stable.20// BREAKING CHANGES are expected.21//22 23// Set callback for all future logging events.24// If this is not called, or NULL is supplied, everything is output on stderr.25// Note: this also call mtmd_log_set() internally26MTMD_API void mtmd_helper_log_set(ggml_log_callback log_callback, void * user_data);27 28// helper function to construct a mtmd_bitmap from a file29// it calls mtmd_helper_bitmap_init_from_buf() internally30// returns nullptr on failure31// this function is thread-safe32MTMD_API mtmd_bitmap * mtmd_helper_bitmap_init_from_file(mtmd_context * ctx, const char * fname);33 34// helper function to construct a mtmd_bitmap from a buffer containing a file35// supported formats:36// image: formats supported by stb_image: jpg, png, bmp, gif, etc.37// audio: formats supported by miniaudio: wav, mp3, flac38// note: audio files will be auto-detected based on magic bytes39// returns nullptr on failure40// this function is thread-safe41MTMD_API mtmd_bitmap * mtmd_helper_bitmap_init_from_buf(mtmd_context * ctx, const unsigned char * buf, size_t len);42 43// helper to count the total number of tokens from a list of chunks, useful to keep track of KV cache44MTMD_API size_t mtmd_helper_get_n_tokens(const mtmd_input_chunks * chunks);45 46// helper to count the total position of tokens from a list of chunks, useful to keep track of n_past47// normally, n_pos is equal to n_tokens, but for M-RoPE it is different48MTMD_API llama_pos mtmd_helper_get_n_pos(const mtmd_input_chunks * chunks);49 50// helper to get the list of relative positions corresponding to the embedding tokens, to be used by M-RoPE51// out_pos must have length == mtmd_helper_get_n_tokens(image)52MTMD_API void mtmd_helper_image_get_decoder_pos(const mtmd_image_tokens * image, llama_pos pos_0, struct mtmd_decoder_pos * out_pos);53 54// helper function that automatically:55// 1. run llama_decode() on text chunks56// 2. run mtmd_encode() on image chunks, then mtmd_get_output_embd() and then llama_decode()57// if any of the mtmd_encode() or llama_decode() calls return non-zero, stop and forward the error58// otherwise, returns 0 on success59// this function is NOT thread-safe60MTMD_API int32_t mtmd_helper_eval_chunks(mtmd_context * ctx,61 struct llama_context * lctx,62 const mtmd_input_chunks * chunks,63 llama_pos n_past,64 llama_seq_id seq_id,65 int32_t n_batch,66 bool logits_last,67 llama_pos * new_n_past);68 69// works like mtmd_helper_eval_chunks(), but only for a single chunk70// this function is NOT thread-safe71MTMD_API int32_t mtmd_helper_eval_chunk_single(mtmd_context * ctx,72 struct llama_context * lctx,73 const mtmd_input_chunk * chunk,74 llama_pos n_past,75 llama_seq_id seq_id,76 int32_t n_batch,77 bool logits_last,78 llama_pos * new_n_past);79 80// helper function to decode an image whose embeddings have already been calculated81// this helper will handle batching and pre/post decoding setup (for ex. gemma 3 requires non-causal attention)82// ret 0 on success, -1 on chunk not being a valid image chunk, 1 on decode failure83MTMD_API int32_t mtmd_helper_decode_image_chunk(mtmd_context * ctx,84 struct llama_context * lctx,85 const mtmd_input_chunk * chunk,86 float * encoded_embd,87 llama_pos n_past,88 llama_seq_id seq_id,89 int32_t n_batch,90 llama_pos * new_n_past);91 92#ifdef __cplusplus93} // extern "C"94#endif95 96//97// C++ wrappers98//99 100#endif101 