pytorch - 💡(How to fix) Fix Dynamo is leaking internal state during optimizer tracing [1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
pytorch/pytorch#182706Fetched 2026-05-07 03:30:34
View on GitHub
Comments
1
Participants
2
Timeline
75
Reactions
0
Assignees
Timeline (top)
mentioned ×34subscribed ×34labeled ×5assigned ×1

Code Example

import torch
from copy import deepcopy

m = torch.nn.Linear(2, 2)
opt = torch.optim.AdamW(m.parameters(), lr=0.01)
m(torch.randn(2, 2)).sum().backward()
opt.step()

ORIGINAL_LR = opt.param_groups[0]["lr"]


def fn():
    # 1. Mutate: registers the param_group VT for SideEffects flush.
    opt.param_groups[0]["lr"] = 5.0
    # 2. Graph break: dynamo flushes tracked VT mutations onto the live dict.
    sd = deepcopy(opt.state_dict())
    # 3. Undo: restore original. Visible to eager Python from this point on.
    opt.param_groups[0]["lr"] = ORIGINAL_LR
    return sd


print(f"Before: capturable = {[g['capturable'] for g in opt.param_groups]}")
print(f"        lr         = {[g['lr'] for g in opt.param_groups]}")
torch._dynamo.optimize("eager_noexcept", nopython=False)(fn)()
print(f"After:  capturable = {[g['capturable'] for g in opt.param_groups]}")
print(f"        lr         = {[g['lr'] for g in opt.param_groups]}")

---

Before: capturable = [False]
        lr         = [0.01]
After:  capturable = [True]
        lr         = [0.01]
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

import torch
from copy import deepcopy

m = torch.nn.Linear(2, 2)
opt = torch.optim.AdamW(m.parameters(), lr=0.01)
m(torch.randn(2, 2)).sum().backward()
opt.step()

ORIGINAL_LR = opt.param_groups[0]["lr"]


def fn():
    # 1. Mutate: registers the param_group VT for SideEffects flush.
    opt.param_groups[0]["lr"] = 5.0
    # 2. Graph break: dynamo flushes tracked VT mutations onto the live dict.
    sd = deepcopy(opt.state_dict())
    # 3. Undo: restore original. Visible to eager Python from this point on.
    opt.param_groups[0]["lr"] = ORIGINAL_LR
    return sd


print(f"Before: capturable = {[g['capturable'] for g in opt.param_groups]}")
print(f"        lr         = {[g['lr'] for g in opt.param_groups]}")
torch._dynamo.optimize("eager_noexcept", nopython=False)(fn)()
print(f"After:  capturable = {[g['capturable'] for g in opt.param_groups]}")
print(f"        lr         = {[g['lr'] for g in opt.param_groups]}")

This outputs:

Before: capturable = [False]
        lr         = [0.01]
After:  capturable = [True]
        lr         = [0.01]

Versions

main

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

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

pytorch - 💡(How to fix) Fix Dynamo is leaking internal state during optimizer tracing [1 comments, 2 participants]