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

Issue with type conversion for BatchNorm inference in THNN #1296

Open
libfun opened this issue Nov 10, 2017 · 1 comment
Open

Issue with type conversion for BatchNorm inference in THNN #1296

libfun opened this issue Nov 10, 2017 · 1 comment

Comments

@libfun
Copy link

libfun commented Nov 10, 2017

TL;DR:
THNN/BatchNormalization.c, Line 53

invstd = 1 / sqrt(THTensor_(get1d)(running_var, f) + eps);

The variable eps is double, so THTensor_(get1d)(running_var, f) converts to double, so whole 1 / sqrt thing becomes double, and in the end converts to real, because invstd is real.

If eps is converted to float before, the output should match the expected behavior.

I was trying to convert Pytorch model to Caffe on CPU and all conv layers worked flawlessly and produced equal output tensors, but on BatchNorm layers something broke and the output was very different. After some investigation I found that Caffe implementation produced the same output as direct numpy equivalent in float32, but Pytorch didn't. After setting types to float64 for values described above I was able to reproduce Pytorch batchnorm output with numpy.

This issue breaks direct conversion of Pytorch models to Caffe, and also seems like genuine unexpected behavior, since everything is expected be float32 during both training and inference.

@libfun
Copy link
Author

libfun commented Nov 10, 2017

Xposted issue in pytorch: pytorch/pytorch#3624

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

1 participant