Skip to content

torch.save doesn't support cloudpickle #38098

@twoertwein

Description

@twoertwein

🐛 Bug

Objects, such as lambda functions, that are supported by cloudpickle cannot be pickled with torch.save even when the user asks to use cloudpickle. torch.save calls pickle_module.Pickler, in case of cloudpickle this is the Pickler from pickle [1]!

This becomes more important when tensors cannot be pickled anymore [2].

[1] https://github.com/cloudpipe/cloudpickle/blob/70dc1df5e2ffd3f842bc7b0c2634f5c05d8333f7/cloudpickle/cloudpickle.py#L65
[2]

warnings.warn("pickle support for Storage will be removed in 1.5. Use `torch.save` instead", FutureWarning)

To Reproduce

import cloudpickle
import torch

data = {"foo": lambda: True}
path = "foo.pickle"

with open(path, mode="wb") as file:
    cloudpickle.dump(data, file)  # works

torch.save(data, path, pickle_module=cloudpickle)  # fails

Environment

$ python collect_env.py
Collecting environment information...
PyTorch version: 1.5.0
Is debug build: No
CUDA used to build PyTorch: 10.2

OS: Ubuntu 16.04.6 LTS
GCC version: (Ubuntu 9.2.1-17ubuntu1~16.04) 9.2.1 20191102
CMake version: version 3.15.1

Python version: 3.8
Is CUDA available: Yes
CUDA runtime version: 10.2.89
GPU models and configuration: GPU 0: GeForce RTX 2080 Ti
Nvidia driver version: 440.64
cuDNN version: /usr0/local/cuda-9.0/lib64/libcudnn.so.7.0.5

Versions of relevant libraries:
[pip] numpy==1.18.4
[pip] torch==1.5.0
[pip] torchvision==0.6.0a0+82fd1c8
[conda] blas                      2.15                        mkl    conda-forge
[conda] cudatoolkit               10.2.89              hfd86e86_1  
[conda] hamiltorch                0.3.1.dev3               pypi_0    pypi
[conda] libblas                   3.8.0                    15_mkl    conda-forge
[conda] libcblas                  3.8.0                    15_mkl    conda-forge
[conda] liblapack                 3.8.0                    15_mkl    conda-forge
[conda] liblapacke                3.8.0                    15_mkl    conda-forge
[conda] mkl                       2020.1                      217    conda-forge
[conda] numpy                     1.18.4           py38h8854b6b_0    conda-forge
[conda] pytorch                   1.5.0           py3.8_cuda10.2.89_cudnn7.6.5_0    pytorch
[conda] torchvision               0.6.0                py38_cu102    pytorch

Metadata

Metadata

Assignees

No one assigned

    Labels

    module: pickleProblems related to pickling of PyTorch objectsmodule: serializationIssues related to serialization (e.g., via pickle, or otherwise) of PyTorch objectstriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions