blob: 9390d0c54f39f2f1e287ad1db8552186d1ad6ed9 [file] [log] [blame]
Matthew Benthamf1aeab92023-05-30 13:35:34 +00001/*
Mohammed Suhail Munshi7467ba82023-12-05 14:27:31 +00002 * Copyright (c) 2016-2024 Arm Limited.
Matthew Benthamf1aeab92023-05-30 13:35:34 +00003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
Mohammed Suhail Munshi7467ba82023-12-05 14:27:31 +000024#ifndef ACL_ARM_COMPUTE_FUNCTION_INFO_ACTIVATIONLAYERINFO_H
25#define ACL_ARM_COMPUTE_FUNCTION_INFO_ACTIVATIONLAYERINFO_H
Matthew Benthamf1aeab92023-05-30 13:35:34 +000026
SiCong Li91295492023-07-21 18:16:13 +010027#include "arm_compute/core/CoreTypes.h"
Mohammed Suhail Munshi7467ba82023-12-05 14:27:31 +000028#include "arm_compute/core/Error.h"
Matthew Benthamf1aeab92023-05-30 13:35:34 +000029#include "arm_compute/core/QuantizationInfo.h"
Matthew Benthamf1aeab92023-05-30 13:35:34 +000030
SiCong Li91295492023-07-21 18:16:13 +010031#include <array>
Mohammed Suhail Munshi7467ba82023-12-05 14:27:31 +000032#include <memory>
33
34#ifdef __aarch64__
35#include <arm_neon.h>
36#endif // __arch64__
Matthew Benthamf1aeab92023-05-30 13:35:34 +000037
38namespace arm_compute
39{
SiCong Li91295492023-07-21 18:16:13 +010040/** Available activation functions */
41enum class ActivationFunction
42{
43 LOGISTIC, /**< Logistic ( \f$ f(x) = \frac{1}{1 + e^{-x}} \f$ ) */
44 TANH, /**< Hyperbolic tangent ( \f$ f(x) = a \cdot tanh(b \cdot x) \f$ ) */
45 RELU, /**< Rectifier ( \f$ f(x) = max(0,x) \f$ ) */
46 BOUNDED_RELU, /**< Upper Bounded Rectifier ( \f$ f(x) = min(a, max(0,x)) \f$ ) */
47 LU_BOUNDED_RELU, /**< Lower and Upper Bounded Rectifier ( \f$ f(x) = min(a, max(b,x)) \f$ ) */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010048 LEAKY_RELU, /**< Leaky Rectifier ( \f$ f(x) = \begin{cases} \alpha x & \quad \text{if } x \text{ < 0}\\ x & \quad \text{if } x \geq \text{ 0 } \end{cases} \f$ ) */
49 SOFT_RELU, /**< Soft Rectifier ( \f$ f(x)= log(1+e^x) \f$ ) */
50 ELU, /**< Exponential Linear Unit ( \f$ f(x) = \begin{cases} \alpha (exp(x) - 1) & \quad \text{if } x \text{ < 0}\\ x & \quad \text{if } x \geq \text{ 0 } \end{cases} \f$ ) */
51 ABS, /**< Absolute ( \f$ f(x)= |x| \f$ ) */
52 SQUARE, /**< Square ( \f$ f(x)= x^2 \f$ )*/
53 SQRT, /**< Square root ( \f$ f(x) = \sqrt{x} \f$ )*/
54 LINEAR, /**< Linear ( \f$ f(x)= ax + b \f$ ) */
55 IDENTITY, /**< Identity ( \f$ f(x)= x \f$ ) */
56 HARD_SWISH, /**< Hard-swish ( \f$ f(x) = (x \text{ReLU6}(x+3))/6 = x \min(\max(0,x+3),6)/6 \f$ ) */
57 SWISH, /**< Swish ( \f$ f(x) = \frac{x}{1 + e^{-ax}} = x \text{logistic}(ax) \f$ ) */
58 GELU /**< GELU ( \f$ f(x) = x * 1/2 * 1 + erf(x / \sqrt{2}) \f$ ) */
SiCong Li91295492023-07-21 18:16:13 +010059};
Matthew Benthamf1aeab92023-05-30 13:35:34 +000060/** Activation Layer Information class */
61class ActivationLayerInfo
62{
63public:
64 typedef arm_compute::ActivationFunction ActivationFunction;
65
66 /** Lookup table */
Mohammed Suhail Munshi7467ba82023-12-05 14:27:31 +000067#ifdef __aarch64__
68 using LookupTable256 = std::array<qasymm8_t, 256>;
69 using LookupTable65536 = std::array<float16_t, 65536>;
70#endif // __aarch64__
Matthew Benthamf1aeab92023-05-30 13:35:34 +000071
72 ActivationLayerInfo() = default;
73 /** Default Constructor
74 *
75 * @param[in] f The activation function to use.
76 * @param[in] a (Optional) The alpha parameter used by some activation functions
77 * (@ref ActivationFunction::BOUNDED_RELU, @ref ActivationFunction::LU_BOUNDED_RELU, @ref ActivationFunction::LINEAR, @ref ActivationFunction::TANH).
78 * @param[in] b (Optional) The beta parameter used by some activation functions (@ref ActivationFunction::LINEAR, @ref ActivationFunction::LU_BOUNDED_RELU, @ref ActivationFunction::TANH).
79 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010080 ActivationLayerInfo(ActivationFunction f, float a = 0.0f, float b = 0.0f) : _act(f), _a(a), _b(b), _enabled(true)
Matthew Benthamf1aeab92023-05-30 13:35:34 +000081 {
82 }
83 /** Get the type of activation function */
84 ActivationFunction activation() const
85 {
86 return _act;
87 }
88 /** Get the alpha value */
89 float a() const
90 {
91 return _a;
92 }
93 /** Get the beta value */
94 float b() const
95 {
96 return _b;
97 }
98 /** Check if initialised */
99 bool enabled() const
100 {
101 return _enabled;
102 }
103
104#ifdef __aarch64__
105 const LookupTable256 &lut() const
106 {
107 return _lut;
108 }
109 void setLookupTable256(LookupTable256 &lut)
110 {
111 _lut = std::move(lut);
112 }
Mohammed Suhail Munshi7467ba82023-12-05 14:27:31 +0000113
114 const LookupTable65536 &lut_fp16() const
115 {
116 ARM_COMPUTE_ERROR_ON(_lut_fp16 == nullptr);
117 return *_lut_fp16;
118 }
119 void setLookupTable65536(std::shared_ptr<LookupTable65536> lut)
120 {
121 _lut_fp16 = lut;
122 }
Matthew Benthamf1aeab92023-05-30 13:35:34 +0000123#endif // __aarch64__
124private:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100125 ActivationFunction _act = {ActivationLayerInfo::ActivationFunction::IDENTITY};
Matthew Benthamf1aeab92023-05-30 13:35:34 +0000126 float _a = {};
127 float _b = {};
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100128 bool _enabled = {false};
Matthew Benthamf1aeab92023-05-30 13:35:34 +0000129
130#ifdef __aarch64__
Mohammed Suhail Munshi7467ba82023-12-05 14:27:31 +0000131 LookupTable256 _lut = {};
132 std::shared_ptr<LookupTable65536> _lut_fp16{nullptr};
Matthew Benthamf1aeab92023-05-30 13:35:34 +0000133#endif // __aarch64__
134};
135} // namespace arm_compute
Mohammed Suhail Munshi7467ba82023-12-05 14:27:31 +0000136#endif // ACL_ARM_COMPUTE_FUNCTION_INFO_ACTIVATIONLAYERINFO_H