Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 1 | /* |
Ramy Elgammal | 14d7b53 | 2023-01-30 04:56:47 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2020, 2023 Arm Limited. |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +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 | */ |
Jakub Sujak | ee301b3 | 2021-06-04 09:46:08 +0100 | [diff] [blame] | 24 | #include "ElementwiseUnary.h" |
Ramy Elgammal | 14d7b53 | 2023-01-30 04:56:47 +0000 | [diff] [blame] | 25 | #include "tests/validation/Helpers.h" |
| 26 | #include "utils/TypePrinter.h" |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 27 | namespace arm_compute |
| 28 | { |
| 29 | namespace test |
| 30 | { |
| 31 | namespace validation |
| 32 | { |
| 33 | namespace reference |
| 34 | { |
| 35 | template <typename T> |
Ramy Elgammal | 14d7b53 | 2023-01-30 04:56:47 +0000 | [diff] [blame] | 36 | SimpleTensor<T> elementwise_unary(const SimpleTensor<T> &src, SimpleTensor<T> &dst, ElementWiseUnary op) |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 37 | { |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 38 | for(int i = 0; i < src.num_elements(); ++i) |
| 39 | { |
| 40 | switch(op) |
| 41 | { |
| 42 | case ElementWiseUnary::RSQRT: |
| 43 | dst[i] = 1.f / std::sqrt(src[i]); |
| 44 | break; |
| 45 | case ElementWiseUnary::EXP: |
| 46 | dst[i] = std::exp(src[i]); |
| 47 | break; |
Usama Arif | f6e475c | 2019-05-10 12:06:28 +0100 | [diff] [blame] | 48 | case ElementWiseUnary::NEG: |
| 49 | dst[i] = -src[i]; |
| 50 | break; |
Usama Arif | c255aa7 | 2019-05-13 16:26:29 +0100 | [diff] [blame] | 51 | case ElementWiseUnary::LOG: |
| 52 | dst[i] = std::log(src[i]); |
| 53 | break; |
Manuel Bottini | 6ac5992 | 2019-05-15 14:06:02 +0100 | [diff] [blame] | 54 | case ElementWiseUnary::ABS: |
| 55 | dst[i] = std::abs(src[i]); |
| 56 | break; |
Michalis Spyrou | 0af4418 | 2019-05-17 14:04:47 +0100 | [diff] [blame] | 57 | case ElementWiseUnary::SIN: |
| 58 | dst[i] = std::sin(src[i]); |
| 59 | break; |
Usama Arif | 0a5a57a | 2019-05-23 14:20:33 +0100 | [diff] [blame] | 60 | case ElementWiseUnary::ROUND: |
Michalis Spyrou | 748a7c8 | 2019-10-07 13:00:44 +0100 | [diff] [blame] | 61 | dst[i] = arm_compute::support::cpp11::nearbyint(src[i]); |
Usama Arif | 0a5a57a | 2019-05-23 14:20:33 +0100 | [diff] [blame] | 62 | break; |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 63 | default: |
| 64 | ARM_COMPUTE_ERROR("Not implemented"); |
| 65 | } |
| 66 | } |
Ramy Elgammal | 14d7b53 | 2023-01-30 04:56:47 +0000 | [diff] [blame] | 67 | return dst; |
| 68 | } |
| 69 | template <> |
| 70 | SimpleTensor<int8_t> elementwise_unary(const SimpleTensor<int8_t> &src, SimpleTensor<int8_t> &dst, ElementWiseUnary op) |
| 71 | { |
| 72 | if(dst.data_type() == DataType::QASYMM8_SIGNED) |
| 73 | { |
| 74 | SimpleTensor<float> src_tmp = convert_from_asymmetric(src); |
| 75 | SimpleTensor<float> dst_tmp(src.shape(), DataType::F32); |
| 76 | for(int i = 0; i < src.num_elements(); ++i) |
| 77 | { |
| 78 | switch(op) |
| 79 | { |
| 80 | case ElementWiseUnary::RSQRT: |
| 81 | if(src_tmp[i] != 0) |
| 82 | { |
| 83 | dst_tmp[i] = 1.f / std::sqrt(src_tmp[i]); |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | // rsqrt(0) give 'inf' so set to the maximum in int8: 127 |
| 88 | dst_tmp[i] = (127.0f - dst.quantization_info().uniform().offset) * dst.quantization_info().uniform().scale ; |
| 89 | } |
| 90 | break; |
Viet-Hoa Do | fd472f0 | 2023-03-15 14:05:06 +0000 | [diff] [blame] | 91 | |
| 92 | case ElementWiseUnary::LOG: |
| 93 | if(src_tmp[i] != 0) |
| 94 | { |
| 95 | dst_tmp[i] = std::log(src_tmp[i]); |
| 96 | } |
| 97 | else |
| 98 | { |
| 99 | dst_tmp[i] = (-128.0f - dst.quantization_info().uniform().offset) * dst.quantization_info().uniform().scale ; |
| 100 | } |
| 101 | break; |
| 102 | |
Ramy Elgammal | 14d7b53 | 2023-01-30 04:56:47 +0000 | [diff] [blame] | 103 | default: |
Viet-Hoa Do | fd472f0 | 2023-03-15 14:05:06 +0000 | [diff] [blame] | 104 | elementwise_unary(src_tmp, dst_tmp, op); |
| 105 | break; |
Ramy Elgammal | 14d7b53 | 2023-01-30 04:56:47 +0000 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | dst = convert_to_asymmetric<int8_t>(dst_tmp, dst.quantization_info()); |
| 109 | } |
| 110 | else |
| 111 | { |
| 112 | ARM_COMPUTE_ERROR("Not implemented"); |
| 113 | } |
| 114 | return dst; |
| 115 | } |
| 116 | template <> |
| 117 | SimpleTensor<uint8_t> elementwise_unary(const SimpleTensor<uint8_t> &src, SimpleTensor<uint8_t> &dst, ElementWiseUnary op) |
| 118 | { |
| 119 | if(dst.data_type() == DataType::QASYMM8) |
| 120 | { |
| 121 | SimpleTensor<float> src_tmp = convert_from_asymmetric(src); |
| 122 | SimpleTensor<float> dst_tmp(src.shape(), DataType::F32); |
| 123 | for(int i = 0; i < src.num_elements(); ++i) |
| 124 | { |
| 125 | switch(op) |
| 126 | { |
| 127 | case ElementWiseUnary::RSQRT: |
| 128 | if(src_tmp[i] != 0) |
| 129 | { |
| 130 | dst_tmp[i] = 1.f / std::sqrt(src_tmp[i]); |
| 131 | } |
| 132 | else |
| 133 | { |
| 134 | // rsqrt(0) give 'inf' so set to the maximum in uint8: 255 |
| 135 | dst_tmp[i] = (255.0f - dst.quantization_info().uniform().offset)* dst.quantization_info().uniform().scale; |
| 136 | } |
| 137 | break; |
Viet-Hoa Do | fd472f0 | 2023-03-15 14:05:06 +0000 | [diff] [blame] | 138 | |
| 139 | case ElementWiseUnary::LOG: |
| 140 | if(src_tmp[i] != 0) |
| 141 | { |
| 142 | dst_tmp[i] = std::log(src_tmp[i]); |
| 143 | } |
| 144 | else |
| 145 | { |
| 146 | dst_tmp[i] = -dst.quantization_info().uniform().offset * dst.quantization_info().uniform().scale; |
| 147 | } |
| 148 | break; |
| 149 | |
Ramy Elgammal | 14d7b53 | 2023-01-30 04:56:47 +0000 | [diff] [blame] | 150 | default: |
Viet-Hoa Do | fd472f0 | 2023-03-15 14:05:06 +0000 | [diff] [blame] | 151 | elementwise_unary(src_tmp, dst_tmp, op); |
| 152 | break; |
Ramy Elgammal | 14d7b53 | 2023-01-30 04:56:47 +0000 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | dst = convert_to_asymmetric<uint8_t>(dst_tmp, dst.quantization_info()); |
| 156 | } |
| 157 | else |
| 158 | { |
| 159 | ARM_COMPUTE_ERROR("Not implemented"); |
| 160 | } |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 161 | return dst; |
| 162 | } |
| 163 | |
Ramy Elgammal | 14d7b53 | 2023-01-30 04:56:47 +0000 | [diff] [blame] | 164 | template SimpleTensor<float> elementwise_unary(const SimpleTensor<float> &src, SimpleTensor<float> &dst, ElementWiseUnary op); |
| 165 | template SimpleTensor<half> elementwise_unary(const SimpleTensor<half> &src, SimpleTensor<half> &dst, ElementWiseUnary op); |
| 166 | template SimpleTensor<int32_t> elementwise_unary(const SimpleTensor<int32_t> &src, SimpleTensor<int32_t> &dst, ElementWiseUnary op); |
| 167 | |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 168 | } // namespace reference |
| 169 | } // namespace validation |
| 170 | } // namespace test |
| 171 | } // namespace arm_compute |