Repetition Penalty

easy
Unit 1 · LLMs0/3 to milestone

LLMs sometimes get stuck repeating themselves. A repetition penalty θ>1\theta > 1 makes tokens that have already been generated less likely before sampling the next one:

  • if the token's logit is positive, divide it by θ\theta
  • if it's negative or zero, multiply it by θ\theta

(Dividing a negative logit would make it more likely — a classic bug.)

Implement apply_repetition_penalty(logits, generated_ids, penalty) returning a new NumPy array. Each token is penalised once, however many times it appeared. Don't modify the input array.

Example

apply_repetition_penalty(np.array([2.0, -1.0, 0.5]), [0, 1, 1], 2.0)
# array([ 1. , -2. ,  0.5])
#llm#sampling#decoding3 visible + 2 hidden tests
How LLMs Work· 15 of 19
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Repetition Penalty · TechGradify