Bug? torch.cdist gives similar results for different p values

Hi!

I discovered that torch.cdist(x1, x2, p=2.0, compute_mode='use_mm_for_euclid_dist_if_necessary') function keeps giving similar results even after I changed the p values. Does it means there is a bug in the code?

Thanks!

Here are the code snippets:

x = torch.linspace(0.0, 1.0, 2500).reshape(-1,1)
cdist = torch.cdist(x,x,p=2.0)
print(cdist)

tensor([[0.0000e+00, 4.0016e-04, 8.0032e-04,  ..., 9.9920e-01, 9.9960e-01,
         1.0000e+00],
        [4.0016e-04, 0.0000e+00, 4.0016e-04,  ..., 9.9880e-01, 9.9920e-01,
         9.9960e-01],
        [8.0032e-04, 4.0016e-04, 0.0000e+00,  ..., 9.9840e-01, 9.9880e-01,
         9.9920e-01],
        ...,
        [9.9920e-01, 9.9880e-01, 9.9840e-01,  ..., 0.0000e+00, 4.8828e-04,
         8.0972e-04],
        [9.9960e-01, 9.9920e-01, 9.9880e-01,  ..., 4.8828e-04, 0.0000e+00,
         4.2286e-04],
        [1.0000e+00, 9.9960e-01, 9.9920e-01,  ..., 8.0972e-04, 4.2286e-04,
         0.0000e+00]])

cdist = torch.cdist(x,x,p=1.0)
print(cdist)

tensor([[0.0000e+00, 4.0016e-04, 8.0032e-04,  ..., 9.9920e-01, 9.9960e-01,
         1.0000e+00],
        [4.0016e-04, 0.0000e+00, 4.0016e-04,  ..., 9.9880e-01, 9.9920e-01,
         9.9960e-01],
        [8.0032e-04, 4.0016e-04, 0.0000e+00,  ..., 9.9840e-01, 9.9880e-01,
         9.9920e-01],
        ...,
        [9.9920e-01, 9.9880e-01, 9.9840e-01,  ..., 0.0000e+00, 4.8828e-04,
         8.0972e-04],
        [9.9960e-01, 9.9920e-01, 9.9880e-01,  ..., 4.8828e-04, 0.0000e+00,
         4.2286e-04],
        [1.0000e+00, 9.9960e-01, 9.9920e-01,  ..., 8.0972e-04, 4.2286e-04,
         0.0000e+00]])

cdist = torch.cdist(x,x,p=float('inf'))
print(cdist)

tensor([[0.0000e+00, 4.0016e-04, 8.0032e-04,  ..., 9.9920e-01, 9.9960e-01,
         1.0000e+00],
        [4.0016e-04, 0.0000e+00, 4.0016e-04,  ..., 9.9880e-01, 9.9920e-01,
         9.9960e-01],
        [8.0032e-04, 4.0016e-04, 0.0000e+00,  ..., 9.9840e-01, 9.9880e-01,
         9.9920e-01],
        ...,
        [9.9920e-01, 9.9880e-01, 9.9840e-01,  ..., 0.0000e+00, 4.8828e-04,
         8.0972e-04],
        [9.9960e-01, 9.9920e-01, 9.9880e-01,  ..., 4.8828e-04, 0.0000e+00,
         4.2286e-04],
        [1.0000e+00, 9.9960e-01, 9.9920e-01,  ..., 8.0972e-04, 4.2286e-04,
         0.0000e+00]])

For 1 element vectors, the norms for different p are all the absolute value, so you are seeing numerical differences of computing thesamw result.

Best regards

Thomas

1 Like

Hi @tom! Great thanks for your explanation! I was wondering why the results are different when I compute the same distance using ((x[:, None, :] - x[:, :, None])**2).sum(-1). Perhaps, there is something that I don’t really understand.

I think you want a the None in the first and second position and a square toot at the end.