torch.compiler.substitute_in_graph#
- torch.compiler.substitute_in_graph(original_fn, *, can_constant_fold_through=False, skip_signature_check=False)[源]#
注册一个函数的 polyfill 处理程序,通常是 C 扩展中的 C 函数,用于在图内联原始函数时代替原始函数使用。
注意
polyfill 处理程序仅在内联原始函数时使用。当直接调用原始函数时,不使用它。在 eager 模式下,装饰的函数调用高性能的 C 函数而不是 polyfill 处理程序。
polyfill 处理程序是一个函数,在内联原始函数时将被调用以代替原始函数。polyfill 处理程序应具有与原始函数相同的签名和行为。
- 参数
- 返回
一个注册原始函数 polyfill 处理程序的装饰器。
- 返回类型
示例
>>> import operator >>> operator.indexOf([1, 2, 3, 4, 5], 3) 2 >>> torch.compile(operator.indexOf, fullgraph=True)([1, 2, 3, 4, 5], 3) ... # xdoctest: +SKIP("Long tracebacks") Traceback (most recent call last): ... torch._dynamo.exc.Unsupported: ... >>> @torch.compiler.substitute_in_graph(operator.indexOf) ... def indexOf(a, b, /): ... for i, item in enumerate(a): ... if item is b or item == b: ... return i ... raise ValueError("sequence.index(x): x not in sequence") >>> >>> torch.compile(operator.indexOf, fullgraph=True)([1, 2, 3, 4, 5], 3) 2