RuntimeError: 在使用 `accelerate` 启动器且多 GPU 时,不能同时设置 `device_map` 和 `device`。
RuntimeError: You cannot set both `device_map` and `device` when using the `accelerate` launcher with multiple GPUs.
ID: huggingface/accelerate-multi-gpu-device-map-conflict
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| accelerate>=0.20.0 | active | — | — | — |
| transformers>=4.30.0 | active | — | — | — |
根因分析
在使用 accelerate 启动器且多 GPU 时,设备是自动管理的;同时提供 device_map 和 device 参数会导致设备放置冲突。
English
When using accelerate launcher with multiple GPUs, the device is automatically managed; providing both device_map and device arguments causes a conflict in device placement.
官方文档
https://huggingface.co/docs/accelerate/en/package_reference/launcher解决方案
-
Remove the `device` argument and only use `device_map='auto'` (or a custom dict) when loading the model. Example: `model = AutoModelForCausalLM.from_pretrained('model-name', device_map='auto')`. The accelerate launcher will handle multi-GPU placement automatically. -
If you must set a specific device, do not use the accelerate launcher; instead, use `with torch.device('cuda:0'): model = ...` and manually wrap with DataParallel or DistributedDataParallel. -
Use `accelerate launch` without any device_map or device argument in the script; let accelerate handle device placement via its config file (e.g., `--num_processes=4`).
无效尝试
常见但无效的做法:
-
80% 失败
The error is raised explicitly; both arguments are passed and cause a conflict in the model loading logic.
-
50% 失败
With device=0, the model is placed only on GPU 0, wasting other GPUs and potentially causing OOM on GPU 0.
-
40% 失败
This bypasses accelerate's device management entirely, causing the model to be on a single GPU and not utilizing multi-GPU parallelism.