-
Notifications
You must be signed in to change notification settings - Fork 24.2k
Optimizer classes not dill
picklable after using torch.compile
#126154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
dill
picklable after using torch.compile
Cross-posted to |
These appearances of pytorch/torch/csrc/dynamo/eval_frame.c Line 140 in dccb5cf
pytorch/torch/csrc/dynamo/eval_frame.c Line 700 in dccb5cf
are the only 2 in all of this repo, and there is no such package as This would seem to explain this: import torch._C._dynamo
print(torch._C._dynamo.eval_frame._CacheEntry.__module__) # torch._C.dynamo.eval_frame Should these be changed to EDIT: Actually, |
Seems to make some progress, leading to a new error: import dill
import torch._C._dynamo
print(torch._C._dynamo.eval_frame._CacheEntry.__module__) # torch._C._dynamo.eval_frame
torch._C._dynamo.eval_frame._CacheEntry.__module__ = "eval_frame"
print(torch._C._dynamo.eval_frame._CacheEntry.__module__) # eval_frame
with open("fake.txt", "wb") as f:
dill.dump(torch._C._dynamo.eval_frame._CacheEntry, f) # PicklingError: Can't pickle <class 'pybind11_builtins.pybind11_type'>: it's not found as pybind11_builtins.pybind11_type |
I patched
class _Pickler:
...
def save_global(self, obj, name=None):
write = self.write
memo = self.memo
if name is None:
name = getattr(obj, '__qualname__', None)
if name is None:
name = obj.__name__
module_name = whichmodule(obj, name)
try:
try:
__import__(module_name, level=0)
module = sys.modules[module_name]
obj2, parent = _getattribute(module, name)
except ModuleNotFoundError:
parent_name, module_name = module_name.rsplit(".", 1)
__import__(parent_name, level=0)
parent = sys.modules[parent_name]
module, parent = _getattribute(parent, module_name)
obj2, module = _getattribute(module, name)
except (ImportError, KeyError, AttributeError):
raise PicklingError(
"Can't pickle %r: it's not found as %s.%s" %
(obj, module_name, name)) from None
else:
if obj2 is not obj:
raise PicklingError(
"Can't pickle %r: it's not the same object as %s.%s" %
(obj, module_name, name))
... And continued on to get:
while trying to serialize Which I have seen elsewhere: |
@ezyang this seems like the same exact problem that was solved by: I am sure we could hack this the same way but I wonder if I'm onto something with my patch to |
Well, it's generally considered quite naughty to monkeypatch stuff in Python standard library as a library. So yeah, probably better to stop exportin gthings as static methods... |
I meant as a proposed change to the standard library, not as a monkey patch. This is basically the situation we are in: import pickle
import types
eval_frame = types.ModuleType("eval_frame")
def reset_code():
...
reset_code.__module__ = "__main__.eval_frame"
eval_frame.reset_code = reset_code
with open("reset_code.pt", "wb") as f:
pickle._Pickler(f).dump(eval_frame.reset_code) # _pickle.PicklingError: Can't pickle <function reset_code at 0x7f2383e651f0>: import of module '__main__.eval_frame' failed
import torch
torch.sys.path # is actually just sys.path
import torch.sys # ModuleNotFoundError: No module named 'torch.sys' But unlike I will try to add this into the discussion that you guys have already had on cpython. |
I refined my patch and made the case for the change here: |
…age C-modules There have been recurring issue with PyModule_Create modules in PyTorch; when trying to serialize attributes of these C-modules, pickle fails to import the C-module because it is not a package This is the current issue that brought this to my attention: pytorch/pytorch#126154 The existing hack to this issue has been to insert the C-module into sys.modules in order to enable pickle to find them: https://github.com/pytorch/pytorch/pull/38136/files#diff-d7e90d0f94b43db763b44fba679a5c1b4cabe3668aaf34f2aee07de8e2d1b2faR524-R528 Instead of relying on this hack, we can change `pickle`'s approach to loading, which is currently equivalent to `import package.c_module`; instead, we could do `from package import c_module`, which 1) does not care if `c_module` is a package or not 2) is fully backward compatible with the previous approach and 3) slots in nicely to the `fromlist` parameter of `__import__`, which we are already using to load modules in `pickle`
There have been recurring issue with PyModule_Create modules in PyTorch; when trying to serialize attributes of these C-modules, pickle fails to import the C-module because it is not a package This is the current issue that brought this to my attention: pytorch/pytorch#126154 The existing hack to this issue has been to insert the C-module into sys.modules in order to enable pickle to find them: https://github.com/pytorch/pytorch/pull/38136/files#diff-d7e90d0f94b43db763b44fba679a5c1b4cabe3668aaf34f2aee07de8e2d1b2faR524-R528 Instead of relying on this hack, we can change `pickle`'s approach to loading, which is currently equivalent to `import package.c_module`; instead, we could do `from package import c_module`, which 1) does not care if `c_module` is a package or not 2) is fully backward compatible with the previous approach and 3) slots in nicely to the `fromlist` parameter of `__import__`, which we are already using to load modules in `pickle`
There have been recurring issue with PyModule_Create modules in PyTorch; when trying to serialize attributes of these C-modules, pickle fails to import the C-module because it is not a package This is the current issue that brought this to my attention: pytorch/pytorch#126154 The existing hack to this issue has been to insert the C-module into sys.modules in order to enable pickle to find them: https://github.com/pytorch/pytorch/pull/38136/files#diff-d7e90d0f94b43db763b44fba679a5c1b4cabe3668aaf34f2aee07de8e2d1b2faR524-R528 Instead of relying on this hack, we can change `pickle`'s approach to loading, which is currently equivalent to `import package.c_module`; instead, we could do `from package import c_module`, which 1) does not care if `c_module` is a package or not 2) is fully backward compatible with the previous approach and 3) slots in nicely to the `fromlist` parameter of `__import__`, which we are already using to load modules in `pickle`
OK good luck :) |
🐛 Describe the bug
To get started:
$ pip install torch dill
After you use
torch.compile
,Optimizer
subclasses stop beingdill
picklable:This seems to be partly the reason:
pytorch/torch/_dynamo/eval_frame.py
Line 1506 in 8bf9e99
I think it boils down to
torch._C._dynamo.eval_frame
not being importable as a package, but as an attribute oftorch._C._dynamo
(like an imported module):eval_frame
is defined here:pytorch/torch/csrc/dynamo/init.cpp
Line 58 in 556e4ec
Is there some change to
dill
or C++-generatedmodule
s that solve this?Versions
cc @mruberry @mikaylagawarecki @msaroufim @ezyang @bdhirsh @anijain2305 @chauhang
The text was updated successfully, but these errors were encountered: