Back Home

GitHub Repo

Transformers Merges Repetition Penalty Fix to Prevent Padding Tokens from Affecting EOS Scores

On November 4, Transformers merged a generation fix to keep padding tokens masked by the attention mask out of repetition penalties. The change addresses EOS score bias in mixed-length batches; whether it is included in a released package still needs to be checked.

Marcus Qwertyus · Public domain · Image source
zh-Hant

Hugging Face Transformers merged fix #50149 into the main branch on November 4, addressing an issue where repetition penalties treated padding tokens as valid history. When padding and the end-of-sequence token share a token ID, the same prompt can receive different EOS scores when generated alone versus in a batch with sequences of different lengths, affecting the decision to stop generation. Merge record

The issue occurs during score processing in generation. The official documentation explains that RepetitionPenaltyLogitsProcessor also includes the prompt content by default for decoder models. When the penalty is greater than one, it reduces the likelihood of selecting tokens that have already appeared. So even when the attention mask tells the model to ignore padding positions, the subsequent repetition penalty must still distinguish which positions belong to valid history. Generation utilities documentation

The original report reproduced the issue with left padding, pad_token_id == eos_token_id, and a penalty of 2.0. In the minimal example, adding padding positions alone lowered the EOS score for the same input from 3.0 to 1.5, changing the next token chosen by greedy decoding. However, the author also explicitly noted that ordinary generation tests with a pretrained model still produced the same continuation; output divergence appeared only after adding a controlled EOS bias. The Qwen test likewise showed only a score difference, so the issue should not be taken to mean that all batched generation fails. Reproduction

The fix preserves the penalty for actual prompt tokens and newly generated tokens while excluding masked padding positions. During review, the implementation changed to prompt_ignore_length, which can be specified separately for each batch element; the main-branch documentation now lists a tensor form of the parameter. Teams maintaining offline evaluations or batched inference should consider adding a regression check: with a fixed prompt and generation settings, compare processed EOS scores, output lengths, and stop reasons for single-item and mixed-length batches. This validation approach follows from the defect mechanism; what is confirmed so far is the merge into the main branch, so check whether the installed version includes the fix before deployment. Review discussion, main-branch documentation

Sources

  1. Trainer disables find_unused_parameters under any gradient checkpointing, not just reentrant #49443
  2. Transformers Trainer 開發版文件
  3. DistributedDataParallel — PyTorch 2.14 documentation
  4. Chunked prefill mishandles prefix-cache continuation — Transformers issue #49483
  5. Cache strategies — Transformers documentation
  6. SAM 3: text-only prompts drop the geometry CLS token that Meta's implementation always adds
  7. SAM3 — Transformers documentation
  8. apply_chat_template sanitize_control_tokens silently fails when a template prints a dict or list #50154
  9. Utilities for tokenizers:apply_chat_template
  10. Chat templates
  11. Support chat sanitization on processors #49609
  12. Mamba-1 多 token 快取接續缺陷與社群重現
  13. Transformers Mamba 實作
  14. Mamba 官方文件與 cache_params 語意
  15. Tokenizer — apply_chat_template
  16. Exclude attention-masked padding from repetition penalty — PR #50149
  17. RepetitionPenaltyLogitsProcessor 補齊 token 缺陷與重現案例 — Issue #50147
  18. Transformers 主分支生成工具文件