Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 Arm Limited. |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [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 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_TEST_ACTIVATION_LAYER_H |
| 25 | #define ARM_COMPUTE_TEST_ACTIVATION_LAYER_H |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 26 | |
Moritz Pflanzer | 82e70a1 | 2017-08-08 16:20:45 +0100 | [diff] [blame] | 27 | #include "tests/SimpleTensor.h" |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 28 | #include "tests/validation/Helpers.h" |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 29 | |
| 30 | namespace arm_compute |
| 31 | { |
| 32 | namespace test |
| 33 | { |
| 34 | namespace validation |
| 35 | { |
| 36 | namespace reference |
| 37 | { |
Giorgio Arena | 7302302 | 2018-09-04 14:55:55 +0100 | [diff] [blame] | 38 | template <typename T> |
| 39 | inline T activate_float(T x, T a, T b, ActivationLayerInfo::ActivationFunction activation) |
| 40 | { |
| 41 | T ret; |
| 42 | |
| 43 | switch(activation) |
| 44 | { |
| 45 | case ActivationLayerInfo::ActivationFunction::ABS: |
| 46 | ret = std::abs(x); |
| 47 | break; |
| 48 | case ActivationLayerInfo::ActivationFunction::LINEAR: |
| 49 | ret = a * x + b; |
| 50 | break; |
| 51 | case ActivationLayerInfo::ActivationFunction::LOGISTIC: |
| 52 | ret = static_cast<T>(1) / (static_cast<T>(1) + std::exp(-x)); |
| 53 | break; |
| 54 | case ActivationLayerInfo::ActivationFunction::RELU: |
| 55 | ret = std::max<T>(static_cast<T>(0), x); |
| 56 | break; |
| 57 | case ActivationLayerInfo::ActivationFunction::BOUNDED_RELU: |
| 58 | ret = std::min<T>(a, std::max(static_cast<T>(0), x)); |
| 59 | break; |
| 60 | case ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU: |
| 61 | ret = std::min<T>(a, std::max<T>(b, x)); |
| 62 | break; |
| 63 | case ActivationLayerInfo::ActivationFunction::LEAKY_RELU: |
| 64 | ret = (x > 0) ? x : a * x; |
| 65 | break; |
| 66 | case ActivationLayerInfo::ActivationFunction::SOFT_RELU: |
| 67 | ret = std::log(static_cast<T>(1) + std::exp(x)); |
| 68 | break; |
Georgios Pinitas | fb0fdcd | 2019-08-22 17:10:04 +0100 | [diff] [blame] | 69 | case ActivationLayerInfo::ActivationFunction::ELU: |
| 70 | ret = (x > 0) ? x : a * (std::exp(x) - static_cast<T>(1)); |
| 71 | break; |
Giorgio Arena | 7302302 | 2018-09-04 14:55:55 +0100 | [diff] [blame] | 72 | case ActivationLayerInfo::ActivationFunction::SQRT: |
| 73 | ret = std::sqrt(x); |
| 74 | break; |
| 75 | case ActivationLayerInfo::ActivationFunction::SQUARE: |
| 76 | ret = x * x; |
| 77 | break; |
| 78 | case ActivationLayerInfo::ActivationFunction::TANH: |
| 79 | ret = a * std::tanh(b * x); |
| 80 | break; |
Usama Arif | 80e55db | 2019-05-14 17:48:47 +0100 | [diff] [blame] | 81 | case ActivationLayerInfo::ActivationFunction::IDENTITY: |
| 82 | ret = x; |
| 83 | break; |
morgolock | 07df3d4 | 2020-02-27 11:46:28 +0000 | [diff] [blame] | 84 | case ActivationLayerInfo::ActivationFunction::HARD_SWISH: |
Giorgio Arena | 8b2a7d3 | 2020-02-11 17:21:31 +0000 | [diff] [blame] | 85 | ret = x * ((std::min(std::max(static_cast<T>(x + 3), static_cast<T>(0.0f)), static_cast<T>(6.0f))) * 0.166666667f); |
morgolock | 07df3d4 | 2020-02-27 11:46:28 +0000 | [diff] [blame] | 86 | break; |
Giorgio Arena | 7302302 | 2018-09-04 14:55:55 +0100 | [diff] [blame] | 87 | default: |
| 88 | ARM_COMPUTE_ERROR("Unsupported activation function"); |
| 89 | break; |
| 90 | } |
| 91 | |
| 92 | return ret; |
| 93 | } |
| 94 | |
Isabella Gottardi | 651540f | 2018-09-13 15:33:35 +0100 | [diff] [blame] | 95 | template <typename T> |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 96 | SimpleTensor<T> activation_layer(const SimpleTensor<T> &src, ActivationLayerInfo info, const QuantizationInfo &oq_info = QuantizationInfo()); |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 97 | } // namespace reference |
| 98 | } // namespace validation |
| 99 | } // namespace test |
| 100 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 101 | #endif /* ARM_COMPUTE_TEST_ACTIVATION_LAYER_H */ |