Skip to content

Commit 9e17072

Browse files
committed
Add "--instruct" argument for usage with Alpaca (#240)
Also start adding prompts in "./prompts"
1 parent 22213a1 commit 9e17072

File tree

5 files changed

+64
-40
lines changed

5 files changed

+64
-40
lines changed

main.cpp

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,6 @@ bool llama_model_load(const std::string & fname, llama_model & model, gpt_vocab
176176
}
177177
}
178178

179-
const ggml_type wtype2 = GGML_TYPE_F32;
180-
181179
auto & ctx = model.ctx;
182180

183181
size_t ctx_size = 0;
@@ -237,7 +235,6 @@ bool llama_model_load(const std::string & fname, llama_model & model, gpt_vocab
237235

238236
const int n_embd = hparams.n_embd;
239237
const int n_layer = hparams.n_layer;
240-
const int n_ctx = hparams.n_ctx;
241238
const int n_vocab = hparams.n_vocab;
242239

243240
model.layers.resize(n_layer);
@@ -539,9 +536,7 @@ bool llama_eval(
539536
const int n_vocab = hparams.n_vocab;
540537
const int n_rot = hparams.n_embd/hparams.n_head;
541538

542-
const int d_key = n_embd/n_head;
543-
544-
// TODO: check if this size scales with n_ctx linearly and remove constant. somehow I feel it wasn't the case
539+
// TODO: check if this size scales with n_ctx linearly and remove constant. somehow I feel it wasn't the case
545540
// static size_t buf_size = hparams.n_ctx*1024*1024;
546541
static size_t buf_size = 512u*1024*1024;
547542
static void * buf = malloc(buf_size);
@@ -792,7 +787,7 @@ int main(int argc, char ** argv) {
792787
if (gpt_params_parse(argc, argv, params) == false) {
793788
return 1;
794789
}
795-
790+
796791
if (params.n_ctx > 2048) {
797792
fprintf(stderr, "%s: warning: model does not support context sizes greater than 2048 tokens (%d specified);"
798793
"expect poor results\n", __func__, params.n_ctx);
@@ -820,7 +815,7 @@ int main(int argc, char ** argv) {
820815
// load the model
821816
{
822817
const int64_t t_start_us = ggml_time_us();
823-
if (!llama_model_load(params.model, model, vocab, params.n_ctx)) {
818+
if (!llama_model_load(params.model, model, vocab, params.n_ctx)) {
824819
fprintf(stderr, "%s: failed to load model from '%s'\n", __func__, params.model.c_str());
825820
return 1;
826821
}
@@ -849,9 +844,25 @@ int main(int argc, char ** argv) {
849844

850845
params.n_predict = std::min(params.n_predict, model.hparams.n_ctx - (int) embd_inp.size());
851846

847+
// prefix & suffix for instruct mode
848+
const std::vector<gpt_vocab::id> inp_pfx = ::llama_tokenize(vocab, "\n\n### Instruction:\n\n", true);
849+
const std::vector<gpt_vocab::id> inp_sfx = ::llama_tokenize(vocab, "\n\n### Response:\n\n", false);
850+
851+
// in instruct mode, we inject a prefix and a suffix to each input by the user
852+
if (params.instruct) {
853+
fprintf(stderr, "== Instruction mode enabled ==\n");
854+
params.interactive = true;
855+
params.antiprompt = "### Instruction:\n\n";
856+
}
857+
852858
// tokenize the reverse prompt
853859
std::vector<gpt_vocab::id> antiprompt_inp = ::llama_tokenize(vocab, params.antiprompt, false);
854860

861+
// enable interactive mode if reverse prompt is specified
862+
if (!antiprompt_inp.empty()) {
863+
params.interactive = true;
864+
}
865+
855866
fprintf(stderr, "\n");
856867
fprintf(stderr, "%s: prompt: '%s'\n", __func__, params.prompt.c_str());
857868
fprintf(stderr, "%s: number of tokens in prompt = %zu\n", __func__, embd_inp.size());
@@ -872,7 +883,7 @@ int main(int argc, char ** argv) {
872883

873884
fprintf(stderr, "%s: interactive mode on.\n", __func__);
874885

875-
if(antiprompt_inp.size()) {
886+
if (antiprompt_inp.size()) {
876887
fprintf(stderr, "%s: reverse prompt: '%s'\n", __func__, params.antiprompt.c_str());
877888
fprintf(stderr, "%s: number of tokens in reverse prompt = %zu\n", __func__, antiprompt_inp.size());
878889
for (int i = 0; i < (int) antiprompt_inp.size(); i++) {
@@ -894,31 +905,27 @@ int main(int argc, char ** argv) {
894905
std::vector<gpt_vocab::id> last_n_tokens(last_n_size);
895906
std::fill(last_n_tokens.begin(), last_n_tokens.end(), 0);
896907

897-
898908
if (params.interactive) {
899909
fprintf(stderr, "== Running in interactive mode. ==\n"
900910
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__)) || defined (_WIN32)
901911
" - Press Ctrl+C to interject at any time.\n"
902912
#endif
903913
" - Press Return to return control to LLaMa.\n"
904-
" - If you want to submit another line, end your input in '\\'.\n");
914+
" - If you want to submit another line, end your input in '\\'.\n\n");
915+
is_interacting = true;
905916
}
906917

907-
int remaining_tokens = params.n_predict;
908918
int input_consumed = 0;
909919
bool input_noecho = false;
910920

911-
// prompt user immediately after the starting prompt has been loaded
912-
if (params.interactive_start) {
913-
is_interacting = true;
914-
}
921+
int remaining_tokens = params.n_predict;
915922

916923
// set the color for the prompt which will be output initially
917924
if (params.use_color) {
918925
printf(ANSI_COLOR_YELLOW);
919926
}
920927

921-
while (remaining_tokens > 0) {
928+
while (remaining_tokens > 0 || params.interactive) {
922929
// predict
923930
if (embd.size() > 0) {
924931
const int64_t t_start_us = ggml_time_us();
@@ -971,13 +978,13 @@ int main(int argc, char ** argv) {
971978
last_n_tokens.erase(last_n_tokens.begin());
972979
last_n_tokens.push_back(embd_inp[input_consumed]);
973980
++input_consumed;
974-
if (embd.size() > params.n_batch) {
981+
if ((int) embd.size() > params.n_batch) {
975982
break;
976983
}
977984
}
978985

979986
// reset color to default if we there is no pending user input
980-
if (!input_noecho && params.use_color && embd_inp.size() == input_consumed) {
987+
if (!input_noecho && params.use_color && (int) embd_inp.size() == input_consumed) {
981988
printf(ANSI_COLOR_RESET);
982989
}
983990
}
@@ -999,19 +1006,26 @@ int main(int argc, char ** argv) {
9991006
is_interacting = true;
10001007
}
10011008
if (is_interacting) {
1009+
if (params.instruct) {
1010+
input_consumed = embd_inp.size();
1011+
embd_inp.insert(embd_inp.end(), inp_pfx.begin(), inp_pfx.end());
1012+
1013+
printf("\n> ");
1014+
}
1015+
10021016
// currently being interactive
1003-
bool another_line=true;
1017+
bool another_line = true;
10041018
while (another_line) {
10051019
fflush(stdout);
10061020
char buf[256] = {0};
10071021
int n_read;
1008-
if(params.use_color) printf(ANSI_BOLD ANSI_COLOR_GREEN);
1022+
if (params.use_color) printf(ANSI_BOLD ANSI_COLOR_GREEN);
10091023
if (scanf("%255[^\n]%n%*c", buf, &n_read) <= 0) {
10101024
// presumable empty line, consume the newline
10111025
std::ignore = scanf("%*c");
10121026
n_read=0;
10131027
}
1014-
if(params.use_color) printf(ANSI_COLOR_RESET);
1028+
if (params.use_color) printf(ANSI_COLOR_RESET);
10151029

10161030
if (n_read > 0 && buf[n_read-1]=='\\') {
10171031
another_line = true;
@@ -1026,6 +1040,10 @@ int main(int argc, char ** argv) {
10261040
std::vector<gpt_vocab::id> line_inp = ::llama_tokenize(vocab, buf, false);
10271041
embd_inp.insert(embd_inp.end(), line_inp.begin(), line_inp.end());
10281042

1043+
if (params.instruct) {
1044+
embd_inp.insert(embd_inp.end(), inp_sfx.begin(), inp_sfx.end());
1045+
}
1046+
10291047
remaining_tokens -= line_inp.size();
10301048

10311049
input_noecho = true; // do not echo this again
@@ -1037,8 +1055,12 @@ int main(int argc, char ** argv) {
10371055

10381056
// end of text token
10391057
if (embd.back() == 2) {
1040-
fprintf(stderr, " [end of text]\n");
1041-
break;
1058+
if (params.interactive) {
1059+
is_interacting = true;
1060+
} else {
1061+
fprintf(stderr, " [end of text]\n");
1062+
break;
1063+
}
10421064
}
10431065
}
10441066

prompts/alpaca.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Below is an instruction that describes a task. Write a response that appropriately completes the request.

prompts/chat-with-bob.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
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.
2+
3+
User: Hello, Bob.
4+
Bob: Hello. How may I help you today?
5+
User: Please tell me the largest city in Europe.
6+
Bob: Sure. The largest city in Europe is Moscow, the capital of Russia.
7+
User:

utils.cpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,8 @@ bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
3838
} else if (arg == "-p" || arg == "--prompt") {
3939
params.prompt = argv[++i];
4040
} else if (arg == "-f" || arg == "--file") {
41-
4241
std::ifstream file(argv[++i]);
43-
44-
std::copy(std::istreambuf_iterator<char>(file),
45-
std::istreambuf_iterator<char>(),
46-
back_inserter(params.prompt));
47-
42+
std::copy(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>(), back_inserter(params.prompt));
4843
} else if (arg == "-n" || arg == "--n_predict") {
4944
params.n_predict = std::stoi(argv[++i]);
5045
} else if (arg == "--top_k") {
@@ -65,9 +60,8 @@ bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
6560
params.model = argv[++i];
6661
} else if (arg == "-i" || arg == "--interactive") {
6762
params.interactive = true;
68-
} else if (arg == "--interactive-start") {
69-
params.interactive = true;
70-
params.interactive_start = true;
63+
} else if (arg == "-ins" || arg == "--instruct") {
64+
params.instruct = true;
7165
} else if (arg == "--color") {
7266
params.use_color = true;
7367
} else if (arg == "-r" || arg == "--reverse-prompt") {
@@ -85,13 +79,13 @@ bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
8579
return true;
8680
}
8781

88-
void gpt_print_usage(int argc, char ** argv, const gpt_params & params) {
82+
void gpt_print_usage(int /*argc*/, char ** argv, const gpt_params & params) {
8983
fprintf(stderr, "usage: %s [options]\n", argv[0]);
9084
fprintf(stderr, "\n");
9185
fprintf(stderr, "options:\n");
9286
fprintf(stderr, " -h, --help show this help message and exit\n");
9387
fprintf(stderr, " -i, --interactive run in interactive mode\n");
94-
fprintf(stderr, " --interactive-start run in interactive mode and poll user input at startup\n");
88+
fprintf(stderr, " -ins, --instruct run in instruction mode (use with Alpaca models)\n");
9589
fprintf(stderr, " -r PROMPT, --reverse-prompt PROMPT\n");
9690
fprintf(stderr, " in interactive mode, poll user input upon seeing PROMPT\n");
9791
fprintf(stderr, " --color colorise output to distinguish prompt and user input from generations\n");
@@ -398,7 +392,7 @@ gpt_vocab::id llama_sample_top_p_top_k(
398392
logits_id.push_back(std::make_pair(logits[i]*scale*repeat_penalty, i));
399393
} else {
400394
logits_id.push_back(std::make_pair(logits[i]*scale/repeat_penalty, i));
401-
}
395+
}
402396
} else {
403397
logits_id.push_back(std::make_pair(logits[i]*scale, i));
404398
}

utils.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ struct gpt_params {
2727

2828
int32_t n_batch = 8; // batch size for prompt processing
2929

30-
std::string model = "models/lamma-7B/ggml-model.bin"; // model path
31-
std::string prompt;
30+
std::string model = "models/lamma-7B/ggml-model.bin"; // model path
31+
std::string prompt = "";
32+
std::string antiprompt = ""; // string upon seeing which more user input is prompted
3233

3334
bool use_color = false; // use color to distinguish generations and inputs
3435

3536
bool interactive = false; // interactive mode
36-
bool interactive_start = false; // reverse prompt immediately
37-
std::string antiprompt = ""; // string upon seeing which more user input is prompted
37+
bool instruct = false; // instruction mode (used for Alpaca models)
3838
};
3939

4040
bool gpt_params_parse(int argc, char ** argv, gpt_params & params);

0 commit comments

Comments
 (0)