Skip to content

[precompile] Install and serve a multi-graph artifact on live code objects - #195921

Closed
bobrenjc93 wants to merge 57 commits into
gh/bobrenjc93/1123/basefrom
gh/bobrenjc93/1123/head
Closed

bobrenjc93 wants to merge 57 commits into
gh/bobrenjc93/1123/basefrom
gh/bobrenjc93/1123/head

Conversation

@bobrenjc93

@bobrenjc93 bobrenjc93 commented Sep 3, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

A source driver dispatches the entry frame and the continuations the entry's
bytecode names. A model that breaks inside nested module forwards is the
other case, and the common one: those frames are entered by an ordinary call,
so no name in the entry reaches them and a source driver would run them eager
-- correct, but with every compiled variant unused. Such a capture is now
served installed: the artifact carries the whole package under an opaque
banner, and its driver rebuilds it and hands the frames to the frame
evaluator, which reaches them by code object. The split keys on frame
REACHABILITY, computed as a closure, not on whether the capture graph-broke.

precompile_load/serve_cache_entry wire a loaded cache entry onto a
function through a serving-mode package in its own isolated compile region;
the returned _ServedCallable is a context manager whose unload() waits
for in-flight calls, uninstalls, clears the region and releases the recorded
backends, and a call after torch._dynamo.reset() cleared the installed
entries raises rather than silently recompiling. _InstalledArtifact is the
handle the installed driver returns: load installs nothing, the first call
or __enter__ installs under a lock so two racing first calls cannot both
install onto the same live code objects, the rebuilt entry gets its defaults
back from the artifact's _ENTRY_BINDING, unload/exit takes the install
back out, and a bare call after unload() raises instead of re-installing.
PrecompiledCallable is the public shape of that handle, mapping
PackageError and RecompileError to PrecompileError, and load returns it
for an artifact whose SERVING_MODE is installed so the caller can unload what
the first call installed.

Test Plan:

python test/dynamo/test_precompile_package.py
python test/dynamo/test_package.py -k serving_package
python test/test_precompile.py

Authored with an AI assistant.

[no ci]

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Sep 3, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/195921

Note: Links to docs will display an error until the docs builds have been completed.

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@pytorch-bot pytorch-bot Bot added ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests module: dynamo labels Sep 3, 2026
This was referenced Sep 3, 2026
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
bobrenjc93 added a commit that referenced this pull request Sep 24, 2026
…ing it

A graph break inside a function the entry calls (a child module's forward,
say) makes Dynamo compile that function as a frame of its own. The entry
reaches it by an ordinary call, not by LOAD_GLOBAL of a resume name, so the
standalone dispatcher in `_build_multigraph_forward` can never intercept it.
`_build_multigraph_artifact` therefore refused every such capture ("cannot
reach from the entry"), which rules out most real models with a graph break
below the top-level function.

This renders those captures as `SERVING_MODE = "installed"` instead, which
`_parse_artifact_metadata` already reads. The artifact carries the Dynamo cache
entry and backend artifacts as one `_PACKAGE` blob, plus the readable
`UNREACHABLE_WITHOUT_INSTALL` list. Its driver, `_build_installed_forward`,
imports the captured function and rebuilds `CompilePackage(fn, dynamo)`. It
then calls `CompilePackage.install`, which puts the guarded entries onto the
live code objects exactly as a warm torch.compile package load does, and
serves every call under `set_stance("fail_on_recompile")`.

Serving under `fail_on_recompile` keeps the standalone contract: the artifact
serves only what capture exercised, and an uncovered call raises rather than
compiling. The alternative, letting the installed artifact recompile at serve
time (the old #195921 design), needs serve-time-compile accounting and
isolation machinery this stack does not have. Everything used here is already
on main, so the change is Python-only.

Deliberately left for follow-ups: `unload()`/`__exit__` do not yet take the
install back out (`CompilePackage.uninstall` resets only the entry's code
object), and an uncovered call surfaces as fail_on_recompile's RuntimeError
rather than a PrecompileError.

Test Plan:

The new end-to-end test captures `calls_breaking_helper` (whose helper graph
breaks) on two shapes, checks the artifact renders as installed, and serves
it in a fresh process for both backends. Because the fresh process serves
under fail_on_recompile, a correct result also proves every frame, the
helper's included, hit an installed entry instead of compiling. The loader
then checks that an uncovered dtype raises.

```
python test/test_precompile.py -k cannot_reach -k emit_multigraph_driver -k served_installed -k round_trip_in_a_fresh -k single_graph_serves
python test/test_precompile.py
```

This PR was authored with the help of an AI assistant (Claude).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ghstack-source-id: 2df4555
Pull-Request: #198523
bobrenjc93 added a commit that referenced this pull request Sep 24, 2026
…ing it

A graph break inside a function the entry calls (a child module's forward,
say) makes Dynamo compile that function as a frame of its own. The entry
reaches it by an ordinary call, not by LOAD_GLOBAL of a resume name, so the
standalone dispatcher in `_build_multigraph_forward` can never intercept it.
`_build_multigraph_artifact` therefore refused every such capture ("cannot
reach from the entry"), which rules out most real models with a graph break
below the top-level function.

This renders those captures as `SERVING_MODE = "installed"` instead, which
`_parse_artifact_metadata` already reads. The artifact carries the Dynamo cache
entry and backend artifacts as one `_PACKAGE` blob, plus the readable
`UNREACHABLE_WITHOUT_INSTALL` list. Its driver, `_build_installed_forward`,
imports the captured function and rebuilds `CompilePackage(fn, dynamo)`. It
then calls `CompilePackage.install`, which puts the guarded entries onto the
live code objects exactly as a warm torch.compile package load does, and
serves every call under `set_stance("fail_on_recompile")`.

Serving under `fail_on_recompile` keeps the standalone contract: the artifact
serves only what capture exercised, and an uncovered call raises rather than
compiling. The alternative, letting the installed artifact recompile at serve
time (the old #195921 design), needs serve-time-compile accounting and
isolation machinery this stack does not have. Everything used here is already
on main, so the change is Python-only.

Deliberately left for follow-ups: `unload()`/`__exit__` do not yet take the
install back out (`CompilePackage.uninstall` resets only the entry's code
object), and an uncovered call surfaces as fail_on_recompile's RuntimeError
rather than a PrecompileError.

Test Plan:

The new end-to-end test captures `calls_breaking_helper` (whose helper graph
breaks) on two shapes, checks the artifact renders as installed, and serves
it in a fresh process for both backends. Because the fresh process serves
under fail_on_recompile, a correct result also proves every frame, the
helper's included, hit an installed entry instead of compiling. The loader
then checks that an uncovered dtype raises.

```
python test/test_precompile.py -k cannot_reach -k emit_multigraph_driver -k served_installed -k round_trip_in_a_fresh -k single_graph_serves
python test/test_precompile.py
```

This PR was authored with the help of an AI assistant (Claude).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ghstack-source-id: 2ffb419
Pull-Request: #198523
bobrenjc93 added a commit that referenced this pull request Sep 24, 2026
…ing it

A graph break inside a function the entry calls (a child module's forward,
say) makes Dynamo compile that function as a frame of its own. The entry
reaches it by an ordinary call, not by LOAD_GLOBAL of a resume name, so the
standalone dispatcher in `_build_multigraph_forward` can never intercept it.
`_build_multigraph_artifact` therefore refused every such capture ("cannot
reach from the entry"), which rules out most real models with a graph break
below the top-level function.

This renders those captures as `SERVING_MODE = "installed"` instead, which
`_parse_artifact_metadata` already reads. The artifact carries the Dynamo cache
entry and backend artifacts as one `_PACKAGE` blob, plus the readable
`UNREACHABLE_WITHOUT_INSTALL` list. Its driver, `_build_installed_forward`,
imports the captured function and rebuilds `CompilePackage(fn, dynamo)`. It
then calls `CompilePackage.install`, which puts the guarded entries onto the
live code objects exactly as a warm torch.compile package load does, and
serves every call under `set_stance("fail_on_recompile")`.

Serving under `fail_on_recompile` keeps the standalone contract: the artifact
serves only what capture exercised, and an uncovered call raises rather than
compiling. The alternative, letting the installed artifact recompile at serve
time (the old #195921 design), needs serve-time-compile accounting and
isolation machinery this stack does not have. Everything used here is already
on main, so the change is Python-only.

Deliberately left for follow-ups: `unload()`/`__exit__` do not yet take the
install back out (`CompilePackage.uninstall` resets only the entry's code
object), and an uncovered call surfaces as fail_on_recompile's RuntimeError
rather than a PrecompileError.

Test Plan:

The new end-to-end test captures `calls_breaking_helper` (whose helper graph
breaks) on two shapes, checks the artifact renders as installed, and serves
it in a fresh process for both backends. Because the fresh process serves
under fail_on_recompile, a correct result also proves every frame, the
helper's included, hit an installed entry instead of compiling. The loader
then checks that an uncovered dtype raises.

```
python test/test_precompile.py -k cannot_reach -k emit_multigraph_driver -k served_installed -k round_trip_in_a_fresh -k single_graph_serves
python test/test_precompile.py
```

This PR was authored with the help of an AI assistant (Claude).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ghstack-source-id: 9c00b6b
Pull-Request: #198523
bobrenjc93 added a commit that referenced this pull request Oct 10, 2026
…ing it

A graph break inside a function the entry calls (a child module's forward,
say) makes Dynamo compile that function as a frame of its own. The entry
reaches it by an ordinary call, not by LOAD_GLOBAL of a resume name, so the
standalone dispatcher in `_build_multigraph_forward` can never intercept it.
`_build_multigraph_artifact` therefore refused every such capture ("cannot
reach from the entry"), which rules out most real models with a graph break
below the top-level function.

This renders those captures as `SERVING_MODE = "installed"` instead, which
`_parse_artifact_metadata` already reads. The artifact carries the Dynamo cache
entry and backend artifacts as one `_PACKAGE` blob, plus the readable
`UNREACHABLE_WITHOUT_INSTALL` list. Its driver, `_build_installed_forward`,
imports the captured function and rebuilds `CompilePackage(fn, dynamo)`. It
then calls `CompilePackage.install`, which puts the guarded entries onto the
live code objects exactly as a warm torch.compile package load does, and
serves every call under `set_stance("fail_on_recompile")`.

Serving under `fail_on_recompile` keeps the standalone contract: the artifact
serves only what capture exercised, and an uncovered call raises rather than
compiling. The alternative, letting the installed artifact recompile at serve
time (the old #195921 design), needs serve-time-compile accounting and
isolation machinery this stack does not have. Everything used here is already
on main, so the change is Python-only.

Deliberately left for follow-ups: `unload()`/`__exit__` do not yet take the
install back out (`CompilePackage.uninstall` resets only the entry's code
object), and an uncovered call surfaces as fail_on_recompile's RuntimeError
rather than a PrecompileError.

Test Plan:

The new end-to-end test captures `calls_breaking_helper` (whose helper graph
breaks) on two shapes, checks the artifact renders as installed, and serves
it in a fresh process for both backends. Because the fresh process serves
under fail_on_recompile, a correct result also proves every frame, the
helper's included, hit an installed entry instead of compiling. The loader
then checks that an uncovered dtype raises.

```
python test/test_precompile.py -k cannot_reach -k emit_multigraph_driver -k served_installed -k round_trip_in_a_fresh -k single_graph_serves
python test/test_precompile.py
```

This PR was authored with the help of an AI assistant (Claude).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ghstack-source-id: d01d837
Pull-Request: #198523
bobrenjc93 added a commit that referenced this pull request Oct 10, 2026
…ing it

A graph break inside a function the entry calls (a child module's forward,
say) makes Dynamo compile that function as a frame of its own. The entry
reaches it by an ordinary call, not by LOAD_GLOBAL of a resume name, so the
standalone dispatcher in `_build_multigraph_forward` can never intercept it.
`_build_multigraph_artifact` therefore refused every such capture ("cannot
reach from the entry"), which rules out most real models with a graph break
below the top-level function.

This renders those captures as `SERVING_MODE = "installed"` instead, which
`_parse_artifact_metadata` already reads. The artifact carries the Dynamo cache
entry and backend artifacts as one `_PACKAGE` blob, plus the readable
`UNREACHABLE_WITHOUT_INSTALL` list. Its driver, `_build_installed_forward`,
imports the captured function and rebuilds `CompilePackage(fn, dynamo)`. It
then calls `CompilePackage.install`, which puts the guarded entries onto the
live code objects exactly as a warm torch.compile package load does, and
serves every call under `set_stance("fail_on_recompile")`.

Serving under `fail_on_recompile` keeps the standalone contract: the artifact
serves only what capture exercised, and an uncovered call raises rather than
compiling. The alternative, letting the installed artifact recompile at serve
time (the old #195921 design), needs serve-time-compile accounting and
isolation machinery this stack does not have. Everything used here is already
on main, so the change is Python-only.

Deliberately left for follow-ups: `unload()`/`__exit__` do not yet take the
install back out (`CompilePackage.uninstall` resets only the entry's code
object), and an uncovered call surfaces as fail_on_recompile's RuntimeError
rather than a PrecompileError.

Test Plan:

The new end-to-end test captures `calls_breaking_helper` (whose helper graph
breaks) on two shapes, checks the artifact renders as installed, and serves
it in a fresh process for both backends. Because the fresh process serves
under fail_on_recompile, a correct result also proves every frame, the
helper's included, hit an installed entry instead of compiling. The loader
then checks that an uncovered dtype raises.

```
python test/test_precompile.py -k cannot_reach -k emit_multigraph_driver -k served_installed -k round_trip_in_a_fresh -k single_graph_serves
python test/test_precompile.py
```

This PR was authored with the help of an AI assistant (Claude).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ghstack-source-id: ab5e097
Pull-Request: #198523
bobrenjc93 added a commit that referenced this pull request Oct 10, 2026
…ing it

A graph break inside a function the entry calls (a child module's forward,
say) makes Dynamo compile that function as a frame of its own. The entry
reaches it by an ordinary call, not by LOAD_GLOBAL of a resume name, so the
standalone dispatcher in `_build_multigraph_forward` can never intercept it.
`_build_multigraph_artifact` therefore refused every such capture ("cannot
reach from the entry"), which rules out most real models with a graph break
below the top-level function.

This renders those captures as `SERVING_MODE = "installed"` instead, which
`_parse_artifact_metadata` already reads. The artifact carries the Dynamo cache
entry and backend artifacts as one `_PACKAGE` blob, plus the readable
`UNREACHABLE_WITHOUT_INSTALL` list. Its driver, `_build_installed_forward`,
imports the captured function and rebuilds `CompilePackage(fn, dynamo)`. It
then calls `CompilePackage.install`, which puts the guarded entries onto the
live code objects exactly as a warm torch.compile package load does, and
serves every call under `set_stance("fail_on_recompile")`.

Serving under `fail_on_recompile` keeps the standalone contract: the artifact
serves only what capture exercised, and an uncovered call raises rather than
compiling. The alternative, letting the installed artifact recompile at serve
time (the old #195921 design), needs serve-time-compile accounting and
isolation machinery this stack does not have. Everything used here is already
on main, so the change is Python-only.

Deliberately left for follow-ups: `unload()`/`__exit__` do not yet take the
install back out (`CompilePackage.uninstall` resets only the entry's code
object), and an uncovered call surfaces as fail_on_recompile's RuntimeError
rather than a PrecompileError.

Test Plan:

The new end-to-end test captures `calls_breaking_helper` (whose helper graph
breaks) on two shapes, checks the artifact renders as installed, and serves
it in a fresh process for both backends. Because the fresh process serves
under fail_on_recompile, a correct result also proves every frame, the
helper's included, hit an installed entry instead of compiling. The loader
then checks that an uncovered dtype raises.

```
python test/test_precompile.py -k cannot_reach -k emit_multigraph_driver -k served_installed -k round_trip_in_a_fresh -k single_graph_serves
python test/test_precompile.py
```

This PR was authored with the help of an AI assistant (Claude).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ghstack-source-id: ab5e097
Pull-Request: #198523
bobrenjc93 added a commit that referenced this pull request Oct 10, 2026
…ing it

A graph break inside a function the entry calls (a child module's forward,
say) makes Dynamo compile that function as a frame of its own. The entry
reaches it by an ordinary call, not by LOAD_GLOBAL of a resume name, so the
standalone dispatcher in `_build_multigraph_forward` can never intercept it.
`_build_multigraph_artifact` therefore refused every such capture ("cannot
reach from the entry"), which rules out most real models with a graph break
below the top-level function.

This renders those captures as `SERVING_MODE = "installed"` instead, which
`_parse_artifact_metadata` already reads. The artifact carries the Dynamo cache
entry and backend artifacts as one `_PACKAGE` blob, plus the readable
`UNREACHABLE_WITHOUT_INSTALL` list. Its driver, `_build_installed_forward`,
imports the captured function and rebuilds `CompilePackage(fn, dynamo)`. It
then calls `CompilePackage.install`, which puts the guarded entries onto the
live code objects exactly as a warm torch.compile package load does, and
serves every call under `set_stance("fail_on_recompile")`.

Serving under `fail_on_recompile` keeps the standalone contract: the artifact
serves only what capture exercised, and an uncovered call raises rather than
compiling. The alternative, letting the installed artifact recompile at serve
time (the old #195921 design), needs serve-time-compile accounting and
isolation machinery this stack does not have. Everything used here is already
on main, so the change is Python-only.

Deliberately left for follow-ups: `unload()`/`__exit__` do not yet take the
install back out (`CompilePackage.uninstall` resets only the entry's code
object), and an uncovered call surfaces as fail_on_recompile's RuntimeError
rather than a PrecompileError.

Test Plan:

The new end-to-end test captures `calls_breaking_helper` (whose helper graph
breaks) on two shapes, checks the artifact renders as installed, and serves
it in a fresh process for both backends. Because the fresh process serves
under fail_on_recompile, a correct result also proves every frame, the
helper's included, hit an installed entry instead of compiling. The loader
then checks that an uncovered dtype raises.

```
python test/test_precompile.py -k cannot_reach -k emit_multigraph_driver -k served_installed -k round_trip_in_a_fresh -k single_graph_serves
python test/test_precompile.py
```

This PR was authored with the help of an AI assistant (Claude).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ghstack-source-id: ab5e097
Pull-Request: #198523
bobrenjc93 added a commit that referenced this pull request Oct 10, 2026
…ing it

A graph break inside a function the entry calls (a child module's forward,
say) makes Dynamo compile that function as a frame of its own. The entry
reaches it by an ordinary call, not by LOAD_GLOBAL of a resume name, so the
standalone dispatcher in `_build_multigraph_forward` can never intercept it.
`_build_multigraph_artifact` therefore refused every such capture ("cannot
reach from the entry"), which rules out most real models with a graph break
below the top-level function.

This renders those captures as `SERVING_MODE = "installed"` instead, which
`_parse_artifact_metadata` already reads. The artifact carries the Dynamo cache
entry and backend artifacts as one `_PACKAGE` blob, plus the readable
`UNREACHABLE_WITHOUT_INSTALL` list. Its driver, `_build_installed_forward`,
imports the captured function and rebuilds `CompilePackage(fn, dynamo)`. It
then calls `CompilePackage.install`, which puts the guarded entries onto the
live code objects exactly as a warm torch.compile package load does, and
serves every call under `set_stance("fail_on_recompile")`.

Serving under `fail_on_recompile` keeps the standalone contract: the artifact
serves only what capture exercised, and an uncovered call raises rather than
compiling. The alternative, letting the installed artifact recompile at serve
time (the old #195921 design), needs serve-time-compile accounting and
isolation machinery this stack does not have. Everything used here is already
on main, so the change is Python-only.

Deliberately left for follow-ups: `unload()`/`__exit__` do not yet take the
install back out (`CompilePackage.uninstall` resets only the entry's code
object), and an uncovered call surfaces as fail_on_recompile's RuntimeError
rather than a PrecompileError.

Test Plan:

The new end-to-end test captures `calls_breaking_helper` (whose helper graph
breaks) on two shapes, checks the artifact renders as installed, and serves
it in a fresh process for both backends. Because the fresh process serves
under fail_on_recompile, a correct result also proves every frame, the
helper's included, hit an installed entry instead of compiling. The loader
then checks that an uncovered dtype raises.

```
python test/test_precompile.py -k cannot_reach -k emit_multigraph_driver -k served_installed -k round_trip_in_a_fresh -k single_graph_serves
python test/test_precompile.py
```

This PR was authored with the help of an AI assistant (Claude).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ghstack-source-id: ab5e097
Pull-Request: #198523
bobrenjc93 added a commit that referenced this pull request Oct 10, 2026
…ing it

A graph break inside a function the entry calls (a child module's forward,
say) makes Dynamo compile that function as a frame of its own. The entry
reaches it by an ordinary call, not by LOAD_GLOBAL of a resume name, so the
standalone dispatcher in `_build_multigraph_forward` can never intercept it.
`_build_multigraph_artifact` therefore refused every such capture ("cannot
reach from the entry"), which rules out most real models with a graph break
below the top-level function.

This renders those captures as `SERVING_MODE = "installed"` instead, which
`_parse_artifact_metadata` already reads. The artifact carries the Dynamo cache
entry and backend artifacts as one `_PACKAGE` blob, plus the readable
`UNREACHABLE_WITHOUT_INSTALL` list. Its driver, `_build_installed_forward`,
imports the captured function and rebuilds `CompilePackage(fn, dynamo)`. It
then calls `CompilePackage.install`, which puts the guarded entries onto the
live code objects exactly as a warm torch.compile package load does, and
serves every call under `set_stance("fail_on_recompile")`.

Serving under `fail_on_recompile` keeps the standalone contract: the artifact
serves only what capture exercised, and an uncovered call raises rather than
compiling. The alternative, letting the installed artifact recompile at serve
time (the old #195921 design), needs serve-time-compile accounting and
isolation machinery this stack does not have. Everything used here is already
on main, so the change is Python-only.

Deliberately left for follow-ups: `unload()`/`__exit__` do not yet take the
install back out (`CompilePackage.uninstall` resets only the entry's code
object), and an uncovered call surfaces as fail_on_recompile's RuntimeError
rather than a PrecompileError.

Test Plan:

The new end-to-end test captures `calls_breaking_helper` (whose helper graph
breaks) on two shapes, checks the artifact renders as installed, and serves
it in a fresh process for both backends. Because the fresh process serves
under fail_on_recompile, a correct result also proves every frame, the
helper's included, hit an installed entry instead of compiling. The loader
then checks that an uncovered dtype raises.

```
python test/test_precompile.py -k cannot_reach -k emit_multigraph_driver -k served_installed -k round_trip_in_a_fresh -k single_graph_serves
python test/test_precompile.py
```

This PR was authored with the help of an AI assistant (Claude).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ghstack-source-id: ab5e097
Pull-Request: #198523
bobrenjc93 added a commit that referenced this pull request Oct 10, 2026
…ing it

A graph break inside a function the entry calls (a child module's forward,
say) makes Dynamo compile that function as a frame of its own. The entry
reaches it by an ordinary call, not by LOAD_GLOBAL of a resume name, so the
standalone dispatcher in `_build_multigraph_forward` can never intercept it.
`_build_multigraph_artifact` therefore refused every such capture ("cannot
reach from the entry"), which rules out most real models with a graph break
below the top-level function.

This renders those captures as `SERVING_MODE = "installed"` instead, which
`_parse_artifact_metadata` already reads. The artifact carries the Dynamo cache
entry and backend artifacts as one `_PACKAGE` blob, plus the readable
`UNREACHABLE_WITHOUT_INSTALL` list. Its driver, `_build_installed_forward`,
imports the captured function and rebuilds `CompilePackage(fn, dynamo)`. It
then calls `CompilePackage.install`, which puts the guarded entries onto the
live code objects exactly as a warm torch.compile package load does, and
serves every call under `set_stance("fail_on_recompile")`.

Serving under `fail_on_recompile` keeps the standalone contract: the artifact
serves only what capture exercised, and an uncovered call raises rather than
compiling. The alternative, letting the installed artifact recompile at serve
time (the old #195921 design), needs serve-time-compile accounting and
isolation machinery this stack does not have. Everything used here is already
on main, so the change is Python-only.

Deliberately left for follow-ups: `unload()`/`__exit__` do not yet take the
install back out (`CompilePackage.uninstall` resets only the entry's code
object), and an uncovered call surfaces as fail_on_recompile's RuntimeError
rather than a PrecompileError.

Test Plan:

The new end-to-end test captures `calls_breaking_helper` (whose helper graph
breaks) on two shapes, checks the artifact renders as installed, and serves
it in a fresh process for both backends. Because the fresh process serves
under fail_on_recompile, a correct result also proves every frame, the
helper's included, hit an installed entry instead of compiling. The loader
then checks that an uncovered dtype raises.

```
python test/test_precompile.py -k cannot_reach -k emit_multigraph_driver -k served_installed -k round_trip_in_a_fresh -k single_graph_serves
python test/test_precompile.py
```

This PR was authored with the help of an AI assistant (Claude).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ghstack-source-id: ab5e097
Pull-Request: #198523
bobrenjc93 added a commit that referenced this pull request Oct 10, 2026
…ing it

A graph break inside a function the entry calls (a child module's forward,
say) makes Dynamo compile that function as a frame of its own. The entry
reaches it by an ordinary call, not by LOAD_GLOBAL of a resume name, so the
standalone dispatcher in `_build_multigraph_forward` can never intercept it.
`_build_multigraph_artifact` therefore refused every such capture ("cannot
reach from the entry"), which rules out most real models with a graph break
below the top-level function.

This renders those captures as `SERVING_MODE = "installed"` instead, which
`_parse_artifact_metadata` already reads. The artifact carries the Dynamo cache
entry and backend artifacts as one `_PACKAGE` blob, plus the readable
`UNREACHABLE_WITHOUT_INSTALL` list. Its driver, `_build_installed_forward`,
imports the captured function and rebuilds `CompilePackage(fn, dynamo)`. It
then calls `CompilePackage.install`, which puts the guarded entries onto the
live code objects exactly as a warm torch.compile package load does, and
serves every call under `set_stance("fail_on_recompile")`.

Serving under `fail_on_recompile` keeps the standalone contract: the artifact
serves only what capture exercised, and an uncovered call raises rather than
compiling. The alternative, letting the installed artifact recompile at serve
time (the old #195921 design), needs serve-time-compile accounting and
isolation machinery this stack does not have. Everything used here is already
on main, so the change is Python-only.

Deliberately left for follow-ups: `unload()`/`__exit__` do not yet take the
install back out (`CompilePackage.uninstall` resets only the entry's code
object), and an uncovered call surfaces as fail_on_recompile's RuntimeError
rather than a PrecompileError.

Test Plan:

The new end-to-end test captures `calls_breaking_helper` (whose helper graph
breaks) on two shapes, checks the artifact renders as installed, and serves
it in a fresh process for both backends. Because the fresh process serves
under fail_on_recompile, a correct result also proves every frame, the
helper's included, hit an installed entry instead of compiling. The loader
then checks that an uncovered dtype raises.

```
python test/test_precompile.py -k cannot_reach -k emit_multigraph_driver -k served_installed -k round_trip_in_a_fresh -k single_graph_serves
python test/test_precompile.py
```

This PR was authored with the help of an AI assistant (Claude).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ghstack-source-id: ab5e097
Pull-Request: #198523
pytorchmergebot pushed a commit that referenced this pull request Oct 11, 2026
…ing it (#198523)

**Problem.** A capture whose graph break sits inside a function the entry calls (a child module's `forward`, say) was refused at write time, which rules out most real models with a graph break below the top-level function.

**Fix.** Such a capture is now written as an installed-mode artifact: loading it installs the captured Dynamo entries onto the live code objects and serves every call under `fail_on_recompile`.

**Summary.** A standalone artifact can serve only frames the entry reaches through a graph-break continuation, because its driver dispatches by binding the resume names the entry's bytecode loads. A graph break inside a called function compiles that function as its own frame, which the entry reaches by an ordinary call the dispatcher never sees, so `_build_multigraph_artifact` refused the capture ("cannot reach from the entry"). This PR renders those captures as `SERVING_MODE = "installed"`, carrying the whole Dynamo package in one `_PACKAGE` blob, and their driver `_build_installed_forward` installs it with `CompilePackage.install`, exactly as a warm torch.compile package load does. Every call is served under `fail_on_recompile`, so the standalone contract holds: the artifact serves only what capture exercised, and an uncovered call raises instead of compiling. Everything used is already on main, so the change is Python-only, and unload, error wrapping, the process-global stance and same-process loading are left to the PRs above.

## Background: how a standalone artifact serves

When a captured function graph-breaks, Dynamo splits it into frames: the entry compiled up to the break, and a resume function the entry's rewritten bytecode reaches by `LOAD_GLOBAL`. The standalone driver, `_build_multigraph_forward`, rebuilds each frame from its code object and guard trees and binds a dispatcher under each resume name, so the entry's bytecode loads the dispatcher, which picks the captured variant whose guards match. Nothing is installed into Dynamo, and no compiler runs at serve time.

That reaches only frames named by a resume name. The test fixture:

```python
def breaking_helper(y):
    y = y.sin()
    torch._dynamo.graph_break()
    return y.cos()

def calls_breaking_helper(model, x):
    return breaking_helper(model(x)) + 1
```

The break is inside `breaking_helper`, not in the entry. Dynamo compiles `breaking_helper` (and its own continuation) as separate frames, and the entry reaches it with an ordinary Python call. The standalone dispatcher never sees that call, so served standalone, `breaking_helper` would silently run eager.

```python
# parent: capture(calls_breaking_helper) -> PrecompileError "... cannot reach from the entry ..."
# now:    the artifact is written with SERVING_MODE = "installed" and serves compiled
```

## Render side (`torch/_precompile.py`): a mode choice instead of a refusal

```python
unreachable = _unreachable_frames(frames)   # compiled frames no continuation reaches, by fqn
mode = "installed" if unreachable else "standalone"
binding = _entry_binding(entry_fn) if mode == "standalone" else None
```

An installed artifact writes the following instead of `_FRAMES`, `_BACKENDS` and `_ENTRY_BINDING`:

```python
SERVING_MODE = "installed"
# Frames entered by an ordinary call; served by installing.
UNREACHABLE_WITHOUT_INSTALL = ['mod.breaking_helper', 'mod.torch_dynamo_resume_in_breaking_helper_at_N']
_PACKAGE = '...'   # base64(pickle) of the Dynamo cache entry (guards, transformed
                   # bytecode) and the backend artifacts it calls by id -- OPAQUE
forward = _build_installed_forward()
```

`_parse_artifact_metadata` already reads `SERVING_MODE`, so the format needs no new concept, and `load()` now sets the handle's `installed` flag from it. A reachable frame with no variant still keeps the capture standalone: the driver runs a trivial one eager, as capture did, and refuses a bypassed one when a call reaches it.

## Serve side (`torch/_precompile_driver.py`): hand serving back to Dynamo

```python
package = CompilePackage(fn, dynamo)            # fn imported by module name + FN_NAME
compiled = torch._dynamo.optimize(BACKEND, package=package)(fn)
package.install(package_state["backends"])      # guarded entries onto the live code objects

def forward(*args, **kwargs):
    with torch.compiler.set_stance("fail_on_recompile"):
        return compiled(*args, **kwargs)
```

`CompilePackage.install` puts the captured entries onto the real code objects of `calls_breaking_helper` and `breaking_helper`, so the ordinary call into the helper hits an installed cache entry through Dynamo's own frame evaluator. Before installing, the driver refuses with a `PrecompileError` when the Python version differs from capture (the bytecode is marshalled), when the torch version differs (the guard state is pickled), when the function was defined in `__main__`, or when it is no longer importable.

## Why `fail_on_recompile`, and the rejected alternative

Serving under `fail_on_recompile` keeps the standalone contract: the artifact serves only what capture exercised, and an uncovered call raises rather than compiling. The alternative, letting the installed artifact recompile at serve time (the old #195921 design), needs serve-time-compile accounting and isolation machinery this stack does not have.

## Tests

| Test | Pins |
|---|---|
| `test_multigraph_artifact_installs_a_frame_the_entry_cannot_reach` | the render: `SERVING_MODE = "installed"`, an `UNREACHABLE_WITHOUT_INSTALL` list, `forward = _build_installed_forward()`, and no `_FRAMES` blob |
| `test_a_frame_the_entry_cannot_reach_is_served_installed` (both backends) | captures `calls_breaking_helper` on two shapes, checks the unreachable list names the helper and its continuation, and serves it in a fresh process; since that process serves under `fail_on_recompile`, a correct result proves every frame, the helper's included, hit an installed entry instead of compiling, and an uncovered dtype must raise |

## Left to the PRs above

This PR is kept to the minimum that serves; each gap is its own draft on top.

| Gap in this PR | Addressed in |
|---|---|
| An uncovered call surfaces as fail_on_recompile's `RuntimeError`, not a `PrecompileError`, and a failed install is not wrapped | #198556 |
| `set_stance(...)` per call flips the process-global stance, so another thread's `torch.compile` raises while a call is being served | #198557 (the refusal is bound into the artifact's own callback; compiled_autograd is refused at load) |
| Loading in a process that already compiled those frames leaves live entries that serve misses | #198558 (refused at load) |
| `unload()`/`__exit__` do not take the install back out (`CompilePackage.uninstall` resets only the entry's code object) | not yet; the docs say to serve from a process you can throw away |

Test Plan:

The new end-to-end test captures `calls_breaking_helper` (whose helper graph
breaks) on two shapes, checks the artifact renders as installed, and serves
it in a fresh process for both backends. Because the fresh process serves
under fail_on_recompile, a correct result also proves every frame, the
helper's included, hit an installed entry instead of compiling. The loader
then checks that an uncovered dtype raises.

```
python test/test_precompile.py -k cannot_reach -k emit_multigraph_driver -k served_installed -k round_trip_in_a_fresh -k single_graph_serves
python test/test_precompile.py
```

This PR was authored with the help of an AI assistant (Claude).

Co-Authored-By: Claude Opus 5.5 <[email protected]>

Pull Request resolved: #198523
Approved by: https://github.com/Mingming-Ding
@bobrenjc93 bobrenjc93 closed this Oct 16, 2026
@github-actions
github-actions Bot deleted the gh/bobrenjc93/1123/head branch November 17, 2026 01:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests ciflow/trunk Trigger trunk jobs on your pull request module: dynamo release notes: dynamo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant