Repository navigation
[dynamo] Replace a same-named import alias binding only when the resolved module is live - #197256
bobrenjc93 wants to merge 34 commits into
Conversation
[ghstack-poisoned]
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/197256
Note: Links to docs will display an error until the docs builds have been completed. ⏳ 4 Pending, 2 Unrelated FailuresAs of commit 921b87d with merge base 51d4378 ( UNSTABLE - The following jobs are marked as unstable, possibly due to flakiness on trunk:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This PR needs a
|
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
|
Re-reviewed against the current head 1. > Correction to my earlier review on this PR. I claimed the skip arm turns a previously-impossible Already addressed at 2. > Owned by #197135: the divergence is real at
3. > Already addressed at 4. > Already addressed at 5. > The empty-slot write this commit keeps is now unpinned. [...] Already addressed at 6. > The PR description no longer matches the tests. [...] Open: still true, in the opposite direction, and description-only. Re-measured on 7. > Already addressed at 8. > Already addressed at 9. > Already addressed at 10. > Recommendation: Request Changes [...] Already addressed at |
|
Claude finished @bobrenjc93's task in 40m 32s —— View job One factual correction that matters: the justification given for deferring the Full reviewPR Review: #197256Summary
Code Quality
Testing
RecommendationRequest Changes Narrow. The write rule is right and the earlier rounds converged. What blocks is the |
[ghstack-poisoned]
[ghstack-poisoned]
|
Re-reviewed against the current head 1. > The stack-ordering justification for Open, for the author to decide: the point that the parent (#196896) compiles the shim sequence cleanly, so the intermediate tree regresses a working path until #197135 re-roots the guards, is taken; the options are to move #197135's re-rooting below or into this commit, or to keep the order with the FAQ re-measured against the parent. No code changed on this arm at 2. > Fixed in 3. > Fixed in 4. > Five comments name positions in the stack rather than behaviour [...] Fixed in 5. > Open with item 1: a hook registered after the compile is the silent half of the same ordering question, so it is decided together with it rather than pinned here first. 6. > The PR description is still the stale one [...] Queued for the author with the re-measured figures (six tests, nineteen instances, 97 file tests, the |
|
Claude finished @bobrenjc93's task in 36m 5s —— View job The head commit Full reviewPR Review: #197256SummaryThe PR is titled and described as "replace a same-named import alias binding only when the resolved module is live", i.e. keep the memo as the resolved value and use Code Quality
TestingThree tests fail at
RecommendationRequest Changes Blocking: |
[ghstack-poisoned]
[ghstack-poisoned]
|
Confirmed, and fixed at head |
|
Claude finished @bobrenjc93's task in 21m 5s —— View job Confirmed: Full reviewPR Review: #197256SummaryThe rebuild error is fixed. At Code Quality
Testing
RecommendationRequest Changes — narrowly, and none of it is the write rule, which is right and is now correctly implemented on this head. The blocking items are the stale forward reference at |
[ghstack-poisoned]
|
Re-reviewed against the current head 1. > Fixed in 2. > One direction of the guard-build hard error is the skip arm's own, and it is both untested and explicitly disclaimed. [...] Either qualify the attribution or add the case [...] Fixed in 3. > Open, for the author to decide, as before; unchanged at 4. > Description arithmetic (open). [...] Queued for the author with the re-measured figures; the body is hand-maintained, so it is not rewritten by script. |
|
Claude finished @bobrenjc93's task in 23m 37s —— View job The write rule is correctly implemented again at Full reviewPR Review: #197256Summary
Code Quality
Testing
RecommendationRequest Changes — narrowly, and not against the write rule, which is right and correctly implemented on this head. Blocking: the |
[ghstack-poisoned]
[ghstack-poisoned]
|
Re-reviewed against the current head 1. > Open, for the author to decide, together with the ordering question below: whether the skip arm should apply to the reconstruction and tracing-phase callers at all, or only to 2. > Fixed in 3. > Fixed below, in #197255, where that comment lives: the clause is cut; the sentence keeps the memo-hit and taken-slot halves. 4. > No test covers the skip arm through a reconstruction caller. [...] Open with item 1: which callers the skip arm should cover decides what such a test pins, so it is written once that is settled. 5. > Open, for the author, as before; the description figures are queued. |
|
Claude finished @bobrenjc93's task in 54m 20s —— View job Confirmed the two comment items from the last round are folded at Full reviewPR Review: #197256Summary
Code Quality
Testing
RecommendationRequest Changes — for the new finding only. The write rule is right and correctly implemented here, and the test hygiene of the six new tests checks out (all mutations restored in |
Stack from ghstack (oldest at bottom):
Split from
[dynamo] Rebind a stale module alias instead of failing the trace(#196896) for the review budget, as the top of three:
[dynamo] Graph-break instead of asserting when an import alias is already bound(#196896) turnsthe alias collision into a graph break, #196896 directly below accepts a module
object of the right name in the slot and writes the resolved module over it,
and this commit decides which module stays: the resolved module replaces a
writer's same-named binding only when it is the live
sys.modulesentry.Below this commit,
import_sourcewrites the module it resolved into thealias slot whenever the check passes, and
valueis_import_module's memo,the first object the process resolved under this name,
functools.cached andnever invalidated. A writer that resolves the name itself --
CompilePackage.installbinding a recorded alias toimportlib.import_module(module_name), orAOTCompiledFunction._seed_guard_scopeseeding a guard scope -- bound the live entry at the time it ran, and after a
sys.moduleshandover the memo and that binding are two module objects of thesame name, either of which can be the older one, depending on whether the
handover happened before or after the first trace of the name. When the
handover came first,
install()bound the live module and the memo is theolder object; #196896 then puts the memo back over
install()'s binding, andthe graph -- specialized on what
IMPORT_NAMEpushed, which is what__import__returned, the live entry -- is guarded through the alias on amodule the program no longer holds under that name, so a change to the live
module's attributes goes unseen by the guards. That is the direction the
review of #196896 called harmful, and it is what this commit removes.
Which object the slot holds afterwards now depends on whether the module
resolved here is the live one, decided where it is resolved, on each branch by
its own means. On the normal branch
valueis live whenmodule_name in sys.modules and sys.modules[module_name] is value. On thetorch_packagebranch
valueis a lookup in_package_imported_modules, the importerregistry keyed by the module's own mangled
__name__, not a memo: it is themodule the name resolves to now, live by construction, and a mangled name is
never a
sys.moduleskey (install()'simportlib.import_modulecannot bindone:
ModuleNotFoundError: No module named '<torch_package_0>'), so thismethod is the only writer of such an alias in practice. The write is
f_globals[alias] = valuewhen the slot is empty, already holdsvalue, orvalueis live; a writer's same-named module that is notvaluestays in theslot otherwise. In the code that is two names read at the write:
conflict = alias in f_globals and f_globals[alias] is not valuegates the name check,which is #196896's unchanged, and
write_value = not conflict or live, setonce after it, is what the write tests, so the three outcomes -- an empty or
same-object slot written, a same-named conflict written only when the memo is
live, a name mismatch graph-broken -- read at the site.
Concretely: when a shim hands over to the real module after
install()boundthe shim, the next trace rebinds the alias to the real module it resolves (the
memo, taken at that trace); when the memo is the shim and
install()bound thereal module after the handover, the trace leaves
install()'s binding alone.test_import_alias_keeps_the_installed_live_modulepins the second case andthe difference from #196896: with that commit's
symbolic_convert.pyunderthis test file the alias is the memoized module after the trace, and the
assertIsagainstinstall()'s module fails; here the alias keeps the livemodule, and setting that module's
VALUErecompiles, pinned with aCompileCountergoing from 1 to 2. When neither is live -- memo,install()'smodule and live entry all distinct, a second handover having followed the
install -- the writer's module stays, since the memo would be no less stale,
and the guards then read that module while the graph holds the live one's
values:
test_import_alias_keeps_the_installed_module_when_neither_is_livepins that the alias still holds
install()'s module after the trace, that thecompiled function keeps returning the live module's
x * 9after thatmodule's
VALUEis set to 10 with no recompile -- a divergence from eagerpinned as one with
assertNotEqual-- and that setting the installed module'sVALUErecompiles. That blindness is the memo's own: an empty slot has itbelow these commits and here alike whenever the memo is not the live entry
(the alias then holds the memo and the graph the live module), and the
refusal of an occupied slot was no guard against it. On the
get_globals_source_and_valuepath that empty slot has a write side too: aninlined
STORE_GLOBALreplays through the alias onto the memo while the traceread the live module, the one whose
__dict__is the frame's globals(
_registered_module_for_globalsreturns no other), and the comment at thewrite says so alongside the guard reads.
test_import_alias_of_an_empty_slot_is_the_memo_even_when_stalepins thatempty-slot form as the accepted residual: the memo is taken with one module
live, the alias popped and the name handed to a second module, and the next
trace binds the memo into the empty slot -- the compiled function keeps
returning the live module's
x + 7after that module'sVALUEis set to 8,diverging from eager with
frame_countstill 1, and recompiles when thememo's
VALUEchanges. It passes on the parent, which has the same write foran empty slot; it fails on the mutant that binds the live entry (below), which
is the outcome of #197046 higher in this stack, and that commit rewrites the
pin into a recompile. Resolving the live entry for every caller, and what a
name that has since left
sys.modulesshould resolve to, is that commit.The record does not follow the write: an alias the check accepted but the
trace left alone -- a writer's live module in the slot, the memo older -- is
recorded for the
CompilePackageentry all the same, because the graph'sguards read through the alias whoever bound it, and a process that loads the
package has no writer but
install(); without the record it binds nothingthere and the loaded guards fail on
KeyError on G['__import_<name>'].test_import_alias_the_trace_left_alone_is_recorded_for_installbuilds thatround trip: the second function is traced under its own
CompilePackageonthe skip arm, its entry is pinned to record the alias while the writer's
binding stays, and after a save the alias is deleted and the name handed to a
third module before the load, so
install()binds a module distinct from boththe slot the trace left alone and the module its graph was built from. That
third module carries a
VALUEof its own, 99 against the 7 the graph wasbuilt from, and the loaded function serves
x * 7underfail_on_recompile,diverging from eager: its
EQUALS_MATCHonVALUEis rebuilt at load againstthe installed module, as
G['__import_<name>'].VALUE == 99, so it comparesthat module against itself. That is what a module pickled by name gives any
loaded guard -- the artifact carries the value its graph baked and not the
module -- present below these commits and outside what the record decides
(first FAQ). The record's part is that the third module is behind the alias
at all, where the guard reads it live: setting its
VALUEto 5 fails thatguard, and the test pins the
RuntimeErrorby the guard's own text._import_modulekeeps its behaviour; its docstring now says what the memo is-- the process's first resolution, never invalidated -- and where
import_sourcebinds it -- into an empty slot regardless, over anotherwriter's same-named binding only when it is the live entry -- in place of an
import-cost claim that holds only for a name importlib has not yet seen.
nn_modules_globals_vtkeeps building itsVariableTrackerfrom the memowhile its source is the alias, which can now hold a writer's same-named module
on the skip arm; the comment there says so, and the FAQ below says why the
value is not read back through the alias.
Behaviour changes against the parent, each pinned by a test below. (a) A
writer's same-named module in the slot stays when the resolved module is an
older memo, whether the writer's module is the live entry
(
test_import_alias_keeps_the_installed_live_module,test_import_alias_the_trace_left_alone_is_recorded_for_install) or a secondhandover has since replaced it too
(
test_import_alias_keeps_the_installed_module_when_neither_is_live); on theparent the memo was written over it. (b) An alias accepted but left alone is
recorded for install
(
test_import_alias_the_trace_left_alone_is_recorded_for_install; on theparent every accepted alias was written, so the case did not arise). An empty
slot takes the resolved module as on the parent, live or not
(
test_import_alias_of_an_empty_slot_is_the_memo_even_when_stale), and thetorch_packagebranch's replacement stays unconditional(
test_import_alias_accepts_a_stale_torch_package_module, from #196896, pinsit against the mutant below).
Order to review:
import_sourceintorch/_dynamo/symbolic_convert.py(
liveon each branch,conflict, the comment at the write andwrite_value), then_import_module's docstring and the comment innn_modules_globals_vt, then the four tests intest/dynamo/test_package.pyin the order above.
Test Plan:
Four tests. Three are red on the parent's
torch/_dynamo/symbolic_convert.pyunder this commit's test file (
Ran 13 tests,FAILED (failures=3), each onthe
assertIsof the alias againstinstall()'s module, the parent havingput the memo back; the nine instances of the two commits below pass), and one,
test_import_alias_of_an_empty_slot_is_the_memo_even_when_stale, passes onthe parent by design, pinning the empty-slot write this commit keeps; it fails
on the mutant that binds the live entry (below). One builds the memo-is-older
sequence end to end: a function is compiled with a
CompilePackageand savedwhile
sys.modulesholds a module withVALUE = 2, the entry is handed to asecond module with
VALUE = 7, the package is loaded andinstall()ed so thealias binds that second module, a second function in the same globals is
compiled through
importof the name with aCompileCounter, and the aliasis pinned to still hold
install()'s module; then the live module'sVALUEisset to 8 and the compiled function is pinned to agree with eager with
frame_countgone from 1 to 2, the recompile the guard through the livemodule buys. One extends that sequence by a second handover, to a third module
with
VALUE = 9, before the second function is compiled: the alias is pinnedto still hold
install()'s module, the compiled result to bex * 9and tostay
x * 9after the live module'sVALUEis set to 10 -- a divergence fromeager's
x * 10, pinned as one withassertNotEqual-- withframe_countstill 1, and a change to the installed module's
VALUEto recompile. One runsthe memo-is-older sequence with a
CompilePackageon the second function too:after
install()binds the live module, the second function is traced underits own package, its entry is pinned to record the alias with the writer's
module still in the slot, the package is saved, the alias deleted and the name
handed to a third module with
VALUE = 99, and the second package is loadedand
install()ed: the alias is pinned to hold the third module, the loadedfunction to serve
x * 7underfail_on_recompile-- the value its graph wasbuilt from, diverging from eager's
x * 99, pinned withassertNotEqual--and a change of the third module's
VALUEto fail the loaded guard, theRuntimeErrorpinned by the guard's textG['__import_<name>'].VALUE == 99.One takes the memo with one module live, pops the alias, resets, hands the
name to a second module with
VALUE = 7and compiles with aCompileCounter:the alias is pinned to hold the memo, the result to be
x + 7and to stayx + 7after the live module'sVALUEis set to 8 -- a divergence from eagerpinned with
assertNotEqual,frame_countstill 1 -- and a change to thememo's
VALUEto recompile and agree with eager again.Each is load-bearing, measured on this tree by mutation (
-k test_import_alias, all thirteen instances, after each). Writing the resolvedmodule unconditionally (
write_value = True, the parent's write) fails thethree installed-module cases on their
assertIsof the alias againstinstall()'s module. Binding the live entry whenever the memo is not live(
value = sys.modules.get(module_name, value)withlive = True, the outcomeof #197046) fails
test_import_alias_keeps_the_installed_module_when_neither_is_liveand
test_import_alias_of_an_empty_slot_is_the_memo_even_when_staleon theirassertIsof the alias againstinstall()'s module and against the memo:each accepted divergence is pinned as a divergence, so the commit that removes
it has to rewrite the pin. Recording the alias only when it is written (
if self.package is not None and write_value:) errorstest_import_alias_the_trace_left_alone_is_recorded_for_installwithKeyErroron the entry'simport_sources. Declaring thetorch_packagebranch's resolution not live (
live = False) errorstest_import_alias_accepts_a_stale_torch_package_modulewithInternalTorchDynamoError: AttributeError: module '<torch_package_0>.torch_test_package_import_alias_packaged' has no attribute 'SCALE', the guard build readingSCALEoff the planted module left in theslot. No other test of the thirteen failed under any of these. All four tests
call
_import_module.cache_clear()infinally, next to thesys.modulespop, so that a same-process re-execution does not resolve the first run's
object; #196896's message carries the measurement over all thirteen instances
(26 runs
OK, 10 of the 26 failing without the calls). The threeinstalled-module cases are covered by the write rule itself on a rerun (a
second run resolves run 1's object, which is not the live entry, so
install()'s binding stays), but clear it too, so no test leaves a dead objectin the memo. The whole file at this commit: 91 tests, OK (skipped=40, expected
failures=1).
They live in
test_package.pyrather thantest_misc.pybecause the writerthat empirically reaches the collision path is
CompilePackage.install, andthat file already owns the
DiskDynamoStoresave/load/install idiom and therest of the
import_sourcescoverage.Authored with the assistance of Claude Code.
Co-Authored-By: Claude Fable 5.1 [email protected]