Skip to content

Commit 671d5ca

Browse files
authored
Use fprintf for diagnostic output (#48)
keep printf only for printing model output one can now use ./main ... 2>dev/null to suppress any diagnostic output
1 parent 84d9015 commit 671d5ca

File tree

1 file changed

+46
-46
lines changed

1 file changed

+46
-46
lines changed

main.cpp

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ struct llama_model {
8585

8686
// load the model's weights from a file
8787
bool llama_model_load(const std::string & fname, llama_model & model, gpt_vocab & vocab, int n_ctx) {
88-
printf("%s: loading model from '%s' - please wait ...\n", __func__, fname.c_str());
88+
fprintf(stderr, "%s: loading model from '%s' - please wait ...\n", __func__, fname.c_str());
8989

9090
std::vector<char> f_buf(1024*1024);
9191

@@ -127,16 +127,16 @@ bool llama_model_load(const std::string & fname, llama_model & model, gpt_vocab
127127
n_ff = ((2*(4*hparams.n_embd)/3 + hparams.n_mult - 1)/hparams.n_mult)*hparams.n_mult;
128128
n_parts = LLAMA_N_PARTS.at(hparams.n_embd);
129129

130-
printf("%s: n_vocab = %d\n", __func__, hparams.n_vocab);
131-
printf("%s: n_ctx = %d\n", __func__, hparams.n_ctx);
132-
printf("%s: n_embd = %d\n", __func__, hparams.n_embd);
133-
printf("%s: n_mult = %d\n", __func__, hparams.n_mult);
134-
printf("%s: n_head = %d\n", __func__, hparams.n_head);
135-
printf("%s: n_layer = %d\n", __func__, hparams.n_layer);
136-
printf("%s: n_rot = %d\n", __func__, hparams.n_rot);
137-
printf("%s: f16 = %d\n", __func__, hparams.f16);
138-
printf("%s: n_ff = %d\n", __func__, n_ff);
139-
printf("%s: n_parts = %d\n", __func__, n_parts);
130+
fprintf(stderr, "%s: n_vocab = %d\n", __func__, hparams.n_vocab);
131+
fprintf(stderr, "%s: n_ctx = %d\n", __func__, hparams.n_ctx);
132+
fprintf(stderr, "%s: n_embd = %d\n", __func__, hparams.n_embd);
133+
fprintf(stderr, "%s: n_mult = %d\n", __func__, hparams.n_mult);
134+
fprintf(stderr, "%s: n_head = %d\n", __func__, hparams.n_head);
135+
fprintf(stderr, "%s: n_layer = %d\n", __func__, hparams.n_layer);
136+
fprintf(stderr, "%s: n_rot = %d\n", __func__, hparams.n_rot);
137+
fprintf(stderr, "%s: f16 = %d\n", __func__, hparams.f16);
138+
fprintf(stderr, "%s: n_ff = %d\n", __func__, n_ff);
139+
fprintf(stderr, "%s: n_parts = %d\n", __func__, n_parts);
140140
}
141141

142142
// load vocab
@@ -161,7 +161,7 @@ bool llama_model_load(const std::string & fname, llama_model & model, gpt_vocab
161161
vocab.id_to_token[i] = word;
162162

163163
//if (i < 30000) {
164-
// printf("%s: vocab[%d] = '%s'\n", __func__, i, word.c_str());
164+
// fprintf(stderr, "%s: vocab[%d] = '%s'\n", __func__, i, word.c_str());
165165
//}
166166
}
167167
}
@@ -220,7 +220,7 @@ bool llama_model_load(const std::string & fname, llama_model & model, gpt_vocab
220220

221221
ctx_size += (5 + 10*n_layer)*256; // object overhead
222222

223-
printf("%s: ggml ctx size = %6.2f MB\n", __func__, ctx_size/(1024.0*1024.0));
223+
fprintf(stderr, "%s: ggml ctx size = %6.2f MB\n", __func__, ctx_size/(1024.0*1024.0));
224224
}
225225

226226
// create the ggml context
@@ -307,7 +307,7 @@ bool llama_model_load(const std::string & fname, llama_model & model, gpt_vocab
307307

308308
const size_t memory_size = ggml_nbytes(model.memory_k) + ggml_nbytes(model.memory_v);
309309

310-
printf("%s: memory_size = %8.2f MB, n_mem = %d\n", __func__, memory_size/1024.0/1024.0, n_mem);
310+
fprintf(stderr, "%s: memory_size = %8.2f MB, n_mem = %d\n", __func__, memory_size/1024.0/1024.0, n_mem);
311311
}
312312

313313
const size_t file_offset = fin.tellg();
@@ -325,7 +325,7 @@ bool llama_model_load(const std::string & fname, llama_model & model, gpt_vocab
325325
fname_part += "." + std::to_string(i);
326326
}
327327

328-
printf("%s: loading model part %d/%d from '%s'\n", __func__, i+1, n_parts, fname_part.c_str());
328+
fprintf(stderr, "%s: loading model part %d/%d from '%s'\n", __func__, i+1, n_parts, fname_part.c_str());
329329

330330
fin = std::ifstream(fname_part, std::ios::binary);
331331
fin.rdbuf()->pubsetbuf(f_buf.data(), f_buf.size());
@@ -336,7 +336,7 @@ bool llama_model_load(const std::string & fname, llama_model & model, gpt_vocab
336336
int n_tensors = 0;
337337
size_t total_size = 0;
338338

339-
printf("%s: ", __func__);
339+
fprintf(stderr, "%s: ", __func__);
340340

341341
while (true) {
342342
int32_t n_dims;
@@ -436,7 +436,7 @@ bool llama_model_load(const std::string & fname, llama_model & model, gpt_vocab
436436

437437
if (0) {
438438
static const char * ftype_str[] = { "f32", "f16", "q4_0", "q4_1", };
439-
printf("%24s - [%5d, %5d], type = %6s, split = %d\n", name.data(), ne[0], ne[1], ftype_str[ftype], split_type);
439+
fprintf(stderr, "%24s - [%5d, %5d], type = %6s, split = %d\n", name.data(), ne[0], ne[1], ftype_str[ftype], split_type);
440440
}
441441

442442
size_t bpe = 0;
@@ -499,16 +499,16 @@ bool llama_model_load(const std::string & fname, llama_model & model, gpt_vocab
499499
total_size += ggml_nbytes(tensor)/n_parts;
500500
}
501501

502-
//printf("%42s - [%5d, %5d], type = %6s, %6.2f MB\n", name.data(), ne[0], ne[1], ftype == 0 ? "float" : "f16", ggml_nbytes(tensor)/1024.0/1024.0);
502+
//fprintf(stderr, "%42s - [%5d, %5d], type = %6s, %6.2f MB\n", name.data(), ne[0], ne[1], ftype == 0 ? "float" : "f16", ggml_nbytes(tensor)/1024.0/1024.0);
503503
if (++n_tensors % 8 == 0) {
504-
printf(".");
505-
fflush(stdout);
504+
fprintf(stderr, ".");
505+
fflush(stderr);
506506
}
507507
}
508508

509-
printf(" done\n");
509+
fprintf(stderr, " done\n");
510510

511-
printf("%s: model size = %8.2f MB / num tensors = %d\n", __func__, total_size/1024.0/1024.0, n_tensors);
511+
fprintf(stderr, "%s: model size = %8.2f MB / num tensors = %d\n", __func__, total_size/1024.0/1024.0, n_tensors);
512512
}
513513

514514
fin.close();
@@ -552,7 +552,7 @@ bool llama_eval(
552552

553553
if (mem_per_token > 0 && mem_per_token*N > buf_size) {
554554
const size_t buf_size_new = 1.1*(mem_per_token*N); // add 10% to account for ggml object overhead
555-
//printf("\n%s: reallocating buffer from %zu to %zu bytes\n", __func__, buf_size, buf_size_new);
555+
//fprintf(stderr, "\n%s: reallocating buffer from %zu to %zu bytes\n", __func__, buf_size, buf_size_new);
556556

557557
// reallocate
558558
buf_size = buf_size_new;
@@ -744,7 +744,7 @@ bool llama_eval(
744744
if (mem_per_token == 0) {
745745
mem_per_token = ggml_used_mem(ctx0)/N;
746746
}
747-
//printf("used_mem = %zu\n", ggml_used_mem(ctx0));
747+
//fprintf(stderr, "used_mem = %zu\n", ggml_used_mem(ctx0));
748748

749749
ggml_free(ctx0);
750750

@@ -780,7 +780,7 @@ int main(int argc, char ** argv) {
780780
params.seed = time(NULL);
781781
}
782782

783-
printf("%s: seed = %d\n", __func__, params.seed);
783+
fprintf(stderr, "%s: seed = %d\n", __func__, params.seed);
784784

785785
std::mt19937 rng(params.seed);
786786
if (params.prompt.empty()) {
@@ -822,13 +822,13 @@ int main(int argc, char ** argv) {
822822
// tokenize the reverse prompt
823823
std::vector<gpt_vocab::id> antiprompt_inp = ::llama_tokenize(vocab, params.antiprompt, false);
824824

825-
printf("\n");
826-
printf("%s: prompt: '%s'\n", __func__, params.prompt.c_str());
827-
printf("%s: number of tokens in prompt = %zu\n", __func__, embd_inp.size());
825+
fprintf(stderr, "\n");
826+
fprintf(stderr, "%s: prompt: '%s'\n", __func__, params.prompt.c_str());
827+
fprintf(stderr, "%s: number of tokens in prompt = %zu\n", __func__, embd_inp.size());
828828
for (int i = 0; i < (int) embd_inp.size(); i++) {
829-
printf("%6d -> '%s'\n", embd_inp[i], vocab.id_to_token.at(embd_inp[i]).c_str());
829+
fprintf(stderr, "%6d -> '%s'\n", embd_inp[i], vocab.id_to_token.at(embd_inp[i]).c_str());
830830
}
831-
printf("\n");
831+
fprintf(stderr, "\n");
832832
if (params.interactive) {
833833
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
834834
struct sigaction sigint_action;
@@ -838,19 +838,19 @@ int main(int argc, char ** argv) {
838838
sigaction(SIGINT, &sigint_action, NULL);
839839
#endif
840840

841-
printf("%s: interactive mode on.\n", __func__);
841+
fprintf(stderr, "%s: interactive mode on.\n", __func__);
842842

843843
if(antiprompt_inp.size()) {
844-
printf("%s: reverse prompt: '%s'\n", __func__, params.antiprompt.c_str());
845-
printf("%s: number of tokens in reverse prompt = %zu\n", __func__, antiprompt_inp.size());
844+
fprintf(stderr, "%s: reverse prompt: '%s'\n", __func__, params.antiprompt.c_str());
845+
fprintf(stderr, "%s: number of tokens in reverse prompt = %zu\n", __func__, antiprompt_inp.size());
846846
for (int i = 0; i < (int) antiprompt_inp.size(); i++) {
847-
printf("%6d -> '%s'\n", antiprompt_inp[i], vocab.id_to_token.at(antiprompt_inp[i]).c_str());
847+
fprintf(stderr, "%6d -> '%s'\n", antiprompt_inp[i], vocab.id_to_token.at(antiprompt_inp[i]).c_str());
848848
}
849-
printf("\n");
849+
fprintf(stderr, "\n");
850850
}
851851
}
852-
printf("sampling parameters: temp = %f, top_k = %d, top_p = %f, repeat_last_n = %i, repeat_penalty = %f\n", params.temp, params.top_k, params.top_p, params.repeat_last_n, params.repeat_penalty);
853-
printf("\n\n");
852+
fprintf(stderr, "sampling parameters: temp = %f, top_k = %d, top_p = %f, repeat_last_n = %i, repeat_penalty = %f\n", params.temp, params.top_k, params.top_p, params.repeat_last_n, params.repeat_penalty);
853+
fprintf(stderr, "\n\n");
854854

855855
std::vector<gpt_vocab::id> embd;
856856

@@ -864,7 +864,7 @@ int main(int argc, char ** argv) {
864864

865865

866866
if (params.interactive) {
867-
printf("== Running in interactive mode. ==\n"
867+
fprintf(stderr, "== Running in interactive mode. ==\n"
868868
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
869869
" - Press Ctrl+C to interject at any time.\n"
870870
#endif
@@ -892,7 +892,7 @@ int main(int argc, char ** argv) {
892892
const int64_t t_start_us = ggml_time_us();
893893

894894
if (!llama_eval(model, params.n_threads, n_past, embd, logits, mem_per_token)) {
895-
printf("Failed to predict\n");
895+
fprintf(stderr, "Failed to predict\n");
896896
return 1;
897897
}
898898

@@ -1005,7 +1005,7 @@ int main(int argc, char ** argv) {
10051005

10061006
// end of text token
10071007
if (embd.back() == 2) {
1008-
printf(" [end of text]\n");
1008+
fprintf(stderr, " [end of text]\n");
10091009
break;
10101010
}
10111011
}
@@ -1015,12 +1015,12 @@ int main(int argc, char ** argv) {
10151015
{
10161016
const int64_t t_main_end_us = ggml_time_us();
10171017

1018-
printf("\n\n");
1019-
printf("%s: mem per token = %8zu bytes\n", __func__, mem_per_token);
1020-
printf("%s: load time = %8.2f ms\n", __func__, t_load_us/1000.0f);
1021-
printf("%s: sample time = %8.2f ms\n", __func__, t_sample_us/1000.0f);
1022-
printf("%s: predict time = %8.2f ms / %.2f ms per token\n", __func__, t_predict_us/1000.0f, t_predict_us/1000.0f/n_past);
1023-
printf("%s: total time = %8.2f ms\n", __func__, (t_main_end_us - t_main_start_us)/1000.0f);
1018+
fprintf(stderr, "\n\n");
1019+
fprintf(stderr, "%s: mem per token = %8zu bytes\n", __func__, mem_per_token);
1020+
fprintf(stderr, "%s: load time = %8.2f ms\n", __func__, t_load_us/1000.0f);
1021+
fprintf(stderr, "%s: sample time = %8.2f ms\n", __func__, t_sample_us/1000.0f);
1022+
fprintf(stderr, "%s: predict time = %8.2f ms / %.2f ms per token\n", __func__, t_predict_us/1000.0f, t_predict_us/1000.0f/n_past);
1023+
fprintf(stderr, "%s: total time = %8.2f ms\n", __func__, (t_main_end_us - t_main_start_us)/1000.0f);
10241024
}
10251025

10261026
ggml_free(model.ctx);

0 commit comments

Comments
 (0)