Skip to content

[dynamo] Fix invoke_subgraph reuse of reads served by side effects - #192009

Closed
anijain2305 wants to merge 16 commits into
gh/anijain2305/1291/basefrom
gh/anijain2305/1291/head
Closed

anijain2305 wants to merge 16 commits into
gh/anijain2305/1291/basefrom
gh/anijain2305/1291/head

Conversation

@anijain2305

@anijain2305 anijain2305 commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

A nested compile region that read a value mutated into place before it ran
aborted compilation with an internal assertion:

AssertionError: Freevar has no source: node.op=call_method node.name=sin --
this likely means a function argument was not included in the proxy matching

Three more paths that under-record what a region read, same shape as the
rebound global in the parent commit. In each one the read and the write end up
naming the value differently, so the set intersection in has_mutated_vars comes
up empty and the region looks reusable when it is not.

Attribute reads: with holder.t = x.sin() ahead of the region, the region's
read of holder.t is served by side_effects.load_attr, which bypasses
VariableBuilder, so nothing lands in traced_sources and the mutation check has
nothing to intersect. The hook goes in load_attr rather than the ~20 call sites
that consult has_pending_mutation_of_attr first. load_cell and load_global now
share it; load_cell keeps its own call because an unmutated cell returns
pre_existing_contents without reaching load_attr.

The VariableTracker cache: LazyVariableTracker.create returns a VT cached by
source, so any source already materialized before the region ran contributes
nothing to that region's traced_sources no matter how it is read. That silently
weakens both the mutation check and guard collection, which is documented as
catching guards installed before the subgraph trace began. This is why storing
an intermediate into an already-materialized list still crashed after the
load_attr fix. Cache hits now record the source.

Closure cells: load_cell recorded AttrSource(cell_source, "cell_contents"),
but a write records the source the contents themselves carry,
LocalSource(..., is_derefed_cell_contents=True). Two spellings of one location,
so the intersection never matched. load_cell now records the contents' own
source, falling back to the synthesized one only when the cell has no contents
to ask.

That last one is not only a lost optimisation. When the cell is rebound to
another sourceful tensor between two calls, nothing downstream objects: the
second call reuses the first call's source and silently returns the wrong
answer, 20 where eager gives 110. It reproduces on the base of this stack, so
it predates this work and is the reason the fix is worth landing on its own
merits rather than as crash cleanup.

Test Plan:

Four tests, one per read path, all failing on the parent commit -- the first
three with the assertion above, the fourth with a wrong result.

PYTHONPATH=$PWD/test python test/higher_order_ops/test_invoke_subgraph.py TestInvokeSubgraphReuse

lazy.py and load_cell sit on every sourced read and every closure read, so the
sweep was widened past the usual suites. Each was run twice, once with these
changes and once with them reverted, and the results are identical: test_misc
1 failure and 4 errors, test_functions clean, test_repros 11 errors,
test_higher_order_ops 3 errors, all pre-existing and unrelated.

PYTHONPATH=$PWD/test python test/dynamo/test_misc.py
PYTHONPATH=$PWD/test python test/dynamo/test_functions.py
PYTHONPATH=$PWD/test python test/dynamo/test_repros.py
PYTHONPATH=$PWD/test python test/dynamo/test_higher_order_ops.py

An earlier version of this commit also made build_subgraph_input_mapping
decline instead of raising on a lifted freevar with no source. With the three
recording paths fixed, no shape I could construct reaches that assertion, so
the change was dropped: an internal assertion that fires loudly is a better
tripwire for the next recording gap than a silent loss of reuse, which is
exactly the failure mode nested_compile_region exists to avoid.

This commit was authored with the assistance of Claude Code.

cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @kadeng @chauhang @amjames @jataylo @azahed98

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Aug 4, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/192009

Note: Links to docs will display an error until the docs builds have been completed.

❌ 1 New Failure, 4 Cancelled Jobs, 1 Unrelated Failure

As of commit 7b5fea8 with merge base e289091 (image):

NEW FAILURE - The following job has failed:

  • Lint / lintrunner-pyrefly-partial / lint (gh)
    >>> Lint for torch/fx/experimental/recording.py:

    AI verdict: AI verdict: related

    The commit moved shape_env = args[0] before the isinstance(args[0], ShapeEnv) check, causing Pyrefly to infer shape_env as type object at line 346 (in the except block), triggering missing-attribute lint errors for should_record_events and is_recording.

    Full reasoning on HUD →

CANCELLED JOBS - The following jobs were cancelled. Please retry:

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.

@pytorch-bot

pytorch-bot Bot commented Aug 4, 2026

Copy link
Copy Markdown

This PR needs a release notes: label

If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "topic: not user facing"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

[ghstack-poisoned]
anijain2305 added a commit that referenced this pull request Aug 4, 2026
…rmediate

A nested compile region that read a value mutated into place before it ran
aborted compilation with an internal assertion:

  AssertionError: Freevar has no source: node.op=call_method node.name=sin --
  this likely means a function argument was not included in the proxy matching

Two more paths that under-record what a region read, same shape as the rebound
global in the parent commit, plus a bad failure mode when the recording is
incomplete.

Attribute reads: with `holder.t = x.sin()` ahead of the region, the region's
read of holder.t is served by side_effects.load_attr, which bypasses
VariableBuilder, so nothing lands in traced_sources and the mutation check has
nothing to intersect. The hook goes in load_attr rather than the ~20 call sites
that consult has_pending_mutation_of_attr first. load_cell and load_global now
share it; load_cell keeps its own call because an unmutated cell returns
pre_existing_contents without reaching load_attr.

The VariableTracker cache: LazyVariableTracker.create returns a VT cached by
source, so any source already materialized before the region ran contributes
nothing to that region's traced_sources no matter how it is read. That silently
weakens both the mutation check and guard collection, which is documented as
catching guards installed before the subgraph trace began. This is why storing
an intermediate into an already-materialized list still crashed after the
load_attr fix. Cache hits now record the source.

Finally, build_subgraph_input_mapping logs and returns None instead of raising.
A captured graph intermediate cannot be re-derived at another call site, so
"not reusable" is the correct answer and raising was wrong even when reached.
Three separate under-recording paths turned up while investigating this one, so
a fourth should cost reuse rather than the whole compile.

Test Plan:

Two tests, one per read path, both failing on the parent commit with the
assertion above.

```
python test/higher_order_ops/test_invoke_subgraph.py TestInvokeSubgraphReuse.test_subgraph_reuse_attr_mutated_to_intermediate TestInvokeSubgraphReuse.test_subgraph_reuse_container_item_mutated_to_intermediate
```

The backstop has no automated test. Reaching it now requires defeating both
recorders, and a test that patches SubgraphTracer internals would pin plumbing
rather than behaviour. Verified by hand that with traced_sources blinded the
region logs "not eligible -- freevar has no source" and still returns correct
results, where it previously raised.

lazy.py sits on every sourced read, so the sweep was widened past the usual
suites. Each was diffed against a baseline with the fixes stashed and is
identical (25, 5, 0, 11 and 3 pre-existing failures respectively, from an
unrelated CPU Inductor cpp codegen breakage on this aarch64 host).

```
PYTHONPATH=$PWD/test python test/higher_order_ops/test_invoke_subgraph.py
PYTHONPATH=$PWD/test python test/dynamo/test_misc.py
PYTHONPATH=$PWD/test python test/dynamo/test_functions.py
PYTHONPATH=$PWD/test python test/dynamo/test_repros.py
PYTHONPATH=$PWD/test python test/dynamo/test_higher_order_ops.py
```

This commit was authored with the assistance of Claude Code.


ghstack-source-id: e298800
Pull-Request: #192009
[ghstack-poisoned]
anijain2305 added a commit that referenced this pull request Aug 5, 2026
…rmediate

A nested compile region that read a value mutated into place before it ran
aborted compilation with an internal assertion:

  AssertionError: Freevar has no source: node.op=call_method node.name=sin --
  this likely means a function argument was not included in the proxy matching

Two more paths that under-record what a region read, same shape as the rebound
global in the parent commit, plus a bad failure mode when the recording is
incomplete.

Attribute reads: with `holder.t = x.sin()` ahead of the region, the region's
read of holder.t is served by side_effects.load_attr, which bypasses
VariableBuilder, so nothing lands in traced_sources and the mutation check has
nothing to intersect. The hook goes in load_attr rather than the ~20 call sites
that consult has_pending_mutation_of_attr first. load_cell and load_global now
share it; load_cell keeps its own call because an unmutated cell returns
pre_existing_contents without reaching load_attr.

The VariableTracker cache: LazyVariableTracker.create returns a VT cached by
source, so any source already materialized before the region ran contributes
nothing to that region's traced_sources no matter how it is read. That silently
weakens both the mutation check and guard collection, which is documented as
catching guards installed before the subgraph trace began. This is why storing
an intermediate into an already-materialized list still crashed after the
load_attr fix. Cache hits now record the source.

Finally, build_subgraph_input_mapping logs and returns None instead of raising.
A captured graph intermediate cannot be re-derived at another call site, so
"not reusable" is the correct answer and raising was wrong even when reached.
Three separate under-recording paths turned up while investigating this one, so
a fourth should cost reuse rather than the whole compile.

Test Plan:

Two tests, one per read path, both failing on the parent commit with the
assertion above.

```
python test/higher_order_ops/test_invoke_subgraph.py TestInvokeSubgraphReuse.test_subgraph_reuse_attr_mutated_to_intermediate TestInvokeSubgraphReuse.test_subgraph_reuse_container_item_mutated_to_intermediate
```

The backstop has no automated test. Reaching it now requires defeating both
recorders, and a test that patches SubgraphTracer internals would pin plumbing
rather than behaviour. Verified by hand that with traced_sources blinded the
region logs "not eligible -- freevar has no source" and still returns correct
results, where it previously raised.

lazy.py sits on every sourced read, so the sweep was widened past the usual
suites. Each was diffed against a baseline with the fixes stashed and is
identical (25, 5, 0, 11 and 3 pre-existing failures respectively, from an
unrelated CPU Inductor cpp codegen breakage on this aarch64 host).

```
PYTHONPATH=$PWD/test python test/higher_order_ops/test_invoke_subgraph.py
PYTHONPATH=$PWD/test python test/dynamo/test_misc.py
PYTHONPATH=$PWD/test python test/dynamo/test_functions.py
PYTHONPATH=$PWD/test python test/dynamo/test_repros.py
PYTHONPATH=$PWD/test python test/dynamo/test_higher_order_ops.py
```

This commit was authored with the assistance of Claude Code.


ghstack-source-id: 9ba456e
Pull-Request: #192009
[ghstack-poisoned]
anijain2305 added a commit that referenced this pull request Aug 5, 2026
…rmediate

A nested compile region that read a value mutated into place before it ran
aborted compilation with an internal assertion:

  AssertionError: Freevar has no source: node.op=call_method node.name=sin --
  this likely means a function argument was not included in the proxy matching

Two more paths that under-record what a region read, same shape as the rebound
global in the parent commit, plus a bad failure mode when the recording is
incomplete.

Attribute reads: with `holder.t = x.sin()` ahead of the region, the region's
read of holder.t is served by side_effects.load_attr, which bypasses
VariableBuilder, so nothing lands in traced_sources and the mutation check has
nothing to intersect. The hook goes in load_attr rather than the ~20 call sites
that consult has_pending_mutation_of_attr first. load_cell and load_global now
share it; load_cell keeps its own call because an unmutated cell returns
pre_existing_contents without reaching load_attr.

The VariableTracker cache: LazyVariableTracker.create returns a VT cached by
source, so any source already materialized before the region ran contributes
nothing to that region's traced_sources no matter how it is read. That silently
weakens both the mutation check and guard collection, which is documented as
catching guards installed before the subgraph trace began. This is why storing
an intermediate into an already-materialized list still crashed after the
load_attr fix. Cache hits now record the source.

Finally, build_subgraph_input_mapping logs and returns None instead of raising.
A captured graph intermediate cannot be re-derived at another call site, so
"not reusable" is the correct answer and raising was wrong even when reached.
Three separate under-recording paths turned up while investigating this one, so
a fourth should cost reuse rather than the whole compile.

Test Plan:

Two tests, one per read path, both failing on the parent commit with the
assertion above.

```
python test/higher_order_ops/test_invoke_subgraph.py TestInvokeSubgraphReuse.test_subgraph_reuse_attr_mutated_to_intermediate TestInvokeSubgraphReuse.test_subgraph_reuse_container_item_mutated_to_intermediate
```

The backstop has no automated test. Reaching it now requires defeating both
recorders, and a test that patches SubgraphTracer internals would pin plumbing
rather than behaviour. Verified by hand that with traced_sources blinded the
region logs "not eligible -- freevar has no source" and still returns correct
results, where it previously raised.

lazy.py sits on every sourced read, so the sweep was widened past the usual
suites. Each was diffed against a baseline with the fixes stashed and is
identical (25, 5, 0, 11 and 3 pre-existing failures respectively, from an
unrelated CPU Inductor cpp codegen breakage on this aarch64 host).

```
PYTHONPATH=$PWD/test python test/higher_order_ops/test_invoke_subgraph.py
PYTHONPATH=$PWD/test python test/dynamo/test_misc.py
PYTHONPATH=$PWD/test python test/dynamo/test_functions.py
PYTHONPATH=$PWD/test python test/dynamo/test_repros.py
PYTHONPATH=$PWD/test python test/dynamo/test_higher_order_ops.py
```

This commit was authored with the assistance of Claude Code.

ghstack-source-id: 0f26631
Pull-Request: #192009
anijain2305 added a commit that referenced this pull request Aug 5, 2026
…rmediate

A nested compile region that read a value mutated into place before it ran
aborted compilation with an internal assertion:

  AssertionError: Freevar has no source: node.op=call_method node.name=sin --
  this likely means a function argument was not included in the proxy matching

Two more paths that under-record what a region read, same shape as the rebound
global in the parent commit, plus a bad failure mode when the recording is
incomplete.

Attribute reads: with `holder.t = x.sin()` ahead of the region, the region's
read of holder.t is served by side_effects.load_attr, which bypasses
VariableBuilder, so nothing lands in traced_sources and the mutation check has
nothing to intersect. The hook goes in load_attr rather than the ~20 call sites
that consult has_pending_mutation_of_attr first. load_cell and load_global now
share it; load_cell keeps its own call because an unmutated cell returns
pre_existing_contents without reaching load_attr.

The VariableTracker cache: LazyVariableTracker.create returns a VT cached by
source, so any source already materialized before the region ran contributes
nothing to that region's traced_sources no matter how it is read. That silently
weakens both the mutation check and guard collection, which is documented as
catching guards installed before the subgraph trace began. This is why storing
an intermediate into an already-materialized list still crashed after the
load_attr fix. Cache hits now record the source.

Finally, build_subgraph_input_mapping logs and returns None instead of raising.
A captured graph intermediate cannot be re-derived at another call site, so
"not reusable" is the correct answer and raising was wrong even when reached.
Three separate under-recording paths turned up while investigating this one, so
a fourth should cost reuse rather than the whole compile.

Test Plan:

Two tests, one per read path, both failing on the parent commit with the
assertion above.

```
python test/higher_order_ops/test_invoke_subgraph.py TestInvokeSubgraphReuse.test_subgraph_reuse_attr_mutated_to_intermediate TestInvokeSubgraphReuse.test_subgraph_reuse_container_item_mutated_to_intermediate
```

The backstop has no automated test. Reaching it now requires defeating both
recorders, and a test that patches SubgraphTracer internals would pin plumbing
rather than behaviour. Verified by hand that with traced_sources blinded the
region logs "not eligible -- freevar has no source" and still returns correct
results, where it previously raised.

lazy.py sits on every sourced read, so the sweep was widened past the usual
suites. Each was diffed against a baseline with the fixes stashed and is
identical (25, 5, 0, 11 and 3 pre-existing failures respectively, from an
unrelated CPU Inductor cpp codegen breakage on this aarch64 host).

```
PYTHONPATH=$PWD/test python test/higher_order_ops/test_invoke_subgraph.py
PYTHONPATH=$PWD/test python test/dynamo/test_misc.py
PYTHONPATH=$PWD/test python test/dynamo/test_functions.py
PYTHONPATH=$PWD/test python test/dynamo/test_repros.py
PYTHONPATH=$PWD/test python test/dynamo/test_higher_order_ops.py
```

This commit was authored with the assistance of Claude Code.

ghstack-source-id: 0f26631
Pull-Request: #192009
@anijain2305 anijain2305 changed the title [dynamo] Fix invoke_subgraph reuse crash on regions capturing an intermediate [dynamo][invoke_subgraph] Track sources for load_attr and LazyVT Aug 5, 2026
[ghstack-poisoned]
anijain2305 added a commit that referenced this pull request Aug 5, 2026
…rmediate

A nested compile region that read a value mutated into place before it ran
aborted compilation with an internal assertion:

  AssertionError: Freevar has no source: node.op=call_method node.name=sin --
  this likely means a function argument was not included in the proxy matching

Two more paths that under-record what a region read, same shape as the rebound
global in the parent commit, plus a bad failure mode when the recording is
incomplete.

Attribute reads: with `holder.t = x.sin()` ahead of the region, the region's
read of holder.t is served by side_effects.load_attr, which bypasses
VariableBuilder, so nothing lands in traced_sources and the mutation check has
nothing to intersect. The hook goes in load_attr rather than the ~20 call sites
that consult has_pending_mutation_of_attr first. load_cell and load_global now
share it; load_cell keeps its own call because an unmutated cell returns
pre_existing_contents without reaching load_attr.

The VariableTracker cache: LazyVariableTracker.create returns a VT cached by
source, so any source already materialized before the region ran contributes
nothing to that region's traced_sources no matter how it is read. That silently
weakens both the mutation check and guard collection, which is documented as
catching guards installed before the subgraph trace began. This is why storing
an intermediate into an already-materialized list still crashed after the
load_attr fix. Cache hits now record the source.

Finally, a region whose lifted freevars cannot be re-derived at another call
site is rejected by is_reuse_eligible instead of asserting deep inside
build_subgraph_input_mapping. A captured graph intermediate has no source to
resolve, so "not reusable" is the correct answer and raising was wrong even
when reached. This is not a hypothetical backstop: reads through a closure cell
are a fourth under-recording path, still unfixed, and they reach it today.

The check lives in is_reuse_eligible, alongside every other rejection reason,
rather than being threaded back as an Optional return; build_subgraph_input_mapping
asserts the invariant it now relies on, matching build_reuse_condition. The two
walks share user_arg_proxy_indices and lifted_freevar_info so they cannot drift.
Measured at 0.002% of an 80-layer compile, and paid once per saved region rather
than per call site, since cache hits go through is_reusable instead.

One behaviour change beyond the crash: with an explicit reuse_hash_fn, an
ineligible region raises rather than silently declining to save an entry. A
reuse_hash_fn that quietly does nothing is worse than one that says so.

Test Plan:

Three tests, one per read path, all failing on the parent commit with the
assertion above.

```
PYTHONPATH=$PWD/test python test/higher_order_ops/test_invoke_subgraph.py TestInvokeSubgraphReuse.test_subgraph_reuse_attr_mutated_to_intermediate TestInvokeSubgraphReuse.test_subgraph_reuse_container_item_mutated_to_intermediate TestInvokeSubgraphReuse.test_subgraph_reuse_closure_item_mutated_to_intermediate
```

The first two are fixed by the recording changes and stop at the mutation
check. The third reads through a closure cell, which nothing records, so it
exercises the lifted-freevar rejection itself; with that check reverted it
still raises.

lazy.py sits on every sourced read, so the sweep was widened past the usual
suites. Each was diffed against a baseline with the fixes stashed and is
identical (25, 5, 0, 11 and 3 pre-existing failures respectively, from an
unrelated CPU Inductor cpp codegen breakage on this aarch64 host).

```
PYTHONPATH=$PWD/test python test/higher_order_ops/test_invoke_subgraph.py
PYTHONPATH=$PWD/test python test/dynamo/test_misc.py
PYTHONPATH=$PWD/test python test/dynamo/test_functions.py
PYTHONPATH=$PWD/test python test/dynamo/test_repros.py
PYTHONPATH=$PWD/test python test/dynamo/test_higher_order_ops.py
```

This commit was authored with the assistance of Claude Code.

ghstack-source-id: 10e6365
Pull-Request: #192009
[ghstack-poisoned]
anijain2305 added a commit that referenced this pull request Aug 6, 2026
A nested compile region that read a value mutated into place before it ran
aborted compilation with an internal assertion:

  AssertionError: Freevar has no source: node.op=call_method node.name=sin --
  this likely means a function argument was not included in the proxy matching

Three more paths that under-record what a region read, same shape as the
rebound global in the parent commit. In each one the read and the write end up
naming the value differently, so the set intersection in has_mutated_vars comes
up empty and the region looks reusable when it is not.

Attribute reads: with `holder.t = x.sin()` ahead of the region, the region's
read of holder.t is served by side_effects.load_attr, which bypasses
VariableBuilder, so nothing lands in traced_sources and the mutation check has
nothing to intersect. The hook goes in load_attr rather than the ~20 call sites
that consult has_pending_mutation_of_attr first. load_cell and load_global now
share it; load_cell keeps its own call because an unmutated cell returns
pre_existing_contents without reaching load_attr.

The VariableTracker cache: LazyVariableTracker.create returns a VT cached by
source, so any source already materialized before the region ran contributes
nothing to that region's traced_sources no matter how it is read. That silently
weakens both the mutation check and guard collection, which is documented as
catching guards installed before the subgraph trace began. This is why storing
an intermediate into an already-materialized list still crashed after the
load_attr fix. Cache hits now record the source.

Closure cells: load_cell recorded AttrSource(cell_source, "cell_contents"),
but a write records the source the contents themselves carry,
LocalSource(..., is_derefed_cell_contents=True). Two spellings of one location,
so the intersection never matched. load_cell now records the contents' own
source, falling back to the synthesized one only when the cell has no contents
to ask.

That last one is not only a lost optimisation. When the cell is rebound to
another sourceful tensor between two calls, nothing downstream objects: the
second call reuses the first call's source and silently returns the wrong
answer, 20 where eager gives 110. It reproduces on the base of this stack, so
it predates this work and is the reason the fix is worth landing on its own
merits rather than as crash cleanup.

Test Plan:

Four tests, one per read path, all failing on the parent commit -- the first
three with the assertion above, the fourth with a wrong result.

```
PYTHONPATH=$PWD/test python test/higher_order_ops/test_invoke_subgraph.py TestInvokeSubgraphReuse
```

lazy.py and load_cell sit on every sourced read and every closure read, so the
sweep was widened past the usual suites. Each was run twice, once with these
changes and once with them reverted, and the results are identical: test_misc
1 failure and 4 errors, test_functions clean, test_repros 11 errors,
test_higher_order_ops 3 errors, all pre-existing and unrelated.

```
PYTHONPATH=$PWD/test python test/dynamo/test_misc.py
PYTHONPATH=$PWD/test python test/dynamo/test_functions.py
PYTHONPATH=$PWD/test python test/dynamo/test_repros.py
PYTHONPATH=$PWD/test python test/dynamo/test_higher_order_ops.py
```

An earlier version of this commit also made build_subgraph_input_mapping
decline instead of raising on a lifted freevar with no source. With the three
recording paths fixed, no shape I could construct reaches that assertion, so
the change was dropped: an internal assertion that fires loudly is a better
tripwire for the next recording gap than a silent loss of reuse, which is
exactly the failure mode nested_compile_region exists to avoid.

This commit was authored with the assistance of Claude Code.

ghstack-source-id: adde108
Pull-Request: #192009
[ghstack-poisoned]
anijain2305 added a commit that referenced this pull request Aug 6, 2026
A nested compile region that read a value mutated into place before it ran
aborted compilation with an internal assertion:

  AssertionError: Freevar has no source: node.op=call_method node.name=sin --
  this likely means a function argument was not included in the proxy matching

Three more paths that under-record what a region read, same shape as the
rebound global in the parent commit. In each one the read and the write end up
naming the value differently, so the set intersection in has_mutated_vars comes
up empty and the region looks reusable when it is not.

Attribute reads: with `holder.t = x.sin()` ahead of the region, the region's
read of holder.t is served by side_effects.load_attr, which bypasses
VariableBuilder, so nothing lands in traced_sources and the mutation check has
nothing to intersect. The hook goes in load_attr rather than the ~20 call sites
that consult has_pending_mutation_of_attr first. load_cell and load_global now
share it; load_cell keeps its own call because an unmutated cell returns
pre_existing_contents without reaching load_attr.

The VariableTracker cache: LazyVariableTracker.create returns a VT cached by
source, so any source already materialized before the region ran contributes
nothing to that region's traced_sources no matter how it is read. That silently
weakens both the mutation check and guard collection, which is documented as
catching guards installed before the subgraph trace began. This is why storing
an intermediate into an already-materialized list still crashed after the
load_attr fix. Cache hits now record the source.

Closure cells: load_cell recorded AttrSource(cell_source, "cell_contents"),
but a write records the source the contents themselves carry,
LocalSource(..., is_derefed_cell_contents=True). Two spellings of one location,
so the intersection never matched. load_cell now records the contents' own
source, falling back to the synthesized one only when the cell has no contents
to ask.

That last one is not only a lost optimisation. When the cell is rebound to
another sourceful tensor between two calls, nothing downstream objects: the
second call reuses the first call's source and silently returns the wrong
answer, 20 where eager gives 110. It reproduces on the base of this stack, so
it predates this work and is the reason the fix is worth landing on its own
merits rather than as crash cleanup.

Test Plan:

Four tests, one per read path, all failing on the parent commit -- the first
three with the assertion above, the fourth with a wrong result.

```
PYTHONPATH=$PWD/test python test/higher_order_ops/test_invoke_subgraph.py TestInvokeSubgraphReuse
```

lazy.py and load_cell sit on every sourced read and every closure read, so the
sweep was widened past the usual suites. Each was run twice, once with these
changes and once with them reverted, and the results are identical: test_misc
1 failure and 4 errors, test_functions clean, test_repros 11 errors,
test_higher_order_ops 3 errors, all pre-existing and unrelated.

```
PYTHONPATH=$PWD/test python test/dynamo/test_misc.py
PYTHONPATH=$PWD/test python test/dynamo/test_functions.py
PYTHONPATH=$PWD/test python test/dynamo/test_repros.py
PYTHONPATH=$PWD/test python test/dynamo/test_higher_order_ops.py
```

An earlier version of this commit also made build_subgraph_input_mapping
decline instead of raising on a lifted freevar with no source. With the three
recording paths fixed, no shape I could construct reaches that assertion, so
the change was dropped: an internal assertion that fires loudly is a better
tripwire for the next recording gap than a silent loss of reuse, which is
exactly the failure mode nested_compile_region exists to avoid.

This commit was authored with the assistance of Claude Code.

ghstack-source-id: 975c6e4
Pull-Request: #192009
anijain2305 added a commit that referenced this pull request Aug 6, 2026
A nested compile region that read a value mutated into place before it ran
aborted compilation with an internal assertion:

  AssertionError: Freevar has no source: node.op=call_method node.name=sin --
  this likely means a function argument was not included in the proxy matching

Three more paths that under-record what a region read, same shape as the
rebound global in the parent commit. In each one the read and the write end up
naming the value differently, so the set intersection in has_mutated_vars comes
up empty and the region looks reusable when it is not.

Attribute reads: with `holder.t = x.sin()` ahead of the region, the region's
read of holder.t is served by side_effects.load_attr, which bypasses
VariableBuilder, so nothing lands in traced_sources and the mutation check has
nothing to intersect. The hook goes in load_attr rather than the ~20 call sites
that consult has_pending_mutation_of_attr first. load_cell and load_global now
share it; load_cell keeps its own call because an unmutated cell returns
pre_existing_contents without reaching load_attr.

The VariableTracker cache: LazyVariableTracker.create returns a VT cached by
source, so any source already materialized before the region ran contributes
nothing to that region's traced_sources no matter how it is read. That silently
weakens both the mutation check and guard collection, which is documented as
catching guards installed before the subgraph trace began. This is why storing
an intermediate into an already-materialized list still crashed after the
load_attr fix. Cache hits now record the source.

Closure cells: load_cell recorded AttrSource(cell_source, "cell_contents"),
but a write records the source the contents themselves carry,
LocalSource(..., is_derefed_cell_contents=True). Two spellings of one location,
so the intersection never matched. load_cell now records the contents' own
source, falling back to the synthesized one only when the cell has no contents
to ask.

That last one is not only a lost optimisation. When the cell is rebound to
another sourceful tensor between two calls, nothing downstream objects: the
second call reuses the first call's source and silently returns the wrong
answer, 20 where eager gives 110. It reproduces on the base of this stack, so
it predates this work and is the reason the fix is worth landing on its own
merits rather than as crash cleanup.

Test Plan:

Four tests, one per read path, all failing on the parent commit -- the first
three with the assertion above, the fourth with a wrong result.

```
PYTHONPATH=$PWD/test python test/higher_order_ops/test_invoke_subgraph.py TestInvokeSubgraphReuse
```

lazy.py and load_cell sit on every sourced read and every closure read, so the
sweep was widened past the usual suites. Each was run twice, once with these
changes and once with them reverted, and the results are identical: test_misc
1 failure and 4 errors, test_functions clean, test_repros 11 errors,
test_higher_order_ops 3 errors, all pre-existing and unrelated.

```
PYTHONPATH=$PWD/test python test/dynamo/test_misc.py
PYTHONPATH=$PWD/test python test/dynamo/test_functions.py
PYTHONPATH=$PWD/test python test/dynamo/test_repros.py
PYTHONPATH=$PWD/test python test/dynamo/test_higher_order_ops.py
```

An earlier version of this commit also made build_subgraph_input_mapping
decline instead of raising on a lifted freevar with no source. With the three
recording paths fixed, no shape I could construct reaches that assertion, so
the change was dropped: an internal assertion that fires loudly is a better
tripwire for the next recording gap than a silent loss of reuse, which is
exactly the failure mode nested_compile_region exists to avoid.

This commit was authored with the assistance of Claude Code.

ghstack-source-id: 975c6e4
Pull-Request: #192009
[ghstack-poisoned]
anijain2305 added a commit that referenced this pull request Aug 6, 2026
A nested compile region that read a value mutated into place before it ran
aborted compilation with an internal assertion:

  AssertionError: Freevar has no source: node.op=call_method node.name=sin --
  this likely means a function argument was not included in the proxy matching

Three more paths that under-record what a region read, same shape as the
rebound global in the parent commit. In each one the read and the write end up
naming the value differently, so the set intersection in has_mutated_vars comes
up empty and the region looks reusable when it is not.

Attribute reads: with `holder.t = x.sin()` ahead of the region, the region's
read of holder.t is served by side_effects.load_attr, which bypasses
VariableBuilder, so nothing lands in traced_sources and the mutation check has
nothing to intersect. The hook goes in load_attr rather than the ~20 call sites
that consult has_pending_mutation_of_attr first. load_cell and load_global now
share it; load_cell keeps its own call because an unmutated cell returns
pre_existing_contents without reaching load_attr.

The VariableTracker cache: LazyVariableTracker.create returns a VT cached by
source, so any source already materialized before the region ran contributes
nothing to that region's traced_sources no matter how it is read. That silently
weakens both the mutation check and guard collection, which is documented as
catching guards installed before the subgraph trace began. This is why storing
an intermediate into an already-materialized list still crashed after the
load_attr fix. Cache hits now record the source.

Closure cells: load_cell recorded AttrSource(cell_source, "cell_contents"),
but a write records the source the contents themselves carry,
LocalSource(..., is_derefed_cell_contents=True). Two spellings of one location,
so the intersection never matched. load_cell now records the contents' own
source, falling back to the synthesized one only when the cell has no contents
to ask.

That last one is not only a lost optimisation. When the cell is rebound to
another sourceful tensor between two calls, nothing downstream objects: the
second call reuses the first call's source and silently returns the wrong
answer, 20 where eager gives 110. It reproduces on the base of this stack, so
it predates this work and is the reason the fix is worth landing on its own
merits rather than as crash cleanup.

Test Plan:

Four tests, one per read path, all failing on the parent commit -- the first
three with the assertion above, the fourth with a wrong result.

```
PYTHONPATH=$PWD/test python test/higher_order_ops/test_invoke_subgraph.py TestInvokeSubgraphReuse
```

lazy.py and load_cell sit on every sourced read and every closure read, so the
sweep was widened past the usual suites. Each was run twice, once with these
changes and once with them reverted, and the results are identical: test_misc
1 failure and 4 errors, test_functions clean, test_repros 11 errors,
test_higher_order_ops 3 errors, all pre-existing and unrelated.

```
PYTHONPATH=$PWD/test python test/dynamo/test_misc.py
PYTHONPATH=$PWD/test python test/dynamo/test_functions.py
PYTHONPATH=$PWD/test python test/dynamo/test_repros.py
PYTHONPATH=$PWD/test python test/dynamo/test_higher_order_ops.py
```

An earlier version of this commit also made build_subgraph_input_mapping
decline instead of raising on a lifted freevar with no source. With the three
recording paths fixed, no shape I could construct reaches that assertion, so
the change was dropped: an internal assertion that fires loudly is a better
tripwire for the next recording gap than a silent loss of reuse, which is
exactly the failure mode nested_compile_region exists to avoid.

This commit was authored with the assistance of Claude Code.

ghstack-source-id: 68e2667
Pull-Request: #192009
@anijain2305
anijain2305 requested a review from zou3519 August 6, 2026 14:00
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@anijain2305 anijain2305 changed the title [dynamo][invoke_subgraph] Track sources for load_attr and LazyVT [dynamo] Fix invoke_subgraph reuse of reads served by side effects Aug 10, 2026
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
anijain2305 added a commit that referenced this pull request Aug 11, 2026
A nested compile region that read a value mutated into place before it ran
aborted compilation with an internal assertion:

  AssertionError: Freevar has no source: node.op=call_method node.name=sin --
  this likely means a function argument was not included in the proxy matching

Three more paths that under-record what a region read, same shape as the
rebound global in the parent commit. In each one the read and the write end up
naming the value differently, so the set intersection in has_mutated_vars comes
up empty and the region looks reusable when it is not.

Attribute reads: with `holder.t = x.sin()` ahead of the region, the region's
read of holder.t is served by side_effects.load_attr, which bypasses
VariableBuilder, so nothing lands in traced_sources and the mutation check has
nothing to intersect. The hook goes in load_attr rather than the ~20 call sites
that consult has_pending_mutation_of_attr first. load_cell and load_global now
share it; load_cell keeps its own call because an unmutated cell returns
pre_existing_contents without reaching load_attr.

The VariableTracker cache: LazyVariableTracker.create returns a VT cached by
source, so any source already materialized before the region ran contributes
nothing to that region's traced_sources no matter how it is read. That silently
weakens both the mutation check and guard collection, which is documented as
catching guards installed before the subgraph trace began. This is why storing
an intermediate into an already-materialized list still crashed after the
load_attr fix. Cache hits now record the source.

Closure cells: load_cell recorded AttrSource(cell_source, "cell_contents"),
but a write records the source the contents themselves carry,
LocalSource(..., is_derefed_cell_contents=True). Two spellings of one location,
so the intersection never matched. load_cell now records the contents' own
source, falling back to the synthesized one only when the cell has no contents
to ask.

That last one is not only a lost optimisation. When the cell is rebound to
another sourceful tensor between two calls, nothing downstream objects: the
second call reuses the first call's source and silently returns the wrong
answer, 20 where eager gives 110. It reproduces on the base of this stack, so
it predates this work and is the reason the fix is worth landing on its own
merits rather than as crash cleanup.

Test Plan:

Four tests, one per read path, all failing on the parent commit -- the first
three with the assertion above, the fourth with a wrong result.

```
PYTHONPATH=$PWD/test python test/higher_order_ops/test_invoke_subgraph.py TestInvokeSubgraphReuse
```

lazy.py and load_cell sit on every sourced read and every closure read, so the
sweep was widened past the usual suites. Each was run twice, once with these
changes and once with them reverted, and the results are identical: test_misc
1 failure and 4 errors, test_functions clean, test_repros 11 errors,
test_higher_order_ops 3 errors, all pre-existing and unrelated.

```
PYTHONPATH=$PWD/test python test/dynamo/test_misc.py
PYTHONPATH=$PWD/test python test/dynamo/test_functions.py
PYTHONPATH=$PWD/test python test/dynamo/test_repros.py
PYTHONPATH=$PWD/test python test/dynamo/test_higher_order_ops.py
```

An earlier version of this commit also made build_subgraph_input_mapping
decline instead of raising on a lifted freevar with no source. With the three
recording paths fixed, no shape I could construct reaches that assertion, so
the change was dropped: an internal assertion that fires loudly is a better
tripwire for the next recording gap than a silent loss of reuse, which is
exactly the failure mode nested_compile_region exists to avoid.

This commit was authored with the assistance of Claude Code.

ghstack-source-id: 2615a5d
Pull-Request: #192009
[ghstack-poisoned]
anijain2305 added a commit that referenced this pull request Aug 12, 2026
A nested compile region that read a value mutated into place before it ran
aborted compilation with an internal assertion:

  AssertionError: Freevar has no source: node.op=call_method node.name=sin --
  this likely means a function argument was not included in the proxy matching

Three more paths that under-record what a region read, same shape as the
rebound global in the parent commit. In each one the read and the write end up
naming the value differently, so the set intersection in has_mutated_vars comes
up empty and the region looks reusable when it is not.

Attribute reads: with `holder.t = x.sin()` ahead of the region, the region's
read of holder.t is served by side_effects.load_attr, which bypasses
VariableBuilder, so nothing lands in traced_sources and the mutation check has
nothing to intersect. The hook goes in load_attr rather than the ~20 call sites
that consult has_pending_mutation_of_attr first. load_cell and load_global now
share it; load_cell keeps its own call because an unmutated cell returns
pre_existing_contents without reaching load_attr.

The VariableTracker cache: LazyVariableTracker.create returns a VT cached by
source, so any source already materialized before the region ran contributes
nothing to that region's traced_sources no matter how it is read. That silently
weakens both the mutation check and guard collection, which is documented as
catching guards installed before the subgraph trace began. This is why storing
an intermediate into an already-materialized list still crashed after the
load_attr fix. Cache hits now record the source.

Closure cells: load_cell recorded AttrSource(cell_source, "cell_contents"),
but a write records the source the contents themselves carry,
LocalSource(..., is_derefed_cell_contents=True). Two spellings of one location,
so the intersection never matched. load_cell now records the contents' own
source, falling back to the synthesized one only when the cell has no contents
to ask.

That last one is not only a lost optimisation. When the cell is rebound to
another sourceful tensor between two calls, nothing downstream objects: the
second call reuses the first call's source and silently returns the wrong
answer, 20 where eager gives 110. It reproduces on the base of this stack, so
it predates this work and is the reason the fix is worth landing on its own
merits rather than as crash cleanup.

Test Plan:

Four tests, one per read path, all failing on the parent commit -- the first
three with the assertion above, the fourth with a wrong result.

```
PYTHONPATH=$PWD/test python test/higher_order_ops/test_invoke_subgraph.py TestInvokeSubgraphReuse
```

lazy.py and load_cell sit on every sourced read and every closure read, so the
sweep was widened past the usual suites. Each was run twice, once with these
changes and once with them reverted, and the results are identical: test_misc
1 failure and 4 errors, test_functions clean, test_repros 11 errors,
test_higher_order_ops 3 errors, all pre-existing and unrelated.

```
PYTHONPATH=$PWD/test python test/dynamo/test_misc.py
PYTHONPATH=$PWD/test python test/dynamo/test_functions.py
PYTHONPATH=$PWD/test python test/dynamo/test_repros.py
PYTHONPATH=$PWD/test python test/dynamo/test_higher_order_ops.py
```

An earlier version of this commit also made build_subgraph_input_mapping
decline instead of raising on a lifted freevar with no source. With the three
recording paths fixed, no shape I could construct reaches that assertion, so
the change was dropped: an internal assertion that fires loudly is a better
tripwire for the next recording gap than a silent loss of reuse, which is
exactly the failure mode nested_compile_region exists to avoid.

This commit was authored with the assistance of Claude Code.

ghstack-source-id: d52f2f3
Pull-Request: #192009
anijain2305 added a commit that referenced this pull request Aug 12, 2026
A nested compile region that read a value mutated into place before it ran
aborted compilation with an internal assertion:

  AssertionError: Freevar has no source: node.op=call_method node.name=sin --
  this likely means a function argument was not included in the proxy matching

Three more paths that under-record what a region read, same shape as the
rebound global in the parent commit. In each one the read and the write end up
naming the value differently, so the set intersection in has_mutated_vars comes
up empty and the region looks reusable when it is not.

Attribute reads: with `holder.t = x.sin()` ahead of the region, the region's
read of holder.t is served by side_effects.load_attr, which bypasses
VariableBuilder, so nothing lands in traced_sources and the mutation check has
nothing to intersect. The hook goes in load_attr rather than the ~20 call sites
that consult has_pending_mutation_of_attr first. load_cell and load_global now
share it; load_cell keeps its own call because an unmutated cell returns
pre_existing_contents without reaching load_attr.

The VariableTracker cache: LazyVariableTracker.create returns a VT cached by
source, so any source already materialized before the region ran contributes
nothing to that region's traced_sources no matter how it is read. That silently
weakens both the mutation check and guard collection, which is documented as
catching guards installed before the subgraph trace began. This is why storing
an intermediate into an already-materialized list still crashed after the
load_attr fix. Cache hits now record the source.

Closure cells: load_cell recorded AttrSource(cell_source, "cell_contents"),
but a write records the source the contents themselves carry,
LocalSource(..., is_derefed_cell_contents=True). Two spellings of one location,
so the intersection never matched. load_cell now records the contents' own
source, falling back to the synthesized one only when the cell has no contents
to ask.

That last one is not only a lost optimisation. When the cell is rebound to
another sourceful tensor between two calls, nothing downstream objects: the
second call reuses the first call's source and silently returns the wrong
answer, 20 where eager gives 110. It reproduces on the base of this stack, so
it predates this work and is the reason the fix is worth landing on its own
merits rather than as crash cleanup.

Test Plan:

Four tests, one per read path, all failing on the parent commit -- the first
three with the assertion above, the fourth with a wrong result.

```
PYTHONPATH=$PWD/test python test/higher_order_ops/test_invoke_subgraph.py TestInvokeSubgraphReuse
```

lazy.py and load_cell sit on every sourced read and every closure read, so the
sweep was widened past the usual suites. Each was run twice, once with these
changes and once with them reverted, and the results are identical: test_misc
1 failure and 4 errors, test_functions clean, test_repros 11 errors,
test_higher_order_ops 3 errors, all pre-existing and unrelated.

```
PYTHONPATH=$PWD/test python test/dynamo/test_misc.py
PYTHONPATH=$PWD/test python test/dynamo/test_functions.py
PYTHONPATH=$PWD/test python test/dynamo/test_repros.py
PYTHONPATH=$PWD/test python test/dynamo/test_higher_order_ops.py
```

An earlier version of this commit also made build_subgraph_input_mapping
decline instead of raising on a lifted freevar with no source. With the three
recording paths fixed, no shape I could construct reaches that assertion, so
the change was dropped: an internal assertion that fires loudly is a better
tripwire for the next recording gap than a silent loss of reuse, which is
exactly the failure mode nested_compile_region exists to avoid.

This commit was authored with the assistance of Claude Code.

ghstack-source-id: d52f2f3
Pull-Request: #192009
anijain2305 added a commit that referenced this pull request Aug 12, 2026
A nested compile region that read a value mutated into place before it ran
aborted compilation with an internal assertion:

  AssertionError: Freevar has no source: node.op=call_method node.name=sin --
  this likely means a function argument was not included in the proxy matching

Three more paths that under-record what a region read, same shape as the
rebound global in the parent commit. In each one the read and the write end up
naming the value differently, so the set intersection in has_mutated_vars comes
up empty and the region looks reusable when it is not.

Attribute reads: with `holder.t = x.sin()` ahead of the region, the region's
read of holder.t is served by side_effects.load_attr, which bypasses
VariableBuilder, so nothing lands in traced_sources and the mutation check has
nothing to intersect. The hook goes in load_attr rather than the ~20 call sites
that consult has_pending_mutation_of_attr first. load_cell and load_global now
share it; load_cell keeps its own call because an unmutated cell returns
pre_existing_contents without reaching load_attr.

The VariableTracker cache: LazyVariableTracker.create returns a VT cached by
source, so any source already materialized before the region ran contributes
nothing to that region's traced_sources no matter how it is read. That silently
weakens both the mutation check and guard collection, which is documented as
catching guards installed before the subgraph trace began. This is why storing
an intermediate into an already-materialized list still crashed after the
load_attr fix. Cache hits now record the source.

Closure cells: load_cell recorded AttrSource(cell_source, "cell_contents"),
but a write records the source the contents themselves carry,
LocalSource(..., is_derefed_cell_contents=True). Two spellings of one location,
so the intersection never matched. load_cell now records the contents' own
source, falling back to the synthesized one only when the cell has no contents
to ask.

That last one is not only a lost optimisation. When the cell is rebound to
another sourceful tensor between two calls, nothing downstream objects: the
second call reuses the first call's source and silently returns the wrong
answer, 20 where eager gives 110. It reproduces on the base of this stack, so
it predates this work and is the reason the fix is worth landing on its own
merits rather than as crash cleanup.

Test Plan:

Four tests, one per read path, all failing on the parent commit -- the first
three with the assertion above, the fourth with a wrong result.

```
PYTHONPATH=$PWD/test python test/higher_order_ops/test_invoke_subgraph.py TestInvokeSubgraphReuse
```

lazy.py and load_cell sit on every sourced read and every closure read, so the
sweep was widened past the usual suites. Each was run twice, once with these
changes and once with them reverted, and the results are identical: test_misc
1 failure and 4 errors, test_functions clean, test_repros 11 errors,
test_higher_order_ops 3 errors, all pre-existing and unrelated.

```
PYTHONPATH=$PWD/test python test/dynamo/test_misc.py
PYTHONPATH=$PWD/test python test/dynamo/test_functions.py
PYTHONPATH=$PWD/test python test/dynamo/test_repros.py
PYTHONPATH=$PWD/test python test/dynamo/test_higher_order_ops.py
```

An earlier version of this commit also made build_subgraph_input_mapping
decline instead of raising on a lifted freevar with no source. With the three
recording paths fixed, no shape I could construct reaches that assertion, so
the change was dropped: an internal assertion that fires loudly is a better
tripwire for the next recording gap than a silent loss of reuse, which is
exactly the failure mode nested_compile_region exists to avoid.

This commit was authored with the assistance of Claude Code.

ghstack-source-id: d52f2f3
Pull-Request: #192009
anijain2305 added a commit that referenced this pull request Aug 12, 2026
A nested compile region that read a value mutated into place before it ran
aborted compilation with an internal assertion:

  AssertionError: Freevar has no source: node.op=call_method node.name=sin --
  this likely means a function argument was not included in the proxy matching

Three more paths that under-record what a region read, same shape as the
rebound global in the parent commit. In each one the read and the write end up
naming the value differently, so the set intersection in has_mutated_vars comes
up empty and the region looks reusable when it is not.

Attribute reads: with `holder.t = x.sin()` ahead of the region, the region's
read of holder.t is served by side_effects.load_attr, which bypasses
VariableBuilder, so nothing lands in traced_sources and the mutation check has
nothing to intersect. The hook goes in load_attr rather than the ~20 call sites
that consult has_pending_mutation_of_attr first. load_cell and load_global now
share it; load_cell keeps its own call because an unmutated cell returns
pre_existing_contents without reaching load_attr.

The VariableTracker cache: LazyVariableTracker.create returns a VT cached by
source, so any source already materialized before the region ran contributes
nothing to that region's traced_sources no matter how it is read. That silently
weakens both the mutation check and guard collection, which is documented as
catching guards installed before the subgraph trace began. This is why storing
an intermediate into an already-materialized list still crashed after the
load_attr fix. Cache hits now record the source.

Closure cells: load_cell recorded AttrSource(cell_source, "cell_contents"),
but a write records the source the contents themselves carry,
LocalSource(..., is_derefed_cell_contents=True). Two spellings of one location,
so the intersection never matched. load_cell now records the contents' own
source, falling back to the synthesized one only when the cell has no contents
to ask.

That last one is not only a lost optimisation. When the cell is rebound to
another sourceful tensor between two calls, nothing downstream objects: the
second call reuses the first call's source and silently returns the wrong
answer, 20 where eager gives 110. It reproduces on the base of this stack, so
it predates this work and is the reason the fix is worth landing on its own
merits rather than as crash cleanup.

Test Plan:

Four tests, one per read path, all failing on the parent commit -- the first
three with the assertion above, the fourth with a wrong result.

```
PYTHONPATH=$PWD/test python test/higher_order_ops/test_invoke_subgraph.py TestInvokeSubgraphReuse
```

lazy.py and load_cell sit on every sourced read and every closure read, so the
sweep was widened past the usual suites. Each was run twice, once with these
changes and once with them reverted, and the results are identical: test_misc
1 failure and 4 errors, test_functions clean, test_repros 11 errors,
test_higher_order_ops 3 errors, all pre-existing and unrelated.

```
PYTHONPATH=$PWD/test python test/dynamo/test_misc.py
PYTHONPATH=$PWD/test python test/dynamo/test_functions.py
PYTHONPATH=$PWD/test python test/dynamo/test_repros.py
PYTHONPATH=$PWD/test python test/dynamo/test_higher_order_ops.py
```

An earlier version of this commit also made build_subgraph_input_mapping
decline instead of raising on a lifted freevar with no source. With the three
recording paths fixed, no shape I could construct reaches that assertion, so
the change was dropped: an internal assertion that fires loudly is a better
tripwire for the next recording gap than a silent loss of reuse, which is
exactly the failure mode nested_compile_region exists to avoid.

This commit was authored with the assistance of Claude Code.

ghstack-source-id: d52f2f3
Pull-Request: #192009
@github-actions

Copy link
Copy Markdown
Contributor

Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as Stale.
Feel free to remove the Stale label if you feel this was a mistake.
If you are unable to remove the Stale label please contact a maintainer in order to do so.
If you want the bot to never mark this PR stale again, add the no-stale label.
Stale pull requests will automatically be closed after 30 days of inactivity.

@github-actions github-actions Bot added the Stale label Oct 28, 2026
@github-actions github-actions Bot closed this Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant