Which is Inplace

When I run my code below , I got RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation; But where is the inplace op.Thank you!

    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[0]+ logits[1]
        #
        out = out.contiguous().view(out.size(0), -1)
        out = self.fc3(out)
        return out

Can you provide an the actual error. Or provide a self contained small script with which we can reproduce the error.

@Gurkirt sorry for a late replay. I have simplified my original code, struggled to find out the problem , but I don’t known why. If I set cuda = True, it will cause one 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 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')

The problem has been solved, please refer here