# RuntimeError：使用 Trainer 时，模型已通过 device_map='auto' 加载，不支持 device_map='auto'。请设置 device_map=None 或在单个设备上加载模型。

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

## 根因

Trainer 内部管理设备分配，与 Accelerate 的 `device_map='auto'` 设置的模型并行冲突，导致运行时断言失败。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| transformers 4.42.0 | active | — | — |
| accelerate 0.28.0 | active | — | — |
| torch 2.2.0 | active | — | — |

## 解决方案

1. ```
   Load the model without device_map: `model = AutoModelForCausalLM.from_pretrained('model-name', device_map=None)` and then pass to Trainer.
   ```
2. ```
   Use `accelerate launch` with a config file to manage multi-GPU, and set `device_map=None` in code.
   ```

## 无效尝试

- **** — Trainer does not accept `device_map` parameter; it relies on model's existing device map, causing the same conflict. (100% 失败率)
- **** — DataParallel is incompatible with Trainer's internal gradient accumulation and loss scaling, leading to silent accuracy drop or hang. (80% 失败率)
