# ValueError: 管道返回的输出数量与输入数量不匹配。期望 8 个输出，但得到 4 个。

- **ID:** `huggingface/pipeline-batch-inference-mismatch`
- **领域:** huggingface
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

使用 batch_size > 1 的管道且模型动态丢弃或合并样本（例如由于预处理中的截断或过滤）时，输出数量可能与输入数量不一致。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| transformers>=4.30.0 | active | — | — |
| torch>=1.13.0 | active | — | — |
| python>=3.8 | active | — | — |

## 解决方案

1. ```
   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.
   ```
2. ```
   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`.
   ```

## 无效尝试

- **** — 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. (40% 失败率)
- **** — Manually overriding `__len__` in the dataset to match filtered outputs is fragile and breaks if filtering logic changes. (30% 失败率)
