Matthew Bentham | f1aeab9 | 2023-05-30 13:35:34 +0000 | [diff] [blame] | 1 | /* |
Mohammed Suhail Munshi | 7467ba8 | 2023-12-05 14:27:31 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2024 Arm Limited. |
Matthew Bentham | f1aeab9 | 2023-05-30 13:35:34 +0000 | [diff] [blame] | 3 | * |
| 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 Munshi | 7467ba8 | 2023-12-05 14:27:31 +0000 | [diff] [blame] | 24 | #ifndef ACL_ARM_COMPUTE_FUNCTION_INFO_ACTIVATIONLAYERINFO_H |
| 25 | #define ACL_ARM_COMPUTE_FUNCTION_INFO_ACTIVATIONLAYERINFO_H |
Matthew Bentham | f1aeab9 | 2023-05-30 13:35:34 +0000 | [diff] [blame] | 26 | |
SiCong Li | 9129549 | 2023-07-21 18:16:13 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/CoreTypes.h" |
Mohammed Suhail Munshi | 7467ba8 | 2023-12-05 14:27:31 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/Error.h" |
Matthew Bentham | f1aeab9 | 2023-05-30 13:35:34 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/QuantizationInfo.h" |
Matthew Bentham | f1aeab9 | 2023-05-30 13:35:34 +0000 | [diff] [blame] | 30 | |
SiCong Li | 9129549 | 2023-07-21 18:16:13 +0100 | [diff] [blame] | 31 | #include <array> |
Mohammed Suhail Munshi | 7467ba8 | 2023-12-05 14:27:31 +0000 | [diff] [blame] | 32 | #include <memory> |
| 33 | |
| 34 | #ifdef __aarch64__ |
| 35 | #include <arm_neon.h> |
| 36 | #endif // __arch64__ |
Matthew Bentham | f1aeab9 | 2023-05-30 13:35:34 +0000 | [diff] [blame] | 37 | |
| 38 | namespace arm_compute |
| 39 | { |
SiCong Li | 9129549 | 2023-07-21 18:16:13 +0100 | [diff] [blame] | 40 | /** Available activation functions */ |
| 41 | enum 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 48 | 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 Li | 9129549 | 2023-07-21 18:16:13 +0100 | [diff] [blame] | 59 | }; |
Matthew Bentham | f1aeab9 | 2023-05-30 13:35:34 +0000 | [diff] [blame] | 60 | /** Activation Layer Information class */ |
| 61 | class ActivationLayerInfo |
| 62 | { |
| 63 | public: |
| 64 | typedef arm_compute::ActivationFunction ActivationFunction; |
| 65 | |
| 66 | /** Lookup table */ |
Mohammed Suhail Munshi | 7467ba8 | 2023-12-05 14:27:31 +0000 | [diff] [blame] | 67 | #ifdef __aarch64__ |
| 68 | using LookupTable256 = std::array<qasymm8_t, 256>; |
| 69 | using LookupTable65536 = std::array<float16_t, 65536>; |
| 70 | #endif // __aarch64__ |
Matthew Bentham | f1aeab9 | 2023-05-30 13:35:34 +0000 | [diff] [blame] | 71 | |
| 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 Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 80 | ActivationLayerInfo(ActivationFunction f, float a = 0.0f, float b = 0.0f) : _act(f), _a(a), _b(b), _enabled(true) |
Matthew Bentham | f1aeab9 | 2023-05-30 13:35:34 +0000 | [diff] [blame] | 81 | { |
| 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 Munshi | 7467ba8 | 2023-12-05 14:27:31 +0000 | [diff] [blame] | 113 | |
| 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 Bentham | f1aeab9 | 2023-05-30 13:35:34 +0000 | [diff] [blame] | 123 | #endif // __aarch64__ |
| 124 | private: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 125 | ActivationFunction _act = {ActivationLayerInfo::ActivationFunction::IDENTITY}; |
Matthew Bentham | f1aeab9 | 2023-05-30 13:35:34 +0000 | [diff] [blame] | 126 | float _a = {}; |
| 127 | float _b = {}; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 128 | bool _enabled = {false}; |
Matthew Bentham | f1aeab9 | 2023-05-30 13:35:34 +0000 | [diff] [blame] | 129 | |
| 130 | #ifdef __aarch64__ |
Mohammed Suhail Munshi | 7467ba8 | 2023-12-05 14:27:31 +0000 | [diff] [blame] | 131 | LookupTable256 _lut = {}; |
| 132 | std::shared_ptr<LookupTable65536> _lut_fp16{nullptr}; |
Matthew Bentham | f1aeab9 | 2023-05-30 13:35:34 +0000 | [diff] [blame] | 133 | #endif // __aarch64__ |
| 134 | }; |
| 135 | } // namespace arm_compute |
Mohammed Suhail Munshi | 7467ba8 | 2023-12-05 14:27:31 +0000 | [diff] [blame] | 136 | #endif // ACL_ARM_COMPUTE_FUNCTION_INFO_ACTIVATIONLAYERINFO_H |