# RuntimeError: torch.compile: Recompilation limit reached (100 recompilations) for function 'forward'. Consider using dynamic=True or reducing tensor shape variability.

- **ID:** `pytorch/compile-recompilation-limit`
- **Domain:** pytorch
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 90%

## Root Cause

torch.compile's guard mechanism triggers recompilation when tensor shapes change, and after 100 recompilations, it stops to prevent infinite loops or performance degradation.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| torch>=2.0 | active | — | — |
| torch<=2.5.1 | active | — | — |

## Workarounds

1. **Use dynamic=True in torch.compile to handle dynamic shapes: torch.compile(model, dynamic=True). This allows the compiler to handle shape variations without recompiling.** (90% success)
   ```
   Use dynamic=True in torch.compile to handle dynamic shapes: torch.compile(model, dynamic=True). This allows the compiler to handle shape variations without recompiling.
   ```
2. **Pad or resize tensors to a fixed set of shapes during training to reduce shape variability. For example, use torch.nn.utils.rnn.pad_sequence to pad sequences to a fixed length.** (85% success)
   ```
   Pad or resize tensors to a fixed set of shapes during training to reduce shape variability. For example, use torch.nn.utils.rnn.pad_sequence to pad sequences to a fixed length.
   ```

## Dead Ends

- **Disabling torch.compile entirely to avoid recompilation** — This removes the performance benefits of compilation, which may be critical for large models. (60% fail)
- **Setting torch._dynamo.config.recompile_limit = 0 to disable the limit** — This can lead to infinite recompilation loops, causing severe performance degradation or crashes. (90% fail)
