[dynamo] Propagate traced_sources across nested compile regions (#192905) by syerva-g · Pull Request #200009 · pytorch/pytorch · GitHub
Skip to content

[dynamo] Propagate traced_sources across nested compile regions (#192905) - #200009

Open
syerva-g wants to merge 2 commits into
pytorch:mainfrom
syerva-g:fix-192905-nested-compile-region-guards
Open

syerva-g wants to merge 2 commits into
pytorch:mainfrom
syerva-g:fix-192905-nested-compile-region-guards

Conversation

@syerva-g

@syerva-g syerva-g commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #192905

Summary

When a @nested_compile_region (outer) calls another @nested_compile_region (inner), each speculate_subgraph call creates a fresh SubgraphTracer with an empty traced_sources set (output_graph.py). Sources accessed inside inner (such as layer.flag) are recorded only on inner's SubgraphTracer.traced_sources and are never propagated to subtracer.parent.traced_sources (outer's SubgraphTracer).

Additionally, if inner hits the subgraph reuse cache (stamp_out_subgraph), the inner entry's remapped traced_sources and remapped guards are not propagated to the active outer tracer.

As a result, build_reuse_condition for outer sees an empty traced_sources set, collects 0 guards on attributes accessed inside inner, and silently reuses outer's specialized subgraph on subsequent calls where those attributes differ.

Fix

  1. Propagate subtracer.traced_sources to subtracer.parent.traced_sources when speculate_subgraph completes in torch/_dynamo/variables/higher_order_ops.py.
  2. Record traced_sources and condition on InvokeSubgraphReuseEntry (torch/_guards.py) and propagate remapped traced_sources and guards in stamp_out_subgraph (torch/_dynamo/variables/invoke_subgraph.py).

Test Plan

  • pytest test/higher_order_ops/test_invoke_subgraph.py -k test_subgraph_reuse_nested_region

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

@pytorch-bot

pytorch-bot Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

@pytorch-bot pytorch-bot Bot added ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests module: dynamo labels Oct 7, 2026
@pytorch-bot

pytorch-bot Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

The following ciflow label(s) have been added but CI has not been triggered yet because the workflows are awaiting approval:

  • ciflow/inductor
  • ciflow/torchtitan

Once a maintainer approves the workflows (scroll to the bottom of the PR page), the corresponding CI jobs will be triggered automatically. Please ping one of the reviewers if you do not have access to approve and run workflows.

1 similar comment
@pytorch-bot

pytorch-bot Bot commented Oct 7, 2026

Copy link
Copy Markdown

The following ciflow label(s) have been added but CI has not been triggered yet because the workflows are awaiting approval:

  • ciflow/inductor
  • ciflow/torchtitan

Once a maintainer approves the workflows (scroll to the bottom of the PR page), the corresponding CI jobs will be triggered automatically. Please ping one of the reviewers if you do not have access to approve and run workflows.

@github-actions github-actions Bot added bot-triaged This is a label only to be used by the auto triage bot missing actionable issue A PR without a linked actionable issue or maintainer sponsor triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module labels Oct 7, 2026
@syerva-g

syerva-g commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

@pytorchbot label "release notes: dynamo"

cc @anijain2305 @jansel @zou3519 @aperson30 — could you please take a look and approve the CI run when you get a chance? Thanks!

@syerva-g
syerva-g force-pushed the fix-192905-nested-compile-region-guards branch from 6ff4074 to 76028cc Compare October 14, 2026 18:28
@syerva-g

Copy link
Copy Markdown
Contributor Author

…rch#192905)

When a `@nested_compile_region` (`outer`) calls another `@nested_compile_region`
(`inner`), each `speculate_subgraph` call creates a fresh `SubgraphTracer` with
an empty `traced_sources` set (`output_graph.py`). Sources accessed inside `inner`
(such as `layer.flag`) are recorded only on `inner`'s `SubgraphTracer.traced_sources`
and are never propagated to `subtracer.parent.traced_sources` (`outer`'s
`SubgraphTracer`).

Additionally, if `inner` hits the subgraph reuse cache (`stamp_out_subgraph`),
the inner entry's remapped `traced_sources` and remapped guards are not propagated
to the active outer tracer.

As a result, `build_reuse_condition` for `outer` sees an empty `traced_sources` set,
collects 0 guards on attributes accessed inside `inner`, and silently reuses
`outer`'s specialized subgraph on subsequent calls where those attributes differ.

Fix this by:
1. Propagating `subtracer.traced_sources` to `subtracer.parent.traced_sources`
   when `speculate_subgraph` completes.
2. Recording `traced_sources` and `condition` on `InvokeSubgraphReuseEntry` and
   propagating remapped `traced_sources` and guards in `stamp_out_subgraph`.

Fixes pytorch#192905
…subgraph

stamp_out_subgraph re-installed every guard of the cached reuse condition under the new call's sources, including the TENSOR_MATCH guards on the subgraph args.  Those args were already guarded when the caller wrapped them (and TENSOR_MATCH is a functools.partial bound to the original tensor), so the clone produced a second tensor guard on the same source and guard construction failed with 'Duplicate tensors found' (test_subgraph_reuse_cache_multiple_entries, test_multi_invocation_chunked_loss_with_backward, TestFakeTensorUpdater.test_hop_subgraph_inputs).

Only re-install guards on non-arg, non-tensor sources (e.g. attribute reads inside the inner region such as layer.flag), which is what the enclosing region needs to see in its own reuse condition.
@syerva-g
syerva-g force-pushed the fix-192905-nested-compile-region-guards branch 2 times, most recently from 76028cc to 417a829 Compare October 21, 2026 13:26

This branch was successfully deployed

1 active (outdated) deployment
bedrock — e62f6131 Deployed Oct 7, 2026 by pytorchbot via analyze #3151
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot-triaged This is a label only to be used by the auto triage bot ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests missing actionable issue A PR without a linked actionable issue or maintainer sponsor module: dynamo open source release notes: dynamo triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Nested nested_compile_region silently reuses a body specialized on the inner region's guards

2 participants