Solved by smth in post #2 
              
              
                if you dont have a constraint of memory, then you can use a broadcasting trick: 
x = torch.tensor([1, 2, 3])
y = torch.tensor([2, 3])

x.view(1, -1).eq(y.view(-1, 1)).sum(0)