Skip to content

Accessing state_dict() of models trained with previous pytorch version #42242

@OMalenfantThuot

Description

@OMalenfantThuot

🐛 Bug

Since updating to version 1.6, when trying to access model.state_dict() of a older pytorch trained models (where model is a subclass of torch.nn.module), I'm getting a ModuleAttributeError.

To Reproduce

Steps to reproduce the behavior:

torch.load("path/to/model", map_location='cpu')
print(model.state_dict())

where the model was trained with an older version of pytorch.

The output is

  File "/home/oliviermt/miniconda3/envs/projections/lib/python3.7/site-packages/torch/nn/modules/module.py", line 900, in state_dict
    module.state_dict(destination, prefix + name + '.', keep_vars=keep_vars)
  File "/home/oliviermt/miniconda3/envs/projections/lib/python3.7/site-packages/torch/nn/modules/module.py", line 900, in state_dict
    module.state_dict(destination, prefix + name + '.', keep_vars=keep_vars)
  File "/home/oliviermt/miniconda3/envs/projections/lib/python3.7/site-packages/torch/nn/modules/module.py", line 897, in state_dict
    self._save_to_state_dict(destination, prefix, keep_vars)
  File "/home/oliviermt/miniconda3/envs/projections/lib/python3.7/site-packages/torch/nn/modules/module.py", line 860, in _save_to_state_dict
    if buf is not None and name not in self._non_persistent_buffers_set:
  File "/home/oliviermt/miniconda3/envs/projections/lib/python3.7/site-packages/torch/nn/modules/module.py", line 772, in __getattr__
    type(self).__name__, name))
torch.nn.modules.module.ModuleAttributeError: 'GaussianSmearing' object has no attribute '_non_persistent_buffers_set'

I was able to make it work by adding the following lines after the the torch.load:

for mod in model.modules():
     if not hasattr(mod, "_non_persistent_buffers_set"):
            mod._non_persistent_buffers_set = set()

If it's expected behaviour, then ignore this issue, but I would guess it's not.
Thanks a lot!

Expected behavior

Not needing to add those 3 lines.

Environment

PyTorch version: 1.6.0
Is debug build: No
CUDA used to build PyTorch: 10.2

OS: Ubuntu 16.04.6 LTS
GCC version: (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609
CMake version: version 3.5.1

Python version: 3.7
Is CUDA available: No
CUDA runtime version: No CUDA
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA

Versions of relevant libraries:
[pip3] numpy==1.19.1
[pip3] torch==1.6.0
[pip3] torchvision==0.7.0
[conda] numpy 1.19.1 pypi_0 pypi
[conda] torch 1.6.0 pypi_0 pypi
[conda] torchvision 0.7.0 pypi_0 pypi

Additional context

cc @ezyang @gchanan @zou3519 @albanD @mruberry

Metadata

Metadata

Assignees

No one assigned

    Labels

    high prioritymodule: nnRelated to torch.nntriage reviewtriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions