New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in new autograd backward (with LSTM Cell) #1450

Closed
ChenRocks opened this Issue May 3, 2017 · 4 comments

Comments

Projects
None yet
4 participants
@ChenRocks

ChenRocks commented May 3, 2017

This is how I implement the decoder of a sequence to sequence model

import torch
from torch import nn
from torch.autograd import Variable
from torch.nn import functional as F

def decoder(input_, embedding, lstm, projection, states):
    """ unroll the LSTM Cell, returns the flattened logits"""
    emb = embedding(input_.t())
    hs = []
    for i in range(input_.size(1)):
        h, c = lstm(emb[i], states)
        hs.append(h)
        states = (h, c)
    lstm_out = torch.stack(hs, dim=0)
    logit = projection(lstm_out.contiguous().view(-1, lstm.hidden_size))
    return logit

embedding = nn.Embedding(4, 64, padding_idx=0).cuda()
lstm = nn.LSTMCell(64, 64).cuda()
projection = nn.Linear(64, 4).cuda()

input_ = Variable(torch.LongTensor([[1, 2, 3], [3, 2, 1]])).cuda()
states = (Variable(torch.zeros(2, 64)).cuda(), Variable(torch.zeros(2, 64)).cuda())
target = Variable(torch.LongTensor([[3, 2, 1], [2, 3, 1]])).cuda()

logit = decoder(input_, embedding, lstm, projection, states)
loss = F.cross_entropy(logit, target.t().contiguous().view(-1))
loss.backward()  #  RuntimeError: No grad accumulator for a saved leaf!

I'm not sure about the new autograd mechanics but this worked in the previous version.
If I didn't make the unrolling codes a function it will work. It will also work with CPU.
I compiled from source (699755e) with Python 2.7, CUDA 8.0 and Cudnn 6

@ChenRocks ChenRocks referenced this issue May 3, 2017

Merged

Autograd refactor #1016

7 of 7 tasks complete
@jekbradbury

This comment has been minimized.

Contributor

jekbradbury commented May 3, 2017

Thanks for the minimal reproduction! It broke OpenNMT and a big internal model but we weren't looking forward to isolating what parts of those had the issue.

@apaszke

This comment has been minimized.

Member

apaszke commented May 3, 2017

That's great, I'll take a look today. Thanks!

@apaszke

This comment has been minimized.

Member

apaszke commented May 3, 2017

I've pushed a fix to #1454.

@soumith soumith closed this in #1454 May 3, 2017

@ROZBEH

This comment has been minimized.

ROZBEH commented Jul 28, 2017

@ChenRocks How does the inference work in this case? I mean, once you train it how do you test it in the test phase?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment