huggingface
runtime_error
ai_generated
true
ValueError: 管道返回的输出数量与输入数量不匹配。期望 8 个输出,但得到 4 个。
ValueError: The number of outputs returned by the pipeline does not match the number of inputs. Expected 8 outputs, got 4.
ID: huggingface/pipeline-batch-inference-mismatch
80%修复率
85%置信度
1证据数
2023-08-15首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| transformers>=4.30.0 | active | — | — | — |
| torch>=1.13.0 | active | — | — | — |
| python>=3.8 | active | — | — | — |
根因分析
使用 batch_size > 1 的管道且模型动态丢弃或合并样本(例如由于预处理中的截断或过滤)时,输出数量可能与输入数量不一致。
English
When using pipeline with batch_size > 1 and a model that dynamically drops or merges samples (e.g., due to truncation or filtering in preprocess), the output count can diverge from input count.
官方文档
https://huggingface.co/docs/transformers/main_classes/pipelines#batch-inference解决方案
-
Disable any sample filtering in the pipeline's preprocess step (e.g., set `truncation=False` for summarization). Alternatively, use `return_full_text=False` in text-generation pipelines to avoid output count mismatch.
-
Collect outputs in a list without batching and then manually batch inputs, ensuring each input produces exactly one output. Use `tokenizer.batch_decode` with `skip_special_tokens=True`.
无效尝试
常见但无效的做法:
-
40% 失败
Setting `batch_size=1` avoids the mismatch but kills performance for large datasets. The error is not caused by batch size per se but by sample filtering.
-
30% 失败
Manually overriding `__len__` in the dataset to match filtered outputs is fragile and breaks if filtering logic changes.