# RuntimeError: torch.compile: function 'forward' failed with a graph break. Falling back to eager mode. Consider rewriting the function to avoid control flow or dynamic shapes.

- **ID:** `pytorch/compile-graph-break-fallback`
- **Domain:** pytorch
- **Category:** module_error
- **Error Code:** `TORCH_COMPILE_GRAPH_BREAK`
- **Verification:** ai_generated
- **Fix Rate:** 85%

## Root Cause

The compiled function contains unsupported Python control flow (e.g., if statements, loops with dynamic bounds) or dynamic tensor shapes that prevent the TorchDynamo compiler from capturing a single static computation graph.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| PyTorch 2.1.0 | active | — | — |
| PyTorch 2.2.0 | active | — | — |
| PyTorch 2.3.0 | active | — | — |
| Python 3.10 | active | — | — |
| Python 3.11 | active | — | — |

## Workarounds

1. **Refactor the forward method to use torch.where, torch.masked_select, or precomputed masks instead of if-else branches. Example: if condition: x = x + 1 becomes x = x + condition.float()** (85% success)
   ```
   Refactor the forward method to use torch.where, torch.masked_select, or precomputed masks instead of if-else branches. Example: if condition: x = x + 1 becomes x = x + condition.float()
   ```
2. **Use torch._dynamo.config.log_level = logging.INFO and torch._dynamo.config.verbose = True to print detailed graph break reasons, then restructure the code accordingly. Example: import logging; logging.basicConfig(level=logging.INFO)** (90% success)
   ```
   Use torch._dynamo.config.log_level = logging.INFO and torch._dynamo.config.verbose = True to print detailed graph break reasons, then restructure the code accordingly. Example: import logging; logging.basicConfig(level=logging.INFO)
   ```
3. **Mark the problematic function with @torch.compile(disable=True) to fall back to eager mode for that specific function while keeping compilation for the rest of the model.** (75% success)
   ```
   Mark the problematic function with @torch.compile(disable=True) to fall back to eager mode for that specific function while keeping compilation for the rest of the model.
   ```

## Dead Ends

- **Disable torch.compile entirely and use eager mode** — This removes performance benefits; the model still runs but slower. It does not solve the graph break issue for production deployment. (70% fail)
- **Set torch._dynamo.config.optimize_ddp = False** — This flag only affects DDP integration, not the core graph break problem caused by control flow or dynamic shapes. (95% fail)
- **Use torch.compile with mode='reduce-overhead' instead of default** — Different modes may change compilation behavior but do not eliminate graph breaks from unsupported Python constructs. (80% fail)
