Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 1 | /* |
Jenkins | 18b685f | 2020-08-21 10:26:22 +0100 | [diff] [blame^] | 2 | * Copyright (c) 2017-2020 Arm Limited. |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +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 | */ |
| 24 | #include "ActivationLayer.h" |
| 25 | |
| 26 | #include "arm_compute/core/Types.h" |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 27 | #include "tests/validation/Helpers.h" |
| 28 | |
| 29 | namespace arm_compute |
| 30 | { |
| 31 | namespace test |
| 32 | { |
| 33 | namespace validation |
| 34 | { |
| 35 | namespace reference |
| 36 | { |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 37 | template <typename T> |
Jenkins | 975dfe1 | 2019-09-02 11:47:54 +0100 | [diff] [blame] | 38 | SimpleTensor<T> activation_layer(const SimpleTensor<T> &src, ActivationLayerInfo info, const QuantizationInfo &oq_info) |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 39 | { |
Jenkins | 975dfe1 | 2019-09-02 11:47:54 +0100 | [diff] [blame] | 40 | ARM_COMPUTE_UNUSED(oq_info); |
| 41 | |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 42 | // Create reference |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 43 | SimpleTensor<T> dst{ src.shape(), src.data_type(), 1 }; |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 44 | |
| 45 | // Compute reference |
| 46 | const T a(info.a()); |
| 47 | const T b(info.b()); |
Jenkins | 6a7771e | 2020-05-28 11:28:36 +0100 | [diff] [blame] | 48 | #if defined(_OPENMP) |
| 49 | #pragma omp parallel for |
| 50 | #endif /* _OPENMP */ |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 51 | for(int i = 0; i < src.num_elements(); ++i) |
| 52 | { |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 53 | dst[i] = activate_float<T>(src[i], a, b, info.activation()); |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | return dst; |
| 57 | } |
| 58 | |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 59 | template <> |
Jenkins | 975dfe1 | 2019-09-02 11:47:54 +0100 | [diff] [blame] | 60 | SimpleTensor<uint8_t> activation_layer<uint8_t>(const SimpleTensor<uint8_t> &src, ActivationLayerInfo info, const QuantizationInfo &oq_info) |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 61 | { |
Jenkins | 975dfe1 | 2019-09-02 11:47:54 +0100 | [diff] [blame] | 62 | const QuantizationInfo dst_qinfo = oq_info.empty() ? src.quantization_info() : oq_info; |
| 63 | |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 64 | SimpleTensor<float> src_tmp = convert_from_asymmetric(src); |
| 65 | SimpleTensor<float> dst_tmp = activation_layer<float>(src_tmp, info); |
Jenkins | 0e205f7 | 2019-11-28 16:53:35 +0000 | [diff] [blame] | 66 | SimpleTensor<uint8_t> dst = convert_to_asymmetric<uint8_t>(dst_tmp, dst_qinfo); |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 67 | return dst; |
| 68 | } |
| 69 | |
Jenkins | 975dfe1 | 2019-09-02 11:47:54 +0100 | [diff] [blame] | 70 | template <> |
Jenkins | 36ccc90 | 2020-02-21 11:10:48 +0000 | [diff] [blame] | 71 | SimpleTensor<int8_t> activation_layer<int8_t>(const SimpleTensor<int8_t> &src, ActivationLayerInfo info, const QuantizationInfo &oq_info) |
| 72 | { |
| 73 | const QuantizationInfo dst_qinfo = oq_info.empty() ? src.quantization_info() : oq_info; |
| 74 | |
| 75 | SimpleTensor<float> src_tmp = convert_from_asymmetric(src); |
| 76 | SimpleTensor<float> dst_tmp = activation_layer<float>(src_tmp, info); |
| 77 | SimpleTensor<int8_t> dst = convert_to_asymmetric<int8_t>(dst_tmp, dst_qinfo); |
| 78 | return dst; |
| 79 | } |
| 80 | |
| 81 | template <> |
Jenkins | 975dfe1 | 2019-09-02 11:47:54 +0100 | [diff] [blame] | 82 | SimpleTensor<int16_t> activation_layer<int16_t>(const SimpleTensor<int16_t> &src, ActivationLayerInfo info, const QuantizationInfo &oq_info) |
| 83 | { |
| 84 | const QuantizationInfo dst_qinfo = oq_info.empty() ? src.quantization_info() : oq_info; |
| 85 | |
| 86 | SimpleTensor<float> src_tmp = convert_from_symmetric(src); |
| 87 | SimpleTensor<float> dst_tmp = activation_layer<float>(src_tmp, info); |
| 88 | SimpleTensor<int16_t> dst = convert_to_symmetric<int16_t>(dst_tmp, dst_qinfo); |
| 89 | return dst; |
| 90 | } |
Jenkins | 6a7771e | 2020-05-28 11:28:36 +0100 | [diff] [blame] | 91 | template SimpleTensor<int32_t> activation_layer(const SimpleTensor<int32_t> &src, ActivationLayerInfo info, const QuantizationInfo &oq_info); |
Jenkins | 975dfe1 | 2019-09-02 11:47:54 +0100 | [diff] [blame] | 92 | template SimpleTensor<float> activation_layer(const SimpleTensor<float> &src, ActivationLayerInfo info, const QuantizationInfo &oq_info); |
| 93 | template SimpleTensor<half> activation_layer(const SimpleTensor<half> &src, ActivationLayerInfo info, const QuantizationInfo &oq_info); |
Anthony Barbier | 8140e1e | 2017-12-14 23:48:46 +0000 | [diff] [blame] | 94 | } // namespace reference |
| 95 | } // namespace validation |
| 96 | } // namespace test |
| 97 | } // namespace arm_compute |