Skip to content

Commit 129c7d1

Browse files
beillerggerganov
andauthored
Add repetition penalty (abetlen#20)
* Adding repeat penalization * Update utils.h * Update utils.cpp * Numeric fix Should probably still scale by temp even if penalized * Update comments, more proper application I see that numbers can go negative so a fix from a referenced commit * Minor formatting --------- Co-authored-by: Georgi Gerganov <[email protected]>
1 parent 702fddf commit 129c7d1

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

main.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,7 @@ int main(int argc, char ** argv) {
792792
printf("%6d -> '%s'\n", embd_inp[i], vocab.id_to_token.at(embd_inp[i]).c_str());
793793
}
794794
printf("\n");
795-
printf("sampling parameters: temp = %f, top_k = %d, top_p = %f\n", params.temp, params.top_k, params.top_p);
795+
printf("sampling parameters: temp = %f, top_k = %d, top_p = %f, repeat_last_n = %i, repeat_penalty = %f\n", params.temp, params.top_k, params.top_p, params.repeat_last_n, params.repeat_penalty);
796796
printf("\n\n");
797797

798798
std::vector<gpt_vocab::id> embd;
@@ -801,6 +801,10 @@ int main(int argc, char ** argv) {
801801
size_t mem_per_token = 0;
802802
llama_eval(model, params.n_threads, 0, { 0, 1, 2, 3 }, logits, mem_per_token);
803803

804+
int last_n_size = params.repeat_last_n;
805+
std::vector<gpt_vocab::id> last_n_tokens(last_n_size);
806+
std::fill(last_n_tokens.begin(), last_n_tokens.end(), 0);
807+
804808
for (int i = embd.size(); i < embd_inp.size() + params.n_predict; i++) {
805809
// predict
806810
if (embd.size() > 0) {
@@ -821,6 +825,7 @@ int main(int argc, char ** argv) {
821825
// sample next token
822826
const float top_p = params.top_p;
823827
const float temp = params.temp;
828+
const float repeat_penalty = params.repeat_penalty;
824829

825830
const int n_vocab = model.hparams.n_vocab;
826831

@@ -829,7 +834,10 @@ int main(int argc, char ** argv) {
829834
{
830835
const int64_t t_start_sample_us = ggml_time_us();
831836

832-
id = llama_sample_top_p(vocab, logits.data() + (logits.size() - n_vocab), top_p, temp, rng);
837+
id = llama_sample_top_p(vocab, logits.data() + (logits.size() - n_vocab), last_n_tokens, repeat_penalty, top_p, temp, rng);
838+
839+
last_n_tokens.erase(last_n_tokens.begin());
840+
last_n_tokens.push_back(id);
833841

834842
t_sample_us += ggml_time_us() - t_start_sample_us;
835843
}
@@ -840,6 +848,8 @@ int main(int argc, char ** argv) {
840848
// if here, it means we are still processing the input prompt
841849
for (int k = i; k < embd_inp.size(); k++) {
842850
embd.push_back(embd_inp[k]);
851+
last_n_tokens.erase(last_n_tokens.begin());
852+
last_n_tokens.push_back(embd_inp[k]);
843853
if (embd.size() > params.n_batch) {
844854
break;
845855
}

utils.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
2323
params.top_p = std::stof(argv[++i]);
2424
} else if (arg == "--temp") {
2525
params.temp = std::stof(argv[++i]);
26+
} else if (arg == "--repeat_last_n") {
27+
params.repeat_last_n = std::stoi(argv[++i]);
28+
} else if (arg == "--repeat_penalty") {
29+
params.repeat_penalty = std::stof(argv[++i]);
2630
} else if (arg == "-b" || arg == "--batch_size") {
2731
params.n_batch = std::stoi(argv[++i]);
2832
} else if (arg == "-m" || arg == "--model") {
@@ -52,6 +56,8 @@ void gpt_print_usage(int argc, char ** argv, const gpt_params & params) {
5256
fprintf(stderr, " -n N, --n_predict N number of tokens to predict (default: %d)\n", params.n_predict);
5357
fprintf(stderr, " --top_k N top-k sampling (default: %d)\n", params.top_k);
5458
fprintf(stderr, " --top_p N top-p sampling (default: %.1f)\n", params.top_p);
59+
fprintf(stderr, " --repeat_last_n N last n tokens to consider for penalize (default: %d)\n", params.repeat_last_n);
60+
fprintf(stderr, " --repeat_penalty N penalize repeat sequence of tokens (default: %.1f)\n", params.repeat_penalty);
5561
fprintf(stderr, " --temp N temperature (default: %.1f)\n", params.temp);
5662
fprintf(stderr, " -b N, --batch_size N batch size for prompt processing (default: %d)\n", params.n_batch);
5763
fprintf(stderr, " -m FNAME, --model FNAME\n");
@@ -372,6 +378,8 @@ gpt_vocab::id gpt_sample_top_k_top_p(
372378
gpt_vocab::id llama_sample_top_p(
373379
const gpt_vocab & vocab,
374380
const float * logits,
381+
std::vector<gpt_vocab::id> & last_n_tokens,
382+
double repeat_penalty,
375383
double top_p,
376384
double temp,
377385
std::mt19937 & rng) {
@@ -383,7 +391,18 @@ gpt_vocab::id llama_sample_top_p(
383391
{
384392
const double scale = 1.0/temp;
385393
for (int i = 0; i < n_logits; ++i) {
386-
logits_id.push_back(std::make_pair(logits[i]*scale, i));
394+
// repetition penalty from CTRL paper (https://arxiv.org/abs/1909.05858)
395+
// credit https://github.com/facebookresearch/llama/compare/main...shawwn:llama:main
396+
if (std::find(last_n_tokens.begin(), last_n_tokens.end(), i) != last_n_tokens.end()) {
397+
// if score < 0 then repetition penalty has to multiplied to reduce the previous token probability
398+
if (logits[i] < 0.0) {
399+
logits_id.push_back(std::make_pair(logits[i]*scale*repeat_penalty, i));
400+
} else {
401+
logits_id.push_back(std::make_pair(logits[i]*scale/repeat_penalty, i));
402+
}
403+
} else {
404+
logits_id.push_back(std::make_pair(logits[i]*scale, i));
405+
}
387406
}
388407
}
389408

utils.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ struct gpt_params {
1616
int32_t seed = -1; // RNG seed
1717
int32_t n_threads = std::min(4, (int32_t) std::thread::hardware_concurrency());
1818
int32_t n_predict = 128; // new tokens to predict
19+
int32_t repeat_last_n = 64; // last n tokens to penalize
1920

2021
// sampling parameters
2122
int32_t top_k = 40; // unused
2223
float top_p = 0.95f;
2324
float temp = 0.80f;
25+
float repeat_penalty = 1.30f;
2426

2527
int32_t n_batch = 8; // batch size for prompt processing
2628

@@ -89,6 +91,8 @@ gpt_vocab::id gpt_sample_top_k_top_p(
8991
gpt_vocab::id llama_sample_top_p(
9092
const gpt_vocab & vocab,
9193
const float * logits,
94+
std::vector<gpt_vocab::id> & last_n_tokens,
95+
double repeat_penalty,
9296
double top_p,
9397
double temp,
9498
std::mt19937 & rng);

0 commit comments

Comments
 (0)