giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 1 | /* |
Georgios Pinitas | 1813422 | 2020-09-03 21:00:23 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2020 Arm Limited. |
giuros01 | 164a272 | 2018-11-20 18:34: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 "ElementwiseOperations.h" |
| 25 | |
| 26 | #include "arm_compute/core/Types.h" |
| 27 | #include "tests/validation/Helpers.h" |
| 28 | |
| 29 | namespace arm_compute |
| 30 | { |
| 31 | namespace test |
| 32 | { |
| 33 | namespace validation |
| 34 | { |
| 35 | namespace reference |
| 36 | { |
| 37 | namespace |
| 38 | { |
| 39 | template <typename T> |
| 40 | T arithm_op(ArithmeticOperation op, T src1, T src2, ConvertPolicy convert_policy) |
| 41 | { |
| 42 | using intermediate_type = typename common_promoted_signed_type<T>::intermediate_type; |
| 43 | |
| 44 | intermediate_type val; |
| 45 | |
Usama Arif | 81e671e | 2019-05-13 13:33:14 +0100 | [diff] [blame] | 46 | switch(op) |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 47 | { |
Usama Arif | 81e671e | 2019-05-13 13:33:14 +0100 | [diff] [blame] | 48 | case ArithmeticOperation::ADD: |
| 49 | { |
| 50 | val = static_cast<intermediate_type>(src1) + static_cast<intermediate_type>(src2); |
| 51 | break; |
| 52 | } |
| 53 | case ArithmeticOperation::SUB: |
| 54 | { |
| 55 | val = static_cast<intermediate_type>(src1) - static_cast<intermediate_type>(src2); |
| 56 | break; |
| 57 | } |
| 58 | case ArithmeticOperation::MIN: |
| 59 | { |
| 60 | val = std::min(static_cast<intermediate_type>(src1), static_cast<intermediate_type>(src2)); |
| 61 | break; |
| 62 | } |
| 63 | case ArithmeticOperation::MAX: |
| 64 | { |
| 65 | val = std::max(static_cast<intermediate_type>(src1), static_cast<intermediate_type>(src2)); |
| 66 | break; |
| 67 | } |
| 68 | case ArithmeticOperation::SQUARED_DIFF: |
| 69 | { |
| 70 | intermediate_type tmp = (static_cast<intermediate_type>(src1) - static_cast<intermediate_type>(src2)); |
| 71 | val = tmp * tmp; |
| 72 | break; |
| 73 | } |
| 74 | case ArithmeticOperation::DIV: |
| 75 | { |
| 76 | val = (static_cast<intermediate_type>(src1) / static_cast<intermediate_type>(src2)); |
Georgios Pinitas | 1813422 | 2020-09-03 21:00:23 +0100 | [diff] [blame] | 77 | if(std::is_integral<T>::value) |
| 78 | { |
| 79 | // Implement flooring division |
| 80 | val = (src2 == 0) ? 0 : val; |
| 81 | if(static_cast<int32_t>(src1) % static_cast<int32_t>(src2) != 0 && ((src1 < 0) != (src2 < 0))) |
| 82 | { |
| 83 | --val; |
| 84 | } |
| 85 | } |
Usama Arif | 81e671e | 2019-05-13 13:33:14 +0100 | [diff] [blame] | 86 | break; |
| 87 | } |
| 88 | case ArithmeticOperation::POWER: |
| 89 | { |
| 90 | val = std::pow(static_cast<intermediate_type>(src1), static_cast<intermediate_type>(src2)); |
| 91 | break; |
| 92 | } |
giuros01 | 1e6e1b8 | 2019-05-14 16:12:53 +0100 | [diff] [blame] | 93 | case ArithmeticOperation::PRELU: |
| 94 | { |
| 95 | const T x = static_cast<intermediate_type>(src1); |
| 96 | const T alpha = static_cast<intermediate_type>(src2); |
| 97 | val = (x > 0 ? x : alpha * x); |
| 98 | break; |
| 99 | } |
Usama Arif | 81e671e | 2019-05-13 13:33:14 +0100 | [diff] [blame] | 100 | default: |
| 101 | { |
| 102 | ARM_COMPUTE_ERROR("Not handled"); |
| 103 | } |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 104 | } |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 105 | T result; |
Usama Arif | 81e671e | 2019-05-13 13:33:14 +0100 | [diff] [blame] | 106 | if(op == ArithmeticOperation::ADD || op == ArithmeticOperation::SUB || op == ArithmeticOperation::DIV || op == ArithmeticOperation::POWER) |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 107 | { |
| 108 | result = (convert_policy == ConvertPolicy::SATURATE) ? saturate_cast<T>(val) : static_cast<T>(val); |
| 109 | } |
| 110 | else |
| 111 | { |
| 112 | result = static_cast<T>(val); |
| 113 | } |
| 114 | return result; |
| 115 | } |
| 116 | |
| 117 | template <size_t dim> |
| 118 | struct BroadcastUnroll |
| 119 | { |
| 120 | template <typename T> |
| 121 | static void unroll(ArithmeticOperation op, const SimpleTensor<T> &src1, const SimpleTensor<T> &src2, SimpleTensor<T> &dst, |
| 122 | ConvertPolicy convert_policy, Coordinates &id_src1, Coordinates &id_src2, Coordinates &id_dst) |
| 123 | { |
| 124 | const bool src1_is_broadcast = (src1.shape()[dim - 1] != dst.shape()[dim - 1]); |
| 125 | const bool src2_is_broadcast = (src2.shape()[dim - 1] != dst.shape()[dim - 1]); |
| 126 | |
| 127 | id_src1.set(dim - 1, 0); |
| 128 | id_src2.set(dim - 1, 0); |
| 129 | id_dst.set(dim - 1, 0); |
| 130 | |
| 131 | for(size_t i = 0; i < dst.shape()[dim - 1]; ++i, ++id_dst[dim - 1]) |
| 132 | { |
| 133 | BroadcastUnroll < dim - 1 >::unroll(op, src1, src2, dst, convert_policy, id_src1, id_src2, id_dst); |
| 134 | |
| 135 | id_src1[dim - 1] += !src1_is_broadcast; |
| 136 | id_src2[dim - 1] += !src2_is_broadcast; |
| 137 | } |
| 138 | } |
| 139 | }; |
| 140 | |
| 141 | template <> |
| 142 | struct BroadcastUnroll<0> |
| 143 | { |
| 144 | template <typename T> |
| 145 | static void unroll(ArithmeticOperation op, const SimpleTensor<T> &src1, const SimpleTensor<T> &src2, SimpleTensor<T> &dst, |
| 146 | ConvertPolicy convert_policy, Coordinates &id_src1, Coordinates &id_src2, Coordinates &id_dst) |
| 147 | { |
| 148 | dst[coord2index(dst.shape(), id_dst)] = arithm_op(op, src1[coord2index(src1.shape(), id_src1)], src2[coord2index(src2.shape(), id_src2)], convert_policy); |
| 149 | } |
| 150 | }; |
| 151 | } // namespace |
| 152 | |
| 153 | template <typename T> |
| 154 | SimpleTensor<T> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<T> &src1, const SimpleTensor<T> &src2, SimpleTensor<T> &dst, ConvertPolicy convert_policy) |
| 155 | { |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 156 | Coordinates id_src1{}; |
| 157 | Coordinates id_src2{}; |
| 158 | Coordinates id_dst{}; |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 159 | |
| 160 | BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1, src2, dst, convert_policy, id_src1, id_src2, id_dst); |
| 161 | |
| 162 | return dst; |
| 163 | } |
| 164 | |
| 165 | template <> |
| 166 | SimpleTensor<uint8_t> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<uint8_t> &src1, const SimpleTensor<uint8_t> &src2, SimpleTensor<uint8_t> &dst, ConvertPolicy convert_policy) |
| 167 | { |
| 168 | if(dst.data_type() == DataType::QASYMM8) |
| 169 | { |
| 170 | SimpleTensor<float> src1_tmp = convert_from_asymmetric(src1); |
| 171 | SimpleTensor<float> src2_tmp = convert_from_asymmetric(src2); |
| 172 | SimpleTensor<float> dst_tmp(TensorShape::broadcast_shape(src1.shape(), src2.shape()), dst.data_type()); |
| 173 | |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 174 | Coordinates id_src1{}; |
| 175 | Coordinates id_src2{}; |
| 176 | Coordinates id_dst{}; |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 177 | |
| 178 | BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1_tmp, src2_tmp, dst_tmp, convert_policy, id_src1, id_src2, id_dst); |
| 179 | |
Michele Di Giorgio | 4aff98f | 2019-08-28 16:27:26 +0100 | [diff] [blame] | 180 | dst = convert_to_asymmetric<uint8_t>(dst_tmp, dst.quantization_info()); |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 181 | return dst; |
| 182 | } |
| 183 | else |
| 184 | { |
| 185 | // DataType::U8 |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 186 | Coordinates id_src1{}; |
| 187 | Coordinates id_src2{}; |
| 188 | Coordinates id_dst{}; |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 189 | |
| 190 | BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1, src2, dst, convert_policy, id_src1, id_src2, id_dst); |
| 191 | |
| 192 | return dst; |
| 193 | } |
| 194 | } |
Michalis Spyrou | 8d4d1b8 | 2019-11-28 11:31:23 +0000 | [diff] [blame] | 195 | template <> |
| 196 | SimpleTensor<int8_t> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<int8_t> &src1, const SimpleTensor<int8_t> &src2, SimpleTensor<int8_t> &dst, ConvertPolicy convert_policy) |
| 197 | { |
| 198 | if(dst.data_type() == DataType::QASYMM8_SIGNED) |
| 199 | { |
| 200 | SimpleTensor<float> src1_tmp = convert_from_asymmetric(src1); |
| 201 | SimpleTensor<float> src2_tmp = convert_from_asymmetric(src2); |
| 202 | SimpleTensor<float> dst_tmp(TensorShape::broadcast_shape(src1.shape(), src2.shape()), dst.data_type()); |
| 203 | |
| 204 | Coordinates id_src1{}; |
| 205 | Coordinates id_src2{}; |
| 206 | Coordinates id_dst{}; |
| 207 | |
| 208 | BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1_tmp, src2_tmp, dst_tmp, convert_policy, id_src1, id_src2, id_dst); |
| 209 | |
| 210 | dst = convert_to_asymmetric<int8_t>(dst_tmp, dst.quantization_info()); |
| 211 | return dst; |
| 212 | } |
| 213 | else |
| 214 | { |
| 215 | // DataType::S8 |
| 216 | Coordinates id_src1{}; |
| 217 | Coordinates id_src2{}; |
| 218 | Coordinates id_dst{}; |
| 219 | |
| 220 | BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1, src2, dst, convert_policy, id_src1, id_src2, id_dst); |
| 221 | |
| 222 | return dst; |
| 223 | } |
| 224 | } |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 225 | |
Michele Di Giorgio | 6997fc9 | 2019-06-18 10:23:22 +0100 | [diff] [blame] | 226 | template <> |
| 227 | SimpleTensor<int16_t> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<int16_t> &src1, const SimpleTensor<int16_t> &src2, SimpleTensor<int16_t> &dst, ConvertPolicy convert_policy) |
| 228 | { |
| 229 | if(dst.data_type() == DataType::QSYMM16) |
| 230 | { |
| 231 | SimpleTensor<float> src1_tmp = convert_from_symmetric<int16_t>(src1); |
| 232 | SimpleTensor<float> src2_tmp = convert_from_symmetric<int16_t>(src2); |
| 233 | SimpleTensor<float> dst_tmp(TensorShape::broadcast_shape(src1.shape(), src2.shape()), dst.data_type()); |
| 234 | |
| 235 | Coordinates id_src1{}; |
| 236 | Coordinates id_src2{}; |
| 237 | Coordinates id_dst{}; |
| 238 | |
| 239 | BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1_tmp, src2_tmp, dst_tmp, convert_policy, id_src1, id_src2, id_dst); |
| 240 | |
| 241 | dst = convert_to_symmetric<int16_t>(dst_tmp, dst.quantization_info()); |
| 242 | return dst; |
| 243 | } |
| 244 | else |
| 245 | { |
| 246 | // DataType::S16 |
| 247 | Coordinates id_src1{}; |
| 248 | Coordinates id_src2{}; |
| 249 | Coordinates id_dst{}; |
| 250 | |
| 251 | BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1, src2, dst, convert_policy, id_src1, id_src2, id_dst); |
| 252 | |
| 253 | return dst; |
| 254 | } |
| 255 | } |
| 256 | |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 257 | template SimpleTensor<int32_t> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<int32_t> &src1, const SimpleTensor<int32_t> &src2, SimpleTensor<int32_t> &dst, |
| 258 | ConvertPolicy convert_policy); |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 259 | template SimpleTensor<half> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<half> &src1, const SimpleTensor<half> &src2, SimpleTensor<half> &dst, ConvertPolicy convert_policy); |
| 260 | template SimpleTensor<float> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<float> &src1, const SimpleTensor<float> &src2, SimpleTensor<float> &dst, ConvertPolicy convert_policy); |
| 261 | |
| 262 | template <typename T> |
| 263 | SimpleTensor<T> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<T> &src1, const SimpleTensor<T> &src2, DataType dst_data_type, ConvertPolicy convert_policy) |
| 264 | { |
Michele Di Giorgio | 6997fc9 | 2019-06-18 10:23:22 +0100 | [diff] [blame] | 265 | ARM_COMPUTE_ERROR_ON_MSG(is_data_type_quantized(dst_data_type), "For quantized data types, the quantized output tensor should be passed directly."); |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 266 | |
| 267 | SimpleTensor<T> dst(TensorShape::broadcast_shape(src1.shape(), src2.shape()), dst_data_type); |
| 268 | arithmetic_operation<T>(op, src1, src2, dst, convert_policy); |
| 269 | return dst; |
| 270 | } |
| 271 | |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 272 | template SimpleTensor<int32_t> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<int32_t> &src1, const SimpleTensor<int32_t> &src2, DataType dst_data_type, |
| 273 | ConvertPolicy convert_policy); |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 274 | template SimpleTensor<int16_t> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<int16_t> &src1, const SimpleTensor<int16_t> &src2, DataType dst_data_type, |
| 275 | ConvertPolicy convert_policy); |
| 276 | template SimpleTensor<int8_t> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<int8_t> &src1, const SimpleTensor<int8_t> &src2, DataType dst_data_type, ConvertPolicy convert_policy); |
| 277 | template SimpleTensor<half> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<half> &src1, const SimpleTensor<half> &src2, DataType dst_data_type, ConvertPolicy convert_policy); |
| 278 | template SimpleTensor<float> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<float> &src1, const SimpleTensor<float> &src2, DataType dst_data_type, ConvertPolicy convert_policy); |
| 279 | |
| 280 | } // namespace reference |
| 281 | } // namespace validation |
| 282 | } // namespace test |
| 283 | } // namespace arm_compute |