RuRo commented on a change in pull request #17849: Fix SoftReLU fused operator
numerical stability
URL: https://github.com/apache/incubator-mxnet/pull/17849#discussion_r393288295
##########
File path: src/operator/fusion/fused_op-inl.h
##########
@@ -550,7 +550,10 @@ __device__ inline DType sigmoid(const DType val) {
template <typename DType>
__device__ inline DType softrelu(const DType val) {
- return logf(1 + expf(val));
+ // Avoid overflow of exp for large inputs.
+ // The threshold 20 is chosen such that softrelu(a) = a
+ // for a > 20 using floating precision.
+ return val > 20 ? val : logf(1 + expf(val));
Review comment:
I took the reference threshold from MxNets own [mshadow
impl](https://github.com/apache/incubator-mxnet/blob/ea2dabaae5e1453572c9105f77322977bd14c108/src/operator/mshadow_op.h#L420).
`log(1 + exp(v))` is litteraly the same floating point value as `v` for
float32 and `15.004232 <= v < 88.72284`. For `v < 15.004232`, there is a
non-zero difference `log(1 + exp(v)) - v`, and for `v >= 88.72284`, `exp(v)` is
`+inf`.
Given that choosing any value in that range should yield exactly the same
results, I would intuitively pick a lower value since it skips the
`log(1+exp(v))` computation in favour of identity `v` earlier.
Although, I guess, 88.72284 is slightly less "magic number" than 20, since
it's `log(FLT_MAX)`.
I am not particularly committed to either version, so the final decision is
up to you
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services