forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Activation.cpp
288 lines (255 loc) · 9.1 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#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>();
Vec threshold_v = Vec(threshold);
scalar_t value = value_scalar.to<scalar_t>();
Vec value_v = Vec(value);
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, value_v, x <= threshold_v);
});
});
}
#if AT_MKL_ENABLED()
template <typename T>
void MKLCdfNorm(int64_t N, const T* X, T* Y);
template <>
void MKLCdfNorm<float>(int64_t N, const float* X, float* Y) {
vsCdfNorm(N, X, Y);
}
template <>
void MKLCdfNorm<double>(int64_t N, const double* X, double* Y) {
vdCdfNorm(N, X, Y);
}
template <typename T>
void MKLMul(int64_t N, const T* A, const T* B, T* Y);
template <>
void MKLMul<float>(int64_t N, const float* A, const float* B, float* Y) {
vsMul(N, A, B, Y);
}
template <>
void MKLMul<double>(int64_t N, const double* A, const double* B, double* Y) {
vdMul(N, A, B, Y);
}
template <typename T>
void MKLExp(int64_t N, const T* X, T* Y);
template <>
void MKLExp<float>(int64_t N, const float* X, float* Y) {
vsExp(N, X, Y);
}
template <>
void MKLExp<double>(int64_t N, const double* X, double* Y) {
vdExp(N, X, Y);
}
template <typename T>
void GeluMKLKernelImpl(TensorIterator* it) {
if (!it->can_use_32bit_indexing()) {
for (auto& sub_it : it->with_32bit_indexing()) {
GeluMKLKernelImpl<T>(&sub_it);
}
return;
}
const int64_t N = it->numel();
const T* X_data = static_cast<T*>(it->data_ptr(1));
T* Y_data = static_cast<T*>(it->data_ptr(0));
MKLCdfNorm<T>(N, X_data, Y_data);
MKLMul<T>(N, X_data, Y_data, Y_data);
}
template <typename T>
void GeluBackwardMKLKernelImpl(TensorIterator* it) {
if (!it->can_use_32bit_indexing()) {
for (auto& sub_it : it->with_32bit_indexing()) {
GeluBackwardMKLKernelImpl<T>(&sub_it);
}
return;
}
constexpr T kBeta = M_2_SQRTPI * M_SQRT1_2 * T(0.5);
const int64_t N = it->numel();
const T* dY_data = static_cast<T*>(it->data_ptr(1));
const T* X_data = static_cast<T*>(it->data_ptr(2));
T* dX_data = static_cast<T*>(it->data_ptr(0));
Tensor cdf = at::empty({N}, it->input(1).options());
T* cdf_data = cdf.template data_ptr<T>();
MKLCdfNorm<T>(N, X_data, cdf_data);
for (int64_t i = 0; i < N; ++i) {
dX_data[i] = T(-0.5) * X_data[i] * X_data[i];
}
MKLExp(N, dX_data, dX_data);
for (int64_t i = 0; i < N; ++i) {
dX_data[i] = dY_data[i] * (cdf_data[i] + kBeta * X_data[i] * dX_data[i]);
}
}
#else // AT_MKL_ENABLED()
template <typename T>
void GeluMKLKernelImpl(TensorIterator* /* it */) {
AT_ASSERTM(false, "ATen not compiled with MKL");
}
template <typename T>
void GeluBackwardMKLKernelImpl(TensorIterator* /* it */) {
AT_ASSERTM(false, "ATen not compiled with MKL");
}
#endif // AT_MKL_ENABLED()
void elu_kernel(TensorIterator& it, Scalar alpha, Scalar scale, Scalar input_scale) {
AT_DISPATCH_FLOATING_TYPES(it.dtype(), "elu_cpu", [&]() {
auto negcoef = alpha.to<scalar_t>() * scale.to<scalar_t>();
auto poscoef = scale.to<scalar_t>();
auto negiptcoef = input_scale.to<scalar_t>();
cpu_kernel(it, [=](scalar_t a) -> scalar_t {
return a <= scalar_t(0) ? (std::exp(a * negiptcoef) - scalar_t(1)) * negcoef : a * poscoef;
});
});
}
void elu_backward_kernel(TensorIterator& it, Scalar alpha, Scalar scale, Scalar input_scale) {
AT_DISPATCH_FLOATING_TYPES(it.dtype(), "elu_backward_cpu", [&]() {
auto negcoef = alpha.to<scalar_t>() * scale.to<scalar_t>();
auto poscoef = scale.to<scalar_t>();
auto negiptcoef = input_scale.to<scalar_t>();
cpu_kernel(it, [=](scalar_t a, scalar_t b) -> scalar_t {
return b <= scalar_t(0) ? a * negiptcoef * (b + negcoef) : a * poscoef;
});
});
}
// 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(TensorIterator& it) {
if (at::hasMKL() && it.is_contiguous()) {
AT_DISPATCH_FLOATING_TYPES(it.dtype(), "GeluKernelImpl", [&]() {
GeluMKLKernelImpl<scalar_t>(&it);
});
} else {
AT_DISPATCH_FLOATING_TYPES(it.dtype(), "GeluKernelImpl", [&]() {
using Vec = vec256::Vec256<scalar_t>;
const Vec kAlphaVec(M_SQRT1_2);
const Vec kOneVec(1);
const Vec kPointFiveVec(0.5);
cpu_kernel_vec(
it,
[](scalar_t x) {
constexpr scalar_t kAlpha = M_SQRT1_2;
return x * scalar_t(0.5) * (scalar_t(1) + std::erf(x * kAlpha));
},
[&](Vec x_vec) {
return x_vec * kPointFiveVec *
(kOneVec + (x_vec * kAlphaVec).erf());
});
});
}
}
void GeluBackwardKernelImpl(TensorIterator& it) {
if (hasMKL() && it.is_contiguous()) {
AT_DISPATCH_FLOATING_TYPES(it.dtype(), "GeluBackwardKernelImpl", [&]() {
GeluBackwardMKLKernelImpl<scalar_t>(&it);
});
} else {
AT_DISPATCH_FLOATING_TYPES(it.dtype(), "GeluBackwardKernelImpl", [&]() {
using Vec = vec256::Vec256<scalar_t>;
const Vec kAlphaVec(M_SQRT1_2);
const Vec kBetaVec(M_2_SQRTPI * M_SQRT1_2 * 0.5);
const Vec kOneVec(1);
const Vec kPointFiveVec(0.5);
const Vec kMinusPointFiveVec(-0.5);
cpu_kernel_vec(
it,
[](scalar_t dy, scalar_t x) {
constexpr scalar_t kAlpha = M_SQRT1_2;
constexpr scalar_t kBeta = M_2_SQRTPI * M_SQRT1_2 * 0.5;
const scalar_t cdf =
scalar_t(0.5) * (scalar_t(1) + std::erf(x * kAlpha));
const scalar_t pdf = kBeta * std::exp(x * x * scalar_t(-0.5));
return dy * (cdf + x * pdf);
},
[&](Vec dy_vec, Vec x_vec) {
const Vec cdf_vec =
kPointFiveVec * (kOneVec + (x_vec * kAlphaVec).erf());
const Vec pdf_vec =
kBetaVec * (x_vec * x_vec * kMinusPointFiveVec).exp();
return dy_vec * (cdf_vec + x_vec * pdf_vec);
});
});
}
}
void hardshrink_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 softshrink_kernel(TensorIterator& iter, Scalar lambd) {
AT_DISPATCH_FLOATING_TYPES_AND_HALF(iter.dtype(), "softshrink_cuda", [&]() {
auto lambd_val = lambd.to<scalar_t>();
cpu_kernel(iter, [=](scalar_t a) -> scalar_t {
return a > lambd_val ? a - lambd_val : (a < -lambd_val ? a + lambd_val : scalar_t(0));
});
});
}
void shrink_backward_kernel(TensorIterator& iter, Scalar lambd) {
AT_DISPATCH_FLOATING_TYPES(iter.dtype(), "shrink_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;
});
});
}
void hardtanh_backward_kernel(TensorIterator& iter, Scalar min, Scalar max) {
AT_DISPATCH_FLOATING_TYPES_AND_HALF(iter.dtype(), "hardshrink_backward_cpu", [&] {
auto min_val = min.to<scalar_t>();
auto max_val = max.to<scalar_t>();
cpu_kernel_vec(
iter,
[=](scalar_t grad_val, scalar_t self_val) {
return (self_val <= min_val || self_val >= max_val) ? scalar_t(0) : grad_val;
},
[=](Vec256<scalar_t> grad_val, Vec256<scalar_t> self_val) {
return ((self_val > min_val) & (self_val < max_val)) & grad_val;
});
});
}
} // namespace
REGISTER_DISPATCH(threshold_stub, &threshold_kernel);
REGISTER_DISPATCH(elu_stub, &elu_kernel);
REGISTER_DISPATCH(elu_backward_stub, &elu_backward_kernel);
REGISTER_DISPATCH(GeluKernel, &GeluKernelImpl);
REGISTER_DISPATCH(GeluBackwardKernel, &GeluBackwardKernelImpl);
REGISTER_DISPATCH(hardtanh_backward_stub, &hardtanh_backward_kernel);
REGISTER_DISPATCH(hardshrink_stub, &hardshrink_kernel);
REGISTER_DISPATCH(softshrink_stub, &softshrink_kernel);
REGISTER_DISPATCH(shrink_backward_stub, &shrink_backward_kernel);
} // namespace native
} // namespace at