forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LeakyReLU.cu
74 lines (61 loc) · 1.33 KB
/
LeakyReLU.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
66
67
68
69
70
71
72
73
74
#include <THCUNN/THCUNN.h>
#include <TH/THHalf.h>
#include <THCUNN/THCHalfAutoNumerics.cuh>
#include <THC/THCApply.cuh>
template <typename T>
struct LeakyReLUUpdateOutput
{
const T negval_;
LeakyReLUUpdateOutput(T negval)
: negval_(negval)
{}
__device__ __forceinline__ void operator()(T *out, T *in)
{
T x = *in;
*out = (x > 0) ? x : x * negval_;
}
};
// in-place variant
template <typename T>
struct LeakyReLUUpdateOutputIP
{
const T negval_;
LeakyReLUUpdateOutputIP(T negval)
: negval_(negval)
{}
__device__ __forceinline__ void operator()(T *x)
{
*x = (*x > 0) ? *x : negval_ * (*x);
}
};
template <typename T>
struct LeakyReLUUpdateGradInput
{
const T negval_;
LeakyReLUUpdateGradInput(T negval)
: negval_(negval)
{}
__device__ __forceinline__ void operator()(
T* gradInput,
T* input,
T* gradOutput) const
{
*gradInput = (*input > 0) ? *gradOutput : (*gradOutput) * negval_;
}
};
template <typename T>
struct LeakyReLUUpdateGradInputIP
{
const T negval_;
LeakyReLUUpdateGradInputIP(T negval)
: negval_(negval)
{}
__device__ __forceinline__ void operator()(
T* gradOutput,
T* input) const
{
*gradOutput = (*input > 0) ? *gradOutput : (*gradOutput) * negval_;
}
};
#include <THCUNN/generic/LeakyReLU.cu>
#include <THC/THCGenerateFloatTypes.h>