This is the pytorch repo. When I use torch.compile to compile a function that calls another already compiled function, will there be optimizations across the function boundary?
User
Go on
User
If you want to write a Python script that imports torch, you should not do it in this repo's root folder
Assistant
Yes, when you use torch.compile on a function that calls another already compiled function, optimizations will occur across the function boundary.
Here is what happens under the hood:
Inlining Behavior: By default, torch.compile (specifically TorchDynamo) traces through the inner function call, effectively inlining its operations into the outer function's graph. The torch.compile decorator on the inner function is ignored during this process.
User
How do you prove it using the test scripts you just wrote?
Assistant
I can demonstrate this using the output from repro_compile_order.py, which is the most definitive test.
Here is the proof based on that script's execution:
1. The inner function is physically inlined into the outer graph
When we ran outer(x), the custom backend printed the captured graph. Look at the operations listed in the Outer Graph:
[OUTER BACKEND] Compiling outer graph
opcode name target args
------------- ------ -------- -------
placeholder l_x_ L_x_ ()
call_function y sin (l_x_,) <-- Body of 'inner' (inlined)
call_function cos cos (y,) <-- Body of 'outer'
output output output ((cos,),)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Single Graph: The compiler produces a single computation graph that contains operations from both the outer and inner functions.
Cross-Function Optimization: Because the operations are in the same graph, the backend compiler (e.g., Inductor) can perform optimizations like operator fusion across the function boundary. For example, a multiplication in the inner function can be fused with an addition in the outer function.
Key Considerations
Inner Compilation Options Ignored: The configuration of the inner torch.compile (e.g., backend, mode) is ignored in favor of the outer function's configuration. The inner function is simply re-traced in the context of the outer function.
Graph Breaks: If the inner function contains a graph break (e.g., torch._dynamo.graph_break(), data-dependent control flow, or unsupported operations), it will cause a graph break in the outer function as well, potentially splitting the graph and limiting optimizations at that specific point.
allow_in_graph: If you explicitly decorate the inner function with @torch._dynamo.allow_in_graph, it will be treated as a "black box" node in the graph, preventing inlining and thus limiting cross-boundary optimizations (though the call itself is captured).
Example
import torch
@torch.compile
def inner(x):
return torch.sin(x)
@torch.compile
def outer(x):
# 'inner' is inlined here; its operations are fused with 'outer'
y = inner(x)
return torch.cos(y)
# When outer(x) is called, a single graph containing both sin and cos is generated.
Proof: The graph contains sin directly. If the functions were compiled separately without optimization across the boundary, this graph would show a call to the function inner (a "black box" call) instead of the raw sin operation.
2. The inner compiler is bypassed
In the script, I assigned different backends to inner and outer that print distinct messages ([INNER BACKEND] vs [OUTER BACKEND]).
When running inner(x) directly:
Output: [INNER BACKEND] Compiling inner graph
When running outer(x):
Output: [OUTER BACKEND] Compiling outer graph
Proof: Crucially, when outer(x) ran, the [INNER BACKEND] message did not appear. This proves that torch.compile did not "call" the compiled version of inner. Instead, it ignored the @torch.compile decorator on inner entirely, treated it as standard Python code, and traced through it to build a single, unified graph for outer.
3. Contrast with allow_in_graph
In the counter-example script repro_allow_in_graph.py, where I explicitly forced inner to be treated as a black box using @torch._dynamo.allow_in_graph, the graph looked different:
opcode name target args
------------- ------ -------- -------
call_function y inner (l_x_,) <-- Function call (NOT inlined)
call_function cos cos (y,)
This contrast confirms that the default behavior (first example) is indeed inlining and cross-function optimization.