# RuntimeError: 在使用 `accelerate` 启动器且多 GPU 时，不能同时设置 `device_map` 和 `device`。

- **ID:** `huggingface/accelerate-multi-gpu-device-map-conflict`
- **领域:** huggingface
- **类别:** config_error
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

在使用 accelerate 启动器且多 GPU 时，设备是自动管理的；同时提供 device_map 和 device 参数会导致设备放置冲突。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| accelerate>=0.20.0 | active | — | — |
| transformers>=4.30.0 | active | — | — |

## 解决方案

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

## 无效尝试

- **** — The error is raised explicitly; both arguments are passed and cause a conflict in the model loading logic. (80% 失败率)
- **** — With device=0, the model is placed only on GPU 0, wasting other GPUs and potentially causing OOM on GPU 0. (50% 失败率)
- **** — This bypasses accelerate's device management entirely, causing the model to be on a single GPU and not utilizing multi-GPU parallelism. (40% 失败率)
