Skip to content

Commit 8f84210

Browse files
committed
log : add comments + adjust defaults
ggml-ci
1 parent 2afe0a0 commit 8f84210

File tree

7 files changed

+63
-55
lines changed

7 files changed

+63
-55
lines changed

common/arg.cpp

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -385,20 +385,6 @@ gpt_params_context gpt_params_parser_init(gpt_params & params, llama_example ex,
385385
exit(0);
386386
}
387387
));
388-
add_opt(llama_arg(
389-
{"-v", "--verbose"},
390-
"print verbose information",
391-
[](gpt_params & params) {
392-
params.verbosity = 1;
393-
}
394-
));
395-
add_opt(llama_arg(
396-
{"--verbosity"}, "N",
397-
format("set specific verbosity level (default: %d)", params.verbosity),
398-
[](gpt_params & params, int value) {
399-
params.verbosity = value;
400-
}
401-
));
402388
add_opt(llama_arg(
403389
{"--verbose-prompt"},
404390
format("print a verbose prompt before generation (default: %s)", params.verbose_prompt ? "true" : "false"),
@@ -1967,16 +1953,18 @@ gpt_params_context gpt_params_parser_init(gpt_params & params, llama_example ex,
19671953
}
19681954
).set_env("LLAMA_LOG_COLORS"));
19691955
add_opt(llama_arg(
1970-
{"-lv", "--log-verbose"},
1956+
{"-v", "--verbose", "--log-verbose"},
19711957
"Set verbosity level to infinity (i.e. log all messages, useful for debugging)",
1972-
[](gpt_params &) {
1958+
[](gpt_params & params) {
1959+
params.verbosity = INT_MAX;
19731960
gpt_log_set_verbosity_thold(INT_MAX);
19741961
}
19751962
));
19761963
add_opt(llama_arg(
1977-
{"--log-verbosity"}, "THOLD",
1964+
{"-lv", "--verbosity", "--log-verbosity"}, "THOLD",
19781965
"Set the verbosity threshold. Messages with a higher verbosity will be ignored.",
1979-
[](gpt_params &, int value) {
1966+
[](gpt_params & params, int value) {
1967+
params.verbosity = value;
19801968
gpt_log_set_verbosity_thold(value);
19811969
}
19821970
).set_env("LLAMA_LOG_VERBOSITY"));

common/log.cpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,21 @@ struct gpt_log_entry {
113113
};
114114

115115
struct gpt_log {
116+
// default capacity - will be expanded if needed
117+
gpt_log() : gpt_log(256) {}
118+
116119
gpt_log(size_t capacity) {
117120
file = nullptr;
118121
timestamps = false;
119122
running = false;
120123
t_start = t_us();
124+
125+
// initial message size - will be expanded if longer messages arrive
121126
entries.resize(capacity);
122127
for (auto & entry : entries) {
123128
entry.msg.resize(256);
124129
}
130+
125131
head = 0;
126132
tail = 0;
127133

@@ -160,6 +166,7 @@ struct gpt_log {
160166
std::lock_guard<std::mutex> lock(mtx);
161167

162168
if (!running) {
169+
// discard messages while the worker thread is paused
163170
return;
164171
}
165172

@@ -274,11 +281,13 @@ struct gpt_log {
274281

275282
running = false;
276283

277-
auto & entry = entries[tail];
284+
// push an entry to signal the worker thread to stop
285+
{
286+
auto & entry = entries[tail];
287+
entry.is_end = true;
278288

279-
entry.is_end = true;
280-
281-
tail = (tail + 1) % entries.size();
289+
tail = (tail + 1) % entries.size();
290+
}
282291

283292
cv.notify_one();
284293
}
@@ -331,12 +340,16 @@ struct gpt_log {
331340
}
332341
};
333342

343+
//
344+
// public API
345+
//
346+
334347
struct gpt_log * gpt_log_init() {
335-
return new gpt_log{256};
348+
return new gpt_log;
336349
}
337350

338351
struct gpt_log * gpt_log_main() {
339-
static struct gpt_log log{256};
352+
static struct gpt_log log;
340353

341354
return &log;
342355
}

common/log.h

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,23 @@
1010
# define LOG_ATTRIBUTE_FORMAT(...) __attribute__((format(printf, __VA_ARGS__)))
1111
#endif
1212

13-
#define LOG_DEFAULT_DEBUG 10
14-
#define LOG_DEFAULT_LLAMA 5
13+
#define LOG_DEFAULT_DEBUG 1
14+
#define LOG_DEFAULT_LLAMA 0
1515

1616
// needed by the LOG_TMPL macro to avoid computing log arguments if the verbosity lower
1717
// set via gpt_log_set_verbosity()
1818
extern int gpt_log_verbosity_thold;
1919

2020
void gpt_log_set_verbosity_thold(int verbosity); // not thread-safe
2121

22+
// the gpt_log uses an internal worker thread to print/write log messages
23+
// when the worker thread is paused, incoming log messages are discarded
2224
struct gpt_log;
2325

2426
struct gpt_log * gpt_log_init();
25-
struct gpt_log * gpt_log_main();
26-
void gpt_log_pause (struct gpt_log * log);
27-
void gpt_log_resume(struct gpt_log * log);
27+
struct gpt_log * gpt_log_main(); // singleton, automatically destroys itself on exit
28+
void gpt_log_pause (struct gpt_log * log); // pause the worker thread, not thread-safe
29+
void gpt_log_resume(struct gpt_log * log); // resume the worker thread, not thread-safe
2830
void gpt_log_free (struct gpt_log * log);
2931

3032
LOG_ATTRIBUTE_FORMAT(3, 4)
@@ -34,6 +36,16 @@ void gpt_log_set_file (struct gpt_log * log, const char * file); // n
3436
void gpt_log_set_colors (struct gpt_log * log, bool colors); // not thread-safe
3537
void gpt_log_set_timestamps(struct gpt_log * log, bool timestamps);
3638

39+
// helper macros for logging
40+
// use these to avoid computing log arguments if the verbosity is lower than the threshold
41+
//
42+
// for example:
43+
//
44+
// LOG_DBG("this is a debug message: %d\n", expensive_function());
45+
//
46+
// this will avoid calling expensive_function() if the verbosity is lower than LOG_DEFAULT_DEBUG
47+
//
48+
3749
#define LOG_TMPL(level, verbosity, ...) \
3850
do { \
3951
if ((verbosity) <= gpt_log_verbosity_thold) { \

examples/export-lora/export-lora.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ int main(int argc, char ** argv) {
406406
return 1;
407407
}
408408

409-
g_verbose = (params.verbosity == 1);
409+
g_verbose = (params.verbosity > 1);
410410
try {
411411
lora_merge_ctx ctx(params.model, params.lora_adapters, params.lora_outfile, params.cpuparams.n_threads);
412412
ctx.run_merge();

examples/imatrix/imatrix.cpp

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
static void print_usage(int, char ** argv) {
2323
LOG("\nexample usage:\n");
2424
LOG("\n %s \\\n"
25-
" -m model.gguf -f some-text.txt [-o imatrix.dat] [--process-output] [--verbosity 1] \\\n"
25+
" -m model.gguf -f some-text.txt [-o imatrix.dat] [--process-output] \\\n"
2626
" [--no-ppl] [--chunk 123] [--output-frequency 10] [--save-frequency 0] \\\n"
2727
" [--in-file imatrix-prev-0.dat --in-file imatrix-prev-1.dat ...]\n" , argv[0]);
2828
LOG("\n");
@@ -129,9 +129,7 @@ bool IMatrixCollector::collect_imatrix(struct ggml_tensor * t, bool ask, void *
129129
LOG_ERR("%s: inconsistent size for %s (%d vs %d)\n", __func__, wname.c_str(), (int)e.values.size(), (int)src1->ne[0]*n_as);
130130
exit(1); //GGML_ABORT("fatal error");
131131
}
132-
if (m_params.verbosity > 1) {
133-
printf("%s[%d]: %32s, %s, %5d x %5d, %d\n", __func__, m_last_call, wname.c_str(), ggml_op_name(t->op), (int)src1->ne[0], (int)src1->ne[2], (int)src1->type);
134-
}
132+
LOG_DBGV(2, "%s[%d]: %32s, %s, %5d x %5d, %d\n", __func__, m_last_call, wname.c_str(), ggml_op_name(t->op), (int)src1->ne[0], (int)src1->ne[2], (int)src1->type);
135133
// loop over all possible experts, regardless if they are used or not in the batch
136134
for (int ex = 0; ex < n_as; ++ex) {
137135
size_t e_start = ex*src1->ne[0];
@@ -180,9 +178,7 @@ bool IMatrixCollector::collect_imatrix(struct ggml_tensor * t, bool ask, void *
180178
exit(1); //GGML_ABORT("fatal error");
181179
}
182180
++e.ncall;
183-
if (m_params.verbosity > 1) {
184-
LOG("%s[%d]: %32s, %s, %5d x %5d, %d\n", __func__, m_last_call, wname.c_str(), ggml_op_name(t->op), (int)src1->ne[0], (int)src1->ne[1], (int)src1->type);
185-
}
181+
LOG_DBGV(2, "%s[%d]: %32s, %s, %5d x %5d, %d\n", __func__, m_last_call, wname.c_str(), ggml_op_name(t->op), (int)src1->ne[0], (int)src1->ne[1], (int)src1->type);
186182
for (int row = 0; row < (int)src1->ne[1]; ++row) {
187183
const float * x = data + row * src1->ne[0];
188184
for (int j = 0; j < (int)src1->ne[0]; ++j) {
@@ -292,29 +288,28 @@ void IMatrixCollector::save_imatrix(int ncall) const {
292288
out.write(m_params.prompt_file.c_str(), len);
293289
}
294290

295-
if (m_params.verbosity > 0) {
296-
LOG_INF("\n%s: stored collected data after %d chunks in %s\n", __func__, m_last_call, fname.c_str());
297-
}
291+
LOGV(1, "\n");
292+
LOG_DBGV(1, "%s: stored collected data after %d chunks in %s\n", __func__, m_last_call, fname.c_str());
298293
}
299294

300295
bool IMatrixCollector::load_imatrix(const char * fname) {
301296
std::ifstream in(fname, std::ios::binary);
302297
if (!in) {
303-
printf("%s: failed to open %s\n",__func__, fname);
298+
LOG_ERR("%s: failed to open %s\n",__func__, fname);
304299
return false;
305300
}
306301
int n_entries;
307302
in.read((char*)&n_entries, sizeof(n_entries));
308303
if (in.fail() || n_entries < 1) {
309-
printf("%s: no data in file %s\n", __func__, fname);
304+
LOG_ERR("%s: no data in file %s\n", __func__, fname);
310305
return false;
311306
}
312307
for (int i = 0; i < n_entries; ++i) {
313308
int len; in.read((char *)&len, sizeof(len));
314309
std::vector<char> name_as_vec(len+1);
315310
in.read((char *)name_as_vec.data(), len);
316311
if (in.fail()) {
317-
printf("%s: failed reading name for entry %d from %s\n",__func__,i+1, fname);
312+
LOG_ERR("%s: failed reading name for entry %d from %s\n",__func__,i+1, fname);
318313
return false;
319314
}
320315
name_as_vec[len] = 0;
@@ -325,7 +320,7 @@ bool IMatrixCollector::load_imatrix(const char * fname) {
325320
int nval;
326321
in.read((char *)&nval, sizeof(nval));
327322
if (in.fail() || nval < 1) {
328-
printf("%s: failed reading number of values for entry %d\n",__func__,i);
323+
LOG_ERR("%s: failed reading number of values for entry %d\n",__func__,i);
329324
m_stats = {};
330325
return false;
331326
}
@@ -338,7 +333,7 @@ bool IMatrixCollector::load_imatrix(const char * fname) {
338333
std::vector<float> tmp(nval);
339334
in.read((char*)tmp.data(), nval*sizeof(float));
340335
if (in.fail()) {
341-
printf("%s: failed reading data for entry %d\n",__func__,i);
336+
LOG_ERR("%s: failed reading data for entry %d\n",__func__,i);
342337
m_stats = {};
343338
return false;
344339
}
@@ -548,13 +543,13 @@ static bool compute_imatrix(llama_context * ctx, const gpt_params & params) {
548543
workers, nll, nll2, logit_history.data() + start + first, prob_history.data() + start + first);
549544
count += n_ctx - first - 1;
550545

551-
printf("[%d]%.4lf,", i + 1, std::exp(nll / count));
546+
LOG("[%d]%.4lf,", i + 1, std::exp(nll / count));
552547
fflush(stdout);
553548

554549
logits.clear();
555550
}
556551
}
557-
printf("\n");
552+
LOG("\n");
558553

559554
if (params.compute_ppl) {
560555
nll2 /= count;
@@ -563,9 +558,9 @@ static bool compute_imatrix(llama_context * ctx, const gpt_params & params) {
563558
nll2 -= nll * nll;
564559
if (nll2 > 0) {
565560
nll2 = sqrt(nll2/(count-1));
566-
printf("Final estimate: PPL = %.4lf +/- %.5lf\n", ppl, nll2*ppl);
561+
LOG("Final estimate: PPL = %.4lf +/- %.5lf\n", ppl, nll2*ppl);
567562
} else {
568-
printf("Unexpected negative standard deviation of log(prob)\n");
563+
LOG("Unexpected negative standard deviation of log(prob)\n");
569564
}
570565
}
571566

@@ -577,7 +572,6 @@ int main(int argc, char ** argv) {
577572

578573
params.n_ctx = 512;
579574
params.logits_all = true;
580-
params.verbosity = 1;
581575

582576
if (!gpt_params_parse(argc, argv, params, LLAMA_EXAMPLE_IMATRIX, print_usage)) {
583577
return 1;
@@ -590,15 +584,15 @@ int main(int argc, char ** argv) {
590584
g_collector.set_params(params);
591585

592586
for (const auto & in_file : params.in_files) {
593-
printf("%s : loading imatrix from '%s'\n", __func__, in_file.c_str());
587+
LOG_INF("%s : loading imatrix from '%s'\n", __func__, in_file.c_str());
594588
if (!g_collector.load_imatrix(in_file.c_str())) {
595589
LOG_ERR("%s : failed to load %s\n", __func__, in_file.c_str());
596590
return 1;
597591
}
598592
}
599593

600594
if (params.in_files.size() > 1) {
601-
printf("%s : saving combined imatrix to '%s'\n", __func__, params.out_file.c_str());
595+
LOG_INF("%s : saving combined imatrix to '%s'\n", __func__, params.out_file.c_str());
602596
g_collector.save_imatrix();
603597
}
604598

tests/test-arg-parser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ int main(void) {
8585

8686
argv = {"binary_name", "--verbose"};
8787
assert(true == gpt_params_parse(argv.size(), list_str_to_char(argv).data(), params, LLAMA_EXAMPLE_COMMON));
88-
assert(params.verbosity == 1);
88+
assert(params.verbosity > 1);
8989

9090
argv = {"binary_name", "-m", "abc.gguf", "--predict", "6789", "--batch-size", "9090"};
9191
assert(true == gpt_params_parse(argv.size(), list_str_to_char(argv).data(), params, LLAMA_EXAMPLE_COMMON));

tests/test-log.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55

66
int main() {
77
const int n_thread = 8;
8-
const int n_msg = 1000;
98

109
std::thread threads[n_thread];
1110
for (int i = 0; i < n_thread; i++) {
12-
threads[i] = std::thread([i, n_msg]() {
11+
threads[i] = std::thread([i]() {
12+
const int n_msg = 1000;
13+
1314
for (int j = 0; j < n_msg; j++) {
1415
const int log_type = std::rand() % 4;
1516

0 commit comments

Comments
 (0)