Skip to content

[precompile] Rebuild a trivial frame as plain Python only when a resume name reaches it - #198908

Closed
bobrenjc93 wants to merge 6 commits into
gh/bobrenjc93/1519/basefrom
gh/bobrenjc93/1519/head
Closed

bobrenjc93 wants to merge 6 commits into
gh/bobrenjc93/1519/basefrom
gh/bobrenjc93/1519/head

Conversation

@bobrenjc93

@bobrenjc93 bobrenjc93 commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Addresses aorenste's review of #198214, which is approved and so is left
untouched.

The standalone driver rebuilt every trivial non-entry record as a plain function,
opening its module for the globals. Dynamo records every function it intercepts
while a continuation runs eager, so a callback the entry calls after
.backward() gets a trivial record too, with no resume names. When that callback
is defined in the capturing script its record names __main__, and _import
refuses __main__, so loading the artifact failed with an error blaming the
entry even when the entry's module is importable:

# mylib.py
def step(model, x, cb):
    loss = (model(x) * 2).sum()
    loss.backward()
    return cb(loss)

Nothing in the artifact can call such a record: the continuation calls whatever
cb the serving caller passes. The plain-function branch is now gated on the
record having a resume name, not on it not being the entry. A record with none
falls through to the no-variant path, which defers its refusal to a call that
never comes and binds nothing. The entry has no resume names, so it still
reaches the entry refusal as before. The resume-name pre-check uses the same
gate, so it opens exactly the modules _make_dispatcher does.

The test also folds the reset / scrub-minted-names / alias sys.modules /
exec-the-driver sequence, copied across four tests, into two helpers,
_scrub_minted and _multigraph_driver.

Test Plan:

test_trivial_continuation_is_served_as_plain_python now adds a trivial
__main__ record with no resume names to the artifact and still serves it.
With the old gate restored, all three variants fail with "captured from a
function defined in the capturing script's main module".

python test/test_precompile.py -k test_multigraph_driver_dispatches -k test_trivial_continuation -k round_trips_a_hand_built
python test/test_precompile.py
lintrunner test/test_precompile.py torch/_precompile_driver.py

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

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

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ No Failures

As of commit f523f14 with merge base 2cb7870 (image):
💚 Looks good so far! There are no failures yet. 💚

🟢 GREEN LIGHT - PR approved to be merged without human review:

  • Triviality
    • Single-cause bugfix: _make_dispatcher and the pre-check gate the plain-function rebuild on resume_names, not not is_entry
    • Test-only consolidation of four copies of the scrub/alias/exec setup into _scrub_minted and _multigraph_driver; each site matches the original
  • Correctness
    • The entry is recorded without install_to_global, so _multigraph_frames gives it no resume_names and it still reaches the entry refusal
    • An unnamed trivial record now gets a refuse stub that the resume_names binding loop never seeds, so _import is never called for its module
  • Tests
    • test_trivial_continuation_is_served_as_plain_python captures _callback_step, asserts (trivial, resume_names) == (True, []), and serves it under __main__
    • With the old gate, _scope would _import("__main__") for that record and the load would refuse, so the test pins the fix
  • Socialization
    • Private torch/_precompile_driver.py only; no signature, default, or message changes, and a load that wrongly failed now succeeds

reason: clean
Reviewed commit: f523f14

Inference job

🐛 Report a wrong verdict

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

@bobrenjc93

Copy link
Copy Markdown
Contributor Author

@claude

@claude

claude Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

Claude finished @bobrenjc93's task in 1m 39s —— View job


I found no correctness problems in the gate change. There are two small follow-ups below, and neither blocks the PR.

Review details

Summary

Before this PR, the driver rebuilt every trivial non-entry record as a plain function. Now it only does that for a trivial record that has a resume name. Unnamed trivial records, such as a __main__ callback Dynamo recorded while the continuation ran eager, fall through to the no-variant path. That path builds a refuse stub that is never bound, so _import("__main__") is never called. The pre-check at torch/_precompile_driver.py:692 uses the same predicate as _make_dispatcher, so the two still agree on which modules get opened.

I also checked these cases:

  • The entry has no resume names, so a trivial entry still reaches the refusal at :575. Nothing changed there.
  • A trivial record with no resume names and with co_freevars used to become a _plain factory. Now it becomes a refuse stub. Neither is bound, and __precompile_artifact__ can be set on either one.
  • _unreachable_frames only looks at frames with variants, so these records don't push a capture onto the installed path.

Code Quality

  • Stale comment, torch/_precompile_driver.py:556-560. The no-variants branch now also handles trivial records that have no resume name. The comment still says only "A continuation must still bind its resume names, so its refusal is deferred to a call that reaches it". Add a clause saying an unnamed record lands here too and binds nothing. The explanation currently lives only at :719-720, about 160 lines away.

Testing

  • The regression record is hand-built, not captured, test/test_precompile.py:2939. The test builds callback by copying frames[1] and changing resume_names and python_module. That covers the driver's gate. It doesn't cover the motivating case, where Dynamo itself records a callback invoked after .backward() (as in the mylib.step(..., cb) example in the PR description). If Dynamo ever gave such a record a resume name or variants, this test would still pass. Consider a cb shape that goes through _multigraph_frames, and assert that the extra record has trivial=True and resume_names == []. The existing frames[1:] assertions already follow this pattern for the closure lambda. If capturing it in the test isn't practical, say in the test comment that the record is hand-built.

Recommendation

Approve. The fix is small and consistent across both call sites, and it has a regression test. The notes above are follow-ups.

[ghstack-poisoned]
@bobrenjc93

Copy link
Copy Markdown
Contributor Author

@claude review

[ghstack-poisoned]
@bobrenjc93

Copy link
Copy Markdown
Contributor Author

@claude review

@claude

claude Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

Claude finished @bobrenjc93's task in 1m 9s —— View job


The previous review findings are fixed at HEAD ff889e9, but the new captured-callback test now re-mints Dynamo globals after the scrub, which undoes what the scrub is there to check.

Review details

Summary

The PR rebuilds a trivial record as plain Python only when the record has a resume name, and it uses the same gate in _make_dispatcher (torch/_precompile_driver.py:544) and in the resume-name pre-check (:693). The changes since the last review fix both stale comments (torch/_precompile.py:2069-2072 and torch/_precompile_driver.py:559-561). They also replace the hand-built callback with a real capture of _callback_step, which asserts (trivial, resume_names) == (True, []). One new problem comes with that capture.

Testing

  • The callback capture runs after the scrub and puts minted names back (test/test_precompile.py:2942-2953). The order is:

    1. torch._dynamo.reset()
    2. self._scrub_minted(step.__globals__)
    3. _multigraph_driver(...)
    4. the new torch._dynamo.optimize(...)(_callback_step)(...) capture

    _callback_step is defined in this test module, so the capture installs globals into the same module dict that was just scrubbed. OutputGraph.install_global picks names with unique_id_unbound_in(prefix, self.global_scope) (torch/_dynamo/output_graph.py:3651). Once the scrub has emptied the scope, the capture tends to hand out the same first-free names, such as __builtins_dict___0 and __import_*, that step's guards and rebuilt bytecode read. __compiled_fn* names may collide too, since reset() came first. The original capture's cleanup hooks have already run, so these new bindings are still in place when build() and forward(served, x) run.

    As a result, this test can no longer catch a driver that fails to bind those names. That property is the whole reason _scrub_minted exists ("the names Dynamo minted into the captured module must not be what makes the guards pass").

    Fix: capture _callback_step and compute recorded before torch._dynamo.reset() and _scrub_minted. For example, move the cb_package block right after frames = _multigraph_frames(...), so that the scrub removes what both captures minted.
    Fix this →

Recommendation

Request Changes. The driver change is correct. The new capture should move ahead of the reset and scrub so the test still checks that the driver binds its own globals.

pytorchgreenlight[bot]
pytorchgreenlight Bot previously approved these changes Oct 10, 2026
[ghstack-poisoned]
@bobrenjc93

Copy link
Copy Markdown
Contributor Author

@claude review

@claude

claude Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

Claude finished @bobrenjc93's task in 57s —— View job


The driver change at HEAD 220ac64 is still correct, but last review's blocking test-ordering issue is unchanged: the _callback_step capture still runs after _scrub_minted, so it can put back the globals the scrub is meant to remove.

Review details

Summary

The PR gates the plain-function rebuild of a trivial record on the record having a resume name, and uses the same predicate in _make_dispatcher (torch/_precompile_driver.py:539) and in the resume-name pre-check (:691). The two stale comments raised in earlier reviews are fixed. Since ff889e9, the only new commits are base updates. The PR diff itself has not changed, so the test issue below is still open.

Testing

  • The callback capture re-mints Dynamo globals after the scrub (test/test_precompile.py:2945-2961). The order is still:

    1. torch._dynamo.reset()
    2. self._scrub_minted(step.__globals__)
    3. _multigraph_driver(...)
    4. torch._dynamo.optimize(...)(_callback_step)(...)
    5. build() and forward(served, x)

    _callback_step is defined at module level in this test file (:131), so step 4 installs __compiled_fn*, __builtins_dict__* and __import_* globals into the same module dict that step 2 emptied. install_global picks the first free name, so those names can be the same ones step's guards and rebuilt bytecode read. Nothing removes them before build() runs, because the scrub cleanup only runs at teardown. The test therefore no longer proves the driver binds its own globals, which is the property _scrub_minted exists to check (:666-668).

    There are two possible fixes:

    • Move the cb_package capture and the recorded assertion to just after frames = _multigraph_frames(...), before the reset and scrub.
    • Or keep the order and call the scrub returned by _scrub_minted again after the capture: scrub = self._scrub_minted(...), then scrub() after line 2956. The helper already returns scrub for this purpose.

    Fix this →

Recommendation

Request Changes. The fix is a small reordering in the test, and it has been open since the last review.

[ghstack-poisoned]
bobrenjc93 added a commit that referenced this pull request Oct 10, 2026
…me name reaches it

Addresses aorenste's review of #198214, which is approved and so is left
untouched.

The standalone driver rebuilt every trivial non-entry record as a plain function,
opening its module for the globals. Dynamo records every function it intercepts
while a continuation runs eager, so a callback the entry calls after
`.backward()` gets a trivial record too, with no resume names. When that callback
is defined in the capturing script its record names `__main__`, and `_import`
refuses `__main__`, so loading the artifact failed with an error blaming the
entry even when the entry's module is importable:

```python
def step(model, x, cb):
    loss = (model(x) * 2).sum()
    loss.backward()
    return cb(loss)
```

Nothing in the artifact can call such a record: the continuation calls whatever
`cb` the serving caller passes. The plain-function branch is now gated on the
record having a resume name, not on it not being the entry. A record with none
falls through to the no-variant path, which defers its refusal to a call that
never comes and binds nothing. The entry has no resume names, so it still
reaches the entry refusal as before. The resume-name pre-check uses the same
gate, so it opens exactly the modules `_make_dispatcher` does.

The test also folds the reset / scrub-minted-names / alias sys.modules /
exec-the-driver sequence, copied across four tests, into two helpers,
`_scrub_minted` and `_multigraph_driver`.

Test Plan:

`test_trivial_continuation_is_served_as_plain_python` now captures
`_callback_step` (the shape above, with an identity `cb`), checks that Dynamo
recorded the callback as trivial with no resume names, adds that record, named
`__main__`, to the artifact, and still serves it. That capture runs before the
reset and `_scrub_minted`, so the scrub also removes the globals it mints and the
test still checks that the driver binds its own.
With the old gate restored, all three variants fail with "captured from a
function defined in the capturing script's __main__ module".

```
python test/test_precompile.py -k test_multigraph_driver_dispatches -k test_trivial_continuation -k round_trips_a_hand_built
python test/test_precompile.py
lintrunner test/test_precompile.py torch/_precompile_driver.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: f8e7c51
Pull-Request: #198908
@bobrenjc93

Copy link
Copy Markdown
Contributor Author

@claude review

@claude

claude Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

Claude finished @bobrenjc93's task in 40s —— View job


I found no remaining issues at HEAD f523f14. The test-ordering problem from the last two reviews is fixed, because the _callback_step capture now runs before torch._dynamo.reset() and _scrub_minted.

Review details

Summary

Before this PR, the standalone driver rebuilt every trivial non-entry record as plain Python. It now does that only for a trivial record that has a resume name. _make_dispatcher (torch/_precompile_driver.py:539) and the resume-name pre-check (:691) use the same gate. As a result, a trivial callback record with no resume name, such as one naming __main__, gets a refuse stub that nothing binds, and its module is never imported.

The earlier findings are all resolved:

  • The _multigraph_frames docstring (torch/_precompile.py:2060-2063) is updated.
  • The comment on the no-variant branch (torch/_precompile_driver.py:557-559) is updated.
  • The regression record now comes from a real capture of _callback_step, and the test asserts (trivial, resume_names) == (True, []).
  • That capture now runs before the scrub (test/test_precompile.py:2945-2958), so any globals it mints are removed before build() serves.

I found no new problems in the _scrub_minted / _multigraph_driver refactor. The one behaviour change it brings is that test_multigraph_driver_dispatches_entry_frame now also scrubs __resume_at*, which is harmless.

Recommendation

Approve.

@bobrenjc93

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge -i

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged while ignoring the following 0 checks:

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

bobrenjc93 added a commit that referenced this pull request Oct 10, 2026
…me name reaches it

Addresses aorenste's review of #198214, which is approved and so is left
untouched.

The standalone driver rebuilt every trivial non-entry record as a plain function,
opening its module for the globals. Dynamo records every function it intercepts
while a continuation runs eager, so a callback the entry calls after
`.backward()` gets a trivial record too, with no resume names. When that callback
is defined in the capturing script its record names `__main__`, and `_import`
refuses `__main__`, so loading the artifact failed with an error blaming the
entry even when the entry's module is importable:

```python
def step(model, x, cb):
    loss = (model(x) * 2).sum()
    loss.backward()
    return cb(loss)
```

Nothing in the artifact can call such a record: the continuation calls whatever
`cb` the serving caller passes. The plain-function branch is now gated on the
record having a resume name, not on it not being the entry. A record with none
falls through to the no-variant path, which defers its refusal to a call that
never comes and binds nothing. The entry has no resume names, so it still
reaches the entry refusal as before. The resume-name pre-check uses the same
gate, so it opens exactly the modules `_make_dispatcher` does.

The test also folds the reset / scrub-minted-names / alias sys.modules /
exec-the-driver sequence, copied across four tests, into two helpers,
`_scrub_minted` and `_multigraph_driver`.

Test Plan:

`test_trivial_continuation_is_served_as_plain_python` now captures
`_callback_step` (the shape above, with an identity `cb`), checks that Dynamo
recorded the callback as trivial with no resume names, adds that record, named
`__main__`, to the artifact, and still serves it. That capture runs before the
reset and `_scrub_minted`, so the scrub also removes the globals it mints and the
test still checks that the driver binds its own.
With the old gate restored, all three variants fail with "captured from a
function defined in the capturing script's __main__ module".

```
python test/test_precompile.py -k test_multigraph_driver_dispatches -k test_trivial_continuation -k round_trips_a_hand_built
python test/test_precompile.py
lintrunner test/test_precompile.py torch/_precompile_driver.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: f8e7c51
Pull-Request: #198908
bobrenjc93 added a commit that referenced this pull request Oct 10, 2026
…me name reaches it

Addresses aorenste's review of #198214, which is approved and so is left
untouched.

The standalone driver rebuilt every trivial non-entry record as a plain function,
opening its module for the globals. Dynamo records every function it intercepts
while a continuation runs eager, so a callback the entry calls after
`.backward()` gets a trivial record too, with no resume names. When that callback
is defined in the capturing script its record names `__main__`, and `_import`
refuses `__main__`, so loading the artifact failed with an error blaming the
entry even when the entry's module is importable:

```python
def step(model, x, cb):
    loss = (model(x) * 2).sum()
    loss.backward()
    return cb(loss)
```

Nothing in the artifact can call such a record: the continuation calls whatever
`cb` the serving caller passes. The plain-function branch is now gated on the
record having a resume name, not on it not being the entry. A record with none
falls through to the no-variant path, which defers its refusal to a call that
never comes and binds nothing. The entry has no resume names, so it still
reaches the entry refusal as before. The resume-name pre-check uses the same
gate, so it opens exactly the modules `_make_dispatcher` does.

The test also folds the reset / scrub-minted-names / alias sys.modules /
exec-the-driver sequence, copied across four tests, into two helpers,
`_scrub_minted` and `_multigraph_driver`.

Test Plan:

`test_trivial_continuation_is_served_as_plain_python` now captures
`_callback_step` (the shape above, with an identity `cb`), checks that Dynamo
recorded the callback as trivial with no resume names, adds that record, named
`__main__`, to the artifact, and still serves it. That capture runs before the
reset and `_scrub_minted`, so the scrub also removes the globals it mints and the
test still checks that the driver binds its own.
With the old gate restored, all three variants fail with "captured from a
function defined in the capturing script's __main__ module".

```
python test/test_precompile.py -k test_multigraph_driver_dispatches -k test_trivial_continuation -k round_trips_a_hand_built
python test/test_precompile.py
lintrunner test/test_precompile.py torch/_precompile_driver.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: f8e7c51
Pull-Request: #198908
bobrenjc93 added a commit that referenced this pull request Oct 10, 2026
…me name reaches it

Addresses aorenste's review of #198214, which is approved and so is left
untouched.

The standalone driver rebuilt every trivial non-entry record as a plain function,
opening its module for the globals. Dynamo records every function it intercepts
while a continuation runs eager, so a callback the entry calls after
`.backward()` gets a trivial record too, with no resume names. When that callback
is defined in the capturing script its record names `__main__`, and `_import`
refuses `__main__`, so loading the artifact failed with an error blaming the
entry even when the entry's module is importable:

```python
def step(model, x, cb):
    loss = (model(x) * 2).sum()
    loss.backward()
    return cb(loss)
```

Nothing in the artifact can call such a record: the continuation calls whatever
`cb` the serving caller passes. The plain-function branch is now gated on the
record having a resume name, not on it not being the entry. A record with none
falls through to the no-variant path, which defers its refusal to a call that
never comes and binds nothing. The entry has no resume names, so it still
reaches the entry refusal as before. The resume-name pre-check uses the same
gate, so it opens exactly the modules `_make_dispatcher` does.

The test also folds the reset / scrub-minted-names / alias sys.modules /
exec-the-driver sequence, copied across four tests, into two helpers,
`_scrub_minted` and `_multigraph_driver`.

Test Plan:

`test_trivial_continuation_is_served_as_plain_python` now captures
`_callback_step` (the shape above, with an identity `cb`), checks that Dynamo
recorded the callback as trivial with no resume names, adds that record, named
`__main__`, to the artifact, and still serves it. That capture runs before the
reset and `_scrub_minted`, so the scrub also removes the globals it mints and the
test still checks that the driver binds its own.
With the old gate restored, all three variants fail with "captured from a
function defined in the capturing script's __main__ module".

```
python test/test_precompile.py -k test_multigraph_driver_dispatches -k test_trivial_continuation -k round_trips_a_hand_built
python test/test_precompile.py
lintrunner test/test_precompile.py torch/_precompile_driver.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: f8e7c51
Pull-Request: #198908
bobrenjc93 added a commit that referenced this pull request Oct 10, 2026
…me name reaches it

Addresses aorenste's review of #198214, which is approved and so is left
untouched.

The standalone driver rebuilt every trivial non-entry record as a plain function,
opening its module for the globals. Dynamo records every function it intercepts
while a continuation runs eager, so a callback the entry calls after
`.backward()` gets a trivial record too, with no resume names. When that callback
is defined in the capturing script its record names `__main__`, and `_import`
refuses `__main__`, so loading the artifact failed with an error blaming the
entry even when the entry's module is importable:

```python
def step(model, x, cb):
    loss = (model(x) * 2).sum()
    loss.backward()
    return cb(loss)
```

Nothing in the artifact can call such a record: the continuation calls whatever
`cb` the serving caller passes. The plain-function branch is now gated on the
record having a resume name, not on it not being the entry. A record with none
falls through to the no-variant path, which defers its refusal to a call that
never comes and binds nothing. The entry has no resume names, so it still
reaches the entry refusal as before. The resume-name pre-check uses the same
gate, so it opens exactly the modules `_make_dispatcher` does.

The test also folds the reset / scrub-minted-names / alias sys.modules /
exec-the-driver sequence, copied across four tests, into two helpers,
`_scrub_minted` and `_multigraph_driver`.

Test Plan:

`test_trivial_continuation_is_served_as_plain_python` now captures
`_callback_step` (the shape above, with an identity `cb`), checks that Dynamo
recorded the callback as trivial with no resume names, adds that record, named
`__main__`, to the artifact, and still serves it. That capture runs before the
reset and `_scrub_minted`, so the scrub also removes the globals it mints and the
test still checks that the driver binds its own.
With the old gate restored, all three variants fail with "captured from a
function defined in the capturing script's __main__ module".

```
python test/test_precompile.py -k test_multigraph_driver_dispatches -k test_trivial_continuation -k round_trips_a_hand_built
python test/test_precompile.py
lintrunner test/test_precompile.py torch/_precompile_driver.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: f8e7c51
Pull-Request: #198908
bobrenjc93 added a commit that referenced this pull request Oct 10, 2026
…me name reaches it

Addresses aorenste's review of #198214, which is approved and so is left
untouched.

The standalone driver rebuilt every trivial non-entry record as a plain function,
opening its module for the globals. Dynamo records every function it intercepts
while a continuation runs eager, so a callback the entry calls after
`.backward()` gets a trivial record too, with no resume names. When that callback
is defined in the capturing script its record names `__main__`, and `_import`
refuses `__main__`, so loading the artifact failed with an error blaming the
entry even when the entry's module is importable:

```python
def step(model, x, cb):
    loss = (model(x) * 2).sum()
    loss.backward()
    return cb(loss)
```

Nothing in the artifact can call such a record: the continuation calls whatever
`cb` the serving caller passes. The plain-function branch is now gated on the
record having a resume name, not on it not being the entry. A record with none
falls through to the no-variant path, which defers its refusal to a call that
never comes and binds nothing. The entry has no resume names, so it still
reaches the entry refusal as before. The resume-name pre-check uses the same
gate, so it opens exactly the modules `_make_dispatcher` does.

The test also folds the reset / scrub-minted-names / alias sys.modules /
exec-the-driver sequence, copied across four tests, into two helpers,
`_scrub_minted` and `_multigraph_driver`.

Test Plan:

`test_trivial_continuation_is_served_as_plain_python` now captures
`_callback_step` (the shape above, with an identity `cb`), checks that Dynamo
recorded the callback as trivial with no resume names, adds that record, named
`__main__`, to the artifact, and still serves it. That capture runs before the
reset and `_scrub_minted`, so the scrub also removes the globals it mints and the
test still checks that the driver binds its own.
With the old gate restored, all three variants fail with "captured from a
function defined in the capturing script's __main__ module".

```
python test/test_precompile.py -k test_multigraph_driver_dispatches -k test_trivial_continuation -k round_trips_a_hand_built
python test/test_precompile.py
lintrunner test/test_precompile.py torch/_precompile_driver.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: f8e7c51
Pull-Request: #198908
bobrenjc93 added a commit that referenced this pull request Oct 10, 2026
…me name reaches it

Addresses aorenste's review of #198214, which is approved and so is left
untouched.

The standalone driver rebuilt every trivial non-entry record as a plain function,
opening its module for the globals. Dynamo records every function it intercepts
while a continuation runs eager, so a callback the entry calls after
`.backward()` gets a trivial record too, with no resume names. When that callback
is defined in the capturing script its record names `__main__`, and `_import`
refuses `__main__`, so loading the artifact failed with an error blaming the
entry even when the entry's module is importable:

```python
def step(model, x, cb):
    loss = (model(x) * 2).sum()
    loss.backward()
    return cb(loss)
```

Nothing in the artifact can call such a record: the continuation calls whatever
`cb` the serving caller passes. The plain-function branch is now gated on the
record having a resume name, not on it not being the entry. A record with none
falls through to the no-variant path, which defers its refusal to a call that
never comes and binds nothing. The entry has no resume names, so it still
reaches the entry refusal as before. The resume-name pre-check uses the same
gate, so it opens exactly the modules `_make_dispatcher` does.

The test also folds the reset / scrub-minted-names / alias sys.modules /
exec-the-driver sequence, copied across four tests, into two helpers,
`_scrub_minted` and `_multigraph_driver`.

Test Plan:

`test_trivial_continuation_is_served_as_plain_python` now captures
`_callback_step` (the shape above, with an identity `cb`), checks that Dynamo
recorded the callback as trivial with no resume names, adds that record, named
`__main__`, to the artifact, and still serves it. That capture runs before the
reset and `_scrub_minted`, so the scrub also removes the globals it mints and the
test still checks that the driver binds its own.
With the old gate restored, all three variants fail with "captured from a
function defined in the capturing script's __main__ module".

```
python test/test_precompile.py -k test_multigraph_driver_dispatches -k test_trivial_continuation -k round_trips_a_hand_built
python test/test_precompile.py
lintrunner test/test_precompile.py torch/_precompile_driver.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: f8e7c51
Pull-Request: #198908
bobrenjc93 added a commit that referenced this pull request Oct 10, 2026
…me name reaches it

Addresses aorenste's review of #198214, which is approved and so is left
untouched.

The standalone driver rebuilt every trivial non-entry record as a plain function,
opening its module for the globals. Dynamo records every function it intercepts
while a continuation runs eager, so a callback the entry calls after
`.backward()` gets a trivial record too, with no resume names. When that callback
is defined in the capturing script its record names `__main__`, and `_import`
refuses `__main__`, so loading the artifact failed with an error blaming the
entry even when the entry's module is importable:

```python
def step(model, x, cb):
    loss = (model(x) * 2).sum()
    loss.backward()
    return cb(loss)
```

Nothing in the artifact can call such a record: the continuation calls whatever
`cb` the serving caller passes. The plain-function branch is now gated on the
record having a resume name, not on it not being the entry. A record with none
falls through to the no-variant path, which defers its refusal to a call that
never comes and binds nothing. The entry has no resume names, so it still
reaches the entry refusal as before. The resume-name pre-check uses the same
gate, so it opens exactly the modules `_make_dispatcher` does.

The test also folds the reset / scrub-minted-names / alias sys.modules /
exec-the-driver sequence, copied across four tests, into two helpers,
`_scrub_minted` and `_multigraph_driver`.

Test Plan:

`test_trivial_continuation_is_served_as_plain_python` now captures
`_callback_step` (the shape above, with an identity `cb`), checks that Dynamo
recorded the callback as trivial with no resume names, adds that record, named
`__main__`, to the artifact, and still serves it. That capture runs before the
reset and `_scrub_minted`, so the scrub also removes the globals it mints and the
test still checks that the driver binds its own.
With the old gate restored, all three variants fail with "captured from a
function defined in the capturing script's __main__ module".

```
python test/test_precompile.py -k test_multigraph_driver_dispatches -k test_trivial_continuation -k round_trips_a_hand_built
python test/test_precompile.py
lintrunner test/test_precompile.py torch/_precompile_driver.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: f8e7c51
Pull-Request: #198908
cyyever pushed a commit to BakeLab/pytorch that referenced this pull request Oct 11, 2026
…me name reaches it (pytorch#198908)

Addresses aorenste's review of pytorch#198214, which is approved and so is left
untouched.

The standalone driver rebuilt every trivial non-entry record as a plain function,
opening its module for the globals. Dynamo records every function it intercepts
while a continuation runs eager, so a callback the entry calls after
`.backward()` gets a trivial record too, with no resume names. When that callback
is defined in the capturing script its record names `__main__`, and `_import`
refuses `__main__`, so loading the artifact failed with an error blaming the
entry even when the entry's module is importable:

```python
# mylib.py
def step(model, x, cb):
    loss = (model(x) * 2).sum()
    loss.backward()
    return cb(loss)
```

Nothing in the artifact can call such a record: the continuation calls whatever
`cb` the serving caller passes. The plain-function branch is now gated on the
record having a resume name, not on it not being the entry. A record with none
falls through to the no-variant path, which defers its refusal to a call that
never comes and binds nothing. The entry has no resume names, so it still
reaches the entry refusal as before. The resume-name pre-check uses the same
gate, so it opens exactly the modules `_make_dispatcher` does.

The test also folds the reset / scrub-minted-names / alias sys.modules /
exec-the-driver sequence, copied across four tests, into two helpers,
`_scrub_minted` and `_multigraph_driver`.

Test Plan:

`test_trivial_continuation_is_served_as_plain_python` now adds a trivial
`__main__` record with no resume names to the artifact and still serves it.
With the old gate restored, all three variants fail with "captured from a
function defined in the capturing script's __main__ module".

```
python test/test_precompile.py -k test_multigraph_driver_dispatches -k test_trivial_continuation -k round_trips_a_hand_built
python test/test_precompile.py
lintrunner test/test_precompile.py torch/_precompile_driver.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: pytorch#198908
Approved by: https://github.com/pytorchgreenlight
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk Trigger trunk jobs on your pull request Merged topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants