huggingface
runtime_error
ai_generated
partial
运行时错误:PEFT模型与动态适配器不兼容torch.compile
RuntimeError: torch.compile is not supported for PEFT models with dynamic adapters
ID: huggingface/torch-compile-incompatible-with-peft
80%修复率
85%置信度
1证据数
2024-03-15首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| transformers>=4.38.0 | active | — | — | — |
| peft>=0.9.0 | active | — | — | — |
| torch>=2.2.0 | active | — | — | — |
根因分析
当适配器被动态加载或切换时,torch.compile在PEFT模型上失败,因为计算图在运行时发生变化。
English
torch.compile fails on PEFT models when adapters are dynamically loaded or switched because the computation graph changes during runtime.
官方文档
https://huggingface.co/docs/peft/main/en/developer_guides/torch_compile解决方案
-
Use `torch.compile` only after loading all adapters and disable dynamic switching: `model = torch.compile(model, dynamic=False)`
-
Avoid torch.compile for PEFT models with dynamic adapters; use eager mode with `model.eval()` and manual fusion instead.
-
Set environment variable `TORCH_COMPILE_DISABLE=1` and rely on `torch.inference_mode()` for inference.
无效尝试
常见但无效的做法:
-
95% 失败
The issue is not about optimization mode; dynamic adapter switching breaks torch.compile's graph tracing regardless of mode.
-
90% 失败
The incompatibility is fundamental to PEFT's dynamic nature, not a torch bug; nightly versions have the same limitation.
-
85% 失败
torch.compile captures the graph at compile time; gradient disabling does not prevent graph changes from dynamic adapters.