huggingface config_error ai_generated true

ValueError: repetition_penalty and no_repeat_ngram_size cannot be set simultaneously as they may conflict.

ID: huggingface/transformers-generation-config-repeat-penalty-conflict

Also available as: JSON · Markdown · 中文
90%Fix Rate
88%Confidence
1Evidence
2023-07-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
transformers>=4.25.0 active

Root Cause

Both repetition_penalty and no_repeat_ngram_size are set in generation config or passed to model.generate(), but they can produce contradictory effects on token selection.

generic

中文

在生成配置或 model.generate() 中同时设置了 repetition_penalty 和 no_repeat_ngram_size,但它们可能对 token 选择产生矛盾的效果。

Official Documentation

https://huggingface.co/docs/transformers/en/main_classes/text_generation#transformers.GenerationConfig

Workarounds

  1. 95% success Remove one of the parameters: use only `repetition_penalty=1.2` (typical range 1.0-2.0) to discourage repetition, or `no_repeat_ngram_size=3` to block n-gram repeats. Example: `model.generate(inputs, repetition_penalty=1.2)` or `model.generate(inputs, no_repeat_ngram_size=3)`
    Remove one of the parameters: use only `repetition_penalty=1.2` (typical range 1.0-2.0) to discourage repetition, or `no_repeat_ngram_size=3` to block n-gram repeats. Example: `model.generate(inputs, repetition_penalty=1.2)` or `model.generate(inputs, no_repeat_ngram_size=3)`
  2. 80% success If you need both effects, implement a custom repetition penalty that combines both strategies manually in a logits processor, then pass it via `model.generate(logits_processor=[...])`.
    If you need both effects, implement a custom repetition penalty that combines both strategies manually in a logits processor, then pass it via `model.generate(logits_processor=[...])`.
  3. 85% success Check the generation config before calling: `if hasattr(model.generation_config, 'repetition_penalty') and model.generation_config.repetition_penalty is not None: model.generation_config.no_repeat_ngram_size = None`
    Check the generation config before calling: `if hasattr(model.generation_config, 'repetition_penalty') and model.generation_config.repetition_penalty is not None: model.generation_config.no_repeat_ngram_size = None`

中文步骤

  1. Remove one of the parameters: use only `repetition_penalty=1.2` (typical range 1.0-2.0) to discourage repetition, or `no_repeat_ngram_size=3` to block n-gram repeats. Example: `model.generate(inputs, repetition_penalty=1.2)` or `model.generate(inputs, no_repeat_ngram_size=3)`
  2. If you need both effects, implement a custom repetition penalty that combines both strategies manually in a logits processor, then pass it via `model.generate(logits_processor=[...])`.
  3. Check the generation config before calling: `if hasattr(model.generation_config, 'repetition_penalty') and model.generation_config.repetition_penalty is not None: model.generation_config.no_repeat_ngram_size = None`

Dead Ends

Common approaches that don't work:

  1. 60% fail

    Setting no_repeat_ngram_size=0 is valid but still triggers the conflict check; setting repetition_penalty=0.0 is invalid (must be >=1.0) and raises a different error.

  2. 50% fail

    The error is raised before generation; silent catch will cause the generation to proceed with default values, potentially producing unwanted repetition.

  3. 70% fail

    The GenerationConfig itself raises this error during validation, so it will fail before any generation occurs.