Fix batchnorm layer numerics by replacing powx()
#5136
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I had problems to train ResNet on ImageNet, by using the solver-setting debug_info: true, I saw that there were nan's produced by the BatchNorm layer.
The problem was that the square inside the variance-term was computed via the caffe_gpu_powx function. This is slow on the one hand, but also unstable, especially for negative numbers. Therefore it was replaced by a caffe_gpu_mul call.
In the same way, I added a caffe_gpu_sqrt function in order to replace the caffe_gpu_powx(..., ..., 0.5, ...), that could lead to the same problems.
Additionally I added Dtype() castings to make sure, where appropriate.
Note: I didn't change the CPU version as it seams to be working. Nevertheless, using caffe_sqr instead of caffe_powx (..., 2) could speed up the layer... of course, also here a sqrt-function could be better than a caffe_powx (..., 0.5).