Repository navigation
[dynamo] Memoize import_source per translator with an explicit dict - #197746
bobrenjc93 wants to merge 3 commits into
Conversation
[ghstack-poisoned]
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/197746
Note: Links to docs will display an error until the docs builds have been completed. ⏳ 2 Pending, 2 Unrelated FailuresAs of commit 2d8f708 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 14m 1s —— View job The code change is correct and behavior-neutral, but the 7-line comment that is the substance of this PR states a justification that is incomplete — the "slot already taken" branch it claims a memo hit rules out is in fact reachable within a single compile by an inlined frame whose own memo is empty. Full reviewPR Review: #197746SummaryReplaces Code Quality1. The comment says So for a module name the root translator already resolved, an inlined frame misses its memo, runs the body, and does reach the slot-taken check at line 2442. What makes that benign is the This is worth fixing rather than nitpicking, because the next commit hangs a real decision on it: if Suggested framing: the flag is safe to omit from the key because every path that reaches the slot-taken check for an already-resolved name satisfies 2.
3. Seven lines of nested subordinate clauses to justify a one-key dict, against PerformanceNot a blocker, but noting the tradeoff since it is the one measurable difference from Separately, and out of scope for a no-behavior-change refactor: because the namespace being memoized ( RecommendationNeeds Discussion The code is correct and I found no behavioral defect: the memo read (line 2424), the write placement past the assertion (line 2449), and the
Note: I reviewed statically and did not run the test plan — no built PyTorch is available in this checkout, so |
[ghstack-poisoned]
|
Re-reviewed against the current head 1. > Fixed, in the commit that owns the flag: the comment here no longer argues about 2. > Fixed in 3. > Fixed in |
|
Claude finished @bobrenjc93's task in 17m 40s —— View job The refactor is correct and I found no behavioral difference from Full reviewPR Review: #197746SummaryReplaces Code Quality1. The comment it replaces said the cache "only exists for the duration of this 2.
3. In the previous draft this clause was load-bearing: it supported the (incorrect) argument that a memo hit rules out the slot-taken branch. With that argument removed, the clause states a placement without saying why the placement matters. The consequence worth recording is that a refusal memoizes nothing — an PerformanceTwo differences from the decorator, neither a blocker, flagged so the choices are deliberate:
RecommendationApprove (after the comment fixes) I verified the mechanics and found no defect: the memo read (line 2420), the
Note: reviewed statically. No built PyTorch is available in this checkout, so the test plan ( |
[ghstack-poisoned]
|
@pytorchbot merge -i |
Stack from ghstack (oldest at bottom):
A refactor with no behaviour change, to make room for the commit directly
above, which gives
import_sourcea second argument:graph_break_ok, theflag that decides whether an alias slot already holding something else is a
graph break or a hard error.
cache_methodkeys on the whole argument tuple, so that flag would silentlybecome 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 valueholds by then, so the check is skipped, and the package record and
update_co_namesare idempotent -- but the memo exists to make resolutionhappen 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_namealone also lets the newargument be passed by keyword at the call sites, which the decorator forbade.
So the decorator gives way to an explicit
_import_source_memodict declaredin
InstructionTranslatorBase.__init__, read at the top of the method andwritten 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:
Behaviour-neutrality was also measured directly on this tree, on a frame doing
from torch.autograd.profiler import profilewithconfig.record_runtime_overheadon, so that
IMPORT_NAMEand codegen's pregraph marker both resolvetorch.autograd.profiler: the sequence of aliasesimport_sourceresolvesacross the compile is identical before and after this commit.
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