-
Notifications
You must be signed in to change notification settings - Fork 173
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
cloudpickle pickle 'CudnnModule' object #405
Comments
Thanks for the reproducer. I'll look into it. This script should not error out. |
Any updates on this @pierreglaser? |
@pierreglaser , it will really help to at least get some clarity about the root cause to understand if this is something that should be fixed in cloudpickle or in pytorch. |
I have looked at this in more detail. I'm going to lay out the full story and hopefully we'll decide on a fix fast.
(cloudpickle_py37) ~/repos/cloudpickle (master)❯_ python
Python 3.7.3 (default, Apr 3 2019, 19:16:38)
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> type(os)
<class 'module'>
>>> import pickle
>>> pickle.dumps(os)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't pickle module objects
>>> import os
>>> import cloudpickle
>>> type(os)
<class 'module'>
>>> cloudpickle.dumps(os)
b'\x80\x04\x952\x00\x00\x00\x00\x00\x00\x00\x8c\x17cloudpickle.cloudpickle\x94\x8c\tsubimport\x94\x93\x94\x8c\x02os\x94\x85\x94R\x94.'
>>> import torch.backends.cudnn as c
>>> type(c)
<class 'torch.backends.cudnn.CudnnModule'>
>>> cloudpickle.dumps(c)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/pierreglaser/repos/cloudpickle/cloudpickle/cloudpickle_fast.py", line 102, in dumps
cp.dump(obj)
File "/home/pierreglaser/repos/cloudpickle/cloudpickle/cloudpickle_fast.py", line 563, in dump
return Pickler.dump(self, obj)
File "/usr/lib/python3.7/pickle.py", line 437, in dump
self.save(obj)
File "/usr/lib/python3.7/pickle.py", line 524, in save
rv = reduce(self.proto)
TypeError: can't pickle CudnnModule objects
If Here is a POC modification of the def _subimport(name):
__import__(name)
return sys.modules[name]
class CudnnModule(PropModule):
def __init__(self, m, name):
super(CudnnModule, self).__init__(m, name)
def __reduce__(self):
return _subimport, (self.__name__,)
enabled = ContextProp(torch._C._get_cudnn_enabled, torch._C._set_cudnn_enabled)
deterministic = ContextProp(torch._C._get_cudnn_deterministic, torch._C._set_cudnn_deterministic)
benchmark = ContextProp(torch._C._get_cudnn_benchmark, torch._C._set_cudnn_benchmark)
allow_tf32 = ContextProp(torch._C._get_cudnn_allow_tf32, torch._C._set_cudnn_allow_tf32) Notice the new (pytorch_py37) ~/repos/cloudpickle (master)❯_ python
Python 3.7.9 (default, Aug 18 2020, 06:22:45)
[GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch.backends.cudnn as c
>>> import pickle
>>> import cloudpickle
>>> pickle.loads(pickle.dumps(c)) is c
True
>>> cloudpickle.loads(cloudpickle.dumps(c)) is c
True |
Thanks for digging and for offering the solutions. I agree that it makes more sense for this to be fixed in PyTorch. |
I think this is related to #397 , but in that case the reproducer works with pickle and not with cloudpickle. The difference there is that the custom import pickle
import sys
from types import ModuleType
class Facade(ModuleType):
def __init__(self, name):
super().__init__(name)
self.foo = 42
sys.modules['my_module'] = Facade('my_module')
# Pickle a function which uses the module with the facade
import my_module
def foo():
return my_module.foo == 42
print(pickle.loads(pickle.dumps(foo)))
print(pickle.loads(pickle.dumps(foo()))) And outputs the following (Python 3.8.7)
While substituting pickle with cloudpickle in the above snippet gives
I confirm that adding the custom |
I was spawning parallel processes using Joblib for training distributed PyTorch code, and Joblib uses cloudpickle to do its pickling. I've narrowed down the problem to cloudpickle, as demonstrated in the minimal repro below:
The above script works when I use Python's pickle library instead. Is there a way to fix this?
The text was updated successfully, but these errors were encountered: