Skip to content

Commit

Permalink
log : simplify init
Browse files Browse the repository at this point in the history
  • Loading branch information
ggerganov committed Sep 12, 2024
1 parent c426837 commit 2dff6f4
Show file tree
Hide file tree
Showing 24 changed files with 43 additions and 115 deletions.
11 changes: 11 additions & 0 deletions common/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,17 @@ bool parse_cpu_mask(const std::string & mask, bool (&boolmask)[GGML_MAX_N_THREAD
return true;
}

void gpt_init() {
llama_log_set([](ggml_log_level level, const char * text, void * /*user_data*/) {
if (LOG_DEFAULT_LLAMA <= gpt_log_verbosity_env) {
gpt_log_add(gpt_log_main(), level, "%s", text);
}
}, NULL);


LOG_INF("build: %d (%s) with %s for %s\n", LLAMA_BUILD_NUMBER, LLAMA_COMMIT, LLAMA_COMPILER, LLAMA_BUILD_TARGET);
}

std::string gpt_params_get_system_info(const gpt_params & params) {
std::ostringstream os;

Expand Down
3 changes: 3 additions & 0 deletions common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,9 @@ struct gpt_params {
bool batched_bench_output_jsonl = false;
};

// call once at the start of a program using common
void gpt_init();

std::string gpt_params_get_system_info(const gpt_params & params);

bool parse_cpu_range(const std::string& range, bool(&boolmask)[GGML_MAX_N_THREADS]);
Expand Down
2 changes: 1 addition & 1 deletion common/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ struct gpt_log_entry {
if (level != GGML_LOG_LEVEL_NONE) {
if (timestamp) {
// [M.s.ms.us]
fprintf(fcur, "" LOG_COL_BLUE "%05d.%02d.%03d.%03d" LOG_COL_DEFAULT " ",
fprintf(fcur, "" LOG_COL_BLUE "%d.%02d.%03d.%03d" LOG_COL_DEFAULT " ",
(int) (timestamp / 1000000 / 60),
(int) (timestamp / 1000000 % 60),
(int) (timestamp / 1000 % 1000),
Expand Down
6 changes: 1 addition & 5 deletions examples/batched-bench/batched-bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ static void print_usage(int, char ** argv) {
}

int main(int argc, char ** argv) {
llama_log_set([](ggml_log_level level, const char * text, void * /*user_data*/) {
if (LOG_DEFAULT_LLAMA <= gpt_log_verbosity_env) {
gpt_log_add(gpt_log_main(), level, "%s", text);
}
}, NULL);
gpt_init();

gpt_params params;

Expand Down
6 changes: 1 addition & 5 deletions examples/batched/batched.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ static void print_usage(int, char ** argv) {
}

int main(int argc, char ** argv) {
llama_log_set([](ggml_log_level level, const char * text, void * /*user_data*/) {
if (LOG_DEFAULT_LLAMA <= gpt_log_verbosity_env) {
gpt_log_add(gpt_log_main(), level, "%s", text);
}
}, NULL);
gpt_log_init();

gpt_params params;

Expand Down
7 changes: 2 additions & 5 deletions examples/convert-llama2c-to-ggml/convert-llama2c-to-ggml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -872,16 +872,13 @@ static std::string basename(const std::string &path) {
}

int main(int argc, char ** argv) {
llama_log_set([](ggml_log_level level, const char * text, void * /*user_data*/) {
if (LOG_DEFAULT_LLAMA <= gpt_log_verbosity_env) {
gpt_log_add(gpt_log_main(), level, "%s", text);
}
}, NULL);
gpt_init();

struct train_params params = get_default_train_params();
if (!params_parse(argc, argv, &params)) {
return 1;
}

Config config;
TransformerWeights weights = {};
{
Expand Down
8 changes: 1 addition & 7 deletions examples/embedding/embedding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,7 @@ static void batch_decode(llama_context * ctx, llama_batch & batch, float * outpu
}

int main(int argc, char ** argv) {
llama_log_set([](ggml_log_level level, const char * text, void * /*user_data*/) {
if (LOG_DEFAULT_LLAMA <= gpt_log_verbosity_env) {
gpt_log_add(gpt_log_main(), level, "%s", text);
}
}, NULL);
gpt_log_init();

gpt_params params;

Expand All @@ -95,8 +91,6 @@ int main(int argc, char ** argv) {
// For non-causal models, batch size must be equal to ubatch size
params.n_ubatch = params.n_batch;

print_build_info();

llama_backend_init();
llama_numa_init(params.numa);

Expand Down
8 changes: 1 addition & 7 deletions examples/eval-callback/eval-callback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,7 @@ static bool run(llama_context * ctx, const gpt_params & params) {
}

int main(int argc, char ** argv) {
llama_log_set([](ggml_log_level level, const char * text, void * /*user_data*/) {
if (LOG_DEFAULT_LLAMA <= gpt_log_verbosity_env) {
gpt_log_add(gpt_log_main(), level, "%s", text);
}
}, NULL);
gpt_init();

callback_data cb_data;

Expand All @@ -154,8 +150,6 @@ int main(int argc, char ** argv) {
return 1;
}

print_build_info();

llama_backend_init();
llama_numa_init(params.numa);

Expand Down
8 changes: 2 additions & 6 deletions examples/imatrix/imatrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ static bool compute_imatrix(llama_context * ctx, const gpt_params & params) {

if (params.compute_ppl) {
const int first = n_ctx/2;
const auto all_logits = num_batches > 1 ? logits.data() : llama_get_logits(ctx);
const auto * all_logits = num_batches > 1 ? logits.data() : llama_get_logits(ctx);
process_logits(n_vocab, all_logits + first*n_vocab, tokens.data() + start + first, n_ctx - 1 - first,
workers, nll, nll2, logit_history.data() + start + first, prob_history.data() + start + first);
count += n_ctx - first - 1;
Expand Down Expand Up @@ -573,11 +573,7 @@ static bool compute_imatrix(llama_context * ctx, const gpt_params & params) {
}

int main(int argc, char ** argv) {
llama_log_set([](ggml_log_level level, const char * text, void * /*user_data*/) {
if (LOG_DEFAULT_LLAMA <= gpt_log_verbosity_env) {
gpt_log_add(gpt_log_main(), level, "%s", text);
}
}, NULL);
gpt_init();

gpt_params params;

Expand Down
8 changes: 1 addition & 7 deletions examples/infill/infill.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,7 @@ static void sigint_handler(int signo) {
#endif

int main(int argc, char ** argv) {
llama_log_set([](ggml_log_level level, const char * text, void * /*user_data*/) {
if (LOG_DEFAULT_LLAMA <= gpt_log_verbosity_env) {
gpt_log_add(gpt_log_main(), level, "%s", text);
}
}, NULL);
gpt_init();

gpt_params params;
g_params = &params;
Expand Down Expand Up @@ -159,8 +155,6 @@ int main(int argc, char ** argv) {
LOG_WRN("%s: scaling RoPE frequency by %g.\n", __func__, params.rope_freq_scale);
}

print_build_info();

LOG_INF("%s: llama backend init\n", __func__);
llama_backend_init();
llama_numa_init(params.numa);
Expand Down
6 changes: 1 addition & 5 deletions examples/llava/llava-cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,7 @@ static void llava_free(struct llava_context * ctx_llava) {
}

int main(int argc, char ** argv) {
llama_log_set([](ggml_log_level level, const char * text, void * /*user_data*/) {
if (LOG_DEFAULT_LLAMA <= gpt_log_verbosity_env) {
gpt_log_add(gpt_log_main(), level, "%s", text);
}
}, NULL);
gpt_init();

ggml_time_init();

Expand Down
6 changes: 1 addition & 5 deletions examples/llava/minicpmv-cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,7 @@ static const char * llama_loop(struct llava_context * ctx_llava,struct gpt_sampl
}

int main(int argc, char ** argv) {
llama_log_set([](ggml_log_level level, const char * text, void * /*user_data*/) {
if (LOG_DEFAULT_LLAMA <= gpt_log_verbosity_env) {
gpt_log_add(gpt_log_main(), level, "%s", text);
}
}, NULL);
gpt_init();

ggml_time_init();

Expand Down
6 changes: 1 addition & 5 deletions examples/lookahead/lookahead.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,7 @@ struct ngram_container {
};

int main(int argc, char ** argv) {
llama_log_set([](ggml_log_level level, const char * text, void * /*user_data*/) {
if (LOG_DEFAULT_LLAMA <= gpt_log_verbosity_env) {
gpt_log_add(gpt_log_main(), level, "%s", text);
}
}, NULL);
gpt_init();

gpt_params params;

Expand Down
6 changes: 1 addition & 5 deletions examples/lookup/lookup-stats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@
#include <vector>

int main(int argc, char ** argv){
llama_log_set([](ggml_log_level level, const char * text, void * /*user_data*/) {
if (LOG_DEFAULT_LLAMA <= gpt_log_verbosity_env) {
gpt_log_add(gpt_log_main(), level, "%s", text);
}
}, NULL);
gpt_init();

gpt_params params;

Expand Down
6 changes: 1 addition & 5 deletions examples/lookup/lookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@
#include <vector>

int main(int argc, char ** argv){
llama_log_set([](ggml_log_level level, const char * text, void * /*user_data*/) {
if (LOG_DEFAULT_LLAMA <= gpt_log_verbosity_env) {
gpt_log_add(gpt_log_main(), level, "%s", text);
}
}, NULL);
gpt_init();

gpt_params params;

Expand Down
6 changes: 3 additions & 3 deletions examples/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,13 @@ static std::string chat_add_and_format(struct llama_model * model, std::vector<l
llama_chat_msg new_msg{role, content};
auto formatted = llama_chat_format_single(model, g_params->chat_template, chat_msgs, new_msg, role == "user");
chat_msgs.push_back({role, content});
LOG_DBG("formatted: %s\n", formatted.c_str());
LOG_DBG("formatted: '%s'\n", formatted.c_str());
return formatted;
}

int main(int argc, char ** argv) {
gpt_init();

llama_log_set([](ggml_log_level level, const char * text, void * /*user_data*/) {
if (LOG_DEFAULT_LLAMA <= gpt_log_verbosity_env) {
gpt_log_add(gpt_log_main(), level, "%s", text);
Expand Down Expand Up @@ -179,8 +181,6 @@ int main(int argc, char ** argv) {
LOG_WRN("%s: warning: scaling RoPE frequency by %g.\n", __func__, params.rope_freq_scale);
}

print_build_info();

LOG_INF("%s: llama backend init\n", __func__);

llama_backend_init();
Expand Down
6 changes: 1 addition & 5 deletions examples/parallel/parallel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,7 @@ static std::vector<std::string> split_string(const std::string& input, char deli
int main(int argc, char ** argv) {
srand(1234);

llama_log_set([](ggml_log_level level, const char * text, void * /*user_data*/) {
if (LOG_DEFAULT_LLAMA <= gpt_log_verbosity_env) {
gpt_log_add(gpt_log_main(), level, "%s", text);
}
}, NULL);
gpt_init();

gpt_params params;

Expand Down
6 changes: 1 addition & 5 deletions examples/passkey/passkey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ static void print_usage(int, char ** argv) {
}

int main(int argc, char ** argv) {
llama_log_set([](ggml_log_level level, const char * text, void * /*user_data*/) {
if (LOG_DEFAULT_LLAMA <= gpt_log_verbosity_env) {
gpt_log_add(gpt_log_main(), level, "%s", text);
}
}, NULL);
gpt_init();

gpt_params params;

Expand Down
8 changes: 1 addition & 7 deletions examples/perplexity/perplexity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1957,11 +1957,7 @@ static void kl_divergence(llama_context * ctx, const gpt_params & params) {
}

int main(int argc, char ** argv) {
llama_log_set([](ggml_log_level level, const char * text, void * /*user_data*/) {
if (LOG_DEFAULT_LLAMA <= gpt_log_verbosity_env) {
gpt_log_add(gpt_log_main(), level, "%s", text);
}
}, NULL);
gpt_init();

gpt_params params;

Expand Down Expand Up @@ -2005,8 +2001,6 @@ int main(int argc, char ** argv) {
params.n_ctx += params.ppl_stride/2;
}

print_build_info();

llama_backend_init();
llama_numa_init(params.numa);

Expand Down
8 changes: 1 addition & 7 deletions examples/retrieval/retrieval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,7 @@ static void batch_decode(llama_context * ctx, llama_batch & batch, float * outpu
}

int main(int argc, char ** argv) {
llama_log_set([](ggml_log_level level, const char * text, void * /*user_data*/) {
if (LOG_DEFAULT_LLAMA <= gpt_log_verbosity_env) {
gpt_log_add(gpt_log_main(), level, "%s", text);
}
}, NULL);
gpt_init();

gpt_params params;

Expand All @@ -137,8 +133,6 @@ int main(int argc, char ** argv) {
return 1;
}

print_build_info();

LOG_INF("processing files:\n");
for (auto & context_file : params.context_files) {
LOG_INF("%s\n", context_file.c_str());
Expand Down
10 changes: 2 additions & 8 deletions examples/server/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2372,11 +2372,7 @@ inline void signal_handler(int signal) {
}

int main(int argc, char ** argv) {
llama_log_set([](ggml_log_level level, const char * text, void * /*user_data*/) {
if (LOG_DEFAULT_LLAMA <= gpt_log_verbosity_env) {
gpt_log_add(gpt_log_main(), level, "%s", text);
}
}, NULL);
gpt_init();

// own arguments required by this example
gpt_params params;
Expand All @@ -2401,8 +2397,6 @@ int main(int argc, char ** argv) {
llama_backend_init();
llama_numa_init(params.numa);

LOG_INF("build: %d %s\n", LLAMA_BUILD_NUMBER, LLAMA_COMMIT);

LOG_INF("system info: n_threads = %d, n_threads_batch = %d, total_threads = %d\n", params.cpuparams.n_threads, params.cpuparams_batch.n_threads, std::thread::hardware_concurrency());
LOG_INF("\n");
LOG_INF("%s\n", gpt_params_get_system_info(params).c_str());
Expand Down Expand Up @@ -3193,7 +3187,7 @@ int main(int argc, char ** argv) {
}

// print sample chat example to make it clear which template is used
LOG_INF("%s: chat template, built_in: %d, chat_example: %s\n", __func__, params.chat_template.empty(), llama_chat_format_example(ctx_server.model, params.chat_template).c_str());
LOG_INF("%s: chat template, built_in: %d, chat_example: '%s\n'", __func__, params.chat_template.empty(), llama_chat_format_example(ctx_server.model, params.chat_template).c_str());

ctx_server.queue_tasks.on_new_task(std::bind(
&server_context::process_single_task, &ctx_server, std::placeholders::_1));
Expand Down
5 changes: 3 additions & 2 deletions examples/server/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ inline std::string format_chat(const struct llama_model * model, const std::stri
}

const auto formatted_chat = llama_chat_apply_template(model, tmpl, chat, true);
LOG_DBG("formatted_chat: %s\n", formatted_chat.c_str());
LOG_DBG("formatted_chat: '%s'\n", formatted_chat.c_str());

return formatted_chat;
}
Expand Down Expand Up @@ -295,7 +295,8 @@ static json probs_vector_to_json(const llama_context * ctx, const std::vector<co
static bool server_sent_event(httplib::DataSink & sink, const char * event, const json & data) {
const std::string str =
std::string(event) + ": " +
data.dump(-1, ' ', false, json::error_handler_t::replace) + "\n\n";
data.dump(-1, ' ', false, json::error_handler_t::replace) +
"\n\n"; // note: these newlines are important (not sure why though, if you know, add a comment to explain)

LOG_DBG("data stream, to_send: %s", str.c_str());

Expand Down
6 changes: 1 addition & 5 deletions examples/simple/simple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ static void print_usage(int, char ** argv) {
}

int main(int argc, char ** argv) {
llama_log_set([](ggml_log_level level, const char * text, void * /*user_data*/) {
if (LOG_DEFAULT_LLAMA <= gpt_log_verbosity_env) {
gpt_log_add(gpt_log_main(), level, "%s", text);
}
}, NULL);
gpt_init();

gpt_params params;

Expand Down
Loading

0 comments on commit 2dff6f4

Please sign in to comment.