forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Activation.cpp
215 lines (187 loc) · 7.46 KB
/
Activation.cpp
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#define _USE_MATH_DEFINES
#include <ATen/native/Activation.h>
#include <math.h>
#include <ATen/ATen.h>
#include <ATen/Config.h>
#include <ATen/cpu/vec256/vec256.h>
#include <ATen/native/TensorIterator.h>
#include <ATen/native/cpu/Loops.h>
#if AT_MKL_ENABLED()
#include <mkl.h>
#endif // AT_MKL_ENABLED()
namespace at {
namespace native {
namespace {
static void threshold_kernel(
TensorIterator& iter,
Scalar threshold_scalar,
Scalar value_scalar) {
AT_DISPATCH_ALL_TYPES(iter.dtype(), "threshold_cpu", [&] {
using Vec = Vec256<scalar_t>;
scalar_t threshold = threshold_scalar.to<scalar_t>();
scalar_t value = value_scalar.to<scalar_t>();
cpu_kernel_vec(
iter,
[&](scalar_t x, scalar_t other) -> scalar_t {
return x <= threshold ? value : other;
},
[&](Vec x, Vec other) -> Vec {
return Vec::blendv(other, Vec(value), x <= Vec(threshold));
});
});
}
#if AT_MKL_ENABLED()
// TODO(yangxm): Consider to use TensorIterator here.
template <typename T>
void GeluKernelMKLImpl(const Tensor& X, Tensor* Y);
#define DELEGATE_GELU_KERNEL_MKL_IMPL(T, CdfNormFunc, MulFunc) \
template <> \
void GeluKernelMKLImpl<T>(const Tensor& X, Tensor* Y) { \
const int64_t N = X.numel(); \
const T* X_data = X.data_ptr<T>(); \
T* Y_data = Y->data_ptr<T>(); \
CdfNormFunc(N, X_data, Y_data); \
MulFunc(N, X_data, Y_data, Y_data); \
}
DELEGATE_GELU_KERNEL_MKL_IMPL(float, vsCdfNorm, vsMul)
DELEGATE_GELU_KERNEL_MKL_IMPL(double, vdCdfNorm, vdMul)
#undef DELEGATE_GELU_KERNEL_MKL_IMPL
#else // AT_MKL_ENABLED()
template <typename T>
void GeluKernelMKLImpl(const Tensor& X, Tensor* Y) {
AT_ASSERTM(false, "ATen not compiled with MKL");
}
#endif // AT_MKL_ENABLED()
template <typename T>
void GeluKernelImplInternal(const Tensor& X, Tensor* Y) {
const int64_t N = X.numel();
const T* X_data = X.data_ptr<T>();
T* Y_data = Y->data_ptr<T>();
for (int64_t i = 0; i < N; ++i) {
Y_data[i] = X_data[i] * M_SQRT1_2;
}
Y->erf_();
for (int64_t i = 0; i < N; ++i) {
Y_data[i] = (Y_data[i] + T(1)) * X_data[i] * T(0.5);
}
}
// TODO(yangxm): Add another fast kernel using formula
// y = 0.5x * (1 + tanh(sqrt(2/Pi) * (x + 0.044715x^3)))
// and the fast tanh impl from Eigen.
void GeluKernelImpl(const Tensor& X, Tensor* Y) {
if (at::hasMKL()) {
AT_DISPATCH_FLOATING_TYPES(X.scalar_type(), "GeluKernelImpl", [&]() {
GeluKernelMKLImpl<scalar_t>(X, Y);
});
} else {
AT_DISPATCH_FLOATING_TYPES(X.scalar_type(), "GeluKernelImpl", [&]() {
GeluKernelImplInternal<scalar_t>(X, Y);
});
}
}
#if AT_MKL_ENABLED()
template <typename T>
void GeluBackwardKernelMKLImpl(const Tensor& dY, const Tensor& X, Tensor* dX);
// TODO(yangxm): Implement this by using template functions.
#define DELEGATE_GELU_BACKWARD_KERNEL_MKL_IMPL(T, CdfNormFunc, ExpFunc) \
template <> \
void GeluBackwardKernelMKLImpl<T>( \
const Tensor& dY, const Tensor& X, Tensor* dX) { \
constexpr T kAlpha = M_2_SQRTPI * M_SQRT1_2 * T(0.5); \
Tensor scratch = at::native::empty_like(X); \
const int64_t N = X.numel(); \
const T* dY_data = dY.data_ptr<T>(); \
const T* X_data = X.data_ptr<T>(); \
T* dX_data = dX->data_ptr<T>(); \
T* scratch_data = scratch.data_ptr<T>(); \
CdfNormFunc(N, X_data, scratch_data); \
for (int64_t i = 0; i < N; ++i) { \
dX_data[i] = -T(0.5) * X_data[i] * X_data[i]; \
} \
ExpFunc(N, dX_data, dX_data); \
for (int64_t i = 0; i < N; ++i) { \
dX_data[i] = \
dY_data[i] * (scratch_data[i] + X_data[i] * dX_data[i] * kAlpha); \
} \
}
DELEGATE_GELU_BACKWARD_KERNEL_MKL_IMPL(float, vsCdfNorm, vsExp)
DELEGATE_GELU_BACKWARD_KERNEL_MKL_IMPL(double, vdCdfNorm, vdExp)
#undef DELEGATE_GELU_BACKWARD_KERNEL_MKL_IMPL
#else // AT_MKL_ENABLED()
template <typename T>
void GeluBackwardKernelMKLImpl(const Tensor& dY, const Tensor& X, Tensor* dX) {
AT_ASSERTM(false, "ATen not compiled with MKL");
}
#endif // AT_MKL_ENABLED()
template <typename T>
void GeluBackwardKernelImplInternal(
const Tensor& dY,
const Tensor& X,
Tensor* dX) {
constexpr T kAlpha = M_2_SQRTPI * M_SQRT1_2 * T(0.5);
Tensor scratch = at::native::empty_like(X);
const int64_t N = X.numel();
const T* dY_data = dY.data_ptr<T>();
const T* X_data = X.data_ptr<T>();
T* dX_data = dX->data_ptr<T>();
T* scratch_data = scratch.data_ptr<T>();
for (int64_t i = 0; i < N; ++i) {
scratch_data[i] = X_data[i] * M_SQRT1_2;
dX_data[i] = -T(0.5) * X_data[i] * X_data[i];
}
// TODO(yangxm): Consider let forward pass preserve CdfNorm(X) in training
// pass to reduce this extra tensor.
scratch.erf_();
dX->exp_();
for (int64_t i = 0; i < N; ++i) {
dX_data[i] = dY_data[i] *
(T(0.5) * (T(1) + scratch_data[i]) + X_data[i] * dX_data[i] * kAlpha);
}
}
void GeluBackwardKernelImpl(const Tensor& dY, const Tensor& X, Tensor* dX) {
if (hasMKL()) {
AT_DISPATCH_FLOATING_TYPES(
X.scalar_type(), "GeluBackwardKernelImpl", [&]() {
GeluBackwardKernelMKLImpl<scalar_t>(dY, X, dX);
});
} else {
AT_DISPATCH_FLOATING_TYPES(
X.scalar_type(), "GeluBackwardKernelImpl", [&]() {
GeluBackwardKernelImplInternal<scalar_t>(dY, X, dX);
});
}
}
void hardshrink_cpu_kernel(TensorIterator& iter, Scalar lambd) {
AT_DISPATCH_FLOATING_TYPES(iter.dtype(), "hardshrink_cpu", [&] {
auto lambd_val = lambd.to<scalar_t>();
cpu_kernel_vec(iter,
[=](scalar_t self_val) {
return (self_val >= -lambd_val && self_val <= lambd_val) ? scalar_t(0) : self_val;
},
[=](Vec256<scalar_t> self_val) {
return ((self_val < -lambd_val) | (self_val > lambd_val)) & self_val;
}
);
});
}
void hardshrink_backward_cpu_kernel(TensorIterator& iter, Scalar lambd) {
AT_DISPATCH_FLOATING_TYPES(iter.dtype(), "hardshrink_backward_cpu", [&] {
auto lambd_val = lambd.to<scalar_t>();
cpu_kernel_vec(iter,
[=](scalar_t grad_val, scalar_t self_val) {
return (self_val >= -lambd_val && self_val <= lambd_val) ? scalar_t(0) : grad_val;
},
[=](Vec256<scalar_t> grad_val, Vec256<scalar_t> self_val) {
return ((self_val < -lambd_val) | (self_val > lambd_val)) & grad_val;
}
);
});
}
} // namespace
REGISTER_DISPATCH(threshold_stub, &threshold_kernel);
REGISTER_DISPATCH(GeluKernel, &GeluKernelImpl);
REGISTER_DISPATCH(GeluBackwardKernel, &GeluBackwardKernelImpl);
REGISTER_DISPATCH(hardshrink_cpu_stub, &hardshrink_cpu_kernel);
REGISTER_DISPATCH(hardshrink_backward_cpu_stub, &hardshrink_backward_cpu_kernel);
} // namespace native
} // namespace at