Skip to content

[precompile] Render a multi-graph capture as standalone Python source - #195918

Closed
bobrenjc93 wants to merge 7 commits into
gh/bobrenjc93/1120/basefrom
gh/bobrenjc93/1120/head
Closed

bobrenjc93 wants to merge 7 commits into
gh/bobrenjc93/1120/basefrom
gh/bobrenjc93/1120/head

Conversation

@bobrenjc93

@bobrenjc93 bobrenjc93 commented Sep 3, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

The emission half of the dynamo tracer: given a package's cache entry, its
compiled backends and a summary, _build_multigraph_artifact renders the
same (python_code, cache) pair the make_fx tracer returns. The file is
labelled by what has a source form. What was captured -- the frames, their
variant counts, the guards that could not be serialized, the values pinned to
what capture saw -- is emitted as readable literals; the compiled subgraphs are
rendered as source through the same aot_autograd.compile_to_python the make_fx
tracer uses (they are Inductor output, not Dynamo state), and only the guard
trees and the transformed bytecode, which genuinely have no source form, sit
under an OPAQUE banner. Rendered blocks share one namespace and resolve their
siblings as late-bound globals, so every top-level name a block defines is
suffixed per subgraph, driven by AST positions rather than a text match so an
attribute, a nested binding and a dotted import path stay out of the rewrite.

The standalone driver (_build_multigraph_forward) rebuilds each frame's
guard trees, binds continuations under the resume names the entry's bytecode
loads, restores the entry's defaults aligned to the LAST parameters the way
__defaults__ is, and RAISES for a call no captured variant covers rather
than compiling one. The driver keeps one globals dict per module the frames
were compiled in -- a copy of that module's dict overlaid with the artifact's
own bindings -- so a rendered subgraph's unsuffixed imports cannot shadow a
user global the guards were written against. A capture whose entry frame
Dynamo never compiled is refused at capture time, and a capture with frames
the entry cannot reach is warned about here -- they run eager when served,
correct but uncompiled -- with serving those by installing the next commit.
_parse_artifact_metadata reads the dynamo literal set when TRACER says so
(its SERVING_MODE reader is ahead of the installed mode that commit adds),
the make_fx cache envelope gains a tracer tag, both make_fx drivers turn
ambient autocast off on the devices the captured graph dispatches on, and
_graph_has_dynamic_shapes reads example_value so a dynamic Dynamo
graph is not rendered static. PrecompiledModule now derives from
PrecompiledRunnable, the base every loaded artifact will share.

Test Plan:

python test/test_precompile.py
python test/dynamo/test_precompile_package.py
python test/functorch/test_compile_to_python.py -k dynamic_shapes

Authored with an AI assistant.

[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/195918

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

❌ 2 New Failures, 1 Unrelated Failure

As of commit 085aa48 with merge base 1ad121e (image):

NEW FAILURES - The following jobs have failed:

BROKEN TRUNK - The following job failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

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]
@bobrenjc93 bobrenjc93 added ciflow/trunk Trigger trunk jobs on your pull request release notes: dynamo labels Sep 4, 2026
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
bobrenjc93 added a commit that referenced this pull request Sep 4, 2026
The emission half of the dynamo tracer: given a package's cache entry, its
compiled backends and a summary, ``_build_multigraph_artifact`` renders the
same ``(python_code, cache)`` pair the make_fx tracer returns. The file is
labelled by what has a source form. What was captured -- the frames, their
variant counts, the guards that could not be serialized, the values pinned to
what capture saw -- is emitted as readable literals; the compiled subgraphs are
rendered as source through the same aot_autograd.compile_to_python the make_fx
tracer uses (they are Inductor output, not Dynamo state), and only the guard
trees and the transformed bytecode, which genuinely have no source form, sit
under an OPAQUE banner. Rendered blocks share one namespace and resolve their
siblings as late-bound globals, so every top-level name a block defines is
suffixed per subgraph, driven by AST positions rather than a text match so an
attribute, a nested binding and a dotted import path stay out of the rewrite.

The standalone driver (``_build_multigraph_forward``) rebuilds each frame's
guard trees, binds continuations under the resume names the entry's bytecode
loads, restores the entry's defaults aligned to the LAST parameters the way
``__defaults__`` is, and RAISES for a call no captured variant covers rather
than compiling one. The driver keeps one globals dict per module the frames
were compiled in -- a copy of that module's dict overlaid with the artifact's
own bindings -- so a rendered subgraph's unsuffixed imports cannot shadow a
user global the guards were written against. A capture whose entry frame
Dynamo never compiled is refused at capture time, and a capture with frames
the entry cannot reach is warned about here -- they run eager when served,
correct but uncompiled -- with serving those by installing the next commit.
``_parse_artifact_metadata`` reads the dynamo literal set when TRACER says so
(its SERVING_MODE reader is ahead of the installed mode that commit adds),
the make_fx cache envelope gains a ``tracer`` tag, both make_fx drivers turn
ambient autocast off on the devices the captured graph dispatches on, and
``_graph_has_dynamic_shapes`` reads ``example_value`` so a dynamic Dynamo
graph is not rendered static. ``PrecompiledModule`` now derives from
``PrecompiledRunnable``, the base every loaded artifact will share.

Test Plan:

```
python test/test_precompile.py
python test/dynamo/test_precompile_package.py
python test/functorch/test_compile_to_python.py -k dynamic_shapes
```

Authored with an AI assistant.

ghstack-source-id: fd0f6d6
Pull-Request: #195918
[ghstack-poisoned]
[ghstack-poisoned]
bobrenjc93 added a commit that referenced this pull request Sep 5, 2026
The emission half of the dynamo tracer: given a package's cache entry, its
compiled backends and a summary, ``_build_multigraph_artifact`` renders the
same ``(python_code, cache)`` pair the make_fx tracer returns. The file is
labelled by what has a source form. What was captured -- the frames, their
variant counts, the guards that could not be serialized, the values pinned to
what capture saw -- is emitted as readable literals; the compiled subgraphs are
rendered as source through the same aot_autograd.compile_to_python the make_fx
tracer uses (they are Inductor output, not Dynamo state), and only the guard
trees and the transformed bytecode, which genuinely have no source form, sit
under an OPAQUE banner. Rendered blocks share one namespace and resolve their
siblings as late-bound globals, so every top-level name a block defines is
suffixed per subgraph, driven by AST positions rather than a text match so an
attribute, a nested binding and a dotted import path stay out of the rewrite.

The standalone driver (``_build_multigraph_forward``) rebuilds each frame's
guard trees, binds continuations under the resume names the entry's bytecode
loads, restores the entry's defaults aligned to the LAST parameters the way
``__defaults__`` is, and RAISES for a call no captured variant covers rather
than compiling one. The driver keeps one globals dict per module the frames
were compiled in -- a copy of that module's dict overlaid with the artifact's
own bindings -- so a rendered subgraph's unsuffixed imports cannot shadow a
user global the guards were written against. A capture whose entry frame
Dynamo never compiled is refused at capture time, and a capture with frames
the entry cannot reach is warned about here -- they run eager when served,
correct but uncompiled -- with serving those by installing the next commit.
``_parse_artifact_metadata`` reads the dynamo literal set when TRACER says so
(its SERVING_MODE reader is ahead of the installed mode that commit adds),
the make_fx cache envelope gains a ``tracer`` tag, both make_fx drivers turn
ambient autocast off on the devices the captured graph dispatches on, and
``_graph_has_dynamic_shapes`` reads ``example_value`` so a dynamic Dynamo
graph is not rendered static. ``PrecompiledModule`` now derives from
``PrecompiledRunnable``, the base every loaded artifact will share.

Test Plan:

```
python test/test_precompile.py
python test/dynamo/test_precompile_package.py
python test/functorch/test_compile_to_python.py -k dynamic_shapes
```

Authored with an AI assistant.

ghstack-source-id: 72621b9
Pull-Request: #195918
@bobrenjc93 bobrenjc93 closed this Oct 17, 2026
@github-actions
github-actions Bot deleted the gh/bobrenjc93/1120/head branch November 17, 2026 01:43
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