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