|
6 | 6 | template <typename T> |
7 | 7 | struct logSigmoid_updateOutput_functor |
8 | 8 | { |
9 | | - __device__ void operator()(T *output, const T *input) const |
10 | | - { |
11 | | - T z = exp(-*input); |
12 | | - *output = ScalarConvert<double, T>::to(-log(1. + z)); |
| 9 | + __device__ void operator()(T *output, const T *input) const { |
| 10 | + *output = -THCNumerics<T>::log(1.f + THCNumerics<T>::exp(- *input)); |
13 | 11 | } |
14 | 12 | }; |
15 | 13 |
|
16 | 14 | template <typename T> |
17 | 15 | struct logSigmoid_updateGradInput_functor |
18 | 16 | { |
19 | | - __device__ void operator()(T *gradInput, const T *input, const T *gradOutput) const |
20 | | - { |
21 | | - T z = exp(-*input); |
22 | | - *gradInput = ScalarConvert<double, T>::to(*gradOutput * z / (1. + z)); |
| 17 | + __device__ void operator()(T *gradInput, const T *input, const T *gradOutput) const { |
| 18 | + const T z = THCNumerics<T>::exp(- *input); |
| 19 | + *gradInput = *gradOutput * z / (1.f + z); |
23 | 20 | } |
24 | 21 | }; |
25 | 22 |
|
| 23 | +#ifdef CUDA_HALF_TENSOR |
| 24 | +template <> |
| 25 | +struct logSigmoid_updateOutput_functor<half> { |
| 26 | + __device__ __forceinline__ void operator()(half* output, const half *input) const { |
| 27 | +#ifdef CUDA_HALF_INSTRUCTIONS |
| 28 | + const half one = __float2half(1.f); |
| 29 | + *output = __hneg(THCNumerics<half>::log(one + THCNumerics<half>::exp(__hneg(*input)))); |
| 30 | +#else |
| 31 | + float in = __half2float(*input); |
| 32 | + *output = __float2half(-THCNumerics<float>::log(1.f + THCNumerics<float>::exp(-in))); |
| 33 | +#endif |
| 34 | + } |
| 35 | +}; |
| 36 | + |
| 37 | +template <> |
| 38 | +struct logSigmoid_updateGradInput_functor<half> { |
| 39 | + __device__ __forceinline__ void operator()(half* gradInput, const half *input, const half *gradOutput) const { |
| 40 | +#ifdef CUDA_HALF_INSTRUCTIONS |
| 41 | + const half one = __float2half(1.f); |
| 42 | + const half in_exp = THCNumerics<half>::exp(__hneg(*input)); |
| 43 | + *gradInput = hdiv(__hmul(*gradOutput, in_exp), __hadd(one, in_exp)); |
| 44 | +#else |
| 45 | + const float in_exp = THCNumerics<float>::exp(-(__half2float(*input))); |
| 46 | + const float go = __half2float(*gradOutput); |
| 47 | + *gradInput = __float2half(go * in_exp / (1.f + in_exp)); |
| 48 | +#endif |
| 49 | + } |
| 50 | +}; |
| 51 | +#endif |
| 52 | + |
26 | 53 | #include "generic/LogSigmoid.cu" |
27 | 54 | #include "THCGenerateFloatTypes.h" |
0 commit comments