# RuntimeError: torch.compile is not supported for PEFT models with dynamic adapters

- **ID:** `huggingface/torch-compile-incompatible-with-peft`
- **Domain:** huggingface
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

torch.compile fails on PEFT models when adapters are dynamically loaded or switched because the computation graph changes during runtime.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| transformers>=4.38.0 | active | — | — |
| peft>=0.9.0 | active | — | — |
| torch>=2.2.0 | active | — | — |

## Workarounds

1. **Use `torch.compile` only after loading all adapters and disable dynamic switching: `model = torch.compile(model, dynamic=False)`** (85% success)
   ```
   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.** (90% success)
   ```
   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.** (95% success)
   ```
   Set environment variable `TORCH_COMPILE_DISABLE=1` and rely on `torch.inference_mode()` for inference.
   ```

## Dead Ends

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