ValueError: `early_stopping` 设置为 True,但 `num_beams` 为 1。没有 beam search 时,early stopping 无效。
ValueError: `early_stopping` is set to True but `num_beams` is 1. Early stopping has no effect without beam search.
ID: huggingface/generation-early-stopping-conflict
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| transformers>=4.28.0 | active | — | — | — |
| torch>=1.12.0 | active | — | — | — |
根因分析
Hugging Face 的 `generate()` 方法中的 `early_stopping` 参数仅在 beam search(num_beams > 1)时生效。在贪心或多模态采样中设置它无效,会引发 ValueError 以防止静默配置错误。
English
The parameter `early_stopping` in Hugging Face's `generate()` method only applies when using beam search (num_beams > 1). Setting it with greedy or multinomial sampling has no effect and raises a ValueError to prevent silent misconfiguration.
官方文档
https://huggingface.co/docs/transformers/main_classes/text_generation#transformers.GenerationConfig.early_stopping解决方案
-
Set `num_beams` to a value greater than 1 to enable beam search and make `early_stopping` effective. For example: `model.generate(input_ids, num_beams=4, early_stopping=True)`.
-
Remove `early_stopping` from the generation config if greedy decoding is intended. Use `do_sample=False` (default) instead.
无效尝试
常见但无效的做法:
-
30% 失败
Setting `num_beams=1` and `early_stopping=False` silences the error but removes the benefit of early stopping when beam search is intended.
-
60% 失败
Setting `early_stopping='never'` (a string value) is deprecated and may cause a different error in newer versions.