# RuntimeError：您正在尝试训练一个 4-bit 模型，但您设置了 `inference_mode=True`。请将 `inference_mode=False` 设置为训练模式。

- **ID:** `huggingface/peft-4bit-inference-mode`
- **领域:** huggingface
- **类别:** config_error
- **验证级别:** ai_generated
- **修复率:** 98%

## 根因

当使用 PEFT 与 4-bit 量化基础模型时，LoRA 配置中的 `inference_mode` 标志在训练时必须为 False，但被设置为 True。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| peft>=0.5.0 | active | — | — |
| transformers>=4.31.0 | active | — | — |
| bitsandbytes>=0.41.0 | active | — | — |

## 解决方案

1. ```
   在 LoRA 配置中设置 `inference_mode=False`：`lora_config = LoraConfig(r=8, lora_alpha=32, inference_mode=False)`
   ```
2. ```
   如果直接使用 PEFT 模型，调用 `model.train()` 并确保配置初始时设置了 `inference_mode=False`。示例：`peft_config = LoraConfig.from_pretrained('path')` 然后修改 `peft_config.inference_mode = False` 再创建模型。
   ```

## 无效尝试

- **** — The error is specifically about the LoRA `inference_mode` parameter, not the model loading quantization. The LoRA config must be explicitly updated. (85% 失败率)
- **** — The `inference_mode` flag in the LoRA config is separate from the model's training state. `model.train()` does not affect PEFT's internal configuration. (90% 失败率)
