Repository navigation
Move autograd_backward out of FX custom metadata - #180251
tugsbayasgalan wants to merge 10 commits into
Conversation
The old implementation tagged backward nodes by running fx_traceback.annotate({"autograd_backward": True}) around traced autograd.grad. That stored autograd_backward in current_meta["custom"], which is also where user annotations such as {"ac_region_id": 0} live.
This was unsafe because some traceback save/restore paths only shallow-copy metadata. In particular, set_current_meta(node) does current_meta = node.meta.copy(), so current_meta["custom"] aliases node.meta["custom"]. A nested annotate({"autograd_backward": True}) then mutates that shared dict in place. When the nested scope exits, current_meta may be restored, but the source node metadata can stay polluted, which is how forward FlexAttention nodes ended up leaking autograd_backward.
Fix this by treating autograd_backward as internal tracing state instead of user custom metadata. Keep a dedicated current_meta["autograd_backward"] flag during tracing, stamp node.meta["autograd_backward"] on newly created nodes, and update remat/tests to read the top-level meta key. This preserves user annotations in node.meta["custom"] while keeping backward classification separate.
Add non-strict tracing and FlexAttention regressions covering the leak.
Authored with Claude.
[ghstack-poisoned]
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/180251
Note: Links to docs will display an error until the docs builds have been completed. ❗ 1 Active SEVsThere are 1 currently active SEVs. If your PR is affected, please view them below: ❌ 2 New Failures, 1 Cancelled Job, 11 Unrelated FailuresAs of commit c420814 with merge base 875381e ( NEW FAILURES - The following jobs have failed:
CANCELLED JOB - The following job was cancelled. Please retry:
FLAKY - The following jobs failed but were likely due to flakiness present on trunk:
BROKEN TRUNK - The following job failed but were present on the merge base:👉 Rebase onto the `viable/strict` branch to avoid these failures
UNSTABLE - The following job is marked as unstable, possibly due to flakiness on trunk:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
The old implementation tagged backward nodes by running fx_traceback.annotate({"autograd_backward": True}) around traced autograd.grad. That stored autograd_backward in current_meta["custom"], which is also where user annotations such as {"ac_region_id": 0} live.
This was unsafe because some traceback save/restore paths only shallow-copy metadata. In particular, set_current_meta(node) does current_meta = node.meta.copy(), so current_meta["custom"] aliases node.meta["custom"]. A nested annotate({"autograd_backward": True}) then mutates that shared dict in place. When the nested scope exits, current_meta may be restored, but the source node metadata can stay polluted, which is how forward FlexAttention nodes ended up leaking autograd_backward.
Fix this by treating autograd_backward as internal tracing state instead of user custom metadata. Keep a dedicated current_meta["autograd_backward"] flag during tracing, stamp node.meta["autograd_backward"] on newly created nodes, and update remat/tests to read the top-level meta key. This preserves user annotations in node.meta["custom"] while keeping backward classification separate.
Add non-strict tracing and FlexAttention regressions covering the leak.
Authored with Claude.
ghstack-source-id: 38be134
Pull Request resolved: #180251
The old implementation tagged backward nodes by running fx_traceback.annotate({"autograd_backward": True}) around traced autograd.grad. That stored autograd_backward in current_meta["custom"], which is also where user annotations such as {"ac_region_id": 0} live.
This was unsafe because some traceback save/restore paths only shallow-copy metadata. In particular, set_current_meta(node) does current_meta = node.meta.copy(), so current_meta["custom"] aliases node.meta["custom"]. A nested annotate({"autograd_backward": True}) then mutates that shared dict in place. When the nested scope exits, current_meta may be restored, but the source node metadata can stay polluted, which is how forward FlexAttention nodes ended up leaking autograd_backward.
Fix this by treating autograd_backward as internal tracing state instead of user custom metadata. Keep a dedicated current_meta["autograd_backward"] flag during tracing, stamp node.meta["autograd_backward"] on newly created nodes, and update remat/tests to read the top-level meta key. This preserves user annotations in node.meta["custom"] while keeping backward classification separate.
Add non-strict tracing and FlexAttention regressions covering the leak.
Authored with Claude.
cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx kadeng chauhang amjames Lucaskabela jataylo azahed98
[ghstack-poisoned]
The old implementation tagged backward nodes by running fx_traceback.annotate({"autograd_backward": True}) around traced autograd.grad. That stored autograd_backward in current_meta["custom"], which is also where user annotations such as {"ac_region_id": 0} live.
This was unsafe because some traceback save/restore paths only shallow-copy metadata. In particular, set_current_meta(node) does current_meta = node.meta.copy(), so current_meta["custom"] aliases node.meta["custom"]. A nested annotate({"autograd_backward": True}) then mutates that shared dict in place. When the nested scope exits, current_meta may be restored, but the source node metadata can stay polluted, which is how forward FlexAttention nodes ended up leaking autograd_backward.
Fix this by treating autograd_backward as internal tracing state instead of user custom metadata. Keep a dedicated current_meta["autograd_backward"] flag during tracing, stamp node.meta["autograd_backward"] on newly created nodes, and update remat/tests to read the top-level meta key. This preserves user annotations in node.meta["custom"] while keeping backward classification separate.
Add non-strict tracing and FlexAttention regressions covering the leak.
Authored with Claude.
ghstack-source-id: bd739ef
Pull Request resolved: #180251
|
@claude Review this PR |
|
Claude finished @tugsbayasgalan's task in 3m 50s —— View job PR Review: #180251SummarySolid, well-scoped fix for a real metadata pollution bug. The root cause — shallow-copy aliasing in Code Quality
TestingThe two new tests are well-designed and directly cover the reported bug:
Both tests assert the positive case (backward nodes exist / user annotations preserved) alongside the negative case (no leak), which is good. API Design
Backward CompatibilityNo BC concerns. The old metadata path (
RecommendationApprove The fix is correct, well-tested, and properly scoped. The two minor suggestions (docstring on |
|
@claude Are you sure? It seems like if we use nested set_autograd_backward() this pollution will happen? But i guess it is always true so even if there is pollution, it is likely fine? |
|
Claude finished @tugsbayasgalan's task in 1m 2s —— View job
Analysis: Nested
|
|
@claude Any chance we can merge set_autograd_backward with setup_stacktrace_preservation_hooks_from_tensors ? |
|
Claude finished @tugsbayasgalan's task in 1m 4s —— View job
Analysis: Can
|
The old implementation tagged backward nodes by running fx_traceback.annotate({"autograd_backward": True}) around traced autograd.grad. That stored autograd_backward in current_meta["custom"], which is also where user annotations such as {"ac_region_id": 0} live.
This was unsafe because some traceback save/restore paths only shallow-copy metadata. In particular, set_current_meta(node) does current_meta = node.meta.copy(), so current_meta["custom"] aliases node.meta["custom"]. A nested annotate({"autograd_backward": True}) then mutates that shared dict in place. When the nested scope exits, current_meta may be restored, but the source node metadata can stay polluted, which is how forward FlexAttention nodes ended up leaking autograd_backward.
Fix this by treating autograd_backward as internal tracing state instead of user custom metadata. Keep a dedicated current_meta["autograd_backward"] flag during tracing, stamp node.meta["autograd_backward"] on newly created nodes, and update remat/tests to read the top-level meta key. This preserves user annotations in node.meta["custom"] while keeping backward classification separate.
Add non-strict tracing and FlexAttention regressions covering the leak.
Authored with Claude.
cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx kadeng chauhang amjames Lucaskabela jataylo azahed98
[ghstack-poisoned]
The old implementation tagged backward nodes by running fx_traceback.annotate({"autograd_backward": True}) around traced autograd.grad. That stored autograd_backward in current_meta["custom"], which is also where user annotations such as {"ac_region_id": 0} live.
This was unsafe because some traceback save/restore paths only shallow-copy metadata. In particular, set_current_meta(node) does current_meta = node.meta.copy(), so current_meta["custom"] aliases node.meta["custom"]. A nested annotate({"autograd_backward": True}) then mutates that shared dict in place. When the nested scope exits, current_meta may be restored, but the source node metadata can stay polluted, which is how forward FlexAttention nodes ended up leaking autograd_backward.
Fix this by treating autograd_backward as internal tracing state instead of user custom metadata. Keep a dedicated current_meta["autograd_backward"] flag during tracing, stamp node.meta["autograd_backward"] on newly created nodes, and update remat/tests to read the top-level meta key. This preserves user annotations in node.meta["custom"] while keeping backward classification separate.
Add non-strict tracing and FlexAttention regressions covering the leak.
Authored with Claude.
ghstack-source-id: afe6d17
Pull Request resolved: #180251
The old implementation tagged backward nodes by running fx_traceback.annotate({"autograd_backward": True}) around traced autograd.grad. That stored autograd_backward in current_meta["custom"], which is also where user annotations such as {"ac_region_id": 0} live.
This was unsafe because some traceback save/restore paths only shallow-copy metadata. In particular, set_current_meta(node) does current_meta = node.meta.copy(), so current_meta["custom"] aliases node.meta["custom"]. A nested annotate({"autograd_backward": True}) then mutates that shared dict in place. When the nested scope exits, current_meta may be restored, but the source node metadata can stay polluted, which is how forward FlexAttention nodes ended up leaking autograd_backward.
Fix this by treating autograd_backward as internal tracing state instead of user custom metadata. Keep a dedicated current_meta["autograd_backward"] flag during tracing, stamp node.meta["autograd_backward"] on newly created nodes, and update remat/tests to read the top-level meta key. This preserves user annotations in node.meta["custom"] while keeping backward classification separate.
Add non-strict tracing and FlexAttention regressions covering the leak.
Authored with Claude.
cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx kadeng chauhang amjames Lucaskabela jataylo azahed98
[ghstack-poisoned]
The old implementation tagged backward nodes by running fx_traceback.annotate({"autograd_backward": True}) around traced autograd.grad. That stored autograd_backward in current_meta["custom"], which is also where user annotations such as {"ac_region_id": 0} live.
This was unsafe because some traceback save/restore paths only shallow-copy metadata. In particular, set_current_meta(node) does current_meta = node.meta.copy(), so current_meta["custom"] aliases node.meta["custom"]. A nested annotate({"autograd_backward": True}) then mutates that shared dict in place. When the nested scope exits, current_meta may be restored, but the source node metadata can stay polluted, which is how forward FlexAttention nodes ended up leaking autograd_backward.
Fix this by treating autograd_backward as internal tracing state instead of user custom metadata. Keep a dedicated current_meta["autograd_backward"] flag during tracing, stamp node.meta["autograd_backward"] on newly created nodes, and update remat/tests to read the top-level meta key. This preserves user annotations in node.meta["custom"] while keeping backward classification separate.
Add non-strict tracing and FlexAttention regressions covering the leak.
Authored with Claude.
ghstack-source-id: 4405028
Pull Request resolved: #180251
|
@pytorchbot merge |
Merge startedYour change will be merged once all checks pass (ETA 0-4 Hours). Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
Merge failedReason: 1 mandatory check(s) failed. The first few are: Dig deeper by viewing the failures on hud |
Merge startedYour change will be merged while ignoring the following 0 checks: Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
Merge failedReason: 1 jobs have failed, first few of them are: trunk / linux-jammy-rocm-py3.10-mi355 / test (default, 3, 6, linux.rocm.gpu.gfx950.1) Details for Dev Infra teamRaised by workflow job |
The old implementation tagged backward nodes by running fx_traceback.annotate({"autograd_backward": True}) around traced autograd.grad. That stored autograd_backward in current_meta["custom"], which is also where user annotations such as {"ac_region_id": 0} live.
This was unsafe because some traceback save/restore paths only shallow-copy metadata. In particular, set_current_meta(node) does current_meta = node.meta.copy(), so current_meta["custom"] aliases node.meta["custom"]. A nested annotate({"autograd_backward": True}) then mutates that shared dict in place. When the nested scope exits, current_meta may be restored, but the source node metadata can stay polluted, which is how forward FlexAttention nodes ended up leaking autograd_backward.
Fix this by treating autograd_backward as internal tracing state instead of user custom metadata. Keep a dedicated current_meta["autograd_backward"] flag during tracing, stamp node.meta["autograd_backward"] on newly created nodes using setup_stacktrace_preservation_hook.
With this tagging, we can reorder backward nodes during partitioner, so regional_inductor now turns off remat pass. This is fine because, remat pass is intended to be consumed only after:
1. you torch.compile whole training step
2. you make_fx d whole training step
In relevant use cases, regional inductor will happen after SAC decision has been applied to the final graph.
Add non-strict tracing and FlexAttention regressions covering the leak.
Authored with Claude.
Pull Request resolved: #180251
Approved by: https://github.com/Skylion007, https://github.com/aorenste
This reverts commit 8c49f56. Reverted #180251 on behalf of https://github.com/jeanschmidt due to sorry, need to revert in order to revert #179731, feel free to resolve conflicts and merge into main after that ([comment](#180251 (comment)))
|
@pytorchbot merge -i |
Merge failedReason: 1 mandatory check(s) failed. The first few are: Dig deeper by viewing the failures on hud |
|
@pytorchbot merge -i |
Merge failedReason: 1 jobs have failed, first few of them are: trunk / linux-jammy-rocm-py3.10-mi355 / test (default, 1, 6, linux.rocm.gpu.gfx950.1) Details for Dev Infra teamRaised by workflow job |
|
@pytorchbot merge -i |
The old implementation tagged backward nodes by running fx_traceback.annotate({"autograd_backward": True}) around traced autograd.grad. That stored autograd_backward in current_meta["custom"], which is also where user annotations such as {"ac_region_id": 0} live.
This was unsafe because some traceback save/restore paths only shallow-copy metadata. In particular, set_current_meta(node) does current_meta = node.meta.copy(), so current_meta["custom"] aliases node.meta["custom"]. A nested annotate({"autograd_backward": True}) then mutates that shared dict in place. When the nested scope exits, current_meta may be restored, but the source node metadata can stay polluted, which is how forward FlexAttention nodes ended up leaking autograd_backward.
Fix this by treating autograd_backward as internal tracing state instead of user custom metadata. Keep a dedicated current_meta["autograd_backward"] flag during tracing, stamp node.meta["autograd_backward"] on newly created nodes using setup_stacktrace_preservation_hook.
With this tagging, we can reorder backward nodes during partitioner, so regional_inductor now turns off remat pass. This is fine because, remat pass is intended to be consumed only after:
1. you torch.compile whole training step
2. you make_fx d whole training step
In relevant use cases, regional inductor will happen after SAC decision has been applied to the final graph.
Add non-strict tracing and FlexAttention regressions covering the leak.
Authored with Claude.
Pull Request resolved: pytorch#180251
Approved by: https://github.com/Skylion007, https://github.com/aorenste
The old implementation tagged backward nodes by running fx_traceback.annotate({"autograd_backward": True}) around traced autograd.grad. That stored autograd_backward in current_meta["custom"], which is also where user annotations such as {"ac_region_id": 0} live.
This was unsafe because some traceback save/restore paths only shallow-copy metadata. In particular, set_current_meta(node) does current_meta = node.meta.copy(), so current_meta["custom"] aliases node.meta["custom"]. A nested annotate({"autograd_backward": True}) then mutates that shared dict in place. When the nested scope exits, current_meta may be restored, but the source node metadata can stay polluted, which is how forward FlexAttention nodes ended up leaking autograd_backward.
Fix this by treating autograd_backward as internal tracing state instead of user custom metadata. Keep a dedicated current_meta["autograd_backward"] flag during tracing, stamp node.meta["autograd_backward"] on newly created nodes using setup_stacktrace_preservation_hook.
With this tagging, we can reorder backward nodes during partitioner, so regional_inductor now turns off remat pass. This is fine because, remat pass is intended to be consumed only after:
1. you torch.compile whole training step
2. you make_fx d whole training step
In relevant use cases, regional inductor will happen after SAC decision has been applied to the final graph.
Add non-strict tracing and FlexAttention regressions covering the leak.
Authored with Claude.
Pull Request resolved: pytorch#180251
Approved by: https://github.com/Skylion007, https://github.com/aorenste
)" This reverts commit 8c49f56. Reverted pytorch#180251 on behalf of https://github.com/jeanschmidt due to sorry, need to revert in order to revert pytorch#179731, feel free to resolve conflicts and merge into main after that ([comment](pytorch#180251 (comment)))
The old implementation tagged backward nodes by running fx_traceback.annotate({"autograd_backward": True}) around traced autograd.grad. That stored autograd_backward in current_meta["custom"], which is also where user annotations such as {"ac_region_id": 0} live.
This was unsafe because some traceback save/restore paths only shallow-copy metadata. In particular, set_current_meta(node) does current_meta = node.meta.copy(), so current_meta["custom"] aliases node.meta["custom"]. A nested annotate({"autograd_backward": True}) then mutates that shared dict in place. When the nested scope exits, current_meta may be restored, but the source node metadata can stay polluted, which is how forward FlexAttention nodes ended up leaking autograd_backward.
Fix this by treating autograd_backward as internal tracing state instead of user custom metadata. Keep a dedicated current_meta["autograd_backward"] flag during tracing, stamp node.meta["autograd_backward"] on newly created nodes using setup_stacktrace_preservation_hook.
With this tagging, we can reorder backward nodes during partitioner, so regional_inductor now turns off remat pass. This is fine because, remat pass is intended to be consumed only after:
1. you torch.compile whole training step
2. you make_fx d whole training step
In relevant use cases, regional inductor will happen after SAC decision has been applied to the final graph.
Add non-strict tracing and FlexAttention regressions covering the leak.
Authored with Claude.
Pull Request resolved: pytorch#180251
Approved by: https://github.com/Skylion007, https://github.com/aorenste
The old implementation tagged backward nodes by running fx_traceback.annotate({"autograd_backward": True}) around traced autograd.grad. That stored autograd_backward in current_meta["custom"], which is also where user annotations such as {"ac_region_id": 0} live.
This was unsafe because some traceback save/restore paths only shallow-copy metadata. In particular, set_current_meta(node) does current_meta = node.meta.copy(), so current_meta["custom"] aliases node.meta["custom"]. A nested annotate({"autograd_backward": True}) then mutates that shared dict in place. When the nested scope exits, current_meta may be restored, but the source node metadata can stay polluted, which is how forward FlexAttention nodes ended up leaking autograd_backward.
Fix this by treating autograd_backward as internal tracing state instead of user custom metadata. Keep a dedicated current_meta["autograd_backward"] flag during tracing, stamp node.meta["autograd_backward"] on newly created nodes using setup_stacktrace_preservation_hook.
With this tagging, we can reorder backward nodes during partitioner, so regional_inductor now turns off remat pass. This is fine because, remat pass is intended to be consumed only after:
1. you torch.compile whole training step
2. you make_fx d whole training step
In relevant use cases, regional inductor will happen after SAC decision has been applied to the final graph.
Add non-strict tracing and FlexAttention regressions covering the leak.
Authored with Claude.
Pull Request resolved: pytorch#180251
Approved by: https://github.com/Skylion007, https://github.com/aorenste
)" This reverts commit 8c49f56. Reverted pytorch#180251 on behalf of https://github.com/jeanschmidt due to sorry, need to revert in order to revert pytorch#179731, feel free to resolve conflicts and merge into main after that ([comment](pytorch#180251 (comment)))
The old implementation tagged backward nodes by running fx_traceback.annotate({"autograd_backward": True}) around traced autograd.grad. That stored autograd_backward in current_meta["custom"], which is also where user annotations such as {"ac_region_id": 0} live.
This was unsafe because some traceback save/restore paths only shallow-copy metadata. In particular, set_current_meta(node) does current_meta = node.meta.copy(), so current_meta["custom"] aliases node.meta["custom"]. A nested annotate({"autograd_backward": True}) then mutates that shared dict in place. When the nested scope exits, current_meta may be restored, but the source node metadata can stay polluted, which is how forward FlexAttention nodes ended up leaking autograd_backward.
Fix this by treating autograd_backward as internal tracing state instead of user custom metadata. Keep a dedicated current_meta["autograd_backward"] flag during tracing, stamp node.meta["autograd_backward"] on newly created nodes using setup_stacktrace_preservation_hook.
With this tagging, we can reorder backward nodes during partitioner, so regional_inductor now turns off remat pass. This is fine because, remat pass is intended to be consumed only after:
1. you torch.compile whole training step
2. you make_fx d whole training step
In relevant use cases, regional inductor will happen after SAC decision has been applied to the final graph.
Add non-strict tracing and FlexAttention regressions covering the leak.
Authored with Claude.
Pull Request resolved: pytorch#180251
Approved by: https://github.com/Skylion007, https://github.com/aorenste
Stack from ghstack (oldest at bottom):
The old implementation tagged backward nodes by running fx_traceback.annotate({"autograd_backward": True}) around traced autograd.grad. That stored autograd_backward in current_meta["custom"], which is also where user annotations such as {"ac_region_id": 0} live.
This was unsafe because some traceback save/restore paths only shallow-copy metadata. In particular, set_current_meta(node) does current_meta = node.meta.copy(), so current_meta["custom"] aliases node.meta["custom"]. A nested annotate({"autograd_backward": True}) then mutates that shared dict in place. When the nested scope exits, current_meta may be restored, but the source node metadata can stay polluted, which is how forward FlexAttention nodes ended up leaking autograd_backward.
Fix this by treating autograd_backward as internal tracing state instead of user custom metadata. Keep a dedicated current_meta["autograd_backward"] flag during tracing, stamp node.meta["autograd_backward"] on newly created nodes using setup_stacktrace_preservation_hook.
With this tagging, we can reorder backward nodes during partitioner, so regional_inductor now turns off remat pass. This is fine because, remat pass is intended to be consumed only after:
In relevant use cases, regional inductor will happen after SAC decision has been applied to the final graph.
Add non-strict tracing and FlexAttention regressions covering the leak.
Authored with Claude.
cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @kadeng @chauhang @amjames @Lucaskabela @jataylo @azahed98