Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

torch.lerp: discrepancy between CUDA and CPU (with extremal inputs) #78484

Open
kshitij12345 opened this issue May 30, 2022 · 3 comments
Open
Labels
module: NaNs and Infs Problems related to NaN and Inf handling in floating point triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Comments

@kshitij12345
Copy link
Collaborator

馃悰 Describe the bug

import torch

st = torch.tensor(0.2345)
en = torch.tensor(float("inf"))
weight = torch.tensor(-0.9890)

print(torch.lerp(st, en, weight))   # -inf
print(torch.lerp(st.cuda(), en.cuda(), weight.cuda()))  # nan

Versions

master

@kshitij12345 kshitij12345 added the module: NaNs and Infs Problems related to NaN and Inf handling in floating point label May 30, 2022
@ejguan ejguan added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label May 31, 2022
@khushi-411
Copy link
Contributor

Hi, @kshitij12345; I think this is fixed in the master branch and can be closed. Please see:

In [1]: import torch
   ...: 
   ...: st = torch.tensor(0.2345)
   ...: en = torch.tensor(float("inf"))
   ...: weight = torch.tensor(-0.9890)
   ...: 
   ...: print(torch.lerp(st, en, weight))   # -inf
   ...: print(torch.lerp(st.cuda(), en.cuda(), weight.cuda()))  # nan

tensor(nan)
tensor(nan, device='cuda:0')

In [2]: torch.__version__
Out[2]: '2.0.0a0+gitb7c2a65'

Thanks!

@kshitij12345
Copy link
Collaborator Author

kshitij12345 commented Mar 13, 2023

This looks like regression on CPU, as I'd expect -inf as the output.

>>> start = 0.2345
>>> end = -0.9890
>>> weight = float('inf')
>>> start + weight * (end - start)
-inf

@ZailiWang
Copy link
Contributor

ZailiWang commented Mar 15, 2023

Yes this should be a regression, the original CPU result (-inf) should be expected.
I found the PR introduced the bug is #84844, in which weight < 0.5 is updated to abs(weight) < 0.5 so the calc is executed by the formula at the other branch. This judgement is for numerical stability when weight closer to 0, but disregarded this edge case.

import torch

st = torch.tensor(0.2345)
en = torch.tensor(float("inf"))
weight = torch.tensor(-0.9890)

print(st + weight * (en - st)) # -inf
print(en - (en-st)*(torch.tensor(1)-weight)) # nan

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module: NaNs and Infs Problems related to NaN and Inf handling in floating point triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

No branches or pull requests

4 participants