Skip to content

[dynamo] Add subgraph reuse for invoke_subgraph#176478

Closed
anijain2305 wants to merge 9 commits intogh/anijain2305/1061/basefrom
gh/anijain2305/1061/head
Closed

[dynamo] Add subgraph reuse for invoke_subgraph#176478
anijain2305 wants to merge 9 commits intogh/anijain2305/1061/basefrom
gh/anijain2305/1061/head

Conversation

@anijain2305
Copy link
Contributor

@anijain2305 anijain2305 commented Mar 4, 2026

Stack from ghstack (oldest at bottom):

Add an auto-caching mechanism for invoke_subgraph that avoids
re-tracing subgraphs when the same function is called again with
compatible inputs. On the first trace, a fingerprint (input tags,
tensor metadata, guards, traced_sources) is saved. On subsequent
calls, the cache is checked and if a match is found, the subgraph is
stamped out directly without re-tracing.

Authored with Claude.

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

Add an auto-caching mechanism for invoke_subgraph that avoids
re-tracing subgraphs when the same function is called again with
compatible inputs. On the first trace, a fingerprint (input tags,
tensor metadata, guards, traced_sources) is saved. On subsequent
calls, the cache is checked and if a match is found, the subgraph is
stamped out directly without re-tracing.

Authored with Claude.

[ghstack-poisoned]
@pytorch-bot
Copy link

pytorch-bot bot commented Mar 4, 2026

🔗 Helpful Links

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

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

❌ 11 New Failures, 41 Pending, 2 Unrelated Failures

As of commit e1e0b20 with merge base b633f26 (image):

NEW FAILURES - The following jobs have failed:

FLAKY - The following jobs failed but were likely due to flakiness present on trunk:

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.

@pytorch-bot
Copy link

pytorch-bot bot commented Mar 4, 2026

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.

anijain2305 added a commit that referenced this pull request Mar 4, 2026
Add an auto-caching mechanism for invoke_subgraph that avoids
re-tracing subgraphs when the same function is called again with
compatible inputs. On the first trace, a fingerprint (input tags,
tensor metadata, guards, traced_sources) is saved. On subsequent
calls, the cache is checked and if a match is found, the subgraph is
stamped out directly without re-tracing.

Authored with Claude.

ghstack-source-id: ab426b5
Pull Request resolved: #176478
It is possible that a subgraph is morally reusable but does not fall
into the limited support that Dynamo has today. Current limitations:
- The subgraph must not have side effects.
- No variable accessed by the subgraph may have been mutated.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No sourceful variable accessed by the subgraph is mutated. Add a line for why this is important

- The subgraph must not have side effects.
- No variable accessed by the subgraph may have been mutated.
- Output must be a single tensor, or a tuple/list of plain tensors.
- All flattened inputs must be one of: tensor, symnode, constant,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For sourceless or flattened inputs, we rely on the pytree_spec and tags to do the checking. So only acceptable types are supported.

example_value: Any,
condition: "InvokeSubgraphReuseCondition",
) -> None:
from torch._guards import InvokeSubgraphCache, InvokeSubgraphReuseEntry
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add docstring here.

Add an auto-caching mechanism for invoke_subgraph that avoids
re-tracing subgraphs when the same function is called again with
compatible inputs. On the first trace, a fingerprint (input tags,
tensor metadata, guards, traced_sources) is saved. On subsequent
calls, the cache is checked and if a match is found, the subgraph is
stamped out directly without re-tracing.

Authored with Claude.

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

[ghstack-poisoned]
anijain2305 added a commit that referenced this pull request Mar 4, 2026
Add an auto-caching mechanism for invoke_subgraph that avoids
re-tracing subgraphs when the same function is called again with
compatible inputs. On the first trace, a fingerprint (input tags,
tensor metadata, guards, traced_sources) is saved. On subsequent
calls, the cache is checked and if a match is found, the subgraph is
stamped out directly without re-tracing.

Authored with Claude.

ghstack-source-id: ba3a556
Pull Request resolved: #176478
Two-phase check:
(1) Verify that intermediates (tensor metadata, symnode types, constant
values) match the cached input_checks — these are lightweight
structural comparisons that don't require source resolution.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(2) Check for mutation on the remapped vars.

Add an auto-caching mechanism for invoke_subgraph that avoids
re-tracing subgraphs when the same function is called again with
compatible inputs. On the first trace, a fingerprint (input tags,
tensor metadata, guards, traced_sources) is saved. On subsequent
calls, the cache is checked and if a match is found, the subgraph is
stamped out directly without re-tracing.

Authored with Claude.

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

[ghstack-poisoned]
anijain2305 added a commit that referenced this pull request Mar 4, 2026
Add an auto-caching mechanism for invoke_subgraph that avoids
re-tracing subgraphs when the same function is called again with
compatible inputs. On the first trace, a fingerprint (input tags,
tensor metadata, guards, traced_sources) is saved. On subsequent
calls, the cache is checked and if a match is found, the subgraph is
stamped out directly without re-tracing.

Authored with Claude.

ghstack-source-id: 6c67152
Pull Request resolved: #176478
Add an auto-caching mechanism for invoke_subgraph that avoids
re-tracing subgraphs when the same function is called again with
compatible inputs. On the first trace, a fingerprint (input tags,
tensor metadata, guards, traced_sources) is saved. On subsequent
calls, the cache is checked and if a match is found, the subgraph is
stamped out directly without re-tracing.

Authored with Claude.

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

[ghstack-poisoned]
anijain2305 added a commit that referenced this pull request Mar 5, 2026
Add an auto-caching mechanism for invoke_subgraph that avoids
re-tracing subgraphs when the same function is called again with
compatible inputs. On the first trace, a fingerprint (input tags,
tensor metadata, guards, traced_sources) is saved. On subsequent
calls, the cache is checked and if a match is found, the subgraph is
stamped out directly without re-tracing.

Authored with Claude.

ghstack-source-id: 11b2b17
Pull Request resolved: #176478
Add an auto-caching mechanism for invoke_subgraph that avoids
re-tracing subgraphs when the same function is called again with
compatible inputs. On the first trace, a fingerprint (input tags,
tensor metadata, guards, traced_sources) is saved. On subsequent
calls, the cache is checked and if a match is found, the subgraph is
stamped out directly without re-tracing.

Authored with Claude.

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

[ghstack-poisoned]
anijain2305 added a commit that referenced this pull request Mar 5, 2026
Add an auto-caching mechanism for invoke_subgraph that avoids
re-tracing subgraphs when the same function is called again with
compatible inputs. On the first trace, a fingerprint (input tags,
tensor metadata, guards, traced_sources) is saved. On subsequent
calls, the cache is checked and if a match is found, the subgraph is
stamped out directly without re-tracing.

Authored with Claude.

ghstack-source-id: 6fe8db0
Pull Request resolved: #176478
Add an auto-caching mechanism for invoke_subgraph that avoids
re-tracing subgraphs when the same function is called again with
compatible inputs. On the first trace, a fingerprint (input tags,
tensor metadata, guards, traced_sources) is saved. On subsequent
calls, the cache is checked and if a match is found, the subgraph is
stamped out directly without re-tracing.

Authored with Claude.

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

[ghstack-poisoned]
anijain2305 added a commit that referenced this pull request Mar 5, 2026
Add an auto-caching mechanism for invoke_subgraph that avoids
re-tracing subgraphs when the same function is called again with
compatible inputs. On the first trace, a fingerprint (input tags,
tensor metadata, guards, traced_sources) is saved. On subsequent
calls, the cache is checked and if a match is found, the subgraph is
stamped out directly without re-tracing.

Authored with Claude.

ghstack-source-id: 2fd6f4c
Pull Request resolved: #176478
Add an auto-caching mechanism for invoke_subgraph that avoids
re-tracing subgraphs when the same function is called again with
compatible inputs. On the first trace, a fingerprint (input tags,
tensor metadata, guards, traced_sources) is saved. On subsequent
calls, the cache is checked and if a match is found, the subgraph is
stamped out directly without re-tracing.

Authored with Claude.

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

[ghstack-poisoned]
anijain2305 added a commit that referenced this pull request Mar 5, 2026
Add an auto-caching mechanism for invoke_subgraph that avoids
re-tracing subgraphs when the same function is called again with
compatible inputs. On the first trace, a fingerprint (input tags,
tensor metadata, guards, traced_sources) is saved. On subsequent
calls, the cache is checked and if a match is found, the subgraph is
stamped out directly without re-tracing.

Authored with Claude.

ghstack-source-id: b24ed5d
Pull Request resolved: #176478
Add an auto-caching mechanism for invoke_subgraph that avoids
re-tracing subgraphs when the same function is called again with
compatible inputs. On the first trace, a fingerprint (input tags,
tensor metadata, guards, traced_sources) is saved. On subsequent
calls, the cache is checked and if a match is found, the subgraph is
stamped out directly without re-tracing.

Authored with Claude.

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

[ghstack-poisoned]
anijain2305 added a commit that referenced this pull request Mar 5, 2026
Add an auto-caching mechanism for invoke_subgraph that avoids
re-tracing subgraphs when the same function is called again with
compatible inputs. On the first trace, a fingerprint (input tags,
tensor metadata, guards, traced_sources) is saved. On subsequent
calls, the cache is checked and if a match is found, the subgraph is
stamped out directly without re-tracing.

Authored with Claude.

ghstack-source-id: 73b24bb
Pull Request resolved: #176478
Add an auto-caching mechanism for invoke_subgraph that avoids
re-tracing subgraphs when the same function is called again with
compatible inputs. On the first trace, a fingerprint (input tags,
tensor metadata, guards, traced_sources) is saved. On subsequent
calls, the cache is checked and if a match is found, the subgraph is
stamped out directly without re-tracing.

Authored with Claude.

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

[ghstack-poisoned]
anijain2305 added a commit that referenced this pull request Mar 5, 2026
Add an auto-caching mechanism for invoke_subgraph that avoids
re-tracing subgraphs when the same function is called again with
compatible inputs. On the first trace, a fingerprint (input tags,
tensor metadata, guards, traced_sources) is saved. On subsequent
calls, the cache is checked and if a match is found, the subgraph is
stamped out directly without re-tracing.

Authored with Claude.

ghstack-source-id: d14e3e9
Pull Request resolved: #176478
@anijain2305 anijain2305 closed this Mar 5, 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