Skip to content
This repository was archived by the owner on Nov 17, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/operator/fusion/fused_op-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,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));
}

template <typename DType>
Expand Down
3 changes: 3 additions & 0 deletions tests/python/gpu/test_fusion.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ def announce_check(op_name):
for act_type in ['relu', 'sigmoid', 'tanh', 'softrelu', 'softsign']:
announce_check("Activation(act_type='{}')".format(act_type))
check_fused_symbol(mx.sym.Activation(a, act_type=act_type), a=arr)
if act_type == 'softrelu':
# Check that softrelu implementation doesn't overflow on large inputs
check_fused_symbol(mx.sym.Activation(a, act_type=act_type), a=1000 * arr)

# Cast requires dtype
for dtype in ['float16', 'float32', 'float64', 'int32']:
Expand Down