KEMBAR78
load_state_dict unexpectedly does not load Tensor to buffers that currently have None value · Issue #8104 · pytorch/pytorch · GitHub
Skip to content

load_state_dict unexpectedly does not load Tensor to buffers that currently have None value #8104

@pimdh

Description

@pimdh

Issue description

I have some model variations that learn a certain value, while others keep this value fixed. For this purpose I'd like to have a None-able buffer that stores the fixed value and have this value included in the state dict for serialization. The _load_from_state_dict function in the Module class however doesn't overwrite buffers and parameters that currently have the None value. I didn't expect this, leading to a bug in my code.

Although this was unexpected behaviour for me, I understand that this could be desired. In that case perhaps it can be included in the docs.

Code example

import torch
import torch.nn as nn


class X(nn.Module):
    def __init__(self, buf):
        super().__init__()
        self.register_buffer('buffer', buf)


x = X(torch.tensor(1.))
state = x.state_dict()

y = X(torch.tensor(2.))
y.load_state_dict(state)  # Works fine

z = X(None)
z.load_state_dict(state)

# Raises:
# RuntimeError: Error(s) in loading state_dict for X:
# 	Unexpected key(s) in state_dict: "buffer".

System Info

PyTorch version: 0.4.0
Is debug build: No
CUDA used to build PyTorch: 9.1.85

OS: Ubuntu 18.04 LTS
GCC version: (Ubuntu 7.3.0-16ubuntu3) 7.3.0
CMake version: version 3.10.2

Python version: 3.6
Is CUDA available: Yes
CUDA runtime version: Could not collect
GPU models and configuration: GPU 0: GeForce GTX 1080 Ti
Nvidia driver version: 390.48
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.7.1.3

Versions of relevant libraries:
[pip] numpy (1.14.3)
[pip] torch (0.4.0)
[pip] torchvision (0.2.1)
[conda] Could not collect

cc @ezyang @ssnl @gchanan

Metadata

Metadata

Assignees

No one assigned

    Labels

    module: bc-breakingRelated to a BC-breaking changemodule: docsRelated to our documentation, both in docs/ and docblocksmodule: nnRelated to torch.nntriagedThis 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