Thanks for reply. This is mimimum script:

x = Variable(torch.rand(1, 10, 100), requires_grad=True)  # input

"""
y = Variable(torch.rand(1, 10, 100), requires_grad=True)  # target

negative_samples = Variable(torch.rand(1, 5, 100))  # samples = 5
samples = torch.cat([y, negative_samples], dim=1)  # (1, 15, 100)

"""

samples = Variable(torch.rand(1, 15, 100), requires_grad=True)  # for short
# this script will run if replace (1, 15, 100) with (1, 10, 100)

rnn = nn.GRU(100, 100, 1, batch_first=True)

y_hat, _ = rnn(x)  # (1, 10, 100)

logits = torch.bmm(y_hat, samples.permute(0, 2, 1)).squeeze(0)  # (10, 15)

loss = -torch.mean(F.logsigmoid(logits.diag(0) - logits.t()))
loss.backward()


$ python test_pytorch.py 
Traceback (most recent call last):
  File "test_pytorch.py", line 30, in <module>
    loss.backward()
  File "/Users/chotoxautinh/Workspaces/Python/lib/python2.7/site-packages/torch/autograd/variable.py", line 121, in backward
    torch.autograd.backward(self, gradient, retain_graph, create_graph)
  File "/Users/chotoxautinh/Workspaces/Python/lib/python2.7/site-packages/torch/autograd/__init__.py", line 83, in backward
    variables, grad_variables, retain_graph, create_graph)
RuntimeError: The size of tensor a (10) must match the size of tensor b (15) at non-singleton dimension 1