ONE - On-device Neural Engine
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
GELU.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved
3 * Copyright 2021 The TensorFlow Authors. All Rights Reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef __NNFW_CKER_GELU_H__
19#define __NNFW_CKER_GELU_H__
20
21#include "cker/Shape.h"
22#include "cker/Types.h"
23#include "cker/eigen/Utils.h"
24#include <Eigen/Core>
25
26#include <cmath>
27
28namespace nnfw
29{
30namespace cker
31{
32
33namespace gelu_internal
34{
35
36constexpr float kSqrt2dPi = M_2_SQRTPI * M_SQRT1_2; // sqrt( 2 / pi )
37
38} // namespace gelu_internal
39
40inline void GELU(const GELUParams &params, const Shape &input_shape, const float *input_data,
41 const Shape &output_shape, float *output_data)
42{
43 const auto input_map = MapAsVector(input_data, input_shape);
44 auto output_map = MapAsVector(output_data, output_shape);
45
46 if (params.approximate)
47 {
48 // 0.5 * x * ( 1 + tanh( sqrt( 2 / pi ) * ( x + 0.044715 * x^3 ) ) )
49 output_map.array() = 0.5f * input_map.array() *
51 (input_map.array() + 0.044715f * input_map.array().cube()))
52 .tanh());
53 }
54 else
55 {
56 // Note: 0.5 * x * ( 1 + erf( x / sqrt( 2 ) ) ) is commonly used, but cause
57 // catastropic cancellation for large negative inputs. Rewriting the
58 // expression via erfc avoids the numerical stability issues.
59 const float neg_sqrt1_2 = -static_cast<float>(M_SQRT1_2);
60 auto x = input_map.array();
61 auto x_scaled = x * neg_sqrt1_2;
62 auto erfc_x_scaled = x_scaled.matrix().unaryExpr([](float val) { return erfcf(val); });
63 output_map.array() = 0.5f * x * erfc_x_scaled.array();
64 }
65}
66
67} // namespace cker
68} // namespace nnfw
69
70#endif // __NNFW_CKER_GELU_H__
const luci_interpreter::RuntimeShape output_shape
constexpr float kSqrt2dPi
Definition GELU.h:36
void GELU(const GELUParams &params, const Shape &input_shape, const float *input_data, const Shape &output_shape, float *output_data)
Definition GELU.h:40
VectorMap< Scalar > MapAsVector(Scalar *data, const Shape &shape)
Definition Utils.h:43
Definition topk_v2.h:30