Repository: pytorch
Issue: torch.load with dill is unable to unserialize from buffer
Labels: module: serialization,triaged

## 🐛 Bug

I'm using PyTorch `1.8.0` with Dill `0.3.3` and Python `3.6.9` on Linux/Ubuntu.

I serialized a function `foo` using the command `torch.save(foo, buf, pickle_module=dill)`, where `buf` is a `io.BytesIO` object. Then, I wanted to unserialize it using `torch.load(buf, pickle_module=dill)` but got the following error :

> EOFError: Ran out of input
 
I don't get any errors when I'm using `dill.dumps` and `dill.loads`, or if I pass a path to a file to `torch.save` and `torch.load`.

## To Reproduce

Steps to reproduce the behavior:
1. Import torch and dill
    ```python
    import io
    import torch
    import dill
    ```
1. Declare the function:
    ```python
    def foo():
        return 42
    ```
1. Create the buffer:
    ```python
    buf = io.BytesIO()
    ```
1. Serialize the function using torch
    ```python
    torch.save(foo, buf, pickle_module=dill)
    ```
1. Unserialize the function using torch
    ```python
    foo2 = torch.load(buf, pickle_module=dill)
    ```

```python
EOFError                                  Traceback (most recent call last)
[…]
----> 1 foo2 = torch.load(buf, pickle_module=dill)

.../env/lib/python3.6/site-packages/torch/serialization.py in load(f, map_location, pickle_module, **pickle_load_args)
    583                     return torch.jit.load(opened_file)
    584                 return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args)
--> 585         return _legacy_load(opened_file, map_location, pickle_module, **pickle_load_args)
    586 
    587 

.../env/lib/python3.6/site-packages/torch/serialization.py in _legacy_load(f, map_location, pickle_module, **pickle_load_args)
    753             "functionality.".format(type(f)))
    754 
--> 755     magic_number = pickle_module.load(f, **pickle_load_args)
    756     if magic_number != MAGIC_NUMBER:
    757         raise RuntimeError("Invalid magic number; corrupt file?")

.../env/lib/python3.6/site-packages/dill/_dill.py in load(file, ignore, **kwds)
    276 def load(file, ignore=None, **kwds):
    277     """unpickle an object from a file"""
--> 278     return Unpickler(file, ignore=ignore, **kwds).load()
    279 
    280 def loads(str, ignore=None, **kwds):

.../env/lib/python3.6/site-packages/dill/_dill.py in load(self)
    479 
    480     def load(self): #NOTE: if settings change, need to update attributes
--> 481         obj = StockUnpickler.load(self)
    482         if type(obj).__module__ == getattr(_main_module, '__name__', '__main__'):
    483             if not self._ignore:

EOFError: Ran out of input
```

## Expected behavior

The function is successfully unserialized.

## Environment

PyTorch version: 1.8.0+cu111
Is debug build: False
CUDA used to build PyTorch: 11.1
ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04.2 LTS (x86_64)
GCC version: (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Clang version: Could not collect
CMake version: version 3.16.3

Python version: 3.6 (64-bit runtime)
Is CUDA available: True
CUDA runtime version: Could not collect
GPU models and configuration: GPU 0: NVIDIA GeForce RTX 3060
Nvidia driver version: 465.19.01
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.8.1.1
/usr/lib/x86_64-linux-gnu/libcudnn_adv_infer.so.8.1.1
/usr/lib/x86_64-linux-gnu/libcudnn_adv_train.so.8.1.1
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_infer.so.8.1.1
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_train.so.8.1.1
/usr/lib/x86_64-linux-gnu/libcudnn_ops_infer.so.8.1.1
/usr/lib/x86_64-linux-gnu/libcudnn_ops_train.so.8.1.1
HIP runtime version: N/A
MIOpen runtime version: N/A

Versions of relevant libraries:
[pip3] numpy==1.19.5
[pip3] torch==1.8.0+cu111
[pip3] torchvision==0.9.0+cu111
[conda] Could not collect

## Additional context



cc @mruberry