Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32.1k
Closed
Description
Bug report
Bug description:
I discovered this bug while working onpytorch/pytorch#124302.
It looks like callinggetattr_static
is causing an object to be reference leaked:
importgcfrominspectimportgetattr_staticimportweakrefclassC1:passdefmain():obj=C1()weakref.finalize(obj,lambda:print("obj deleted!"))classC2:def__init__(self):self.obj=objc2=C2()getattr_static(c2,"bad",None)print("done main!")main()gc.collect()print("done!")
Output:
done main!done!obj deleted!
If I comment out thegetattr_static
line, the output is as expected:
done main!obj deleted!done!
It looks like this PR#104267 indirectly cached calls togetattr_static
, which is resulting in reference leaks. Perhaps this cache needs to use weak references?
Original PyTorch code for reference (torch.compile
callsgetattr_static
onmod
at some point):
importtorchimportgcimportsysimportweakreffrominspectimportgetattr_staticdefdbg(o):refs=gc.get_referrers(o)print(len(refs),sys.getrefcount(o))returnrefsgm_list= []defbackend(gm,_):gm_list.append(weakref.ref(gm,lambda_:print("gm deleted")))# breakpoint()returngmdefmain():param=torch.nn.Parameter(torch.randn(5,5))classMod(torch.nn.Module):def__init__(self):super().__init__()self.param=paramdefforward(self,x):returnself.param*xmod=Mod()ref=weakref.ref(param,lambda_:print("obj deleted"))opt_mod=torch.compile(mod,backend=backend)print(opt_mod(torch.randn(5,5)))returnrefref=main()gc.collect()print("done!")print(ref)
CPython versions tested on:
3.12
Operating systems tested on:
Linux