# RuntimeError: torch.compile: function 'forward' failed with a graph break. Falling back to eager mode. Consider rewriting the function to avoid unsupported operations.

- **ID:** `pytorch/torch-compile-graph-break-unsupported-op`
- **Domain:** pytorch
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 75%

## Root Cause

torch.compile encountered an operation that cannot be traced or compiled (e.g., dynamic control flow, unsupported Python built-ins), causing a graph break and fallback to eager mode.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| torch>=2.0.0 | active | — | — |
| torch>=2.1.0 | active | — | — |

## Workarounds

1. **Refactor the forward method to remove dynamic control flow (e.g., if-else statements) and use static operations like torch.where or torch.stack with masks.** (80% success)
   ```
   Refactor the forward method to remove dynamic control flow (e.g., if-else statements) and use static operations like torch.where or torch.stack with masks.
   ```
2. **Use torch.compiler.disable() decorator on specific submodules that cause graph breaks, allowing the rest to compile.** (70% success)
   ```
   Use torch.compiler.disable() decorator on specific submodules that cause graph breaks, allowing the rest to compile.
   ```

## Dead Ends

- **Disabling torch.compile entirely and using eager mode** — This removes the performance benefit of compilation; the error is a warning, not a crash, so eager fallback already occurs. (80% fail)
- **Adding @torch.jit.script decorator to the forward function** — TorchScript has different restrictions and may cause additional errors; it's not a direct fix for torch.compile graph breaks. (90% fail)
