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