I expand both x ( n x d ) and y ( m x d) as follows:
n = x.size(0)
m = y.size(0)
d = x.size(1)

x = x.unsqueeze(1).expand(n, m, d)
y = y.unsqueeze(0).expand(n, m, d)

Then calculate dist[i,j] = ||x[i,:]-y[j,:]||^2 in the following way:
dist = torch.pow(x - y, 2).sum(2) 

Both x and y are torch.Tensor. Is this exactly what you want? Or is there a more efficient way?