Skip to content

Commit d206f87

Browse files
committed
cont : llama-cli + common [no ci]
1 parent c1845a9 commit d206f87

File tree

10 files changed

+299
-207
lines changed

10 files changed

+299
-207
lines changed

common/arg.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1950,8 +1950,6 @@ gpt_params_context gpt_params_parser_init(gpt_params & params, llama_example ex,
19501950
else { std::invalid_argument("invalid value"); }
19511951
}
19521952
).set_examples({LLAMA_EXAMPLE_BENCH}));
1953-
#ifndef LOG_DISABLE_LOGS
1954-
// TODO: make this looks less weird
19551953
add_opt(llama_arg(
19561954
{"--log-disable"},
19571955
"Log disable",
@@ -1966,7 +1964,6 @@ gpt_params_context gpt_params_parser_init(gpt_params & params, llama_example ex,
19661964
gpt_log_set_file(gpt_log_main(), value.c_str());
19671965
}
19681966
));
1969-
#endif // LOG_DISABLE_LOGS
19701967

19711968
return ctx_arg;
19721969
}

common/common.cpp

Lines changed: 117 additions & 52 deletions
Large diffs are not rendered by default.

common/common.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,9 @@ static std::vector<T> string_split(const std::string & str, char delim) {
374374
bool string_parse_kv_override(const char * data, std::vector<llama_model_kv_override> & overrides);
375375
void string_process_escapes(std::string & input);
376376

377+
std::string string_from_tokens(const struct llama_context * ctx, const std::vector<llama_token> & tokens);
378+
std::string string_from_batch (const struct llama_context * ctx, const struct llama_batch & batch);
379+
377380
//
378381
// Filesystem utils
379382
//

common/log.cpp

Lines changed: 56 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
#include "log.h"
22

3-
#include <thread>
4-
#include <mutex>
5-
#include <cstdio>
63
#include <condition_variable>
4+
#include <cstdio>
5+
#include <mutex>
6+
#include <thread>
7+
8+
int gpt_log_verbosity_env = getenv("LLAMA_LOG") ? atoi(getenv("LLAMA_LOG")) : LOG_DEFAULT_LLAMA;
79

810
#define LOG_COLORS // TMP
911

@@ -36,46 +38,56 @@ static int64_t t_us() {
3638
struct gpt_log_entry {
3739
enum ggml_log_level level;
3840

39-
int verbosity;
4041
int64_t timestamp;
4142

4243
std::vector<char> msg;
4344

4445
// signals the worker thread to stop
4546
bool is_end;
4647

47-
void print(FILE * file) {
48+
void print(FILE * file = nullptr) const {
49+
FILE * fcur = file;
50+
if (!fcur) {
51+
// stderr displays DBG messages only when the verbosity is high
52+
// these messages can still be logged to a file
53+
if (level == GGML_LOG_LEVEL_DEBUG && gpt_log_verbosity_env < LOG_DEFAULT_DEBUG) {
54+
return;
55+
}
56+
57+
fcur = stdout;
58+
59+
if (level != GGML_LOG_LEVEL_NONE) {
60+
fcur = stderr;
61+
}
62+
}
63+
4864
if (level != GGML_LOG_LEVEL_NONE) {
4965
if (timestamp) {
5066
// [M.s.ms.us]
51-
fprintf(file, "[%04d.%02d.%03d.%03d] ",
67+
fprintf(fcur, "" LOG_COL_BLUE "%05d.%02d.%03d.%03d" LOG_COL_DEFAULT " ",
5268
(int) (timestamp / 1000000 / 60),
5369
(int) (timestamp / 1000000 % 60),
5470
(int) (timestamp / 1000 % 1000),
5571
(int) (timestamp % 1000));
5672
}
5773

5874
switch (level) {
59-
case GGML_LOG_LEVEL_INFO:
60-
fprintf(file, LOG_COL_GREEN "INF " LOG_COL_DEFAULT);
61-
break;
62-
case GGML_LOG_LEVEL_WARN:
63-
fprintf(file, LOG_COL_MAGENTA "WRN " LOG_COL_DEFAULT);
64-
break;
65-
case GGML_LOG_LEVEL_ERROR:
66-
fprintf(file, LOG_COL_RED "ERR " LOG_COL_DEFAULT);
67-
break;
68-
case GGML_LOG_LEVEL_DEBUG:
69-
fprintf(file, LOG_COL_YELLOW "DBG " LOG_COL_DEFAULT);
70-
break;
75+
case GGML_LOG_LEVEL_INFO: fprintf(fcur, LOG_COL_GREEN "I " LOG_COL_DEFAULT); break;
76+
case GGML_LOG_LEVEL_WARN: fprintf(fcur, LOG_COL_MAGENTA "W " ); break;
77+
case GGML_LOG_LEVEL_ERROR: fprintf(fcur, LOG_COL_RED "E " ); break;
78+
case GGML_LOG_LEVEL_DEBUG: fprintf(fcur, LOG_COL_YELLOW "D " ); break;
7179
default:
7280
break;
7381
}
7482
}
7583

76-
fprintf(file, "%s", msg.data());
84+
fprintf(fcur, "%s", msg.data());
7785

78-
fflush(file);
86+
if (level == GGML_LOG_LEVEL_WARN || level == GGML_LOG_LEVEL_ERROR || level == GGML_LOG_LEVEL_DEBUG) {
87+
fprintf(fcur, LOG_COL_DEFAULT);
88+
}
89+
90+
fflush(fcur);
7991
}
8092
};
8193

@@ -120,7 +132,7 @@ struct gpt_log {
120132
gpt_log_entry cur;
121133

122134
public:
123-
void add(enum ggml_log_level level, int verbosity, const char * fmt, va_list args) {
135+
void add(enum ggml_log_level level, const char * fmt, va_list args) {
124136
std::lock_guard<std::mutex> lock(mtx);
125137

126138
if (!running) {
@@ -130,15 +142,34 @@ struct gpt_log {
130142
auto & entry = entries[tail];
131143

132144
{
145+
#if 1
133146
const size_t n = vsnprintf(entry.msg.data(), entry.msg.size(), fmt, args);
134147
if (n >= entry.msg.size()) {
135148
entry.msg.resize(n + 1);
136149
vsnprintf(entry.msg.data(), entry.msg.size(), fmt, args);
137150
}
151+
#else
152+
// hack for bolding arguments
153+
154+
std::stringstream ss;
155+
for (int i = 0; fmt[i] != 0; i++) {
156+
if (fmt[i] == '%') {
157+
ss << LOG_COL_BOLD;
158+
while (fmt[i] != ' ' && fmt[i] != ')' && fmt[i] != ']' && fmt[i] != 0) ss << fmt[i++];
159+
ss << LOG_COL_DEFAULT;
160+
if (fmt[i] == 0) break;
161+
}
162+
ss << fmt[i];
163+
}
164+
const size_t n = vsnprintf(entry.msg.data(), entry.msg.size(), ss.str().c_str(), args);
165+
if (n >= entry.msg.size()) {
166+
entry.msg.resize(n + 1);
167+
vsnprintf(entry.msg.data(), entry.msg.size(), ss.str().c_str(), args);
168+
}
169+
#endif
138170
}
139171

140172
entry.level = level;
141-
entry.verbosity = verbosity;
142173
entry.timestamp = 0;
143174
if (timestamps) {
144175
entry.timestamp = t_us() - t_start;
@@ -192,7 +223,7 @@ struct gpt_log {
192223
break;
193224
}
194225

195-
cur.print(stdout);
226+
cur.print(); // stdout and stderr
196227

197228
if (file) {
198229
cur.print(file);
@@ -267,10 +298,10 @@ void gpt_log_free(struct gpt_log * log) {
267298
delete log;
268299
}
269300

270-
void gpt_log_add(struct gpt_log * log, enum ggml_log_level level, int verbosity, const char * fmt, ...) {
301+
void gpt_log_add(struct gpt_log * log, enum ggml_log_level level, const char * fmt, ...) {
271302
va_list args;
272303
va_start(args, fmt);
273-
log->add(level, verbosity, fmt, args);
304+
log->add(level, fmt, args);
274305
va_end(args);
275306
}
276307

common/log.h

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
#include "ggml.h"
44

5-
#include <cstdarg>
6-
75
#ifndef __GNUC__
86
# define LOG_ATTRIBUTE_FORMAT(...)
97
#elif defined(__MINGW32__)
@@ -12,9 +10,11 @@
1210
# define LOG_ATTRIBUTE_FORMAT(...) __attribute__((format(printf, __VA_ARGS__)))
1311
#endif
1412

15-
#ifndef LOG_VERBOSITY
16-
#define LOG_VERBOSITY 10
17-
#endif
13+
#define LOG_DEFAULT_DEBUG 10
14+
#define LOG_DEFAULT_LLAMA 5
15+
16+
// intialized in log.cpp from environment variable LLAMA_LOG
17+
extern int gpt_log_verbosity_env;
1818

1919
struct gpt_log;
2020

@@ -24,30 +24,28 @@ void gpt_log_pause (struct gpt_log * log);
2424
void gpt_log_resume(struct gpt_log * log);
2525
void gpt_log_free (struct gpt_log * log);
2626

27-
LOG_ATTRIBUTE_FORMAT(4, 5)
28-
void gpt_log_add(struct gpt_log * log, enum ggml_log_level level, int verbosity, const char * fmt, ...);
27+
LOG_ATTRIBUTE_FORMAT(3, 4)
28+
void gpt_log_add(struct gpt_log * log, enum ggml_log_level level, const char * fmt, ...);
2929

3030
void gpt_log_set_file (struct gpt_log * log, const char * file); // not thread-safe
3131
void gpt_log_set_timestamps(struct gpt_log * log, bool timestamps);
3232

3333
#define LOG_TMPL(level, verbosity, ...) \
3434
do { \
35-
if ((verbosity) <= LOG_VERBOSITY) { \
36-
gpt_log_add(gpt_log_main(), (level), (verbosity), __VA_ARGS__); \
35+
if ((verbosity) <= gpt_log_verbosity_env) { \
36+
gpt_log_add(gpt_log_main(), (level), __VA_ARGS__); \
3737
} \
3838
} while (0)
3939

4040
#define LOG(...) LOG_TMPL(GGML_LOG_LEVEL_NONE, 0, __VA_ARGS__)
4141
#define LOGV(verbosity, ...) LOG_TMPL(GGML_LOG_LEVEL_NONE, verbosity, __VA_ARGS__)
4242

43-
#define LOG_INF(...) LOG_TMPL(GGML_LOG_LEVEL_INFO, 0, __VA_ARGS__)
44-
#define LOG_WRN(...) LOG_TMPL(GGML_LOG_LEVEL_WARN, 0, __VA_ARGS__)
45-
#define LOG_ERR(...) LOG_TMPL(GGML_LOG_LEVEL_ERROR, 0, __VA_ARGS__)
46-
#define LOG_DBG(...) LOG_TMPL(GGML_LOG_LEVEL_DEBUG, 0, __VA_ARGS__)
43+
#define LOG_INF(...) LOG_TMPL(GGML_LOG_LEVEL_INFO, 0, __VA_ARGS__)
44+
#define LOG_WRN(...) LOG_TMPL(GGML_LOG_LEVEL_WARN, 0, __VA_ARGS__)
45+
#define LOG_ERR(...) LOG_TMPL(GGML_LOG_LEVEL_ERROR, 0, __VA_ARGS__)
46+
#define LOG_DBG(...) LOG_TMPL(GGML_LOG_LEVEL_DEBUG, LOG_DEFAULT_DEBUG, __VA_ARGS__)
4747

4848
#define LOG_INFV(verbosity, ...) LOG_TMPL(GGML_LOG_LEVEL_INFO, verbosity, __VA_ARGS__)
4949
#define LOG_WRNV(verbosity, ...) LOG_TMPL(GGML_LOG_LEVEL_WARN, verbosity, __VA_ARGS__)
5050
#define LOG_ERRV(verbosity, ...) LOG_TMPL(GGML_LOG_LEVEL_ERROR, verbosity, __VA_ARGS__)
5151
#define LOG_DBGV(verbosity, ...) LOG_TMPL(GGML_LOG_LEVEL_DEBUG, verbosity, __VA_ARGS__)
52-
53-
#define LOG_TOKENS_TOSTR_PRETTY(...) std::string("dummy")

common/sampling.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ llama_token gpt_sampler_last(const struct gpt_sampler * gsmpl) {
325325
}
326326

327327
std::string gpt_sampler_print(const struct gpt_sampler * gsmpl) {
328-
std::string result = "\tlogits ";
328+
std::string result = "logits ";
329329

330330
for (int i = 0; i < llama_sampler_chain_n(gsmpl->chain); i++) {
331331
const auto * smpl = llama_sampler_chain_get(gsmpl->chain, i);

0 commit comments

Comments
 (0)