|
| 1 | +#define _USE_MATH_DEFINES |
| 2 | + |
1 | 3 | #include <ATen/native/Activation.h> |
2 | 4 |
|
| 5 | +#include <math.h> |
| 6 | + |
3 | 7 | #include <ATen/ATen.h> |
| 8 | +#include <ATen/Config.h> |
4 | 9 | #include <ATen/cpu/vec256/vec256.h> |
5 | 10 | #include <ATen/native/TensorIterator.h> |
6 | 11 | #include <ATen/native/cpu/Loops.h> |
7 | 12 |
|
8 | | -namespace at { namespace native { |
| 13 | +#if AT_MKL_ENABLED() |
| 14 | +#include <mkl.h> |
| 15 | +#endif // AT_MKL_ENABLED() |
| 16 | + |
| 17 | +namespace at { |
| 18 | +namespace native { |
| 19 | + |
9 | 20 | namespace { |
10 | 21 |
|
11 | | -static void threshold_kernel(TensorIterator& iter, Scalar threshold_scalar, Scalar value_scalar) { |
| 22 | +static void threshold_kernel( |
| 23 | + TensorIterator& iter, |
| 24 | + Scalar threshold_scalar, |
| 25 | + Scalar value_scalar) { |
12 | 26 | AT_DISPATCH_ALL_TYPES(iter.dtype(), "threshold_cpu", [&] { |
13 | 27 | using Vec = Vec256<scalar_t>; |
14 | 28 | scalar_t threshold = threshold_scalar.to<scalar_t>(); |
15 | 29 | scalar_t value = value_scalar.to<scalar_t>(); |
16 | 30 | binary_kernel_vec( |
17 | | - iter, |
18 | | - [&](scalar_t x, scalar_t other) -> scalar_t { |
19 | | - return x <= threshold ? value : other; |
20 | | - }, |
21 | | - [&](Vec x, Vec other) -> Vec { |
22 | | - return Vec::blendv(other, Vec(value), x <= Vec(threshold)); |
23 | | - }); |
| 31 | + iter, |
| 32 | + [&](scalar_t x, scalar_t other) -> scalar_t { |
| 33 | + return x <= threshold ? value : other; |
| 34 | + }, |
| 35 | + [&](Vec x, Vec other) -> Vec { |
| 36 | + return Vec::blendv(other, Vec(value), x <= Vec(threshold)); |
| 37 | + }); |
24 | 38 | }); |
25 | 39 | } |
26 | 40 |
|
27 | | -} // anonymous namespace |
| 41 | +#if AT_MKL_ENABLED() |
| 42 | + |
| 43 | +// TODO(yangxm): Consider to use TensorIterator here. |
| 44 | +template <typename T> |
| 45 | +void GeluKernelMKLImpl(const Tensor& X, Tensor* Y); |
| 46 | + |
| 47 | +#define DELEGATE_GELU_KERNEL_MKL_IMPL(T, CdfNormFunc, MulFunc) \ |
| 48 | + template <> \ |
| 49 | + void GeluKernelMKLImpl<T>(const Tensor& X, Tensor* Y) { \ |
| 50 | + const int64_t N = X.numel(); \ |
| 51 | + const T* X_data = X.data<T>(); \ |
| 52 | + T* Y_data = Y->data<T>(); \ |
| 53 | + CdfNormFunc(N, X_data, Y_data); \ |
| 54 | + MulFunc(N, X_data, Y_data, Y_data); \ |
| 55 | + } |
| 56 | +DELEGATE_GELU_KERNEL_MKL_IMPL(float, vsCdfNorm, vsMul) |
| 57 | +DELEGATE_GELU_KERNEL_MKL_IMPL(double, vdCdfNorm, vdMul) |
| 58 | +#undef DELEGATE_GELU_KERNEL_MKL_IMPL |
| 59 | + |
| 60 | +#else // AT_MKL_ENABLED() |
| 61 | + |
| 62 | +template <typename T> |
| 63 | +void GeluKernelMKLImpl(const Tensor& X, Tensor* Y) { |
| 64 | + AT_ASSERTM(false, "ATen not compiled with MKL"); |
| 65 | +} |
| 66 | + |
| 67 | +#endif // AT_MKL_ENABLED() |
| 68 | + |
| 69 | +template <typename T> |
| 70 | +void GeluKernelImplInternal(const Tensor& X, Tensor* Y) { |
| 71 | + const int64_t N = X.numel(); |
| 72 | + const T* X_data = X.data<T>(); |
| 73 | + T* Y_data = Y->data<T>(); |
| 74 | + for (int64_t i = 0; i < N; ++i) { |
| 75 | + Y_data[i] = X_data[i] * M_SQRT1_2; |
| 76 | + } |
| 77 | + Y->erf_(); |
| 78 | + for (int64_t i = 0; i < N; ++i) { |
| 79 | + Y_data[i] = (Y_data[i] + T(1)) * X_data[i] * T(0.5); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +// TODO(yangxm): Add another fast kernel using formula |
| 84 | +// y = 0.5x * (1 + tanh(sqrt(2/Pi) * (x + 0.044715x^3))) |
| 85 | +// and the fast tanh impl from Eigen. |
| 86 | +void GeluKernelImpl(const Tensor& X, Tensor* Y) { |
| 87 | + if (at::hasMKL()) { |
| 88 | + AT_DISPATCH_FLOATING_TYPES(X.scalar_type(), "GeluKernelImpl", [&]() { |
| 89 | + GeluKernelMKLImpl<scalar_t>(X, Y); |
| 90 | + }); |
| 91 | + } else { |
| 92 | + AT_DISPATCH_FLOATING_TYPES(X.scalar_type(), "GeluKernelImpl", [&]() { |
| 93 | + GeluKernelImplInternal<scalar_t>(X, Y); |
| 94 | + }); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +} // namespace |
28 | 99 |
|
29 | 100 | REGISTER_DISPATCH(threshold_stub, &threshold_kernel); |
| 101 | +REGISTER_DISPATCH(GeluKernel, &GeluKernelImpl); |
30 | 102 |
|
31 | | -}} // namespace at::native |
| 103 | +} // namespace native |
| 104 | +} // namespace at |
0 commit comments