拓冰建站拓冰建站
首页 / 资讯中心 / 正文

PyTorch torch.compile 编程模型:用 `torch._dynamo.error_on_graph_break` 精确控制图中断的行为

PyTorch torch.compile 编程模型用torch._dynamo.error_on_graph_break精确控制图中断的行为【免费下载链接】pytorchTensors and Dynamic neural networks in Python with strong GPU acceleration项目地址: https://gitcode.com/GitHub_Trending/py/pytorch导读torch.compile面对 graph break图中断时只有fullgraphTrue/False两档粗粒度开关——要么遇断即错要么全程容忍。本文基于 PyTorch 官方文档 Toggling error_on_graph_break系统讲解torch._dynamo.error_on_graph_break()装饰器/上下文管理器的用法、嵌套行为、与fullgraph的优先级关系并结合 torch/_dynamo/decorators.py 与 torch/_dynamo/utils.py 的源码实现说明该机制在 Dynamo 追踪器内部如何落地帮助你在整段代码要求无图中断 个别函数允许中断或默认宽松 关键路径强制无中断两类工程场景中精确编程。读完本文你能掌握error_on_graph_break与fullgraph的本质区别及优先级规则装饰器、with上下文、monkey patch 三种切换方式的完整可运行示例嵌套调用时该设置如何传播可被内层反向覆盖Dynamo 追踪器symbolic convert中该标志位的读取与判定逻辑。一、背景fullgraph的二元困境在torch.compile中处理 graph break 的两种基本模式是fullgraphTrue遇到第一个 graph break 或编译错误即报错并保证只追踪出一张图one graph guaranteefullgraphFalse默认遇到 graph break 后继续追踪生成多张图。问题场景假如希望大部分代码禁止图中断贴近fullgraphTrue的编程模型但有少数病根难除的函数存在难以消除的图中断、可以容忍或者反过来希望整体宽松fullgraphFalse但要求性能敏感的关键段绝不允许图中断。torch._dynamo.error_on_graph_break()就是为此设计。其语义引自 文档原文 并对照 torch/_dynamo/decorators.py 的 docstringtorch.compile持有一个error_on_graph_break设置项初始值为False当该设置为False时遇到 graph break 或编译错误后torch.compile尝试从断点继续编译当设置为True时torch.compile中止编译并把错误传播到用户代码。与fullgraphTrue的关键差异有三点维度fullgraphTrueerror_on_graph_breakTrue单图保证保证只捕获一张图不保证单图捕获可否运行时切换一旦为True不能改回False可在编译过程中任意切换优先级更高更低仅在fullgraphFalse时生效二、error_on_graph_break(False)整体严格、局部放行文档的第一个示例文档第 45–63 行展示了外层严格、内层局部容忍的典型模式torch._dynamo.error_on_graph_break(False) def code_with_a_difficult_graph_break(x): x x 1 torch._dynamo.graph_break() return x 2 def inner(x): return code_with_a_difficult_graph_break(x) # NOTE: fullgraphFalse torch._dynamo.error_on_graph_break(True) torch.compile def fn(x): return inner(x) # No error, but there is a graph break fn(torch.randn(3))运行结果不报错但产生一个 graph break。这正是遵循fullgraphTrue编程模型以最小化图中断但对非性能关键、难以绕过的中断段放行的用法。作为上下文管理器使用同样可以只用with语句圈住需要放行的片段# NOTE: fullgraphFalse torch._dynamo.error_on_graph_break(True) torch.compile def fn(x): x x 1 with torch._dynamo.error_on_graph_break(False): torch._dynamo.graph_break() # no error return x 2 # No error, but there is a graph break fn(torch.randn(3))对无法修改源码的第三方代码monkey patch对框架代码等无法编辑源码的场景可以直接用该装饰器包裹目标的forwardclass ThirdPartyModule(torch.nn.Module): def forward(self, x): x x 1 torch._dynamo.graph_break() return x 2 tp_mod ThirdPartyModule() tp_mod.forward torch._dynamo.error_on_graph_break(False)(tp_mod.forward) torch._dynamo.error_on_graph_break(True) torch.compile def fn(x): return tp_mod.forward(x) # No error, but there is a graph break fn(torch.randn(3))三、error_on_graph_break(True)整体宽松、关键段强制反向用法适合默认走fullgraphFalse灵活模式但保证性能关键段无图中断torch._dynamo.error_on_graph_break(True) def inner2(x): x x 1 torch._dynamo.graph_break() # error return x 2 def inner(x): return inner2(x) # fullgraphFalse, error_on_graph_breakFalse torch.compile def fn(x): x x 4 torch._dynamo.graph_break() # no error return inner(x) try: fn(torch.randn(3)) except Exception as e: print(e)注意装饰器应用的位置顺序fn内部第一个graph_break()发生在error_on_graph_breakFalse的默认作用域内所以放行进入被torch._dynamo.error_on_graph_break(True)包裹的inner2后其graph_break()会直接触发异常。异常信息会引导用户检查fullgraphFalse与error_on_graph_breakFalse的设置对应 torch/_dynamo/symbolic_convert.py 中 Make sure fullgraphFalse and error_on_graph_breakFalse. 的错误提示分支。四、嵌套行为设置对嵌套调用生效且可被内层反向覆盖torch._dynamo.error_on_graph_break()影响其作用域内所有嵌套调用的error_on_graph_break设置def inner(x): x x 1 torch._dynamo.graph_break() return x 2 def inner2(x): with torch._dynamo.error_on_graph_break(False): return inner(x) torch._dynamo.error_on_graph_break(True) torch.compile def fn(x): return inner2(x) # no error fn(torch.randn(3))外层True但inner2进入False区域后才调用含图中断的inner因此不报错。同时它可以被嵌套在另一个error_on_graph_break区域内部文档第二个嵌套示例def inner(x): x x 1 with torch._dynamo.error_on_graph_break(False): torch._dynamo.graph_break() return x 2 def inner2(x): with torch._dynamo.error_on_graph_break(True): return inner(x) torch.compile def fn(x): return inner2(x) # no error fn(torch.randn(3))即中间层inner2打开了True但最内层inner自己又切回False最终该graph_break()被放行。最近一层的作用域生效这与直觉上严格模式不可被内部放宽的担忧相反写代码时需要留意。源码层面这由ErrorOnGraphBreakDecoratorContextManager实现torch/_dynamo/decorators.py__enter__先把当前值压入prev_error_on_graph_break栈再设新值__exit__时弹栈恢复——因此嵌套天然安全。而全局值本身就是一个模块级变量# If True, enforce fullgraphTrue - raise errors on graph break _error_on_graph_break False def _get_error_on_graph_break() - bool: return _error_on_graph_break def _set_error_on_graph_break(value: bool) - None: global _error_on_graph_break _error_on_graph_break valuetorch/_dynamo/utils.py。从源码结构看追踪器每个step()都会把该全局值镜像到自己的self.error_on_graph_break字段见 torch/_dynamo/symbolic_convert.py 的注释并在内联子追踪器结束后把叶子追踪器的值回传给父追踪器torch/_dynamo/symbolic_convert.py这解释了第四节的内层可反向覆盖外层现象。在判定处SymbolicTranslate的 graph break 处理逻辑是只要one_graph即fullgraph或error_on_graph_break任一为真就把 graph break 当作错误抛出# raise original graph break if fullgraph/error_on_graph_breakTrue if self.one_graph or self.error_on_graph_break: ...torch/_dynamo/symbolic_convert.py另一处相同的复合判定见 torch/_dynamo/symbolic_convert.py。五、与fullgraph的交互优先级与不可逆性fullgraphTrue的优先级高于error_on_graph_break——当fullgraphTrue时error_on_graph_break(False)也救不了torch._dynamo.error_on_graph_break(False) def inner(x): x x 1 torch._dynamo.graph_break() return x 2 torch.compile(fullgraphTrue) def fn(x): return inner(x) try: fn(torch.randn(3)) except Exception as e: print(e)会报错。这与 torch/_dynamo/decorators.py docstring 的声明一致Iffullgraphis set, thenerror_on_graph_breakdoes nothing (i.e.fullgraph Truetakes higher precedence)。同时fullgraphTrue不可被改回False。文档给出两个方向的验证示例外层fullgraphFalse包内层fullgraphTrue以及反过来两者遇到内部 graph break 都会报错说明一旦某帧以fullgraphTrue编译后续无法用fullgraphFalse放宽。torch.compile的error_on_graph_break参数与nopython模式的互斥等细节可参见 torch/_dynamo/eval_frame.py 中对该参数的注释Ifnopythonis True,error_on_graph_breakdoes nothing。六、总结对照表fullgraph×error_on_graph_break四种组合完整继承文档末尾的总结表格error_on_graph_breakTrueerror_on_graph_breakFalse默认fullgraphTrue图中断导致报错只报告第一个图中断单图保证。fullgraph不能切换为Falseerror_on_graph_break不生效。要求用户代码完全兼容torch.compile因无图中断而保证无性能损耗。适合对图中断敏感的框架/库代码或追求极限性能的场景可防止下游用户代码意外引入图中断。与fullgraphTrueerror_on_graph_breakTrue相同因为fullgraphTrue时error_on_graph_break不生效。fullgraphFalse默认图中断导致报错只报告第一个图中断无单图保证。error_on_graph_break可切换为False。要求用户代码完全兼容torch.compile保证无性能损耗。适合对用户侧敏感代码可用False局部放行难以绕过的中断段。遇图中断后继续编译报告所有图中断。error_on_graph_break可切换为True。基本无需修改用户代码即可工作但图中断可能拖累性能。适合开箱即用、非奇形代码或不追求极限性能的场景。七、工程实践建议结合本文档与源码证据可以给出三条落地建议库/框架作者对外部用户代码使用torch._dynamo.error_on_graph_break(True)配合fullgraphFalse包裹入口可以像fullgraphTrue一样把图中断当作硬错误暴露给用户同时保留不承诺单图的灵活性避免用户代码悄然引入图中断应用开发者整体用默认fullgraphFalse快速跑通再对性能热点函数逐个加torch._dynamo.error_on_graph_break(True)逐段收紧调试配合文档示例统一在开头启用了torch._logging.set_logs(graph_breaksTrue)用于在不报错但有图中断的场景如第一节、第二节示例里实际看到断点位置验证中断确实发生在预期区域。相关测试与进一步阅读该机制的行为由 test/dynamo/test_modes.py、test/dynamo/test_decorators.py 等测试覆盖graph_break()本身的注册与上报可参见 torch/_dynamo/trace_rules.py 中对error_on_graph_break的相关处理。【免费下载链接】pytorchTensors and Dynamic neural networks in Python with strong GPU acceleration项目地址: https://gitcode.com/GitHub_Trending/py/pytorch创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
分享:

看完干货,该让你的企业上线了

免费需求沟通 · 48 小时内出具建站方案 · 河南本地可上门