One of the variables needed for gradient computation has been modified by an inplace operation;

Hi,
If I setcuda = True, it will cause errorone of the variables needed for gradient computation has been modified by an inplace operation. but cuda = False has no error. It really takes me a lot of time to find out the problem. Do you have any ideas?

class test_model(nn.Module):
    def __init__(self,
                 in_channel=3,
                 num_class=60,
                 num_joint=25,
                 hidden_size =100,
                 window_size=64,
                 ):
        super(test_model, self).__init__()
        self.lstm = nn.LSTM(input_size=in_channel * num_joint,
                             hidden_size=hidden_size, num_layers=3,
                             batch_first=True)
        self.fc = nn.Linear(hidden_size*window_size, num_class)


    def forward(self, x):
        N, C, T, V, M = x.size()
        logits = []
        for i in range(2):
            out = x[:, :, :, :, i].permute(0, 2, 1, 3).contiguous().view(N,T,C*V)
            self.lstm.flatten_parameters()
            out,_ = self.lstm(out)
            logits.append(out)

        # out = torch.max(logits[0], logits[1])
        out =  logits[1]+logits[0]
        out = out.contiguous().view(out.size(0), -1)
        out = self.fc(out)
        return out


if __name__ == '__main__':
    # cronstruct a dataset
    data = []
    label = []
    epoches = 10
    cuda = False
    # cuda = True
    for i in range(epoches):
        data.append(torch.randn((2, 3, 64, 25, 2)))
        label.append(torch.randn((2, 60)))

    # model
    model = test_model()
    if cuda:
        model= model.cuda()
    optimizer = optim.Adam(model.parameters(), lr=0.001)

    # cre = nn.CrossEntropyLoss()
    cre = nn.MSELoss()

    for i in range(epoches):
        if cuda:
            x = Variable(data[i]).cuda()
            y = Variable(label[i]).cuda()
        else:
            x = Variable(data[i])
            y = Variable(label[i])

        out_put = model(x)
        loss = cre(out_put, y)
        # print(loss)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
    print('end')

I also find out , this will only cause error in the new version of pytorch(0.4.0), the previous version have no error. but I still don’t know why, can anybody do me a help?

I have same problem here, only on pytorch 0.4.0 and set device = torch.device(‘cuda’)

Move the self.flatten_parameters() call in def forward to def __init__. It has no effect on CPU, but on CUDA it will modify the LSTM’s parameters part way through the computation which would break backwards (leading to the exception).

Thanks for the reply! It worked!