pytorch runtime_error ai_generated true

RuntimeError: torch.compile:函数 'forward' 发生图断裂,回退到 eager 模式。请考虑重写函数以避免动态控制流。

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

ID: pytorch/torch-compile-graph-break

其他格式: JSON · Markdown 中文 · English
70%修复率
84%置信度
1证据数
2024-01-15首次发现

版本兼容性

版本状态引入弃用备注
torch>=2.1 active
torch>=2.3 active
Python 3.11 active

根因分析

torch.compile 遇到图断裂(例如动态控制流、不支持的操作或 Python 端副作用),导致无法捕获整个计算图,被迫回退到 eager 模式,并可能降低性能。

English

torch.compile encountered a graph break (e.g., dynamic control flow, unsupported operations, or Python-side side effects) that prevents the entire computation graph from being captured, forcing a fallback to eager mode and potentially reducing performance.

generic

官方文档

https://pytorch.org/docs/stable/torch.compiler.html#debugging-graph-breaks

解决方案

  1. Refactor the forward method to remove dynamic control flow. Replace if statements with torch.where or masked operations:
    
    # Before (causes graph break):
    if x.sum() > 0:
        y = x * 2
    else:
        y = x + 1
    
    # After (no graph break):
    mask = (x.sum() > 0).float()
    y = mask * (x * 2) + (1 - mask) * (x + 1)
  2. Use torch.compiler.disable() decorator on specific submodules that cause graph breaks, while keeping the rest compiled:
    
    @torch.compiler.disable
    def custom_op(x):
        return my_python_function(x)
    
    model = torch.compile(model)
  3. Enable torch._dynamo.config.verbose=True and torch._dynamo.config.suppress_errors=True to get detailed logs on where the graph break occurs:
    
    import torch._dynamo
    torch._dynamo.config.verbose = True
    torch._dynamo.config.suppress_errors = True

无效尝试

常见但无效的做法:

  1. Disabling torch.compile entirely with torch.compiler.reset() 95% 失败

    This removes the performance benefits of compilation and does not address the underlying code pattern that causes graph breaks.

  2. Adding @torch.jit.script decorator on top of torch.compile 90% 失败

    torch.jit.script and torch.compile are different systems; mixing them can cause conflicts and unexpected behavior, not fixing the graph break.

  3. Using torch.inference_mode() to disable autograd entirely 85% 失败

    While inference mode reduces overhead, it does not resolve graph breaks caused by dynamic control flow in the model's forward method.