forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ELU.cu
65 lines (55 loc) · 1.49 KB
/
ELU.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <THCUNN/THCUNN.h>
#include <TH/THHalf.h>
#include <THCUNN/THCHalfAutoNumerics.cuh>
#include <THC/THCApply.cuh>
template <typename T>
struct ELUupdateOutput_functor
{
const T negcoef_;
const T poscoef_;
const T negiptcoef_;
ELUupdateOutput_functor(T negcoef, T poscoef, T negiptcoef)
: negcoef_(negcoef)
, poscoef_(poscoef)
, negiptcoef_(negiptcoef)
{}
__device__ void operator()(T *output, const T *input) const
{
*output = *input <= 0 ? (exp(*input * negiptcoef_) - 1) * negcoef_ : *input * poscoef_;
}
};
// in-place variant
template <typename T>
struct ELUupdateOutputIP_functor
{
const T negcoef_;
const T poscoef_;
const T negiptcoef_;
ELUupdateOutputIP_functor(T negcoef, T poscoef, T negiptcoef)
: negcoef_(negcoef)
, poscoef_(poscoef)
, negiptcoef_(negiptcoef)
{}
__device__ void operator()(T *x) const
{
*x = *x <= 0 ? (exp(*x * negiptcoef_) - 1) * negcoef_ : *x * poscoef_;
}
};
template <typename T>
struct ELUupdateGradInput_functor
{
const T negcoef_;
const T poscoef_;
const T negiptcoef_;
ELUupdateGradInput_functor(T negcoef, T poscoef, T negiptcoef)
: negcoef_(negcoef)
, poscoef_(poscoef)
, negiptcoef_(negiptcoef)
{}
__device__ void operator()(T *gradInput, const T *output, const T *gradOutput) const
{
*gradInput = (*output) <= 0 ? (*gradOutput * negiptcoef_ * (*output + negcoef_)) : (*gradOutput * poscoef_);
}
};
#include <THCUNN/generic/ELU.cu>
#include <THC/THCGenerateFloatTypes.h>