diff --git a/common/log.cpp b/common/log.cpp index 2d1e74ad1..0f0cb7902 100644 --- a/common/log.cpp +++ b/common/log.cpp @@ -438,7 +438,7 @@ void common_log_flush(struct common_log * log) { log->resume(); } -static int common_get_verbosity(enum ggml_log_level level) { +int common_log_get_verbosity(enum ggml_log_level level) { switch (level) { case GGML_LOG_LEVEL_DEBUG: return LOG_LEVEL_DEBUG; case GGML_LOG_LEVEL_INFO: return LOG_LEVEL_TRACE; @@ -452,7 +452,7 @@ static int common_get_verbosity(enum ggml_log_level level) { } void common_log_default_callback(enum ggml_log_level level, const char * text, void * /*user_data*/) { - auto verbosity = common_get_verbosity(level); + auto verbosity = common_log_get_verbosity(level); if (verbosity <= common_log_verbosity_thold) { common_log_add(common_log_main(), level, "%s", text); } diff --git a/common/log.h b/common/log.h index 45d82f4dd..f03358252 100644 --- a/common/log.h +++ b/common/log.h @@ -43,6 +43,8 @@ int common_log_get_verbosity_thold(void); void common_log_set_verbosity_thold(int verbosity); // not thread-safe +int common_log_get_verbosity(enum ggml_log_level level); + void common_log_default_callback(enum ggml_log_level level, const char * text, void * user_data); // the common_log uses an internal worker thread to print/write log messages diff --git a/tests/test-llama-archs.cpp b/tests/test-llama-archs.cpp index 35a3286e4..6a05c3b96 100644 --- a/tests/test-llama-archs.cpp +++ b/tests/test-llama-archs.cpp @@ -65,7 +65,7 @@ static void set_tensor_data(struct ggml_tensor * tensor, void * userdata) { } static void usage(char ** argv) { - printf("Usage: %s [-a/--arch arch] [-s/--seed seed] [-o/--out dir] [-v/--verbose] [-h/--help]\n", argv[0]); + printf("Usage: %s [-a/--arch arch] [-s/--seed seed] [-o/--out dir] [-v N] [-h/--help]\n", argv[0]); } static std::vector get_tokens(const uint32_t n_tokens, const uint32_t n_vocab, const size_t seed){ @@ -535,22 +535,27 @@ static bool arch_supported(const llm_arch arch) { return true; } -static int save_models(const llm_arch target_arch, const size_t seed, const ggml_log_level log_level, const std::string & dir) { +static int save_models(const llm_arch target_arch, const size_t seed, const int verbosity, const std::string & dir) { struct user_data_t { struct { ggml_log_callback callback; void * user_data; - } original_logger; - ggml_log_level min_level; // prints below this log level go to debug log + } log_old; + + int verbosity; + + user_data_t(int verbosity) : verbosity(verbosity) { + llama_log_get(&log_old.callback, &log_old.user_data); + } }; - user_data_t ud; - llama_log_get(&ud.original_logger.callback, &ud.original_logger.user_data); - ud.min_level = log_level; + user_data_t ud(verbosity); llama_log_set([](ggml_log_level level, const char * text, void * user_data) { const user_data_t * ud = (const user_data_t *) user_data; - const ggml_log_level level_eff = level >= ud->min_level ? level : GGML_LOG_LEVEL_DEBUG; - ud->original_logger.callback(level_eff, text, ud->original_logger.user_data); + int verbosity = common_log_get_verbosity(level); + if (verbosity <= ud->verbosity) { + ud->log_old.callback(level, text, ud->log_old.user_data); + } }, &ud); for (const llm_arch & arch : llm_arch_all()) { @@ -584,26 +589,31 @@ static int save_models(const llm_arch target_arch, const size_t seed, const ggml llama_model_save_to_file(model_and_ctx.first.get(), path.c_str()); } } - llama_log_set(ud.original_logger.callback, ud.original_logger.user_data); + llama_log_set(ud.log_old.callback, ud.log_old.user_data); return 0; } -static int test_backends(const llm_arch target_arch, const size_t seed, const ggml_log_level log_level) { +static int test_backends(const llm_arch target_arch, const size_t seed, const int verbosity) { struct user_data_t { struct { ggml_log_callback callback; void * user_data; - } original_logger; - ggml_log_level min_level; // prints below this log level go to debug log + } log_old; + + int verbosity; + + user_data_t(int verbosity) : verbosity(verbosity) { + llama_log_get(&log_old.callback, &log_old.user_data); + } }; - user_data_t ud; - llama_log_get(&ud.original_logger.callback, &ud.original_logger.user_data); - ud.min_level = log_level; + user_data_t ud(verbosity); llama_log_set([](ggml_log_level level, const char * text, void * user_data) { const user_data_t * ud = (const user_data_t *) user_data; - const ggml_log_level level_eff = level >= ud->min_level ? level : GGML_LOG_LEVEL_DEBUG; - ud->original_logger.callback(level_eff, text, ud->original_logger.user_data); + int verbosity = common_log_get_verbosity(level); + if (verbosity <= ud->verbosity) { + ud->log_old.callback(level, text, ud->log_old.user_data); + } }, &ud); const std::vector tokens = get_tokens(128, 128, seed); @@ -749,20 +759,23 @@ static int test_backends(const llm_arch target_arch, const size_t seed, const gg } } } - llama_log_set(ud.original_logger.callback, ud.original_logger.user_data); + llama_log_set(ud.log_old.callback, ud.log_old.user_data); return all_ok ? 0 : 1; } int main(int argc, char ** argv) { - // FIXME these tests are disabled in the CI for macOS-latest-cmake-arm64 because they are segfaulting + // init the logger at max verbosity. filter with a custom callback respecting the user-configure verbosity + common_log_set_verbosity_thold(LOG_LEVEL_DEBUG); common_init(); + std::random_device rd; llm_arch arch = LLM_ARCH_UNKNOWN; size_t seed = rd(); - ggml_log_level log_level = GGML_LOG_LEVEL_ERROR; std::string out; + int verbosity = LOG_LEVEL_ERROR; + for (int i = 1; i < argc; i++) { if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) { usage(argv); @@ -789,9 +802,13 @@ int main(int argc, char ** argv) { return 1; } } - if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--verbose") == 0) { - log_level = GGML_LOG_LEVEL_INFO; - continue; + if (strcmp(argv[i], "-v") == 0) { + if (i + 1 < argc) { + verbosity = std::stoull(argv[++i]); + } else { + usage(argv); + return 1; + } } if (strcmp(argv[i], "-o") == 0 || strcmp(argv[i], "--out") == 0) { if (i + 1 < argc) { @@ -806,9 +823,9 @@ int main(int argc, char ** argv) { try { if (!out.empty()) { - return save_models(arch, seed, log_level, out); + return save_models(arch, seed, verbosity, out); } - return test_backends(arch, seed, log_level); + return test_backends(arch, seed, verbosity); } catch (const std::exception & err) { fprintf(stderr, "encountered runtime error: %s\n", err.what()); return -1;