值错误:当设备明确设置为 0 时,无法在 pipeline 中使用 device_map='auto'。请设置 device_map=None 或移除 device 参数。
ValueError: Cannot use device_map='auto' with pipeline when device is explicitly set to 0. Please set device_map=None or remove device argument.
ID: huggingface/pipeline-device-map-conflict
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| transformers>=4.30.0 | active | — | — | — |
| accelerate>=0.20.0 | active | — | — | — |
根因分析
当同时指定 `device` 和 `device_map` 时,pipeline API 发生冲突;`device_map='auto'` 要求模型使用 Accelerate 的设备映射加载,这与固定的设备索引不兼容。
English
The pipeline API conflicts when both `device` and `device_map` are specified; `device_map='auto'` requires the model to be loaded with Accelerate's device mapping, which is incompatible with a fixed device index.
官方文档
https://huggingface.co/docs/transformers/v4.30.0/en/main_classes/pipelines#pipeline-device-map解决方案
-
移除 `device` 参数,仅使用 `device_map='auto'`:pipe = pipeline('text-generation', model='model-name', device_map='auto')。这使 Accelerate 处理设备放置。 -
移除 `device_map` 并显式使用 `device=0`:pipe = pipeline('text-generation', model='model-name', device=0)。这强制模型使用 GPU 0。
无效尝试
常见但无效的做法:
-
Set both `device=0` and `device_map='auto'` and expect the pipeline to resolve the conflict
100% 失败
The pipeline raises a ValueError immediately because the two arguments are mutually exclusive.
-
Use `device_map='sequential'` instead of 'auto'
90% 失败
The same conflict applies; any non-None device_map with an explicit device argument will raise an error.
-
Set `device=-1` to use CPU and keep device_map='auto'
80% 失败
device_map='auto' requires at least one GPU to be available; setting device=-1 will cause a separate error about no GPU found.