Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 1 | /* |
Renato Arantes | 36a75da | 2024-01-26 17:31:18 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2017-2020,2024 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 | */ |
| 24 | #include "ActivationLayer.h" |
| 25 | |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/Types.h" |
Renato Arantes | 36a75da | 2024-01-26 17:31:18 +0000 | [diff] [blame^] | 27 | |
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 | { |
Isabella Gottardi | 651540f | 2018-09-13 15:33:35 +0100 | [diff] [blame] | 38 | template <typename T> |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 39 | SimpleTensor<T> activation_layer(const SimpleTensor<T> &src, ActivationLayerInfo info, const QuantizationInfo &oq_info) |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 40 | { |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 41 | ARM_COMPUTE_UNUSED(oq_info); |
| 42 | |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 43 | // Create reference |
Renato Arantes | 36a75da | 2024-01-26 17:31:18 +0000 | [diff] [blame^] | 44 | SimpleTensor<T> dst{src.shape(), src.data_type(), 1}; |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 45 | |
| 46 | // Compute reference |
| 47 | const T a(info.a()); |
| 48 | const T b(info.b()); |
Michalis Spyrou | d1d7722 | 2020-04-08 14:10:15 +0100 | [diff] [blame] | 49 | #if defined(_OPENMP) |
| 50 | #pragma omp parallel for |
| 51 | #endif /* _OPENMP */ |
Renato Arantes | 36a75da | 2024-01-26 17:31:18 +0000 | [diff] [blame^] | 52 | for (int i = 0; i < src.num_elements(); ++i) |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 53 | { |
Giorgio Arena | 7302302 | 2018-09-04 14:55:55 +0100 | [diff] [blame] | 54 | dst[i] = activate_float<T>(src[i], a, b, info.activation()); |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | return dst; |
| 58 | } |
| 59 | |
Michel Iwaniec | 66cc12f | 2017-12-07 17:26:40 +0000 | [diff] [blame] | 60 | template <> |
Renato Arantes | 36a75da | 2024-01-26 17:31:18 +0000 | [diff] [blame^] | 61 | SimpleTensor<uint8_t> |
| 62 | activation_layer<uint8_t>(const SimpleTensor<uint8_t> &src, ActivationLayerInfo info, const QuantizationInfo &oq_info) |
Michel Iwaniec | 66cc12f | 2017-12-07 17:26:40 +0000 | [diff] [blame] | 63 | { |
Georgios Pinitas | 4b3fba1 | 2019-06-04 17:31:46 +0100 | [diff] [blame] | 64 | const QuantizationInfo dst_qinfo = oq_info.empty() ? src.quantization_info() : oq_info; |
| 65 | |
Michel Iwaniec | 66cc12f | 2017-12-07 17:26:40 +0000 | [diff] [blame] | 66 | SimpleTensor<float> src_tmp = convert_from_asymmetric(src); |
| 67 | SimpleTensor<float> dst_tmp = activation_layer<float>(src_tmp, info); |
Michele Di Giorgio | 4aff98f | 2019-08-28 16:27:26 +0100 | [diff] [blame] | 68 | SimpleTensor<uint8_t> dst = convert_to_asymmetric<uint8_t>(dst_tmp, dst_qinfo); |
Michel Iwaniec | 66cc12f | 2017-12-07 17:26:40 +0000 | [diff] [blame] | 69 | return dst; |
| 70 | } |
| 71 | |
giuros01 | c9573f3 | 2019-06-20 10:30:17 +0100 | [diff] [blame] | 72 | template <> |
Renato Arantes | 36a75da | 2024-01-26 17:31:18 +0000 | [diff] [blame^] | 73 | SimpleTensor<int8_t> |
| 74 | activation_layer<int8_t>(const SimpleTensor<int8_t> &src, ActivationLayerInfo info, const QuantizationInfo &oq_info) |
Georgios Pinitas | 6e1791b | 2019-12-02 19:01:25 +0000 | [diff] [blame] | 75 | { |
| 76 | const QuantizationInfo dst_qinfo = oq_info.empty() ? src.quantization_info() : oq_info; |
| 77 | |
| 78 | SimpleTensor<float> src_tmp = convert_from_asymmetric(src); |
| 79 | SimpleTensor<float> dst_tmp = activation_layer<float>(src_tmp, info); |
| 80 | SimpleTensor<int8_t> dst = convert_to_asymmetric<int8_t>(dst_tmp, dst_qinfo); |
| 81 | return dst; |
| 82 | } |
| 83 | |
| 84 | template <> |
Renato Arantes | 36a75da | 2024-01-26 17:31:18 +0000 | [diff] [blame^] | 85 | SimpleTensor<int16_t> |
| 86 | activation_layer<int16_t>(const SimpleTensor<int16_t> &src, ActivationLayerInfo info, const QuantizationInfo &oq_info) |
giuros01 | c9573f3 | 2019-06-20 10:30:17 +0100 | [diff] [blame] | 87 | { |
| 88 | const QuantizationInfo dst_qinfo = oq_info.empty() ? src.quantization_info() : oq_info; |
| 89 | |
| 90 | SimpleTensor<float> src_tmp = convert_from_symmetric(src); |
| 91 | SimpleTensor<float> dst_tmp = activation_layer<float>(src_tmp, info); |
| 92 | SimpleTensor<int16_t> dst = convert_to_symmetric<int16_t>(dst_tmp, dst_qinfo); |
| 93 | return dst; |
| 94 | } |
Renato Arantes | 36a75da | 2024-01-26 17:31:18 +0000 | [diff] [blame^] | 95 | template SimpleTensor<int32_t> |
| 96 | activation_layer(const SimpleTensor<int32_t> &src, ActivationLayerInfo info, const QuantizationInfo &oq_info); |
| 97 | template SimpleTensor<float> |
| 98 | activation_layer(const SimpleTensor<float> &src, ActivationLayerInfo info, const QuantizationInfo &oq_info); |
| 99 | template SimpleTensor<half> |
| 100 | activation_layer(const SimpleTensor<half> &src, ActivationLayerInfo info, const QuantizationInfo &oq_info); |
| 101 | template SimpleTensor<bfloat16> |
| 102 | activation_layer(const SimpleTensor<bfloat16> &src, ActivationLayerInfo info, const QuantizationInfo &oq_info); |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 103 | } // namespace reference |
| 104 | } // namespace validation |
| 105 | } // namespace test |
| 106 | } // namespace arm_compute |