Skip to content

[guards] Reconstruct fqn-mismatched functions when a guard is rooted at them - #192866

Closed
bobrenjc93 wants to merge 31 commits into
gh/bobrenjc93/983/basefrom
gh/bobrenjc93/983/head
Closed

bobrenjc93 wants to merge 31 commits into
gh/bobrenjc93/983/basefrom
gh/bobrenjc93/983/head

Conversation

@bobrenjc93

@bobrenjc93 bobrenjc93 commented Aug 11, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Second of six fixes the precompile PR at the top of this stack needs. That PR
serializes a Dynamo guard tree per captured variant and rebuilds it at load;
this is what stops the rebuild from failing on ordinary library code.

A serialized guard set cannot be loaded if any guard's source walks through a
function whose qualname does not resolve back to it. GuardsStatePickler looks
the function up by __module__ and __qualname__ and substitutes a _Missing
sentinel when that finds something else. For a function nothing depends on that
is correct. For one a guard is rooted at, evaluating the source against the
sentinel raises while the guard manager is still being built, and the whole load
fails.

Any functools.wraps decorator produces the mismatch, since the wrapper copies
the wrapped function's __module__ and __qualname__ while living in the
decorator's file. That makes this ordinary rather than exotic: it is why a
transformers model cannot be loaded from a compile package at all today.

The sentinel stays for functions no guard needs. A function in
guard_tree_values is reconstructed instead, through the same
_unpickle_nested_function path the <locals> branch already used. Most of the
diff is what reaching that path from the fqn branch turned out to require.

Test Plan:

python test/dynamo/test_guard_serialization.py
python test/dynamo/test_package.py

Authored with an AI assistant.

Differential Revision: D116222522

[ghstack-poisoned]
@bobrenjc93 bobrenjc93 added ciflow/trunk Trigger trunk jobs on your pull request topic: not user facing topic category labels Aug 11, 2026
@pytorch-bot

pytorch-bot Bot commented Aug 11, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❌ 1 New Failure, 1 Unrelated Failure, 2 Unclassified Failures

As of commit 26b01e2 with merge base 1b4e490 (image):

NEW FAILURE - The following job has failed:

UNCLASSIFIED FAILURES - DrCI could not classify the following jobs because the workflow did not run on the merge base. The failures may be pre-existing on trunk or introduced by this PR:

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.

@bobrenjc93

Copy link
Copy Markdown
Contributor Author

@bobrenjc93 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
bobrenjc93 added a commit that referenced this pull request Aug 14, 2026
…at them

A serialized guard set cannot be loaded if any guard's source walks through a
function whose qualname does not resolve back to it. GuardsStatePickler's
reducer_override looks the function up by __module__ and __qualname__, and when
that lookup finds something else -- or nothing -- it substitutes the _Missing
sentinel. That is right for a function nothing depends on, but a source rooted
at it then evaluates attribute access on the sentinel and raises
AttributeError: '_Missing' object has no attribute '__defaults__' while the
guard manager is still being built, so the whole load fails.

Any functools.wraps decorator produces the mismatch, because the wrapper copies
__module__ and __qualname__ from the function it wraps while living in the
decorator's own file. That makes this reachable on ordinary library code rather
than only on exotic shapes: it is why a transformers model cannot currently be
loaded from a compile package at all, and plain
torch._dynamo.config.caching_precompile hits it on the same models, so this is
not specific to any one caller.

The fix keeps the sentinel for functions no guard needs, and falls back to the
existing _unpickle_nested_function reconstruction only when the object is in
guard_tree_values, i.e. only when a guard manager node exists and the reloaded
scope must be able to evaluate sources rooted at it. That is the same
reconstruction the <locals> branch immediately above already uses, so it adds
no new mechanism.

Worth knowing, and now stated in the comment: the reconstruction takes
__globals__ from the loading process. A guard resolving through them is
therefore re-derived there rather than compared against the captured value,
which is over-strict when the module differs and blind to a global rebound
between capture and load. Both predate this change and reproduce through the
<locals> path; this widens the set of functions that reach them.

Test Plan:

The new test builds the mismatch with functools.wraps and roots a guard at the
wrapper via its __defaults__, which is what fails today:

```
python test/dynamo/test_guard_serialization.py -k fqn_mismatched
python test/dynamo/test_guard_serialization.py
```

61 tests, OK (1 skip). With the new branch neutralized the test errors with the
exact production signature, AttributeError: '_Missing' object has no attribute
'__defaults__'; restored, it passes.

Because this is shared serialization rather than one caller's code:

```
python test/dynamo/test_package.py
python test/dynamo/test_decorators.py
python test/dynamo/test_recompiles.py
python test/dynamo/test_precompile_context.py
```

Authored with an AI assistant.


ghstack-source-id: b90aced
Pull-Request: #192866
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@bobrenjc93

Copy link
Copy Markdown
Contributor Author

@bobrenjc93 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
This was referenced Aug 22, 2026
[ghstack-poisoned]
@bobrenjc93

Copy link
Copy Markdown
Contributor Author

@bobrenjc93 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@bobrenjc93

Copy link
Copy Markdown
Contributor Author

@bobrenjc93 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

2 similar comments
@bobrenjc93

Copy link
Copy Markdown
Contributor Author

@bobrenjc93 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@bobrenjc93

Copy link
Copy Markdown
Contributor Author

@bobrenjc93 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@bobrenjc93

Copy link
Copy Markdown
Contributor Author

Superseded by the canonical precompile stack #195353-#195362; this change lives in #195353. Closing to consolidate review there. The branch is kept on GitHub.

@bobrenjc93 bobrenjc93 closed this Sep 3, 2026
@github-actions
github-actions Bot deleted the gh/bobrenjc93/983/head branch November 11, 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 meta-exported module: dynamo topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant