Use named LOG_COL_* colors in examples

This commit is contained in:
xndcn 2025-02-06 11:31:41 +08:00
parent 902368a06b
commit 9500f4436a
8 changed files with 20 additions and 17 deletions

View file

@ -1,5 +1,6 @@
#include "unicode.h"
#include "llama-grammar.h"
#include "log.h"
#include <cstdio>
#include <cstdlib>
@ -47,7 +48,7 @@ static void print_error_message(const std::string & input_str, size_t error_pos,
if (error_pos+1 < input_str.size()) {
fprintf(stdout, "\033[0;31m%s", input_str.substr(error_pos+1).c_str());
}
fprintf(stdout, "\033[0m\n");
fprintf(stdout, LOG_COL_DEFAULT "\n");
}
}

View file

@ -295,7 +295,7 @@ int main(int argc, char ** argv) {
LOG("%s", token_str.c_str());
} else {
// print light cyan
LOG("\033[0;96m%s\033[0m", token_str.c_str());
LOG("\033[0;96m%s" LOG_COL_DEFAULT, token_str.c_str());
}
fflush(stdout);

View file

@ -160,7 +160,7 @@ int main(int argc, char ** argv){
if (params.use_color) {
// color accepted draft token
LOG("\033[34m%s\033[0m", token_str.c_str());
LOG(LOG_COL_BLUE "%s" LOG_COL_DEFAULT, token_str.c_str());
fflush(stdout);
}
continue;

View file

@ -647,7 +647,7 @@ int main(int argc, char ** argv) {
LOG_DBG("n_past = %d\n", n_past);
// Display total tokens alongside total time
if (params.n_print > 0 && n_past % params.n_print == 0) {
LOG_DBG("\n\033[31mTokens consumed so far = %d / %d \033[0m\n", n_past, n_ctx);
LOG_DBG("\n" LOG_COL_RED "Tokens consumed so far = %d / %d " LOG_COL_DEFAULT "\n", n_past, n_ctx);
}
}

View file

@ -85,7 +85,7 @@ static void print_date_time() {
strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", local_time);
LOG_INF("\n");
LOG_INF("\033[35mrun parameters as of %s\033[0m\n", buffer);
LOG_INF(LOG_COL_MAGENTA "run parameters as of %s" LOG_COL_DEFAULT "\n", buffer);
LOG_INF("\n");
}
@ -139,11 +139,11 @@ int main(int argc, char ** argv) {
// load the prompts from an external file if there are any
if (params.prompt.empty()) {
LOG_INF("\033[32mNo new questions so proceed with build-in defaults.\033[0m\n");
LOG_INF(LOG_COL_GREEN "No new questions so proceed with build-in defaults." LOG_COL_DEFAULT "\n");
} else {
// Output each line of the input params.prompts vector and copy to k_prompts
int index = 0;
LOG_INF("\033[32mNow printing the external prompt file %s\033[0m\n\n", params.prompt_file.c_str());
LOG_INF(LOG_COL_GREEN "Now printing the external prompt file %s" LOG_COL_DEFAULT "\n\n", params.prompt_file.c_str());
std::vector<std::string> prompts = split_string(params.prompt, '\n');
for (const auto& prompt : prompts) {
@ -273,7 +273,7 @@ int main(int argc, char ** argv) {
client.n_decoded = 0;
client.i_batch = batch.n_tokens - 1;
LOG_INF("\033[31mClient %3d, seq %4d, started decoding ...\033[0m\n", client.id, client.seq_id);
LOG_INF(LOG_COL_RED "Client %3d, seq %4d, started decoding ..." LOG_COL_DEFAULT "\n", client.id, client.seq_id);
g_seq_id += 1;
@ -376,7 +376,7 @@ int main(int argc, char ** argv) {
const auto t_main_end = ggml_time_us();
LOG_INF("\033[31mClient %3d, seq %3d/%3d, prompt %4d t, response %4d t, time %5.2f s, speed %5.2f t/s, cache miss %d \033[0m \n\nInput: %s\n\033[35mResponse: %s\033[0m\n\n",
LOG_INF(LOG_COL_RED "Client %3d, seq %3d/%3d, prompt %4d t, response %4d t, time %5.2f s, speed %5.2f t/s, cache miss %d " LOG_COL_DEFAULT " \n\nInput: %s\n" LOG_COL_MAGENTA "Response: %s" LOG_COL_DEFAULT "\n\n",
client.id, client.seq_id, n_seq, client.n_prompt, client.n_decoded,
(t_main_end - client.t_start_prompt) / 1e6,
(double) (client.n_prompt + client.n_decoded) / (t_main_end - client.t_start_prompt) * 1e6,
@ -403,8 +403,8 @@ int main(int argc, char ** argv) {
if (params.prompt_file.empty()) {
params.prompt_file = "used built-in defaults";
}
LOG_INF("External prompt file: \033[32m%s\033[0m\n", params.prompt_file.c_str());
LOG_INF("Model and path used: \033[32m%s\033[0m\n\n", params.model.c_str());
LOG_INF("External prompt file: " LOG_COL_GREEN "%s" LOG_COL_DEFAULT "\n", params.prompt_file.c_str());
LOG_INF("Model and path used: " LOG_COL_GREEN "%s" LOG_COL_DEFAULT "\n\n", params.model.c_str());
LOG_INF("Total prompt tokens: %6d, speed: %5.2f t/s\n", n_total_prompt, (double) (n_total_prompt ) / (t_main_end - t_main_start) * 1e6);
LOG_INF("Total gen tokens: %6d, speed: %5.2f t/s\n", n_total_gen, (double) (n_total_gen ) / (t_main_end - t_main_start) * 1e6);

View file

@ -104,6 +104,7 @@
*/
# include "linenoise.h"
# include "log.h"
# include <ctype.h>
# include <errno.h>
@ -527,7 +528,7 @@ static void refreshShowHints(std::string & ab, struct linenoiseState * l, int pl
ab.append(seq);
ab.append(hint, hintlen);
if (color != -1 || bold != 0)
ab.append("\033[0m");
ab.append(LOG_COL_DEFAULT);
/* Call the function to free the hint returned. */
if (freeHintsCallback) freeHintsCallback(hint);

View file

@ -1,4 +1,5 @@
#include "llama.h"
#include "log.h"
#include <cstdio>
#include <cstring>
#include <iostream>
@ -115,7 +116,7 @@ int main(int argc, char ** argv) {
int n_ctx = llama_n_ctx(ctx);
int n_ctx_used = llama_get_kv_cache_used_cells(ctx);
if (n_ctx_used + batch.n_tokens > n_ctx) {
printf("\033[0m\n");
printf(LOG_COL_DEFAULT "\n");
fprintf(stderr, "context size exceeded\n");
exit(0);
}
@ -155,7 +156,7 @@ int main(int argc, char ** argv) {
int prev_len = 0;
while (true) {
// get user input
printf("\033[32m> \033[0m");
printf(LOG_COL_GREEN "> " LOG_COL_DEFAULT);
std::string user;
std::getline(std::cin, user);
@ -181,9 +182,9 @@ int main(int argc, char ** argv) {
std::string prompt(formatted.begin() + prev_len, formatted.begin() + new_len);
// generate a response
printf("\033[33m");
printf(LOG_COL_YELLOW);
std::string response = generate(prompt);
printf("\n\033[0m");
printf("\n" LOG_COL_DEFAULT);
// add the response to the messages
messages.push_back({"assistant", strdup(response.c_str())});

View file

@ -804,7 +804,7 @@ lovely<|t_0.56|><|code_start|><|634|><|596|><|1766|><|1556|><|1306|><|1285|><|14
const int col = std::max(0, std::min((int) k_colors.size() - 1, (int) ((3*p)*float(k_colors.size()))));
LOG_CNT("%s%d%s", k_colors[col].c_str(), i, "\033[0m");
LOG_CNT("%s%d%s", k_colors[col].c_str(), i, LOG_COL_DEFAULT);
//LOG_CNT("%d", i);
}