Repository navigation
[dynamo] Add typed guard provenance: a closed, total classification of where guards are rooted - #195286
Closed
bobrenjc93 wants to merge 15 commits into
Closed
[dynamo] Add typed guard provenance: a closed, total classification of where guards are rooted#195286bobrenjc93 wants to merge 15 commits into
bobrenjc93 wants to merge 15 commits into
Conversation
[ghstack-poisoned]
This was referenced Aug 29, 2026
[ghstack-poisoned]
[ghstack-poisoned]
bobrenjc93
added a commit
that referenced
this pull request
Aug 30, 2026
…f where guards are rooted Guard-serialization consumers (torch._dynamo.package, aot_compile, torch.compiler.precompile) all need to answer the same question: is a guard derived from the traced frame's inputs (dispatch-relevant, must keep) or from the surrounding Python environment (an environment-invariant contract may drop it)? Each consumer answers it today by re-deriving provenance from the guard -- aot_compile keys off is_global, precompile originally parsed the rendered guard name. Rendered names embed input roots inside call expressions (___tuple_iterator_getitem(L['it'], 0), ___from_numpy(L['x']), type(L['x'])), so string classification silently misfiles input guards as droppable environment guards, and a dropped input guard serves a stale specialization with no error. This change makes provenance a first-class, typed property computed structurally from the Source chain: a closed GuardProvenance enum (INPUT, GLOBAL, AMBIENT, SYNTHETIC), declared by every ROOT Source class and delegated through ChainedSource, exposed as Guard.provenance and on GuardFilterEntry so guard_filter_fn policies can key off it. It is deliberately fail-closed in both directions: an unclassified root raises at classification time instead of defaulting, and a totality test walks every Source subclass so a new source cannot land unclassified. AMBIENT separates interpreter/process-wide state (grad mode, shape env, torch-function stack, streams, backward state) from module globals, a distinction is_global conflates; is_global keeps its existing narrow meaning for backward compatibility. Two classification decisions worth calling out. GlobalWeakRefSource is INPUT, not GLOBAL: dynamo-installed weakref proxies (G['__optimizer_...']) stand in for the identity/liveness of traced runtime objects -- their WEAKREF_ALIVE guards distinguish e.g. optimizer instances, so an environment-drop policy must never see them as droppable (an end-to-end test pins this on a compiled optimizer step). ConstantSource is SYNTHETIC on the guard axis only (it cannot be guarded on); its reconstruction bytecode still loads a dynamo-installed global, which the Note spells out so non-guard consumers do not over-infer. Review order: the Note [Guard provenance] and enum in torch/_guards.py, the Source/ChainedSource/Guard plumbing there, then the per-root declarations in torch/_dynamo/source.py, the GuardFilterEntry field, and the tests. Authored with an AI assistant. Test Plan: ``` python test/dynamo/test_sources.py # 8 tests, OK (5 new) python test/dynamo/test_guard_serialization.py # 60 tests, OK (1 skipped) python test/dynamo/test_guard_manager.py # OK python test/dynamo/test_aot_compile.py # 42 tests, OK lintrunner <touched files> # ok ``` Co-Authored-By: Claude Fable 5 <[email protected]> ghstack-source-id: 1b353b6 Pull-Request: #195286
Contributor
Author
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
This was referenced Sep 2, 2026
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
This was referenced Sep 3, 2026
Closed
This was referenced Sep 3, 2026
Contributor
Author
|
@pytorchbot label "release notes: dynamo" |
Contributor
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Stack from ghstack (oldest at bottom):
Guard-serialization consumers (torch._dynamo.package, torch._dynamo.aot_compile,
torch.compiler.precompile) all need the same classification: is a guard derived
from the traced frame's inputs (dispatch-relevant, must keep), or from the
surrounding Python environment (an environment-invariant contract may drop it)?
Today that question is answered ad hoc -- aot_compile keys off is_global, which
is true only for a chain rooted at exactly a GlobalSource and so misses imported
modules (ImportSource) and says nothing about interpreter/process-wide state. This
commit is additive infrastructure: it introduces a typed provenance so the
consumers added later in this stack classify guards structurally rather than
reinventing it.
Provenance is computed once from the typed Source chain, never by parsing the
rendered guard name. That choice is deliberate: rendered names embed input roots
inside call expressions (___tuple_iterator_getitem(L['it'], 0),
___from_numpy(L['x']), type(L['x']), list(dict.keys(L['d']))[0], ...), so a
name-prefix test would misfile those input guards as droppable environment
guards, and a dropped input guard serves a stale specialization with no error.
The classification is a closed GuardProvenance enum (INPUT, GLOBAL, AMBIENT,
SYNTHETIC), declared by every ROOT Source class and delegated through
ChainedSource, exposed as Guard.provenance and on GuardFilterEntry so
guard_filter_fn policies can key off it; GuardProvenance is re-exported from
torch.compiler for that purpose. It is fail-closed in both directions: an
unclassified root raises at classification time instead of defaulting, and a
totality test walks every Source subclass so a new source cannot land
unclassified. GLOBAL covers both module globals and imports (the latter is what
is_global misses); AMBIENT names interpreter/process-wide state (grad mode,
torch-function stack, streams), which is_global simply does not classify;
is_global keeps its existing narrow meaning for backward compatibility.
Three classification decisions worth calling out:
(G['_optimizer...']) stand in for the identity/liveness of traced runtime
objects, so their WEAKREF_ALIVE guards are dispatch-relevant even though the
lookup goes through globals (an end-to-end test pins this on a compiled
optimizer step).
guard rooted there encodes the symbolic-shape constraints derived from the
inputs, so a drop policy must never treat it as environment.
axis only (it cannot be guarded on); the latter's reconstruction bytecode
still loads a dynamo-installed global, which the Note spells out so non-guard
consumers do not over-infer.
Review order: the Note [Guard provenance] and enum in torch/_guards.py, the
Source/ChainedSource/Guard plumbing there, then the per-root declarations in
torch/_dynamo/source.py, the GuardFilterEntry field, and the tests.
Authored with an AI assistant.
Test Plan:
Compatibility notes. GuardFilterEntry (a private dataclass in
torch._dynamo.types, handed to guard_filter_fn callbacks) gains a required
trailing
provenancefield: readers are unaffected, but out-of-tree code thatconstructs entries itself must now pass it. Source.provenance fails closed
(NotImplementedError) for a root Source that does not declare _provenance; it
is evaluated whenever a guard filter is active and unconditionally when guards
are serialized for a package, so an out-of-tree root Source must declare its
provenance before it can be used with caching_precompile or guard_filter_fn.
Both are deliberate: a default would let a new root silently classify as
droppable.
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