Skip to content

Commit 237a61c

Browse files
committed
implement logging redirection
1 parent ccbc006 commit 237a61c

File tree

5 files changed

+73
-1
lines changed

5 files changed

+73
-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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,33 @@ struct clip_logger_state {
203203
ggml_log_level verbosity_thold;
204204
ggml_log_callback log_callback;
205205
void * log_callback_user_data;
206+
// Store the llama callback to forward logs to
207+
ggml_log_callback llama_callback;
208+
void * llama_callback_user_data;
206209
};
207210

208211
extern struct clip_logger_state g_logger_state;
209212

213+
// Callback that redirects to llama's logging system
214+
static void clip_log_callback_llama(enum ggml_log_level level, const char * text, void * user_data) {
215+
(void) user_data;
216+
// Forward to the stored llama callback if available
217+
if (g_logger_state.llama_callback != nullptr) {
218+
g_logger_state.llama_callback(level, text, g_logger_state.llama_callback_user_data);
219+
} else {
220+
// Fallback to default if no llama callback is set
221+
clip_log_callback_default(level, text, user_data);
222+
}
223+
}
224+
225+
// Function to enable llama logging redirection
226+
// This should be called after llama_log_set has been called to set up llama's logging
227+
static inline void clip_log_use_llama_callback(ggml_log_callback llama_cb, void * llama_user_data) {
228+
g_logger_state.llama_callback = llama_cb;
229+
g_logger_state.llama_callback_user_data = llama_user_data;
230+
g_logger_state.log_callback = clip_log_callback_llama;
231+
}
232+
210233
static void clip_log_internal_v(enum ggml_log_level level, const char * format, va_list args) {
211234
if (format == NULL) {
212235
return;

tools/mtmd/clip.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@
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+
nullptr, // llama_callback
36+
nullptr // llama_callback_user_data
37+
};
3238

3339
enum ffn_op_type {
3440
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_use_llama_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)