Skip to content

Commit 38373cf

Browse files
authored
Add SPM infill support (ggml-org#8016)
* add --spm-infill option * support --spm-infill * support --spm-infill
1 parent b851b3f commit 38373cf

File tree

6 files changed

+32
-14
lines changed

6 files changed

+32
-14
lines changed

common/common.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,6 +1026,10 @@ bool gpt_params_find_arg(int argc, char ** argv, const std::string & arg, gpt_pa
10261026
params.input_suffix = argv[i];
10271027
return true;
10281028
}
1029+
if (arg == "--spm-infill") {
1030+
params.spm_infill = true;
1031+
return true;
1032+
}
10291033
if (arg == "--grammar") {
10301034
CHECK_ARG
10311035
sparams.grammar = argv[i];
@@ -1409,6 +1413,8 @@ void gpt_params_print_usage(int /*argc*/, char ** argv, const gpt_params & param
14091413
options.push_back({ "main infill", " --in-prefix-bos", "prefix BOS to user inputs, preceding the `--in-prefix` string" });
14101414
options.push_back({ "main infill", " --in-prefix STRING", "string to prefix user inputs with (default: empty)" });
14111415
options.push_back({ "main infill", " --in-suffix STRING", "string to suffix after user inputs with (default: empty)" });
1416+
options.push_back({ "server infill",
1417+
" --spm-infill", "use Suffix/Prefix/Middle pattern for infill (instead of Prefix/Suffix/Middle) as some models prefer this. (default: %s)", params.spm_infill ? "enabled" : "disabled" });
14121418

14131419
options.push_back({ "sampling" });
14141420
options.push_back({ "*", " --samplers SAMPLERS", "samplers that will be used for generation in the order, separated by \';\'\n"

common/common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,8 @@ struct gpt_params {
250250
std::string cvector_outfile = "control_vector.gguf";
251251
std::string cvector_positive_file = "examples/cvector-generator/positive.txt";
252252
std::string cvector_negative_file = "examples/cvector-generator/negative.txt";
253+
254+
bool spm_infill = false; // suffix/prefix/middle pattern for infill
253255
};
254256

255257
void gpt_params_handle_model_default(gpt_params & params);

examples/infill/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ In this section, we cover the most commonly used options for running the `infill
1515
- `-i, --interactive`: Run the program in interactive mode, allowing you to provide input directly and receive real-time responses.
1616
- `-n N, --n-predict N`: Set the number of tokens to predict when generating text. Adjusting this value can influence the length of the generated text.
1717
- `-c N, --ctx-size N`: Set the size of the prompt context. The default is 512, but LLaMA models were built with a context of 2048, which will provide better results for longer input/inference.
18+
- `--spm-infill`: Use Suffix/Prefix/Middle pattern for infill (instead of Prefix/Suffix/Middle) as some models prefer this.
1819

1920
## Input Prompts
2021

examples/infill/infill.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -210,19 +210,21 @@ int main(int argc, char ** argv) {
210210
suff_rm_leading_spc = false;
211211
}
212212
std::vector<llama_token> embd_inp;
213+
std::vector<llama_token> embd_end;
213214
std::vector<llama_token> inp_pfx = ::llama_tokenize(ctx, params.input_prefix, false);
214215
std::vector<llama_token> inp_sfx = ::llama_tokenize(ctx, params.input_suffix, false);
215216
const int space_token = 29871;
216217
if (suff_rm_leading_spc && inp_sfx[0] == space_token) {
217218
inp_sfx.erase(inp_sfx.begin());
218219
}
219220
inp_pfx.insert(inp_pfx.begin(), llama_token_prefix(model));
221+
inp_sfx.insert(inp_sfx.begin(), llama_token_suffix(model));
222+
embd_inp = params.spm_infill ? inp_sfx : inp_pfx;
223+
embd_end = params.spm_infill ? inp_pfx : inp_sfx;
220224
if (add_bos) {
221-
inp_pfx.insert(inp_pfx.begin(), llama_token_bos(model));
225+
embd_inp.insert(embd_inp.begin(), llama_token_bos(model));
222226
}
223-
inp_sfx.insert(inp_sfx.begin(), llama_token_suffix(model));
224-
embd_inp = inp_pfx;
225-
embd_inp.insert(embd_inp.end(), inp_sfx.begin(), inp_sfx.end());
227+
embd_inp.insert(embd_inp.end(), embd_end.begin(), embd_end.end());
226228

227229
const llama_token middle_token = llama_token_middle(model);
228230
if (middle_token >= 0) {
@@ -526,14 +528,14 @@ int main(int argc, char ** argv) {
526528
inp_sfx.erase(inp_sfx.begin());
527529
}
528530
inp_pfx.insert(inp_pfx.begin(), llama_token_prefix(model));
531+
inp_sfx.insert(inp_sfx.begin(), llama_token_suffix(model));
532+
embd_inp = params.spm_infill ? inp_sfx : inp_pfx;
533+
embd_end = params.spm_infill ? inp_pfx : inp_sfx;
529534
if (add_bos) {
530-
inp_pfx.insert(inp_pfx.begin(), llama_token_bos(model));
535+
embd_inp.insert(embd_inp.begin(), llama_token_bos(model));
531536
}
532-
inp_sfx.insert(inp_sfx.begin(), llama_token_suffix(model));
533-
embd_inp = inp_pfx;
534-
embd_inp.insert(embd_inp.end(), inp_sfx.begin(), inp_sfx.end());
537+
embd_inp.insert(embd_inp.end(), embd_end.begin(), embd_end.end());
535538

536-
const llama_token middle_token = llama_token_middle(model);
537539
if (middle_token >= 0) {
538540
embd_inp.push_back(middle_token);
539541
}

examples/server/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ The project is under active development, and we are [looking for feedback and co
7373
- `-fa`, `--flash-attn` : enable flash attention (default: disabled).
7474
- `-ctk TYPE`, `--cache-type-k TYPE` : KV cache data type for K (default: `f16`, options `f32`, `f16`, `q8_0`, `q4_0`, `q4_1`, `iq4_nl`, `q5_0`, or `q5_1`)
7575
- `-ctv TYPE`, `--cache-type-v TYPE` : KV cache type for V (default `f16`, see `-ctk` for options)
76+
- `--spm-infill` : Use Suffix/Prefix/Middle pattern for infill (instead of Prefix/Suffix/Middle) as some models prefer this.
7677

7778
**If compiled with `LLAMA_SERVER_SSL=ON`**
7879
- `--ssl-key-file FNAME`: path to file a PEM-encoded SSL private key

examples/server/server.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2020,6 +2020,7 @@ struct server_context {
20202020
slot.t_start_generation = 0;
20212021

20222022
if (slot.infill) {
2023+
const bool add_bos = llama_should_add_bos_token(model);
20232024
bool suff_rm_leading_spc = true;
20242025
if (params.input_suffix.find_first_of(' ') == 0 && params.input_suffix.size() > 1) {
20252026
params.input_suffix.erase(0, 1);
@@ -2035,16 +2036,21 @@ struct server_context {
20352036
}
20362037

20372038
prefix_tokens.insert(prefix_tokens.begin(), llama_token_prefix(model));
2038-
prefix_tokens.insert(prefix_tokens.begin(), llama_token_bos(model)); // always add BOS
2039-
prefix_tokens.insert(prefix_tokens.end(), llama_token_suffix(model));
2040-
prefix_tokens.insert(prefix_tokens.end(), suffix_tokens.begin(), suffix_tokens.end());
2039+
suffix_tokens.insert(suffix_tokens.begin(), llama_token_suffix(model));
2040+
2041+
auto embd_inp = params.spm_infill ? suffix_tokens : prefix_tokens;
2042+
auto embd_end = params.spm_infill ? prefix_tokens : suffix_tokens;
2043+
if (add_bos) {
2044+
embd_inp.insert(embd_inp.begin(), llama_token_bos(model));
2045+
}
2046+
embd_inp.insert(embd_inp.end(), embd_end.begin(), embd_end.end());
20412047

20422048
const llama_token middle_token = llama_token_middle(model);
20432049
if (middle_token >= 0) {
2044-
prefix_tokens.push_back(middle_token);
2050+
embd_inp.push_back(middle_token);
20452051
}
20462052

2047-
prompt_tokens = prefix_tokens;
2053+
prompt_tokens = embd_inp;
20482054
} else {
20492055
prompt_tokens = tokenize(slot.prompt, system_prompt.empty()); // add BOS if there isn't system prompt
20502056
}

0 commit comments

Comments
 (0)