mp1 = torch.rand(m,p)
mp2 = torch.rand(m,p)
mmp1 = torch.stack([mp1]*m)
mmp2 = torch.stack([mp2]*m).transpose(0,1)
mm = torch.sum((mmp1-mmp2)**2,2).squeeze()

Then mm is the m*m matrix you want, with mm_ij = d(mp1_i, mp2_j)