Language models love to loop: once a phrase appears, its tokens become more likely to appear again, and greedy or low-temperature decoding can repeat the same sentence forever. Decoding libraries fight this by editing the logits of tokens that have already been generated. Three common rules exist. The repetition penalty (from the CTRL paper) divides the logit of a seen token by a factor when it is positive and multiplies it when it is negative, so the token always becomes less likely. The frequency penalty subtracts an amount proportional to how many times the token has appeared, and the presence penalty subtracts a flat amount once if it has appeared at all.
Implement apply_penalties.
logits is a 1-D float array over the vocabulary and generated_ids is a list of the token ids produced so far.rep > 0 to every distinct generated token: a positive logit becomes logit / rep, a negative logit becomes logit * rep (rep = 1.0 changes nothing).c > 0, subtract freq * c + pres.logits.np.bincount(generated_ids, minlength=len(logits)) gives all counts at once.
Apply the repetition rule before the subtractive penalties, and only where the count is positive.
No results yet
Run your code to check it against the examples, or submit to run every test.