forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SoftPlus.cu
43 lines (36 loc) · 1.03 KB
/
SoftPlus.cu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <THCUNN/THCUNN.h>
#include <TH/THHalf.h>
#include <THCUNN/THCHalfAutoNumerics.cuh>
#include <THC/THCApply.cuh>
template <typename T>
struct softPlusupdateOutput_functor
{
const T threshold;
const T beta;
softPlusupdateOutput_functor(T threshold_, T beta_)
: threshold(threshold_)
, beta(beta_)
{}
__device__ void operator()(T *output, const T *input) const {
T betain = beta * (*input);
*output = ((betain) > threshold) ? *input : (1/beta) * log1p(exp(betain));
}
};
template <typename T>
struct softPlusupdateGradInput_functor
{
const T threshold;
const T beta;
softPlusupdateGradInput_functor(T threshold_, T beta_)
: threshold(threshold_)
, beta(beta_)
{}
__device__ void operator()(T *gradInput, const T *output, const T *gradOutput) const
{
T betaout = beta * (*output);
T exp_bo = exp(betaout);
*gradInput = ((betaout) > threshold) ? *gradOutput : *gradOutput * (exp_bo - 1) / exp_bo;
}
};
#include <THCUNN/generic/SoftPlus.cu>
#include <THC/THCGenerateFloatTypes.h>