Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions tools/mtmd/mtmd-helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,11 @@ static bool is_webp_file(const unsigned char * buf, size_t len) {
}

#ifdef MTMD_VIDEO
static mtmd_bitmap * decode_webp_with_ffmpeg(mtmd_context * mctx, const unsigned char * buf, size_t len, bool placeholder,
static mtmd_bitmap * decode_webp_with_ffmpeg(const mtmd_context * mctx, const unsigned char * buf, size_t len, bool placeholder,
const mtmd_helper_video_init_params & params);
#endif

mtmd_helper_bitmap_wrapper mtmd_helper_bitmap_init_from_buf(mtmd_context * ctx, const unsigned char * buf, size_t len, bool placeholder,
mtmd_helper_bitmap_wrapper mtmd_helper_bitmap_init_from_buf(const mtmd_context * ctx, const unsigned char * buf, size_t len, bool placeholder,
mtmd_helper_init_opt opt) {
// calculate the hash if needed
std::string id;
Expand Down Expand Up @@ -459,7 +459,7 @@ mtmd_helper_bitmap_wrapper mtmd_helper_bitmap_init_from_buf(mtmd_context * ctx,
return {nullptr, nullptr};
}

mtmd_helper_bitmap_wrapper mtmd_helper_bitmap_init_from_file(mtmd_context * ctx, const char * fname, bool placeholder,
mtmd_helper_bitmap_wrapper mtmd_helper_bitmap_init_from_file(const mtmd_context * ctx, const char * fname, bool placeholder,
mtmd_helper_init_opt opt) {
#ifdef _WIN32
int wlen = MultiByteToWideChar(CP_UTF8, 0, fname, -1, NULL, 0);
Expand Down Expand Up @@ -504,7 +504,7 @@ mtmd_helper_bitmap_wrapper mtmd_helper_bitmap_init_from_file(mtmd_context * ctx,
return mtmd_helper_bitmap_init_from_buf(ctx, buf.data(), buf.size(), placeholder, opt);
}

bool mtmd_helper_support_video(mtmd_context * ctx) {
bool mtmd_helper_support_video(const mtmd_context * ctx) {
#ifdef MTMD_VIDEO
return mtmd_support_vision(ctx);
#else
Expand All @@ -520,7 +520,7 @@ bool mtmd_helper_support_video(mtmd_context * ctx) {
#ifdef MTMD_VIDEO

struct mtmd_helper_video {
mtmd_context * mctx;
const mtmd_context * mctx;
Comment on lines 522 to +523

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually unused, so we could also just remove it. I kept it for now in case the code wants to use it in the future.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's ok I think, mtmd_helper_video should be thread-safe (multiple video helper can use the same mctx)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah I misread it, let's remove it then

std::string path;
std::vector<uint8_t> input_buf; // non-empty when initialized from buffer
std::string ffmpeg_bin;
Expand Down Expand Up @@ -886,7 +886,7 @@ static std::string video_resolve_bin(const char * bin_dir, const char * name) {
}

#ifdef MTMD_VIDEO
static mtmd_bitmap * decode_webp_with_ffmpeg(mtmd_context * mctx, const unsigned char * buf, size_t len, bool placeholder,
static mtmd_bitmap * decode_webp_with_ffmpeg(const mtmd_context * mctx, const unsigned char * buf, size_t len, bool placeholder,
const mtmd_helper_video_init_params & params) {
mtmd_helper_video vctx;
vctx.mctx = mctx;
Expand All @@ -913,7 +913,7 @@ static mtmd_bitmap * decode_webp_with_ffmpeg(mtmd_context * mctx, const unsigned
#endif

mtmd_helper_video * mtmd_helper_video_init(
mtmd_context * mctx,
const mtmd_context * mctx,
const char * path,
mtmd_helper_video_init_params params) {
#ifdef MTMD_VIDEO
Expand Down Expand Up @@ -948,7 +948,7 @@ mtmd_helper_video * mtmd_helper_video_init(
}

mtmd_helper_video * mtmd_helper_video_init_from_buf(
mtmd_context * mctx,
const mtmd_context * mctx,
const unsigned char * buf, size_t len,
mtmd_helper_video_init_params params) {
#ifdef MTMD_VIDEO
Expand Down Expand Up @@ -1016,7 +1016,7 @@ int32_t mtmd_helper_video_read_next(mtmd_helper_video * ctx,
#endif
}

bool mtmd_helper_model_can_chat(llama_context * lctx, mtmd_context * mctx) {
bool mtmd_helper_model_can_chat(const llama_context * lctx, const mtmd_context * mctx) {
if (!mctx) {
return true;
}
Expand Down
12 changes: 6 additions & 6 deletions tools/mtmd/mtmd-helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ MTMD_API struct mtmd_helper_init_opt mtmd_helper_init_opt_default(void);
MTMD_API void mtmd_helper_log_set(ggml_log_callback log_callback, void * user_data);

// Returns true if this build includes video support (MTMD_VIDEO was ON at compile time).
MTMD_API bool mtmd_helper_support_video(mtmd_context * ctx);
MTMD_API bool mtmd_helper_support_video(const mtmd_context * ctx);

struct mtmd_helper_bitmap_wrapper {
mtmd_bitmap * bitmap;
Expand All @@ -58,7 +58,7 @@ struct mtmd_helper_bitmap_wrapper {
// returns nullptr on failure
// this function is thread-safe
MTMD_API struct mtmd_helper_bitmap_wrapper mtmd_helper_bitmap_init_from_file(
mtmd_context * ctx,
const mtmd_context * ctx,
const char * fname,
bool placeholder,
struct mtmd_helper_init_opt opt);
Expand All @@ -75,7 +75,7 @@ MTMD_API struct mtmd_helper_bitmap_wrapper mtmd_helper_bitmap_init_from_file(
// returns nullptr on failure
// this function is thread-safe
MTMD_API struct mtmd_helper_bitmap_wrapper mtmd_helper_bitmap_init_from_buf(
mtmd_context * ctx,
const mtmd_context * ctx,
const unsigned char * buf, size_t len,
bool placeholder,
struct mtmd_helper_init_opt opt);
Expand Down Expand Up @@ -153,7 +153,7 @@ struct mtmd_helper_video_info {

// returns NULL on failure (ffprobe not found, file unreadable, etc.)
MTMD_API mtmd_helper_video * mtmd_helper_video_init(
struct mtmd_context * mctx,
const struct mtmd_context * mctx,
const char * path,
struct mtmd_helper_video_init_params params);

Expand All @@ -162,7 +162,7 @@ MTMD_API mtmd_helper_video * mtmd_helper_video_init(
// Note: pipe input is not seekable, so seeking will use output-side seeking
// (ffmpeg decodes and discards frames up to the target position).
MTMD_API mtmd_helper_video * mtmd_helper_video_init_from_buf(
struct mtmd_context * mctx,
const struct mtmd_context * mctx,
const unsigned char * buf, size_t len,
struct mtmd_helper_video_init_params params);
MTMD_API void mtmd_helper_video_free(mtmd_helper_video * ctx);
Expand All @@ -177,7 +177,7 @@ MTMD_API int32_t mtmd_helper_video_read_next(mtmd_helper_video * ctx,
char ** out_text);

// return true if model can be used for chat
MTMD_API bool mtmd_helper_model_can_chat(struct llama_context * lctx, struct mtmd_context * mctx);
MTMD_API bool mtmd_helper_model_can_chat(const struct llama_context * lctx, const struct mtmd_context * mctx);

//
// Audio generation helpers
Expand Down
20 changes: 10 additions & 10 deletions tools/mtmd/mtmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1117,7 +1117,7 @@ std::vector<std::vector<const mtmd_bitmap *>> mtmd_group_mergeable_bitmaps(std::
}

struct mtmd_tokenizer {
mtmd_context * ctx;
const mtmd_context * ctx;

std::string input_text; // note: can contain null bytes; do not use c_str()
bool add_special;
Expand All @@ -1140,9 +1140,9 @@ struct mtmd_tokenizer {
}
}

mtmd_tokenizer(mtmd_context * ctx,
mtmd_tokenizer(const mtmd_context * ctx,
const mtmd_input_text * text,
const mtmd_bitmap ** bmps,
const mtmd_bitmap * const * bmps,
size_t n_bitmaps) : ctx(ctx) {
add_special = text->add_special;
parse_special = text->parse_special;
Expand Down Expand Up @@ -1177,8 +1177,8 @@ struct mtmd_tokenizer {
expand_lazy_bitmaps();
}

mtmd_tokenizer(mtmd_context * ctx,
const mtmd_input_part ** input_parts,
mtmd_tokenizer(const mtmd_context * ctx,
const mtmd_input_part * const * input_parts,
size_t n_parts,
bool add_special) : ctx(ctx) {
this->add_special = add_special;
Expand Down Expand Up @@ -1733,10 +1733,10 @@ struct mtmd_tokenizer {
}
};

int32_t mtmd_tokenize(mtmd_context * ctx,
int32_t mtmd_tokenize(const mtmd_context * ctx,
mtmd_input_chunks * output,
const mtmd_input_text * text,
const mtmd_bitmap ** bitmaps,
const mtmd_bitmap * const * bitmaps,
size_t n_bitmaps) {
try {
mtmd_tokenizer tokenizer(ctx, text, bitmaps, n_bitmaps);
Expand All @@ -1747,9 +1747,9 @@ int32_t mtmd_tokenize(mtmd_context * ctx,
}
}

int32_t mtmd_tokenize_from_parts(mtmd_context * ctx,
int32_t mtmd_tokenize_from_parts(const mtmd_context * ctx,
mtmd_input_chunks * output,
const mtmd_input_part ** parts,
const mtmd_input_part * const * parts,
size_t n_parts,
bool add_special) {
for (size_t i = 0; i < n_parts; i++) {
Expand Down Expand Up @@ -2271,7 +2271,7 @@ void mtmd_bitmap_set_mergeable(mtmd_bitmap * bitmap, bool mergeable) {
bitmap->mergeable = mergeable;
}

mtmd_bitmap * mtmd_bitmap_init_lazy(mtmd_context * ctx,
mtmd_bitmap * mtmd_bitmap_init_lazy(const mtmd_context * ctx,
const char * id,
void * user_data,
mtmd_bitmap_lazy_callback callback) {
Expand Down
10 changes: 5 additions & 5 deletions tools/mtmd/mtmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ typedef int(* mtmd_bitmap_lazy_callback)(
mtmd_bitmap ** out_bitmap,
char ** out_text);

MTMD_API mtmd_bitmap * mtmd_bitmap_init_lazy(mtmd_context * ctx,
MTMD_API mtmd_bitmap * mtmd_bitmap_init_lazy(const mtmd_context * ctx,
const char * id, // usually set to file hash
void * user_data,
mtmd_bitmap_lazy_callback callback);
Expand Down Expand Up @@ -299,10 +299,10 @@ MTMD_API struct mtmd_decoder_pos mtmd_image_tokens_get_decoder_pos(const mtmd_im
// 0 on success
// 1 on number of bitmaps not matching the number of markers
// 2 on media preprocessing error
MTMD_API int32_t mtmd_tokenize(mtmd_context * ctx,
MTMD_API int32_t mtmd_tokenize(const mtmd_context * ctx,
mtmd_input_chunks * output,
const mtmd_input_text * text,
const mtmd_bitmap ** bitmaps,
const mtmd_bitmap * const * bitmaps,
size_t n_bitmaps);

// same as mtmd_tokenize(), but takes an array of mtmd_input_part
Expand All @@ -311,9 +311,9 @@ MTMD_API int32_t mtmd_tokenize(mtmd_context * ctx,
// - when you want to control parse_special for each text part
// note: per-part add_special will be ignored
// return 1 if a part has both text and bitmap set (or neither)
MTMD_API int32_t mtmd_tokenize_from_parts(mtmd_context * ctx,
MTMD_API int32_t mtmd_tokenize_from_parts(const mtmd_context * ctx,
mtmd_input_chunks * output,
const mtmd_input_part ** parts,
const mtmd_input_part * const * parts,
size_t n_parts,
bool add_special);

Expand Down
Loading