Hello, I have this module where I try to add a positional embedding to the Word Embeddings. However, there is no error in the below code. However, when I use the commented line in the forward pass below, it results in a RuntimeError: cuDNN error: CUDNN_STATUS_EXECUTION_FAILED
error.
Code:
class QuestionEmbeddingLayer(nn.Module):
def __init__(self, vocab, args):
super().__init__()
self.word_embedding = nn.Embedding(num_embeddings=vocab.shape[0],embedding_dim=vocab.shape[1]) # dim=300
torch.nn.init.xavier_uniform(self.word_embedding.weight)
self.word_embedding.weight.requires_grad = False
self.word_embedding.weight.data.copy_(vocab)
self.position_enc = nn.Embedding(num_embeddings=150,embedding_dim=300)
tmp = get_sinusoid_encoding_table(150, 300, padding_idx=0)
self.position_enc.weight.data.copy_(tmp)
self.position_enc.weight.requires_grad = False
self.bilstm = nn.LSTM(input_size=300, hidden_size=int(args.d_model/2), num_layers=2, dropout=0.1, bidirectional=True, batch_first=False)
def forward(self, q):
word_emb = self.word_embedding(q.transpose(0,1))
# word_emb = self.word_embedding(q.transpose(0,1)) + self.position_enc(q.transpose(0,1) <- this gives error!!!!!!
output, (hidden, cell) = self.bilstm(word_emb)
q = torch.cat((hidden[-1],hidden[-2]),1)
# (batchsize, hiddensize*2)
return q
def get_sinusoid_encoding_table(n_position, d_hid, padding_idx=None):
''' Sinusoid position encoding table '''
def cal_angle(position, hid_idx):
return position / np.power(10000, 2 * (hid_idx // 2) / d_hid)
def get_posi_angle_vec(position):
return [cal_angle(position, hid_j) for hid_j in range(d_hid)]
sinusoid_table = np.array([get_posi_angle_vec(pos_i) for pos_i in range(n_position)])
sinusoid_table[:, 0::2] = np.sin(sinusoid_table[:, 0::2]) # dim 2i
sinusoid_table[:, 1::2] = np.cos(sinusoid_table[:, 1::2]) # dim 2i+1
if padding_idx is not None:
# zero vector for padding dimension
sinusoid_table[padding_idx] = 0
print(sinusoid_table.shape)
return torch.Tensor(sinusoid_table).cuda()
Any idea why this is acting this way? I’ve checked the shapes, they are all correct. Im using Cuda 9, Pytorch 1.0.0