Repository navigation
[dynamo] Check IMPORT_NAME's result is a module before binding an alias - #197745
bobrenjc93 wants to merge 3 commits into
Conversation
[ghstack-poisoned]
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/197745
Note: Links to docs will display an error until the docs builds have been completed. ✅ You can merge normally! (2 Unrelated Failures)As of commit d239de4 with merge base 51d4378 ( 🟢 GREEN LIGHT - PR approved to be merged without human review:
reason: 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. |
|
Claude finished @bobrenjc93's task in 23m 35s —— View job Reviewed: the code change is a real improvement, but the PR description's central motivation (a second trace dying on Full reviewPR Review: #197745SummaryHoists Testing
Code Quality
RecommendationRequest Changes The code change is right and worth landing. What blocks it is the commit message, which attributes the fix to a failure mode |
[ghstack-poisoned]
|
Re-reviewed against the current head 1. > Fixed in 2. > Declined: the replay arm's values come from records this process wrote through 3. > Fixed in 4. > Fixed in |
|
Claude finished @bobrenjc93's task in 14m 41s —— View job Re-reviewed: the code change is correct and the test was reworked into the right shape, but the PR description still describes the old two-non-module test and a root cause that Full reviewPR Review: #197745SummaryHoists Testing
RecommendationRequest Changes The code change at I could not run |
[ghstack-poisoned]
|
Re-reviewed against the current head 1. > The PR description contradicts the test it cites. [...] > The stated root cause is unreachable. [...] Fixed: the commit message was corrected in the previous push (it names the 2. > Declined: the class-level 3. > Fixed in |
|
Claude finished @bobrenjc93's task in 44m 54s —— View job Re-reviewed at Full reviewPR Review: #197745SummaryHoists Code Quality
Prior roundsVerified rather than restated:
Both declines are fine. On the replay-arm check: RecommendationApprove The hoist is correct, and it also moves the check ahead of I could not run |
|
Starting merge as part of PR stack under #197746 |
…197746) A refactor with no behaviour change, to make room for the commit directly above, which gives `import_source` a second argument: `graph_break_ok`, the flag that decides whether an alias slot already holding something else is a graph break or a hard error. `cache_method` keys on the whole argument tuple, so that flag would silently become part of the key, and a module name that two callers on one translator both resolve would run the body once per distinct flag value instead of once. Nothing about the second run would be wrong -- `f_globals[alias] is value` holds by then, so the check is skipped, and the package record and `update_co_names` are idempotent -- but the memo exists to make resolution happen once, and the flag cannot change the answer a memo hit returns: it is read only when the slot is taken, which a hit rules out, because the memo is written only past that check. First-caller-wins is therefore the same source any caller would have got. Keying on `module_name` alone also lets the new argument be passed by keyword at the call sites, which the decorator forbade. So the decorator gives way to an explicit `_import_source_memo` dict declared in `InstructionTranslatorBase.__init__`, read at the top of the method and written just before the return. Its lifetime is the decorator's: it lives on the translator and dies with it, which is what made the cache safe in the first place -- the alias it hands back names a global in the globals of the compile that minted it. The comment above the method now says why the key is one argument rather than the signature. Test Plan: ``` python test/dynamo/test_package.py -k import_alias python test/dynamo/test_package.py ``` Behaviour-neutrality was also measured directly on this tree, on a frame doing `from torch.autograd.profiler import profile` with `config.record_runtime_overhead` on, so that `IMPORT_NAME` and codegen's pregraph marker both resolve `torch.autograd.profiler`: the sequence of aliases `import_source` resolves across the compile is identical before and after this commit. ``` lintrunner --take RUFF,PYFMT,CODESPELL,GB_REGISTRY torch/_dynamo/symbolic_convert.py torch/_dynamo/graph_break_registry.json test/dynamo/test_package.py ``` Authored with the assistance of Claude Code. Co-Authored-By: Claude Fable 5.1 <[email protected]> Pull Request resolved: #197746 Approved by: https://github.com/pytorchgreenlight ghstack dependencies: #197049, #196823, #197149, #197048, #196825, #196897, #197218, #196909, #196826, #197390, #197391, #197188, #197179, #197195, #197220, #197001, #197569, #197301, #196908, #197319, #197219, #197580, #197050, #197257, #197745
Stack from ghstack (oldest at bottom):
Problem.
IMPORT_NAMEchecked that__import__'s result was a module only afterimport_sourcehad already bound the result into the traced globals and a process-wide cache, so a non-modulesys.modulesentry was left behind in both, and a later trace with a real module in the entry crashed with anInternalTorchDynamoErrorinstead of compiling.Fix. Move the module-type check ahead of the
import_sourcecall, so a non-module never reaches the traced globals or the cache and the graph break happens first; the replay arm needs no check and pushes as-is.Summary.
IMPORT_NAMEtested__import__'s result withisinstance(value, (types.ModuleType, DummyModule))at push time, afterimport_sourcehad already minted__import_<mangled>for the name and written that result intoself.output.global_scope-- the root frame's real globals, a live module__dict__that nothing unwinds -- and into_import_module's process-wide cache.sys.modulesaccepts any object and__import__hands it back verbatim, so a non-module entry was bound under the alias and only then graph-broken on (Bad import result), and a later trace, after a real module had replaced the entry, passed the push-time check on the module__import__returned whileimport_sourceserved the cached non-module: the alias kept the non-module, the pushed variable held the module, and the guards built on the alias readVALUEoff anobject(), anInternalTorchDynamoErroron a trace that should have compiled. Hoisting the check ahead of theimport_sourcecall keeps the non-module out of the traced globals and out of the cache, since the call that filled it no longer runs, so a later trace with a real module in the entry binds the alias to that module and compiles. The hoisted test isisinstance(value, types.ModuleType)alone and sits on the import arm only, because a replayed value is aDummyModule, a plain dataclass that is not aModuleType, whichadd_local_modalready rejected non-modules for when the record was written, so the replay arm pushes with no type test and theDummyModuleimport goes away. Two behaviours change against the parent, both pinned or explained below:IMPORT_NAMEof a non-module entry no longer writes the alias before breaking, and withreplay_record_enabledon such an entry reaches the graph break instead ofadd_local_mod'sAssertionError.The bug: bind first, check second
self.output.global_scopeis the root frame's real globals, a live module__dict__that nothing unwinds, so the non-module stayed bound under the alias for good. The second failure follows from the cache:On the parent's second trace,
__import__returned the module, so the push-time check passed, butimport_sourceserved the cached non-module: the alias kept the non-module, the pushed variable held the module, and the guards built on the alias readVALUEoff anobject().The fix: check before
import_sourceHoisting the check ahead of the
import_sourcecall keeps the non-module out of the traced globals and out of the cache, since the call that filled it no longer runs: a later trace with a real module in the entry binds the alias to that module and compiles.Why
ModuleTypealone, on the import arm only. A replayed value is aDummyModule, a plain dataclass that is not aModuleType, so a check on that arm would refuse every replay -- and needs none,add_local_modhaving rejected non-modules when the record was written. That is why the replay arm now pushes with no type test at all, and why theDummyModulehalf of the old condition, which existed only for it, goes away with the import fromreplay_record.The other nine
import_sourcecallersThe check guards this one call site. The other nine
import_sourcecallers reach it with no module-type test, and a non-module entry under one of their names is still bound verbatim there, unchanged here -- thoughget_globals_source_and_valuealready brings its own test, since_registered_module_for_globalsreturns a name only for atypes.ModuleTypewhose__dict__is the frame's globals. Those names are torch's own and stdlib ones, or a class's__module__; a non-module under one of them is a broken process, not a trace to save.IMPORT_NAMEis the one caller resolving a name the traced bytecode chose, which is what makes its result worth checking.Behaviour changes against the parent
IMPORT_NAMEof a non-modulesys.modulesentry no longer writes the alias before breaking.test_import_alias_is_not_bound_to_a_non_module_importpins it by planting a non-module, requiring theBad import resultgraph break and an unbound alias, and then, with a real module in the entry, a compile that agrees with eager and an alias bound to that module.replay_record_enabledon (off by default), such an entry now reaches that graph break instead of theAssertionErroradd_local_modraised first when it ran ahead of the check. That one is untested, since the path is the parent'sadd_local_modand the config is off by default.The test
The cleanup clears
_import_module's cache as well assys.modulesand the alias, since the memo outlives thesys.modulesentry and a same-process rerun would otherwise resolve this run's module from it.The test lives in
test_package.pyrather thantest_misc.pybecause the alias binding this is about is the oneCompilePackagerecords andinstall()rebinds, and that file already owns the rest of theimport_sourcescoverage.Test Plan:
The new test is red on the parent's
torch/_dynamo/symbolic_convert.py, on thealias the parent wrote before breaking, and its second half fails there too,
with the guard-build
AttributeErroron the cachedobject(); restoring thepush-time
isinstancein place of the hoisted one reproduces the first failure. The whole file is green atthis commit.
The
Bad import resultregistry entry is unchanged: the call moves, itsstrings do not.
Authored with the assistance of Claude Code.
Co-Authored-By: Claude Fable 5.1 [email protected]
cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @kadeng @chauhang @amjames @jataylo @azahed98