# 运行时错误：PEFT模型与动态适配器不兼容torch.compile

- **ID:** `huggingface/torch-compile-incompatible-with-peft`
- **领域:** huggingface
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

当适配器被动态加载或切换时，torch.compile在PEFT模型上失败，因为计算图在运行时发生变化。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| transformers>=4.38.0 | active | — | — |
| peft>=0.9.0 | active | — | — |
| torch>=2.2.0 | active | — | — |

## 解决方案

1. ```
   Use `torch.compile` only after loading all adapters and disable dynamic switching: `model = torch.compile(model, dynamic=False)`
   ```
2. ```
   Avoid torch.compile for PEFT models with dynamic adapters; use eager mode with `model.eval()` and manual fusion instead.
   ```
3. ```
   Set environment variable `TORCH_COMPILE_DISABLE=1` and rely on `torch.inference_mode()` for inference.
   ```

## 无效尝试

- **** — The issue is not about optimization mode; dynamic adapter switching breaks torch.compile's graph tracing regardless of mode. (95% 失败率)
- **** — The incompatibility is fundamental to PEFT's dynamic nature, not a torch bug; nightly versions have the same limitation. (90% 失败率)
- **** — torch.compile captures the graph at compile time; gradient disabling does not prevent graph changes from dynamic adapters. (85% 失败率)
