Skip to content

Commit 87cdbd5

Browse files
authored
feat: use log_printf to print ggml logs (#545)
1 parent b017918 commit 87cdbd5

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

ggml_extend.hpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,25 @@
5656
#define __STATIC_INLINE__ static inline
5757
#endif
5858

59+
__STATIC_INLINE__ void ggml_log_callback_default(ggml_log_level level, const char* text, void*) {
60+
switch (level) {
61+
case GGML_LOG_LEVEL_DEBUG:
62+
LOG_DEBUG(text);
63+
break;
64+
case GGML_LOG_LEVEL_INFO:
65+
LOG_INFO(text);
66+
break;
67+
case GGML_LOG_LEVEL_WARN:
68+
LOG_WARN(text);
69+
break;
70+
case GGML_LOG_LEVEL_ERROR:
71+
LOG_ERROR(text);
72+
break;
73+
default:
74+
LOG_DEBUG(text);
75+
}
76+
}
77+
5978
static_assert(GGML_MAX_NAME >= 128, "GGML_MAX_NAME must be at least 128");
6079

6180
// n-mode trensor-matrix product
@@ -124,13 +143,6 @@ __STATIC_INLINE__ struct ggml_tensor* ggml_kronecker(ggml_context* ctx, struct g
124143
b);
125144
}
126145

127-
__STATIC_INLINE__ void ggml_log_callback_default(ggml_log_level level, const char* text, void* user_data) {
128-
(void)level;
129-
(void)user_data;
130-
fputs(text, stderr);
131-
fflush(stderr);
132-
}
133-
134146
__STATIC_INLINE__ void ggml_tensor_set_f32_randn(struct ggml_tensor* tensor, std::shared_ptr<RNG> rng) {
135147
uint32_t n = (uint32_t)ggml_nelements(tensor);
136148
std::vector<float> random_numbers = rng->randn(n);

stable-diffusion.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ class StableDiffusionGGML {
145145
#endif
146146
#ifdef SD_USE_METAL
147147
LOG_DEBUG("Using Metal backend");
148-
ggml_log_set(ggml_log_callback_default, nullptr);
149148
backend = ggml_backend_metal_init();
150149
#endif
151150
#ifdef SD_USE_VULKAN
@@ -192,6 +191,8 @@ class StableDiffusionGGML {
192191
rng = std::make_shared<PhiloxRNG>();
193192
}
194193

194+
ggml_log_set(ggml_log_callback_default, nullptr);
195+
195196
init_backend();
196197

197198
ModelLoader model_loader;

upscaler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ struct UpscalerGGML {
1919

2020
bool load_from_file(const std::string& esrgan_path,
2121
bool offload_params_to_cpu) {
22+
ggml_log_set(ggml_log_callback_default, nullptr);
2223
#ifdef SD_USE_CUDA
2324
LOG_DEBUG("Using CUDA backend");
2425
backend = ggml_backend_cuda_init(0);
2526
#endif
2627
#ifdef SD_USE_METAL
2728
LOG_DEBUG("Using Metal backend");
28-
ggml_log_set(ggml_log_callback_default, nullptr);
2929
backend = ggml_backend_metal_init();
3030
#endif
3131
#ifdef SD_USE_VULKAN

util.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,10 @@ void log_printf(sd_log_level_t level, const char* file, int line, const char* fo
414414
if (written >= 0 && written < LOG_BUFFER_SIZE) {
415415
vsnprintf(log_buffer + written, LOG_BUFFER_SIZE - written, format, args);
416416
}
417-
strncat(log_buffer, "\n", LOG_BUFFER_SIZE - strlen(log_buffer));
417+
size_t len = strlen(log_buffer);
418+
if (log_buffer[len - 1] != '\n') {
419+
strncat(log_buffer, "\n", LOG_BUFFER_SIZE - len);
420+
}
418421

419422
if (sd_log_cb) {
420423
sd_log_cb(level, log_buffer, sd_log_cb_data);

0 commit comments

Comments
 (0)