#7015·trl

get_repetition_penalty_reward accepts non-positive ngram_size

Author: qsxustcCreated Sep 2, 2026Updated Sep 16, 2026

Reproduction

ngram_size values of zero or less are accepted when constructing the reward function, but fail later during reward computation.

python
from trl.rewards import get_repetition_penalty_reward

reward_fn = get_repetition_penalty_reward(ngram_size=0)
reward_fn([[1, 2, 3]])

outputs:

Traceback (most recent call last):
  File "reproduce.py", line 4, in <module>
    reward_fn([[1, 2, 3]])
  File "trl/rewards/other_rewards.py", line 78, in __call__
    scaling = 1 - len(set(ngrams)) / len(ngrams)
                  ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~
ZeroDivisionError: division by zero

Negative values have the same problem because no n-grams are constructed.

Expected behavior: validate ngram_size when constructing the reward function and raise an actionable ValueError unless it is greater than zero. This is consistent with the existing construction-time validation for max_penalty.

System Info

  • Platform: macOS-26.6.2-arm64-arm-64bit-Mach-O
  • Python version: 3.14.6
  • TRL version: 1.13.0.dev0+323efb2
  • PyTorch version: 2.13.0
  • accelerator(s): cpu
  • Transformers version: 5.16.1
  • Accelerate version: 1.14.0
  • Accelerate config: not found
  • Datasets version: 5.0.1
  • HF Hub version: 1.29.0
  • bitsandbytes version: not installed
  • DeepSpeed version: not installed
  • Liger-Kernel version: not installed
  • PEFT version: not installed
  • vLLM version: not installed

Checklist

  • I have checked that my issue isn't already filed (see open issues)
  • I have included my system information
  • Any code provided is minimal, complete, and reproducible (more on MREs)
  • Any code provided is properly formatted in code blocks, (no screenshot, more on code blocks)
  • Any traceback provided is complete