Add support for configs, add configurable prefixes / suffixes, deprecate instruct mode, add stop prompt
This commit is contained in:
parent
180b693a47
commit
80181c0712
10 changed files with 290 additions and 104 deletions
5
configs/alpaca.txt
Normal file
5
configs/alpaca.txt
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
--clean-interface --interactive-first --keep -1
|
||||||
|
--in-prefix-bos --in-prefix "\n\n### Instruction:\n\n" -r "### Instruction:\n\n"
|
||||||
|
--in-suffix "\n\n### Response:\n\n"
|
||||||
|
|
||||||
|
-p "Below is an instruction that describes a task. Write a response that appropriately completes the request.\n"
|
10
configs/chat-with-bob.txt
Normal file
10
configs/chat-with-bob.txt
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
--interactive-first --keep -1
|
||||||
|
--in-prefix-bos
|
||||||
|
--in-prefix "\nUser: " -r "User: "
|
||||||
|
--in-suffix "\nBob: "
|
||||||
|
|
||||||
|
-p "Transcript of a dialog, where the User interacts with an Assistant named Bob. Bob is helpful, kind, honest, good at writing, and never fails to answer the User's requests immediately and with precision.
|
||||||
|
User: Hello, Bob.
|
||||||
|
Bob: Hello. How may I help you today?
|
||||||
|
User: Please tell me the largest city in Europe.
|
||||||
|
Bob: Sure. The largest city in Europe is Moscow, the capital of Russia."
|
4
configs/vicuna-simple.txt
Normal file
4
configs/vicuna-simple.txt
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
--interactive-first --keep -1
|
||||||
|
--in-prefix-bos
|
||||||
|
--in-prefix "\n### Human: " --reverse-prompt "### Human: "
|
||||||
|
--in-suffix "\n### Assistant: "
|
4
configs/vicuna-stop.txt
Normal file
4
configs/vicuna-stop.txt
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
--interactive-first --keep -1
|
||||||
|
--in-prefix-bos
|
||||||
|
--in-prefix "\n### Human: " --reverse-prompt "### Human: "
|
||||||
|
--in-suffix "\n### Assistant: " --stop-prompt "### Assistant: "
|
5
configs/vicuna.txt
Normal file
5
configs/vicuna.txt
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
--interactive-first --keep -1
|
||||||
|
--in-prefix-bos --in-prefix "\n### Human: " -r "### Human: "
|
||||||
|
--in-suffix "\n### Assistant: "
|
||||||
|
|
||||||
|
-p "A chat between a curious human and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the human's questions."
|
|
@ -3,9 +3,11 @@
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <regex>
|
||||||
|
|
||||||
#if defined(_MSC_VER) || defined(__MINGW32__)
|
#if defined(_MSC_VER) || defined(__MINGW32__)
|
||||||
#include <malloc.h> // using malloc.h with MSC/MINGW
|
#include <malloc.h> // using malloc.h with MSC/MINGW
|
||||||
|
@ -29,6 +31,43 @@ extern "C" __declspec(dllimport) int __stdcall WideCharToMultiByte(unsigned int
|
||||||
#define CP_UTF8 65001
|
#define CP_UTF8 65001
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
void split_args(std::string args_string, std::vector<std::string> & output_args)
|
||||||
|
{
|
||||||
|
std::string current_arg = "";
|
||||||
|
bool in_quotes = false;
|
||||||
|
char quote_type;
|
||||||
|
|
||||||
|
for (char c : args_string) {
|
||||||
|
if (c == '"' || c == '\'') {
|
||||||
|
if (!in_quotes) {
|
||||||
|
in_quotes = true;
|
||||||
|
quote_type = c;
|
||||||
|
} else if (quote_type == c) {
|
||||||
|
in_quotes = false;
|
||||||
|
} else {
|
||||||
|
current_arg += c;
|
||||||
|
}
|
||||||
|
} else if (in_quotes) {
|
||||||
|
current_arg += c;
|
||||||
|
} else if (std::isspace(c)) {
|
||||||
|
if (current_arg != "") {
|
||||||
|
output_args.push_back(current_arg);
|
||||||
|
current_arg = "";
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
current_arg += c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (current_arg != "") {
|
||||||
|
output_args.push_back(current_arg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string unescape(std::string str) {
|
||||||
|
return std::regex_replace(str, std::regex("\\\\n"), "\n");
|
||||||
|
}
|
||||||
|
|
||||||
bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
|
bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
|
||||||
// determine sensible default number of threads.
|
// determine sensible default number of threads.
|
||||||
// std::thread::hardware_concurrency may not be equal to the number of cores, or may return 0.
|
// std::thread::hardware_concurrency may not be equal to the number of cores, or may return 0.
|
||||||
|
@ -46,28 +85,11 @@ bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
|
||||||
std::string arg;
|
std::string arg;
|
||||||
gpt_params default_params;
|
gpt_params default_params;
|
||||||
|
|
||||||
|
// get additional arguments from config files
|
||||||
|
std::vector<std::string> args;
|
||||||
for (int i = 1; i < argc; i++) {
|
for (int i = 1; i < argc; i++) {
|
||||||
arg = argv[i];
|
arg = argv[i];
|
||||||
|
if (arg == "--config") {
|
||||||
if (arg == "-s" || arg == "--seed") {
|
|
||||||
if (++i >= argc) {
|
|
||||||
invalid_param = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
params.seed = std::stoi(argv[i]);
|
|
||||||
} else if (arg == "-t" || arg == "--threads") {
|
|
||||||
if (++i >= argc) {
|
|
||||||
invalid_param = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
params.n_threads = std::stoi(argv[i]);
|
|
||||||
} else if (arg == "-p" || arg == "--prompt") {
|
|
||||||
if (++i >= argc) {
|
|
||||||
invalid_param = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
params.prompt = argv[i];
|
|
||||||
} else if (arg == "-f" || arg == "--file") {
|
|
||||||
if (++i >= argc) {
|
if (++i >= argc) {
|
||||||
invalid_param = true;
|
invalid_param = true;
|
||||||
break;
|
break;
|
||||||
|
@ -78,83 +100,149 @@ bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
|
||||||
invalid_param = true;
|
invalid_param = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
std::string args_string;
|
||||||
|
std::copy(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>(), back_inserter(args_string));
|
||||||
|
if (args_string.back() == '\n') {
|
||||||
|
args_string.pop_back();
|
||||||
|
}
|
||||||
|
split_args(args_string, args);
|
||||||
|
for (int j = 0; j < args.size(); j++) {
|
||||||
|
args[j] = unescape(args[j]);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
args.emplace_back(argv[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// parse args
|
||||||
|
int args_c = static_cast<int>(args.size());
|
||||||
|
for (int i = 0; i < args_c && !invalid_param; i++) {
|
||||||
|
arg = args[i];
|
||||||
|
|
||||||
|
if (arg == "-s" || arg == "--seed") {
|
||||||
|
if (++i >= args_c) {
|
||||||
|
invalid_param = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
params.seed = std::stoi(args[i]);
|
||||||
|
} else if (arg == "-t" || arg == "--threads") {
|
||||||
|
if (++i >= args_c) {
|
||||||
|
invalid_param = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
params.n_threads = std::stoi(args[i]);
|
||||||
|
} else if (arg == "-p" || arg == "--prompt") {
|
||||||
|
if (++i >= args_c) {
|
||||||
|
invalid_param = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
params.prompt = args[i];
|
||||||
|
} else if (arg == "-f" || arg == "--file") {
|
||||||
|
if (++i >= args_c) {
|
||||||
|
invalid_param = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
std::ifstream file(args[i]);
|
||||||
|
if (!file) {
|
||||||
|
fprintf(stderr, "error: failed to open file '%s'\n", args[i].c_str());
|
||||||
|
invalid_param = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
std::copy(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>(), back_inserter(params.prompt));
|
std::copy(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>(), back_inserter(params.prompt));
|
||||||
if (params.prompt.back() == '\n') {
|
if (params.prompt.back() == '\n') {
|
||||||
params.prompt.pop_back();
|
params.prompt.pop_back();
|
||||||
}
|
}
|
||||||
} else if (arg == "-n" || arg == "--n_predict") {
|
} else if (arg == "-n" || arg == "--n_predict") {
|
||||||
if (++i >= argc) {
|
if (++i >= args_c) {
|
||||||
invalid_param = true;
|
invalid_param = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
params.n_predict = std::stoi(argv[i]);
|
params.n_predict = std::stoi(args[i]);
|
||||||
} else if (arg == "--top_k") {
|
} else if (arg == "--top_k") {
|
||||||
if (++i >= argc) {
|
if (++i >= args_c) {
|
||||||
invalid_param = true;
|
invalid_param = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
params.top_k = std::stoi(argv[i]);
|
params.top_k = std::stoi(args[i]);
|
||||||
} else if (arg == "-c" || arg == "--ctx_size") {
|
} else if (arg == "-c" || arg == "--ctx_size") {
|
||||||
if (++i >= argc) {
|
if (++i >= args_c) {
|
||||||
invalid_param = true;
|
invalid_param = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
params.n_ctx = std::stoi(argv[i]);
|
params.n_ctx = std::stoi(args[i]);
|
||||||
} else if (arg == "--memory_f32") {
|
} else if (arg == "--memory_f32") {
|
||||||
params.memory_f16 = false;
|
params.memory_f16 = false;
|
||||||
} else if (arg == "--top_p") {
|
} else if (arg == "--top_p") {
|
||||||
if (++i >= argc) {
|
if (++i >= args_c) {
|
||||||
invalid_param = true;
|
invalid_param = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
params.top_p = std::stof(argv[i]);
|
params.top_p = std::stof(args[i]);
|
||||||
} else if (arg == "--temp") {
|
} else if (arg == "--temp") {
|
||||||
if (++i >= argc) {
|
if (++i >= args_c) {
|
||||||
invalid_param = true;
|
invalid_param = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
params.temp = std::stof(argv[i]);
|
params.temp = std::stof(args[i]);
|
||||||
} else if (arg == "--repeat_last_n") {
|
} else if (arg == "--repeat_last_n") {
|
||||||
if (++i >= argc) {
|
if (++i >= args_c) {
|
||||||
invalid_param = true;
|
invalid_param = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
params.repeat_last_n = std::stoi(argv[i]);
|
params.repeat_last_n = std::stoi(args[i]);
|
||||||
} else if (arg == "--repeat_penalty") {
|
} else if (arg == "--repeat_penalty") {
|
||||||
if (++i >= argc) {
|
if (++i >= args_c) {
|
||||||
invalid_param = true;
|
invalid_param = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
params.repeat_penalty = std::stof(argv[i]);
|
params.repeat_penalty = std::stof(args[i]);
|
||||||
} else if (arg == "-b" || arg == "--batch_size") {
|
} else if (arg == "-b" || arg == "--batch_size") {
|
||||||
if (++i >= argc) {
|
if (++i >= args_c) {
|
||||||
invalid_param = true;
|
invalid_param = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
params.n_batch = std::stoi(argv[i]);
|
params.n_batch = std::stoi(args[i]);
|
||||||
params.n_batch = std::min(512, params.n_batch);
|
params.n_batch = std::min(512, params.n_batch);
|
||||||
} else if (arg == "--keep") {
|
} else if (arg == "--keep") {
|
||||||
if (++i >= argc) {
|
if (++i >= args_c) {
|
||||||
invalid_param = true;
|
invalid_param = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
params.n_keep = std::stoi(argv[i]);
|
params.n_keep = std::stoi(args[i]);
|
||||||
} else if (arg == "-m" || arg == "--model") {
|
} else if (arg == "-m" || arg == "--model") {
|
||||||
if (++i >= argc) {
|
if (++i >= args_c) {
|
||||||
invalid_param = true;
|
invalid_param = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
params.model = argv[i];
|
params.model = args[i];
|
||||||
} else if (arg == "-i" || arg == "--interactive") {
|
} else if (arg == "-i" || arg == "--interactive") {
|
||||||
params.interactive = true;
|
params.interactive = true;
|
||||||
} else if (arg == "--embedding") {
|
} else if (arg == "--embedding") {
|
||||||
params.embedding = true;
|
params.embedding = true;
|
||||||
|
} else if (arg == "--clean-interface") {
|
||||||
|
params.clean_interface = true;
|
||||||
} else if (arg == "--interactive-start") {
|
} else if (arg == "--interactive-start") {
|
||||||
params.interactive = true;
|
params.interactive = true;
|
||||||
} else if (arg == "--interactive-first") {
|
} else if (arg == "--interactive-first") {
|
||||||
params.interactive_start = true;
|
params.interactive_start = true;
|
||||||
} else if (arg == "-ins" || arg == "--instruct") {
|
} else if (arg == "-ins" || arg == "--instruct") {
|
||||||
params.instruct = true;
|
fprintf(stderr, "\n\nWarning: instruct mode is deprecated! Use: \n"
|
||||||
|
"--clean-interface "
|
||||||
|
"--interactive-first "
|
||||||
|
"--keep -1 "
|
||||||
|
"--in-prefix-bos "
|
||||||
|
"--in-prefix \"\\n\\n### Instruction:\\n\\n\" "
|
||||||
|
"--in-suffix \"\\n\\n### Response:\\n\\n\" "
|
||||||
|
"-r \"### Instruction:\\n\\n\" "
|
||||||
|
"\n\n");
|
||||||
|
// params.instruct = true;
|
||||||
|
params.clean_interface = true;
|
||||||
|
params.interactive_start = true;
|
||||||
|
params.n_keep = -1;
|
||||||
|
params.input_prefix_bos = true;
|
||||||
|
params.input_prefix = "\n\n### Instruction:\n\n";
|
||||||
|
params.input_suffix = "\n\n### Response:\n\n";
|
||||||
|
params.antiprompt.push_back("### Instruction:\n\n");
|
||||||
} else if (arg == "--color") {
|
} else if (arg == "--color") {
|
||||||
params.use_color = true;
|
params.use_color = true;
|
||||||
} else if (arg == "--mlock") {
|
} else if (arg == "--mlock") {
|
||||||
|
@ -166,55 +254,71 @@ bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
|
||||||
} else if (arg == "--verbose-prompt") {
|
} else if (arg == "--verbose-prompt") {
|
||||||
params.verbose_prompt = true;
|
params.verbose_prompt = true;
|
||||||
} else if (arg == "-r" || arg == "--reverse-prompt") {
|
} else if (arg == "-r" || arg == "--reverse-prompt") {
|
||||||
if (++i >= argc) {
|
if (++i >= args_c) {
|
||||||
invalid_param = true;
|
invalid_param = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
params.antiprompt.push_back(argv[i]);
|
params.antiprompt.push_back(args[i]);
|
||||||
|
} else if (arg == "--stop-prompt") {
|
||||||
|
if (++i >= args_c) {
|
||||||
|
invalid_param = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
params.stopprompt.push_back(args[i]);
|
||||||
} else if (arg == "--perplexity") {
|
} else if (arg == "--perplexity") {
|
||||||
params.perplexity = true;
|
params.perplexity = true;
|
||||||
} else if (arg == "--ignore-eos") {
|
} else if (arg == "--ignore-eos") {
|
||||||
params.ignore_eos = true;
|
params.ignore_eos = true;
|
||||||
} else if (arg == "--n_parts") {
|
} else if (arg == "--n_parts") {
|
||||||
if (++i >= argc) {
|
if (++i >= args_c) {
|
||||||
invalid_param = true;
|
invalid_param = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
params.n_parts = std::stoi(argv[i]);
|
params.n_parts = std::stoi(args[i]);
|
||||||
} else if (arg == "-h" || arg == "--help") {
|
} else if (arg == "-h" || arg == "--help") {
|
||||||
gpt_print_usage(argc, argv, default_params);
|
gpt_print_usage(argv[0], default_params);
|
||||||
exit(0);
|
exit(0);
|
||||||
} else if (arg == "--random-prompt") {
|
} else if (arg == "--random-prompt") {
|
||||||
params.random_prompt = true;
|
params.random_prompt = true;
|
||||||
|
} else if (arg == "--in-prefix-bos") {
|
||||||
|
params.input_prefix_bos = true;
|
||||||
} else if (arg == "--in-prefix") {
|
} else if (arg == "--in-prefix") {
|
||||||
if (++i >= argc) {
|
if (++i >= args_c) {
|
||||||
invalid_param = true;
|
invalid_param = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
params.input_prefix = argv[i];
|
params.input_prefix = args[i];
|
||||||
|
} else if (arg == "--in-suffix-bos") {
|
||||||
|
params.input_suffix_bos = true;
|
||||||
|
} else if (arg == "--in-suffix") {
|
||||||
|
if (++i >= args_c) {
|
||||||
|
invalid_param = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
params.input_suffix = args[i];
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "error: unknown argument: %s\n", arg.c_str());
|
fprintf(stderr, "error: unknown argument: %s\n", arg.c_str());
|
||||||
gpt_print_usage(argc, argv, default_params);
|
gpt_print_usage(argv[0], default_params);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (invalid_param) {
|
if (invalid_param) {
|
||||||
fprintf(stderr, "error: invalid parameter for argument: %s\n", arg.c_str());
|
fprintf(stderr, "error: invalid parameter for argument: %s\n", arg.c_str());
|
||||||
gpt_print_usage(argc, argv, default_params);
|
gpt_print_usage(argv[0], default_params);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void gpt_print_usage(int /*argc*/, char ** argv, const gpt_params & params) {
|
void gpt_print_usage(char * argv_0, const gpt_params & params) {
|
||||||
fprintf(stderr, "usage: %s [options]\n", argv[0]);
|
fprintf(stderr, "usage: %s [options]\n", argv_0);
|
||||||
fprintf(stderr, "\n");
|
fprintf(stderr, "\n");
|
||||||
fprintf(stderr, "options:\n");
|
fprintf(stderr, "options:\n");
|
||||||
fprintf(stderr, " -h, --help show this help message and exit\n");
|
fprintf(stderr, " -h, --help show this help message and exit\n");
|
||||||
fprintf(stderr, " -i, --interactive run in interactive mode\n");
|
fprintf(stderr, " -i, --interactive run in interactive mode\n");
|
||||||
fprintf(stderr, " --interactive-first run in interactive mode and wait for input right away\n");
|
fprintf(stderr, " --interactive-first run in interactive mode and wait for input right away\n");
|
||||||
fprintf(stderr, " -ins, --instruct run in instruction mode (use with Alpaca models)\n");
|
fprintf(stderr, " --clean-interface hides input prefix & suffix and displays '>' instead\n");
|
||||||
fprintf(stderr, " -r PROMPT, --reverse-prompt PROMPT\n");
|
fprintf(stderr, " -r PROMPT, --reverse-prompt PROMPT\n");
|
||||||
fprintf(stderr, " run in interactive mode and poll user input upon seeing PROMPT (can be\n");
|
fprintf(stderr, " run in interactive mode and poll user input upon seeing PROMPT (can be\n");
|
||||||
fprintf(stderr, " specified more than once for multiple prompts).\n");
|
fprintf(stderr, " specified more than once for multiple prompts).\n");
|
||||||
|
@ -224,7 +328,10 @@ void gpt_print_usage(int /*argc*/, char ** argv, const gpt_params & params) {
|
||||||
fprintf(stderr, " -p PROMPT, --prompt PROMPT\n");
|
fprintf(stderr, " -p PROMPT, --prompt PROMPT\n");
|
||||||
fprintf(stderr, " prompt to start generation with (default: empty)\n");
|
fprintf(stderr, " prompt to start generation with (default: empty)\n");
|
||||||
fprintf(stderr, " --random-prompt start with a randomized prompt.\n");
|
fprintf(stderr, " --random-prompt start with a randomized prompt.\n");
|
||||||
|
fprintf(stderr, " --in-prefix-bos append bos token to input prefix.\n");
|
||||||
fprintf(stderr, " --in-prefix STRING string to prefix user inputs with (default: empty)\n");
|
fprintf(stderr, " --in-prefix STRING string to prefix user inputs with (default: empty)\n");
|
||||||
|
fprintf(stderr, " --in-suffix-bos append bos token to input suffix.\n");
|
||||||
|
fprintf(stderr, " --in-suffix STRING string to suffix user inputs with (default: empty)\n");
|
||||||
fprintf(stderr, " -f FNAME, --file FNAME\n");
|
fprintf(stderr, " -f FNAME, --file FNAME\n");
|
||||||
fprintf(stderr, " prompt file to start generation.\n");
|
fprintf(stderr, " prompt file to start generation.\n");
|
||||||
fprintf(stderr, " -n N, --n_predict N number of tokens to predict (default: %d, -1 = infinity)\n", params.n_predict);
|
fprintf(stderr, " -n N, --n_predict N number of tokens to predict (default: %d, -1 = infinity)\n", params.n_predict);
|
||||||
|
|
|
@ -14,14 +14,14 @@
|
||||||
//
|
//
|
||||||
|
|
||||||
struct gpt_params {
|
struct gpt_params {
|
||||||
int32_t seed = -1; // RNG seed
|
int32_t seed = -1; // RNG seed
|
||||||
int32_t n_threads = std::min(4, (int32_t) std::thread::hardware_concurrency());
|
int32_t n_threads = std::min(4, (int32_t) std::thread::hardware_concurrency()); // max 4 threads (default)
|
||||||
int32_t n_predict = 128; // new tokens to predict
|
int32_t n_predict = 128; // new tokens to predict
|
||||||
int32_t repeat_last_n = 64; // last n tokens to penalize
|
int32_t repeat_last_n = 64; // last n tokens to penalize
|
||||||
int32_t n_parts = -1; // amount of model parts (-1 = determine from model dimensions)
|
int32_t n_parts = -1; // amount of model parts (-1 = determine from model dimensions)
|
||||||
int32_t n_ctx = 512; // context size
|
int32_t n_ctx = 512; // context size
|
||||||
int32_t n_batch = 8; // batch size for prompt processing
|
int32_t n_batch = 8; // batch size for prompt processing
|
||||||
int32_t n_keep = 0; // number of tokens to keep from initial prompt
|
int32_t n_keep = 0; // number of tokens to keep from initial prompt (-1 for all)
|
||||||
|
|
||||||
// sampling parameters
|
// sampling parameters
|
||||||
int32_t top_k = 40;
|
int32_t top_k = 40;
|
||||||
|
@ -32,9 +32,12 @@ struct gpt_params {
|
||||||
std::string model = "models/lamma-7B/ggml-model.bin"; // model path
|
std::string model = "models/lamma-7B/ggml-model.bin"; // model path
|
||||||
std::string prompt = "";
|
std::string prompt = "";
|
||||||
std::string input_prefix = ""; // string to prefix user inputs with
|
std::string input_prefix = ""; // string to prefix user inputs with
|
||||||
|
bool input_prefix_bos = false; // append bos token to input prefix
|
||||||
|
std::string input_suffix = ""; // string to suffix user inputs with
|
||||||
|
bool input_suffix_bos = false; // append bos token to input suffix
|
||||||
|
|
||||||
std::vector<std::string> antiprompt; // string upon seeing which more user input is prompted
|
std::vector<std::string> antiprompt; // string upon seeing which more user input is prompted
|
||||||
|
std::vector<std::string> stopprompt; // string upon seeing which more user input is prompted (without adding prefixes and suffixes)
|
||||||
|
|
||||||
bool memory_f16 = true; // use f16 instead of f32 for memory kv
|
bool memory_f16 = true; // use f16 instead of f32 for memory kv
|
||||||
bool random_prompt = false; // do not randomize prompt if none provided
|
bool random_prompt = false; // do not randomize prompt if none provided
|
||||||
|
@ -51,11 +54,13 @@ struct gpt_params {
|
||||||
bool use_mlock = false; // use mlock to keep model in memory
|
bool use_mlock = false; // use mlock to keep model in memory
|
||||||
bool mem_test = false; // compute maximum memory usage
|
bool mem_test = false; // compute maximum memory usage
|
||||||
bool verbose_prompt = false; // print prompt tokens before generation
|
bool verbose_prompt = false; // print prompt tokens before generation
|
||||||
|
|
||||||
|
bool clean_interface = false; // hides input prefix & suffix and displays '>'
|
||||||
};
|
};
|
||||||
|
|
||||||
bool gpt_params_parse(int argc, char ** argv, gpt_params & params);
|
bool gpt_params_parse(int argc, char ** argv, gpt_params & params);
|
||||||
|
|
||||||
void gpt_print_usage(int argc, char ** argv, const gpt_params & params);
|
void gpt_print_usage(char * argv_0, const gpt_params & params);
|
||||||
|
|
||||||
std::string gpt_random_prompt(std::mt19937 & rng);
|
std::string gpt_random_prompt(std::mt19937 & rng);
|
||||||
|
|
||||||
|
|
|
@ -148,22 +148,18 @@ int main(int argc, char ** argv) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// number of tokens to keep when resetting context
|
// number of tokens to keep when resetting context
|
||||||
if (params.n_keep < 0 || params.n_keep > (int)embd_inp.size() || params.instruct) {
|
if (params.n_keep < 0 || params.n_keep > (int)embd_inp.size()) {
|
||||||
params.n_keep = (int)embd_inp.size();
|
params.n_keep = (int)embd_inp.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
// prefix & suffix for instruct mode
|
// prefix & suffix for instruct mode
|
||||||
const auto inp_pfx = ::llama_tokenize(ctx, "\n\n### Instruction:\n\n", true);
|
const auto inp_pfx = ::llama_tokenize(ctx, params.input_prefix, params.input_prefix_bos);
|
||||||
const auto inp_sfx = ::llama_tokenize(ctx, "\n\n### Response:\n\n", false);
|
std::string input_suffix = params.input_suffix;
|
||||||
|
if (input_suffix.back() == ' ') { input_suffix.pop_back(); } // (remove trailing space workaround)
|
||||||
// in instruct mode, we inject a prefix and a suffix to each input by the user
|
const auto inp_sfx = ::llama_tokenize(ctx, input_suffix, params.input_suffix_bos);
|
||||||
if (params.instruct) {
|
|
||||||
params.interactive_start = true;
|
|
||||||
params.antiprompt.push_back("### Instruction:\n\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
// enable interactive mode if reverse prompt or interactive start is specified
|
// enable interactive mode if reverse prompt or interactive start is specified
|
||||||
if (params.antiprompt.size() != 0 || params.interactive_start) {
|
if (params.antiprompt.size() != 0 || params.stopprompt.size() != 0 || params.interactive_start) {
|
||||||
params.interactive = true;
|
params.interactive = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -205,9 +201,17 @@ int main(int argc, char ** argv) {
|
||||||
fprintf(stderr, "Reverse prompt: '%s'\n", antiprompt.c_str());
|
fprintf(stderr, "Reverse prompt: '%s'\n", antiprompt.c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (params.stopprompt.size()) {
|
||||||
|
for (auto stopprompt : params.stopprompt) {
|
||||||
|
fprintf(stderr, "Stop prompt: '%s'\n", stopprompt.c_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!params.input_prefix.empty()) {
|
if (!params.input_prefix.empty()) {
|
||||||
fprintf(stderr, "Input prefix: '%s'\n", params.input_prefix.c_str());
|
fprintf(stderr, "Input prefix %s: '%s'\n", params.input_prefix_bos ? "(with bos token)" : "", params.input_prefix.c_str());
|
||||||
|
}
|
||||||
|
if (!params.input_suffix.empty()) {
|
||||||
|
fprintf(stderr, "Input suffix %s: '%s'\n", params.input_suffix_bos ? "(with bos token)" : "", params.input_suffix.c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fprintf(stderr, "sampling: temp = %f, top_k = %d, top_p = %f, repeat_last_n = %i, repeat_penalty = %f\n",
|
fprintf(stderr, "sampling: temp = %f, top_k = %d, top_p = %f, repeat_last_n = %i, repeat_penalty = %f\n",
|
||||||
|
@ -229,7 +233,13 @@ int main(int argc, char ** argv) {
|
||||||
is_interacting = params.interactive_start;
|
is_interacting = params.interactive_start;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool is_antiprompt = false;
|
struct Antiprompt {
|
||||||
|
bool any = false;
|
||||||
|
bool trailing_space = false;
|
||||||
|
size_t len;
|
||||||
|
bool is_stop_prompt = false;
|
||||||
|
} antiprompt;
|
||||||
|
|
||||||
bool input_noecho = false;
|
bool input_noecho = false;
|
||||||
|
|
||||||
int n_past = 0;
|
int n_past = 0;
|
||||||
|
@ -299,7 +309,7 @@ int main(int argc, char ** argv) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// replace end of text token with newline token when in interactive mode
|
// replace end of text token with newline token when in interactive mode
|
||||||
if (id == llama_token_eos() && params.interactive && !params.instruct) {
|
if (id == llama_token_eos() && params.interactive && params.input_prefix.empty()) {
|
||||||
id = llama_token_newline.front();
|
id = llama_token_newline.front();
|
||||||
if (params.antiprompt.size() != 0) {
|
if (params.antiprompt.size() != 0) {
|
||||||
// tokenize and inject first reverse prompt
|
// tokenize and inject first reverse prompt
|
||||||
|
@ -345,27 +355,63 @@ int main(int argc, char ** argv) {
|
||||||
// check if we should prompt the user for more
|
// check if we should prompt the user for more
|
||||||
if (params.interactive && (int) embd_inp.size() <= n_consumed) {
|
if (params.interactive && (int) embd_inp.size() <= n_consumed) {
|
||||||
|
|
||||||
// check for reverse prompt
|
// check for reverse prompt or stop prompt
|
||||||
if (params.antiprompt.size()) {
|
if (params.antiprompt.size() || params.stopprompt.size()) {
|
||||||
std::string last_output;
|
std::string last_output;
|
||||||
for (auto id : last_n_tokens) {
|
for (auto id : last_n_tokens) {
|
||||||
last_output += llama_token_to_str(ctx, id);
|
last_output += llama_token_to_str(ctx, id);
|
||||||
}
|
}
|
||||||
|
|
||||||
is_antiprompt = false;
|
antiprompt.any = false;
|
||||||
|
antiprompt.is_stop_prompt = false;
|
||||||
// Check if each of the reverse prompts appears at the end of the output.
|
// Check if each of the reverse prompts appears at the end of the output.
|
||||||
for (std::string & antiprompt : params.antiprompt) {
|
for (std::string & prompt : params.antiprompt) {
|
||||||
if (last_output.find(antiprompt.c_str(), last_output.length() - antiprompt.length(), antiprompt.length()) != std::string::npos) {
|
antiprompt.trailing_space = prompt.back() == ' ';
|
||||||
|
antiprompt.len = prompt.length() - (antiprompt.trailing_space ? 1 : 0);
|
||||||
|
if (last_output.find(prompt.c_str(), last_output.length() - antiprompt.len, antiprompt.len) != std::string::npos) {
|
||||||
is_interacting = true;
|
is_interacting = true;
|
||||||
is_antiprompt = true;
|
antiprompt.any = true;
|
||||||
set_console_color(con_st, CONSOLE_COLOR_USER_INPUT);
|
set_console_color(con_st, CONSOLE_COLOR_USER_INPUT);
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (!antiprompt.any) {
|
||||||
|
for (std::string & prompt : params.stopprompt) {
|
||||||
|
antiprompt.trailing_space = prompt.back() == ' ';
|
||||||
|
antiprompt.len = prompt.length() - (antiprompt.trailing_space ? 1 : 0);
|
||||||
|
if (last_output.find(prompt.c_str(), last_output.length() - antiprompt.len, antiprompt.len) != std::string::npos) {
|
||||||
|
is_interacting = true;
|
||||||
|
antiprompt.any = true;
|
||||||
|
antiprompt.is_stop_prompt = true;
|
||||||
|
set_console_color(con_st, CONSOLE_COLOR_USER_INPUT);
|
||||||
|
fflush(stdout);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (n_past > 0 && is_interacting) {
|
if (n_past > 0 && is_interacting)
|
||||||
|
{
|
||||||
|
std::string buffer;
|
||||||
|
if (!params.clean_interface && !params.input_prefix.empty() && !antiprompt.any) {
|
||||||
|
// avoid printing again user's new line (TODO: try to revert enter press and print newline)
|
||||||
|
int i = params.input_prefix.front() == '\n' ? 1 : 0;
|
||||||
|
for (; i < inp_pfx.size(); i++) {
|
||||||
|
printf("%s", llama_token_to_str(ctx, inp_pfx[i]));
|
||||||
|
}
|
||||||
|
fflush(stdout);
|
||||||
|
}
|
||||||
|
if (antiprompt.any && antiprompt.trailing_space) {
|
||||||
|
// add back removed trailing space to buffer(workaround)
|
||||||
|
buffer += ' ';
|
||||||
|
if (!params.clean_interface) {
|
||||||
|
printf("%s", buffer.c_str());
|
||||||
|
}
|
||||||
|
fflush(stdout);
|
||||||
|
}
|
||||||
|
|
||||||
// potentially set color to indicate we are taking user input
|
// potentially set color to indicate we are taking user input
|
||||||
set_console_color(con_st, CONSOLE_COLOR_USER_INPUT);
|
set_console_color(con_st, CONSOLE_COLOR_USER_INPUT);
|
||||||
|
|
||||||
|
@ -374,19 +420,14 @@ int main(int argc, char ** argv) {
|
||||||
signal(SIGINT, sigint_handler);
|
signal(SIGINT, sigint_handler);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (params.instruct) {
|
if (params.clean_interface) {
|
||||||
printf("\n> ");
|
printf("\n> ");
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string buffer;
|
|
||||||
if (!params.input_prefix.empty()) {
|
|
||||||
buffer += params.input_prefix;
|
|
||||||
printf("%s", buffer.c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string line;
|
std::string line;
|
||||||
bool another_line = true;
|
bool another_line = true;
|
||||||
do {
|
do {
|
||||||
|
// TODO: try to revert going to new line after enter (to enable in-line text writing)
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
std::wstring wline;
|
std::wstring wline;
|
||||||
if (!std::getline(std::wcin, wline)) {
|
if (!std::getline(std::wcin, wline)) {
|
||||||
|
@ -405,18 +446,31 @@ int main(int argc, char ** argv) {
|
||||||
} else {
|
} else {
|
||||||
line.pop_back(); // Remove the continue character
|
line.pop_back(); // Remove the continue character
|
||||||
}
|
}
|
||||||
buffer += line + '\n'; // Append the line to the result
|
buffer += line;
|
||||||
|
if (another_line || !antiprompt.is_stop_prompt) {
|
||||||
|
buffer += '\n'; // Append the line to the result
|
||||||
|
}
|
||||||
} while (another_line);
|
} while (another_line);
|
||||||
|
|
||||||
// done taking input, reset color
|
// done taking input, reset color
|
||||||
set_console_color(con_st, CONSOLE_COLOR_DEFAULT);
|
set_console_color(con_st, CONSOLE_COLOR_DEFAULT);
|
||||||
|
|
||||||
|
if (!params.clean_interface && !params.input_suffix.empty() && !antiprompt.is_stop_prompt) {
|
||||||
|
// avoid printing again user's new line (TODO: try to revert enter press and print newline)
|
||||||
|
int i = params.input_suffix.front() == '\n' ? 1 : 0;
|
||||||
|
for (; i < inp_sfx.size(); i++) {
|
||||||
|
printf("%s", llama_token_to_str(ctx, inp_sfx[i]));
|
||||||
|
}
|
||||||
|
// we won't add back removed trailing space here (workaround)
|
||||||
|
fflush(stdout);
|
||||||
|
}
|
||||||
|
|
||||||
// Add tokens to embd only if the input buffer is non-empty
|
// Add tokens to embd only if the input buffer is non-empty
|
||||||
// Entering a empty line lets the user pass control back
|
// Entering a empty line lets the user pass control back
|
||||||
if (buffer.length() > 1) {
|
if (buffer.length() > 1) {
|
||||||
|
|
||||||
// instruct mode: insert instruction prefix
|
// insert input prefix
|
||||||
if (params.instruct && !is_antiprompt) {
|
if (!params.input_prefix.empty() && !antiprompt.any) {
|
||||||
n_consumed = embd_inp.size();
|
n_consumed = embd_inp.size();
|
||||||
embd_inp.insert(embd_inp.end(), inp_pfx.begin(), inp_pfx.end());
|
embd_inp.insert(embd_inp.end(), inp_pfx.begin(), inp_pfx.end());
|
||||||
}
|
}
|
||||||
|
@ -424,8 +478,8 @@ int main(int argc, char ** argv) {
|
||||||
auto line_inp = ::llama_tokenize(ctx, buffer, false);
|
auto line_inp = ::llama_tokenize(ctx, buffer, false);
|
||||||
embd_inp.insert(embd_inp.end(), line_inp.begin(), line_inp.end());
|
embd_inp.insert(embd_inp.end(), line_inp.begin(), line_inp.end());
|
||||||
|
|
||||||
// instruct mode: insert response suffix
|
// insert response suffix
|
||||||
if (params.instruct) {
|
if (!params.input_suffix.empty() && !antiprompt.is_stop_prompt) {
|
||||||
embd_inp.insert(embd_inp.end(), inp_sfx.begin(), inp_sfx.end());
|
embd_inp.insert(embd_inp.end(), inp_sfx.begin(), inp_sfx.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -442,7 +496,7 @@ int main(int argc, char ** argv) {
|
||||||
|
|
||||||
// end of text token
|
// end of text token
|
||||||
if (!embd.empty() && embd.back() == llama_token_eos()) {
|
if (!embd.empty() && embd.back() == llama_token_eos()) {
|
||||||
if (params.instruct) {
|
if (params.interactive && !params.input_prefix.empty()) {
|
||||||
is_interacting = true;
|
is_interacting = true;
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, " [end of text]\n");
|
fprintf(stderr, " [end of text]\n");
|
||||||
|
|
|
@ -1 +0,0 @@
|
||||||
Below is an instruction that describes a task. Write a response that appropriately completes the request.
|
|
|
@ -1,7 +0,0 @@
|
||||||
Transcript of a dialog, where the User interacts with an Assistant named Bob. Bob is helpful, kind, honest, good at writing, and never fails to answer the User's requests immediately and with precision.
|
|
||||||
|
|
||||||
User: Hello, Bob.
|
|
||||||
Bob: Hello. How may I help you today?
|
|
||||||
User: Please tell me the largest city in Europe.
|
|
||||||
Bob: Sure. The largest city in Europe is Moscow, the capital of Russia.
|
|
||||||
User:
|
|
Loading…
Add table
Add a link
Reference in a new issue