This is the pytorch repo. When I use torch.compile to compile a function that calls…
- User
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?
- Assistant
Short answer: usually no.
torch.compileoptimizes captured graph regions. If you compileinnerand then compileouterthat callsinner_compiled, the call toinner_compiledis typically treated as a separate compiled region (or an opaque call), so you generally don’t get fusion/reordering across that boundary.What to do if you want cross-function optimization:
- Compile the top-level function/module once, and call the regular Python inner function inside it.
- Let Dynamo trace through both functions into one graph region (as long as there are no graph breaks).
- Use tools like
torch._dynamo.explain(...)to check whether graph breaks/splits are happening.
So: nested compiled calls work, but cross-boundary global optimization is limited unless both pieces are captured in the same graph.
- User
How does torch._dynamo.allow_in_graph affect this?
- Assistant
Great follow-up.
torch._dynamo.allow_in_graphchanges this boundary behavior in an important way:- It tells Dynamo frontend: “don’t graph-break on this callable; put it in the graph.”
- But it does not mean “fully optimize this function at Dynamo level” — Dynamo treats it more like a leaf/opaque call.
- In the standard
torch.compilepipeline, downstream tracing (AOTAutograd/backend) can still often see through it, so you may still get kernel-level optimizations.
For your specific nested-compile question:
- If you call an already compiled function from another compiled function, that often remains a boundary (limited cross-boundary optimization).
allow_in_graphis useful when a Python callable would otherwise cause a graph break.- Best chance for cross-function optimization is still: compile the outermost function and call the plain inner function (not pre-compiled), so it can be captured as one region.
So in short:
allow_in_graphcan help avoid graph breaks, but it’s not a magic “merge all boundaries” switch. - User
Can you prove this using a script?
- Assistant
Yep — I verified it with a runnable repro script at
repro_scenarios/prove_nested_compile_boundary.py.I ran:
bashpython repro_scenios/prove_nested_compile_boundary.py(typo-free actual path used:
repro_scenarios/prove_nested_compile_boundary.py)What it shows:
- Case A (
plain inner+ compiledouter): outer graph targets are['sin', 'cos'] - Case B (
pre-compiled inner, warmed first, then compiledouter): outer graph still captures['sin', 'cos'](inner body is inlined into outer graph) - Case C (
@torch._dynamo.allow_in_graphon inner): outer graph captures['inner', 'cos'](opaque function call boundary)
Key result:
- In this setup, calling an already compiled function from another compiled function still allows Dynamo to capture across that boundary.
allow_in_graphactually preserves the callable as a graph node (inner) rather than inlining it, which can reduce cross-function fusion opportunities.
- Case A (
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.