pytorch
runtime_error
ai_generated
partial
运行时错误:torch.compile:函数 'forward' 因图断裂失败。回退到 eager 模式。考虑重写函数以避免 Python 控制流。
RuntimeError: torch.compile: function 'forward' failed with a graph break. Falling back to eager mode. Consider rewriting the function to avoid Python control flow.
ID: pytorch/torch-compile-graph-break-fallback
70%修复率
88%置信度
1证据数
2024-02-10首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| pytorch>=2.0 | active | — | — | — |
| torch.compile | active | — | — | — |
根因分析
torch.compile 遇到了无法在单个计算图中捕获的 Python 控制流(例如 if 语句、循环),导致图断裂并回退到 eager 模式。
English
torch.compile encountered Python control flow (e.g., if statements, loops) that cannot be captured in a single computation graph, causing a graph break and fallback to eager mode.
官方文档
https://pytorch.org/docs/stable/compile.html#troubleshooting解决方案
-
Refactor the forward method to replace Python control flow with tensor operations. For example, use `torch.where(condition, x, y)` instead of `if condition: ... else: ...`
-
Use torch.compiler.disable() decorator on specific submodules that require dynamic control flow: `@torch.compiler.disable`
-
Enable the 'fullgraph' option in torch.compile to get a detailed trace of where graph breaks occur: `torch.compile(model, fullgraph=True)`
无效尝试
常见但无效的做法:
-
Using torch.jit.script instead of torch.compile
80% 失败
torch.jit.script also has limitations with dynamic control flow and may produce similar errors.
-
Disabling torch.compile entirely and using eager mode
50% 失败
Avoids the error but loses all performance benefits of compilation.
-
Adding more decorators like @torch.jit.ignore
70% 失败
May suppress the error but can lead to incorrect graph construction.