Skip to content

Commit 87f40dd

Browse files
authored
Merge pull request #31 from ogad-tether/temp-latest
implement logging redirection
2 parents ccbc006 + e1e7198 commit 87f40dd

File tree

5 files changed

+55
-1
lines changed

5 files changed

+55
-1
lines changed

tools/mtmd/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,37 @@ Built upon `clip.cpp` (similar to `llava.cpp`), `libmtmd` offers several advanta
3737
- **Improved UX/DX:** Features a more intuitive API, inspired by the `Processor` class in the Hugging Face `transformers` library.
3838
- **Flexibility:** Designed to support multiple input types (text, audio, images) while respecting the wide variety of chat templates used by different models.
3939

40+
## Logging Configuration
41+
42+
By default, `libmtmd` logs messages directly to stderr. To integrate `libmtmd` logging with your application's logging system, you can use the `mtmd_log_set_llama_callback()` function to redirect all mtmd/clip logs through llama's logging callback.
43+
44+
**Example usage:**
45+
46+
```c
47+
#include "llama.h"
48+
#include "mtmd.h"
49+
50+
// Your custom logging callback
51+
void my_log_callback(ggml_log_level level, const char * text, void * user_data) {
52+
// Your logging logic here
53+
printf("[%d] %s", level, text);
54+
}
55+
56+
int main() {
57+
// Set up llama's logging
58+
llama_log_set(my_log_callback, NULL);
59+
60+
// Redirect mtmd/clip logging to use the same callback
61+
mtmd_log_set_llama_callback(my_log_callback, NULL);
62+
63+
// Now all mtmd and clip logs will use your custom callback
64+
mtmd_context * ctx = mtmd_init_from_file(...);
65+
// ...
66+
}
67+
```
68+
69+
This ensures that all logging from `libmtmd`, including the underlying `clip.cpp` vision encoder, is routed through your application's logging system consistently.
70+
4071
## How to obtain `mmproj`
4172
4273
Multimodal projector (`mmproj`) files are specific to each model architecture.

tools/mtmd/clip-impl.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,13 @@ struct clip_logger_state {
207207

208208
extern struct clip_logger_state g_logger_state;
209209

210+
// Function to set logging callback (can be used to redirect to llama's logging)
211+
// If not called, will use the default callback (logs to stderr)
212+
static inline void clip_log_set_callback(ggml_log_callback callback, void * user_data) {
213+
g_logger_state.log_callback = callback;
214+
g_logger_state.log_callback_user_data = user_data;
215+
}
216+
210217
static void clip_log_internal_v(enum ggml_log_level level, const char * format, va_list args) {
211218
if (format == NULL) {
212219
return;

tools/mtmd/clip.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@
2828
#include <numeric>
2929
#include <functional>
3030

31-
struct clip_logger_state g_logger_state = {GGML_LOG_LEVEL_CONT, clip_log_callback_default, NULL};
31+
struct clip_logger_state g_logger_state = {
32+
GGML_LOG_LEVEL_CONT, // verbosity_thold
33+
clip_log_callback_default, // log_callback
34+
NULL // log_callback_user_data
35+
};
3236

3337
enum ffn_op_type {
3438
FFN_GELU,

tools/mtmd/mtmd.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,10 @@ void mtmd_free(mtmd_context * ctx) {
363363
}
364364
}
365365

366+
void mtmd_log_set_llama_callback(ggml_log_callback llama_cb, void * llama_user_data) {
367+
clip_log_set_callback(llama_cb, llama_user_data);
368+
}
369+
366370
struct mtmd_tokenizer {
367371
mtmd_context * ctx;
368372
std::vector<const mtmd_bitmap *> bitmaps;

tools/mtmd/mtmd.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ MTMD_API mtmd_context * mtmd_init_from_file(const char * mmproj_fname,
9696

9797
MTMD_API void mtmd_free(mtmd_context * ctx);
9898

99+
// Set up logging to use llama's logging callback
100+
// This redirects all mtmd/clip logging through llama's logging system
101+
// Call this after llama_log_set to ensure mtmd uses the same logging callback
102+
// Example:
103+
// llama_log_set(my_log_callback, my_user_data);
104+
// mtmd_log_set_llama_callback(my_log_callback, my_user_data);
105+
MTMD_API void mtmd_log_set_llama_callback(ggml_log_callback llama_cb, void * llama_user_data);
106+
99107
// whether we need to set non-causal mask before llama_decode
100108
MTMD_API bool mtmd_decode_use_non_causal(mtmd_context * ctx);
101109

0 commit comments

Comments
 (0)