Skip to content

Commit c38627e

Browse files
committed
Fix repetition penalty logits processor
1 parent 2c92943 commit c38627e

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/generation/logits_process.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -406,12 +406,22 @@ export class NoRepeatNGramLogitsProcessor extends LogitsProcessor {
406406
}
407407

408408
/**
409-
* A logits processor that penalises repeated output tokens.
409+
* A logits processor that prevents the repetition of previous tokens through a penalty.
410+
* This penalty is applied at most once per token. Note that, for decoder-only models like most LLMs,
411+
* the considered tokens include the prompt.
412+
*
413+
* In the original [paper](https://arxiv.org/pdf/1909.05858.pdf), the authors suggest the use of a
414+
* penalty of around 1.2 to achieve a good balance between truthful generation and lack of repetition.
415+
* To penalize and reduce repetition, use `penalty` values above 1.0, where a higher value penalizes
416+
* more strongly. To reward and encourage repetition, use `penalty` values between 0.0 and 1.0, where
417+
* a lower value rewards more strongly.
410418
*/
411419
export class RepetitionPenaltyLogitsProcessor extends LogitsProcessor {
412420
/**
413421
* Create a RepetitionPenaltyLogitsProcessor.
414-
* @param {number} penalty The penalty to apply for repeated tokens.
422+
* @param {number} penalty The parameter for repetition penalty.
423+
* - 1.0 means no penalty. Above 1.0 penalizes previously generated tokens.
424+
* - Between 0.0 and 1.0 rewards previously generated tokens.
415425
*/
416426
constructor(penalty) {
417427
super();
@@ -425,13 +435,9 @@ export class RepetitionPenaltyLogitsProcessor extends LogitsProcessor {
425435
* @returns {Object} The logits with repetition penalty processing.
426436
*/
427437
_call(input_ids, logits) {
428-
// Modify the logits corresponding to each element in `input_ids`.
429-
// As a consequence, the logits corresponding to tokens that appear
430-
// many times in the output will be penalised more.
431-
432438
for (let i = 0; i < input_ids.length; ++i) {
433439
const batch_logits_data = /** @type {Float32Array} */(logits[i].data);
434-
for (const input_id of input_ids[i]) {
440+
for (const input_id of new Set(input_ids[i])) {
435441
const token = Number(input_id);
436442
if (batch_logits_data[token] < 0) {
437443
batch_logits_data[token] *= this.penalty;

0 commit comments

Comments
 (0)