Skip to content
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

Expected object of scalar type Float but got scalar type Half with opt_level 02,03 #667

Open
realiti4 opened this issue Dec 27, 2019 · 2 comments

Comments

@realiti4
Copy link

realiti4 commented Dec 27, 2019

pytorch: 1.3.1
cuda: 10.1
os: windows 10

I'm getting this error on my encoder-decoder with attention network if I use opt_level 02 or 03. 01 is working fine :

cell.repeat(self.T -1, 1, 1).permute(1, 0, 2), input_encoded), dim=2)
RuntimeError: Expected object of scalar type Float but got scalar type Half for sequence element 2 in sequence argument at position #1 'tensors'

this is what I'm using:

[encoder, decoder], [encoder_optimizer, decoder_optimizer] = amp.initialize([encoder, decoder],   [encoder_optimizer, decoder_optimizer], opt_level="O2")


with amp.scale_loss(loss, [encoder_optimizer, decoder_optimizer]) as scaled_loss:
        scaled_loss.backward()

and here is what decoder looks like:

`class Decoder(nn.Module):

def __init__(self, encoder_hidden_size, decoder_hidden_size, history_size):
    super(Decoder, self).__init__()
    self.encoder_hidden_size = encoder_hidden_size
    self.decoder_hidden_size = decoder_hidden_size
    self.T = history_size + 1

    self.attn_layer = nn.Sequential(nn.Linear(2 * decoder_hidden_size + encoder_hidden_size, encoder_hidden_size),
                                    nn.Tanh(), nn.Linear(encoder_hidden_size, 1))
    self.lstm1 = nn.LSTM(1, decoder_hidden_size, 1)
    self.out1 = nn.Linear(encoder_hidden_size + 1, 1)
    self.out2 = nn.Linear(decoder_hidden_size + encoder_hidden_size, 24)

    self.out1.weight.data.normal_()

def forward(self, input_encoded, y_history, target=None):
    hidden = self.init_hidden()
    cell = self.init_hidden()     

    for i in range(self.T - 1):
        x = torch.cat((hidden.repeat(self.T - 1, 1, 1).permute(1, 0, 2),
                        cell.repeat(self.T -1, 1, 1).permute(1, 0, 2), input_encoded), dim=2)
        x = F.softmax(
                self.attn_layer(
                    x.view(-1, 2 * self.decoder_hidden_size + self.encoder_hidden_size)
                ).view(-1, self.T - 1), dim=1)  # (batch_size, T - 1)

        context = torch.bmm(x.unsqueeze(1), input_encoded)[:, 0, :]     # batch_size * encoder_hidden_size
        y_tilde = self.out1(torch.cat((context, y_history[:, i]), dim=1))  # (batch_size, out_size)
        
        # Eqn. 16: LSTM
        self.lstm1.flatten_parameters()
        _, lstm_output = self.lstm1(y_tilde.unsqueeze(0), (hidden, cell))

        hidden = lstm_output[0]  # 1 * batch_size * decoder_hidden_size
        cell = lstm_output[1]  # 1 * batch_size * decoder_hidden_size          

    y_pred = self.out2(torch.cat((hidden[0], context), dim=1))
    return predictions`
@realiti4 realiti4 changed the title Expected object of scalar type Float but got scalar type Half Expected object of scalar type Float but got scalar type Half with opt_level 02,03 Dec 27, 2019
@hammad001
Copy link

+1. Facing similar issues.

@cswwp
Copy link

cswwp commented Jul 8, 2020

+1 also same issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants