From 3176c2f5617bdb984f21425c9953b8d4d4a1cf6a Mon Sep 17 00:00:00 2001 From: Leo Zhang <1095412419@qq.com> Date: Mon, 13 May 2024 19:32:50 +0800 Subject: [PATCH] solve process prompt bug fix #6823 for example ``` What credit card company is on the banner in the background?\nAnswer the question using a single word or phrase. ``` notice the different tokenization result of '\n' before fix, the verbose-prompt result is: ``` 2061 -> 'What' 3884 -> ' credit' 2657 -> ' card' 1664 -> ' company' 318 -> ' is' 319 -> ' on' 262 -> ' the' 17625 -> ' banner' 287 -> ' in' 262 -> ' the' 4469 -> ' background' 30 -> '?' 59 -> '\' 77 -> 'n' 33706 -> 'Answer' 262 -> ' the' 1808 -> ' question' 1262 -> ' using' 257 -> ' a' 2060 -> ' single' 1573 -> ' word' 393 -> ' or' 9546 -> ' phrase' 13 -> '.' ``` after fix: ``` 2061 -> 'What' 3884 -> ' credit' 2657 -> ' card' 1664 -> ' company' 318 -> ' is' 319 -> ' on' 262 -> ' the' 17625 -> ' banner' 287 -> ' in' 262 -> ' the' 4469 -> ' background' 30 -> '?' 198 -> ' ' 33706 -> 'Answer' 262 -> ' the' 1808 -> ' question' 1262 -> ' using' 257 -> ' a' 2060 -> ' single' 1573 -> ' word' 393 -> ' or' 9546 -> ' phrase' 13 -> '. ``` --- examples/llava/llava-cli.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/examples/llava/llava-cli.cpp b/examples/llava/llava-cli.cpp index da60ddf2f..db876e6dc 100644 --- a/examples/llava/llava-cli.cpp +++ b/examples/llava/llava-cli.cpp @@ -272,6 +272,15 @@ static void llama_log_callback_logTee(ggml_log_level level, const char * text, v LOG_TEE("%s", text); } +static std::string replaceWithN(std::string& input) { + size_t pos = 0; + while ((pos = input.find("\\n", pos)) != std::string::npos) { + input.replace(pos, 2, "\n"); + pos += 1; + } + return input; +} + int main(int argc, char ** argv) { ggml_time_init(); @@ -308,8 +317,9 @@ int main(int argc, char ** argv) { std::cerr << "error: failed to load image " << image << ". Terminating\n\n"; return 1; } - + // process the prompt + params.prompt = replaceWithN(params.prompt); process_prompt(ctx_llava, image_embed, ¶ms, params.prompt); llama_print_timings(ctx_llava->ctx_llama);