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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RuntimeError: Function 'SqrtBackward0' returned nan values in its 0th output #121

Open
Can-Zhao opened this issue Apr 21, 2023 · 4 comments

Comments

@Can-Zhao
Copy link

Can-Zhao commented Apr 21, 2023

Hi,

I got RuntimeError: Function 'SqrtBackward0' returned nan values in its 0th output
in feats0[kk], feats1[kk] = lpips.normalize_tensor(outs0[kk]), lpips.normalize_tensor(outs1[kk])

It seems that this issue might be solved by changing

norm_factor = torch.sqrt(torch.sum(in_feat**2,dim=1,keepdim=True))

to norm_factor = torch.sqrt(torch.sum(in_feat**2,dim=1,keepdim=True) + eps)

@ashaazami
Copy link

Thanks for posting this. I've been struggling to figure out what's the 'SqrtBackward0' issue and how to fix it. Perhaps, zero output causes numerical instability in the back prop in torch.sqrt!

Perhaps better option is to fix the torch.sqrt function, as in my case I'm directly using a torch.sqrt in my model.

@Sushobhan04
Copy link

Facing the same issue. @Can-Zhao Did adding EPS solve the issue ?

Is there a proposed PR for this ? Should I make one ?

@MahmoudTamam
Copy link

Hello,

I faced same problem and fixed by changing to
norm_factor = torch.sqrt(torch.sum(x ** 2, dim=1, keepdim=True) + 1e-8)

@linyu0219
Copy link

It's a big problem, has impact to other libs @richzhang
Lightning-AI/pytorch-lightning#18712

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants