xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/kernels/internal/reference/gelu.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_GELU_H_
16 #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_GELU_H_
17 
18 #include <cmath>
19 #include <functional>
20 
21 #include "Eigen/Core"
22 #include "unsupported/Eigen/CXX11/Tensor"
23 #include "tensorflow/lite/kernels/internal/common.h"
24 #include "tensorflow/lite/kernels/internal/constants.h"
25 #include "tensorflow/lite/kernels/internal/types.h"
26 
27 namespace tflite {
28 namespace reference_ops {
29 
30 namespace gelu_internal {
31 
32 constexpr float kSqrt2dPi = M_2_SQRTPI * M_SQRT1_2;  // sqrt( 2 / pi )
33 
34 }  // namespace gelu_internal
35 
36 // Plain implementation for GELU. Used for populating lookup table.
GeluTransform(bool approximate)37 inline std::function<float(float)> GeluTransform(bool approximate) {
38   if (approximate) {
39     return [](float in) {
40       // 0.5 * x * ( 1 + tanh( sqrt( 2 / pi ) * ( x + 0.044715 * x^3 ) ) )
41       return 0.5f * in *
42              (1.f + std::tanh(gelu_internal::kSqrt2dPi *
43                               // Note: Avoid std::pow for integer exponents
44                               // as it leads to much slower performance.
45                               (in + 0.044715f * in * in * in)));
46     };
47   } else {
48     return [](float in) {
49       // 0.5 * x * ( 1 + erf( x / sqrt( 2 ) ) )
50       return 0.5f * in * (1.f + std::erf(in * M_SQRT1_2));
51     };
52   }
53 }
54 
55 template <typename T>
Gelu(const RuntimeShape & input_shape,const T * input_data,bool approximate,const RuntimeShape & output_shape,T * output_data)56 inline void Gelu(const RuntimeShape& input_shape, const T* input_data,
57                  bool approximate, const RuntimeShape& output_shape,
58                  T* output_data) {
59   using VectorType = Eigen::VectorX<T>;
60   auto input_map = VectorType::Map(input_data, input_shape.FlatSize());
61   auto output_map = VectorType::Map(output_data, output_shape.FlatSize());
62 
63   if (approximate) {
64     // 0.5 * x * ( 1 + tanh( sqrt( 2 / pi ) * ( x + 0.044715 * x^3 ) ) )
65     output_map.array() = static_cast<T>(0.5) * input_map.array() *
66                          (static_cast<T>(1) +
67                           (static_cast<T>(gelu_internal::kSqrt2dPi) *
68                            (input_map.array() + static_cast<T>(0.044715) *
69                                                     input_map.array().cube()))
70                               .tanh());
71   } else {
72     // 0.5 * x * ( 1 + erf( x / sqrt( 2 ) ) )
73     output_map.array() =
74         static_cast<T>(0.5) * input_map.array() *
75         (static_cast<T>(1) +
76          (input_map.array() * static_cast<T>(M_SQRT1_2)).erf());
77   }
78 }
79 
80 }  // namespace reference_ops
81 }  // namespace tflite
82 
83 #endif  // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_GELU_H_
84