Repository navigation
[guards] Reconstruct fqn-mismatched functions when a guard is rooted at them - #192866
bobrenjc93 wants to merge 31 commits into
Conversation
🔗 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 FailuresAs of commit 26b01e2 with merge base 1b4e490 ( 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 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
…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
|
@bobrenjc93 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
|
@bobrenjc93 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
|
@bobrenjc93 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
2 similar comments
|
@bobrenjc93 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
|
@bobrenjc93 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
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.
GuardsStatePicklerlooksthe function up by
__module__and__qualname__and substitutes a_Missingsentinel 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.wrapsdecorator produces the mismatch, since the wrapper copiesthe wrapped function's
__module__and__qualname__while living in thedecorator'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_valuesis reconstructed instead, through the same_unpickle_nested_functionpath the<locals>branch already used. Most of thediff is what reaching that path from the fqn branch turned out to require.
Test Plan:
Authored with an AI assistant.
Differential Revision: D116222522