giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 1 | /* |
George Wort | a1e7e28 | 2019-01-15 11:00:29 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 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)); |
| 77 | break; |
| 78 | } |
| 79 | case ArithmeticOperation::POWER: |
| 80 | { |
| 81 | val = std::pow(static_cast<intermediate_type>(src1), static_cast<intermediate_type>(src2)); |
| 82 | break; |
| 83 | } |
giuros01 | 1e6e1b8 | 2019-05-14 16:12:53 +0100 | [diff] [blame] | 84 | case ArithmeticOperation::PRELU: |
| 85 | { |
| 86 | const T x = static_cast<intermediate_type>(src1); |
| 87 | const T alpha = static_cast<intermediate_type>(src2); |
| 88 | val = (x > 0 ? x : alpha * x); |
| 89 | break; |
| 90 | } |
Usama Arif | 81e671e | 2019-05-13 13:33:14 +0100 | [diff] [blame] | 91 | default: |
| 92 | { |
| 93 | ARM_COMPUTE_ERROR("Not handled"); |
| 94 | } |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 95 | } |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 96 | T result; |
Usama Arif | 81e671e | 2019-05-13 13:33:14 +0100 | [diff] [blame] | 97 | if(op == ArithmeticOperation::ADD || op == ArithmeticOperation::SUB || op == ArithmeticOperation::DIV || op == ArithmeticOperation::POWER) |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 98 | { |
| 99 | result = (convert_policy == ConvertPolicy::SATURATE) ? saturate_cast<T>(val) : static_cast<T>(val); |
| 100 | } |
| 101 | else |
| 102 | { |
| 103 | result = static_cast<T>(val); |
| 104 | } |
| 105 | return result; |
| 106 | } |
| 107 | |
| 108 | template <size_t dim> |
| 109 | struct BroadcastUnroll |
| 110 | { |
| 111 | template <typename T> |
| 112 | static void unroll(ArithmeticOperation op, const SimpleTensor<T> &src1, const SimpleTensor<T> &src2, SimpleTensor<T> &dst, |
| 113 | ConvertPolicy convert_policy, Coordinates &id_src1, Coordinates &id_src2, Coordinates &id_dst) |
| 114 | { |
| 115 | const bool src1_is_broadcast = (src1.shape()[dim - 1] != dst.shape()[dim - 1]); |
| 116 | const bool src2_is_broadcast = (src2.shape()[dim - 1] != dst.shape()[dim - 1]); |
| 117 | |
| 118 | id_src1.set(dim - 1, 0); |
| 119 | id_src2.set(dim - 1, 0); |
| 120 | id_dst.set(dim - 1, 0); |
| 121 | |
| 122 | for(size_t i = 0; i < dst.shape()[dim - 1]; ++i, ++id_dst[dim - 1]) |
| 123 | { |
| 124 | BroadcastUnroll < dim - 1 >::unroll(op, src1, src2, dst, convert_policy, id_src1, id_src2, id_dst); |
| 125 | |
| 126 | id_src1[dim - 1] += !src1_is_broadcast; |
| 127 | id_src2[dim - 1] += !src2_is_broadcast; |
| 128 | } |
| 129 | } |
| 130 | }; |
| 131 | |
| 132 | template <> |
| 133 | struct BroadcastUnroll<0> |
| 134 | { |
| 135 | template <typename T> |
| 136 | static void unroll(ArithmeticOperation op, const SimpleTensor<T> &src1, const SimpleTensor<T> &src2, SimpleTensor<T> &dst, |
| 137 | ConvertPolicy convert_policy, Coordinates &id_src1, Coordinates &id_src2, Coordinates &id_dst) |
| 138 | { |
| 139 | dst[coord2index(dst.shape(), id_dst)] = arithm_op(op, src1[coord2index(src1.shape(), id_src1)], src2[coord2index(src2.shape(), id_src2)], convert_policy); |
| 140 | } |
| 141 | }; |
| 142 | } // namespace |
| 143 | |
| 144 | template <typename T> |
| 145 | SimpleTensor<T> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<T> &src1, const SimpleTensor<T> &src2, SimpleTensor<T> &dst, ConvertPolicy convert_policy) |
| 146 | { |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 147 | Coordinates id_src1{}; |
| 148 | Coordinates id_src2{}; |
| 149 | Coordinates id_dst{}; |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 150 | |
| 151 | BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1, src2, dst, convert_policy, id_src1, id_src2, id_dst); |
| 152 | |
| 153 | return dst; |
| 154 | } |
| 155 | |
| 156 | template <> |
| 157 | SimpleTensor<uint8_t> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<uint8_t> &src1, const SimpleTensor<uint8_t> &src2, SimpleTensor<uint8_t> &dst, ConvertPolicy convert_policy) |
| 158 | { |
| 159 | if(dst.data_type() == DataType::QASYMM8) |
| 160 | { |
| 161 | SimpleTensor<float> src1_tmp = convert_from_asymmetric(src1); |
| 162 | SimpleTensor<float> src2_tmp = convert_from_asymmetric(src2); |
| 163 | SimpleTensor<float> dst_tmp(TensorShape::broadcast_shape(src1.shape(), src2.shape()), dst.data_type()); |
| 164 | |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 165 | Coordinates id_src1{}; |
| 166 | Coordinates id_src2{}; |
| 167 | Coordinates id_dst{}; |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 168 | |
| 169 | BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1_tmp, src2_tmp, dst_tmp, convert_policy, id_src1, id_src2, id_dst); |
| 170 | |
Michele Di Giorgio | 4aff98f | 2019-08-28 16:27:26 +0100 | [diff] [blame] | 171 | dst = convert_to_asymmetric<uint8_t>(dst_tmp, dst.quantization_info()); |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 172 | return dst; |
| 173 | } |
| 174 | else |
| 175 | { |
| 176 | // DataType::U8 |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 177 | Coordinates id_src1{}; |
| 178 | Coordinates id_src2{}; |
| 179 | Coordinates id_dst{}; |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 180 | |
| 181 | BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1, src2, dst, convert_policy, id_src1, id_src2, id_dst); |
| 182 | |
| 183 | return dst; |
| 184 | } |
| 185 | } |
Michalis Spyrou | 8d4d1b8 | 2019-11-28 11:31:23 +0000 | [diff] [blame] | 186 | template <> |
| 187 | SimpleTensor<int8_t> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<int8_t> &src1, const SimpleTensor<int8_t> &src2, SimpleTensor<int8_t> &dst, ConvertPolicy convert_policy) |
| 188 | { |
| 189 | if(dst.data_type() == DataType::QASYMM8_SIGNED) |
| 190 | { |
| 191 | SimpleTensor<float> src1_tmp = convert_from_asymmetric(src1); |
| 192 | SimpleTensor<float> src2_tmp = convert_from_asymmetric(src2); |
| 193 | SimpleTensor<float> dst_tmp(TensorShape::broadcast_shape(src1.shape(), src2.shape()), dst.data_type()); |
| 194 | |
| 195 | Coordinates id_src1{}; |
| 196 | Coordinates id_src2{}; |
| 197 | Coordinates id_dst{}; |
| 198 | |
| 199 | BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1_tmp, src2_tmp, dst_tmp, convert_policy, id_src1, id_src2, id_dst); |
| 200 | |
| 201 | dst = convert_to_asymmetric<int8_t>(dst_tmp, dst.quantization_info()); |
| 202 | return dst; |
| 203 | } |
| 204 | else |
| 205 | { |
| 206 | // DataType::S8 |
| 207 | Coordinates id_src1{}; |
| 208 | Coordinates id_src2{}; |
| 209 | Coordinates id_dst{}; |
| 210 | |
| 211 | BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1, src2, dst, convert_policy, id_src1, id_src2, id_dst); |
| 212 | |
| 213 | return dst; |
| 214 | } |
| 215 | } |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 216 | |
Michele Di Giorgio | 6997fc9 | 2019-06-18 10:23:22 +0100 | [diff] [blame] | 217 | template <> |
| 218 | SimpleTensor<int16_t> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<int16_t> &src1, const SimpleTensor<int16_t> &src2, SimpleTensor<int16_t> &dst, ConvertPolicy convert_policy) |
| 219 | { |
| 220 | if(dst.data_type() == DataType::QSYMM16) |
| 221 | { |
| 222 | SimpleTensor<float> src1_tmp = convert_from_symmetric<int16_t>(src1); |
| 223 | SimpleTensor<float> src2_tmp = convert_from_symmetric<int16_t>(src2); |
| 224 | SimpleTensor<float> dst_tmp(TensorShape::broadcast_shape(src1.shape(), src2.shape()), dst.data_type()); |
| 225 | |
| 226 | Coordinates id_src1{}; |
| 227 | Coordinates id_src2{}; |
| 228 | Coordinates id_dst{}; |
| 229 | |
| 230 | BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1_tmp, src2_tmp, dst_tmp, convert_policy, id_src1, id_src2, id_dst); |
| 231 | |
| 232 | dst = convert_to_symmetric<int16_t>(dst_tmp, dst.quantization_info()); |
| 233 | return dst; |
| 234 | } |
| 235 | else |
| 236 | { |
| 237 | // DataType::S16 |
| 238 | Coordinates id_src1{}; |
| 239 | Coordinates id_src2{}; |
| 240 | Coordinates id_dst{}; |
| 241 | |
| 242 | BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1, src2, dst, convert_policy, id_src1, id_src2, id_dst); |
| 243 | |
| 244 | return dst; |
| 245 | } |
| 246 | } |
| 247 | |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 248 | template SimpleTensor<int32_t> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<int32_t> &src1, const SimpleTensor<int32_t> &src2, SimpleTensor<int32_t> &dst, |
| 249 | ConvertPolicy convert_policy); |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 250 | template SimpleTensor<half> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<half> &src1, const SimpleTensor<half> &src2, SimpleTensor<half> &dst, ConvertPolicy convert_policy); |
| 251 | template SimpleTensor<float> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<float> &src1, const SimpleTensor<float> &src2, SimpleTensor<float> &dst, ConvertPolicy convert_policy); |
| 252 | |
| 253 | template <typename T> |
| 254 | SimpleTensor<T> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<T> &src1, const SimpleTensor<T> &src2, DataType dst_data_type, ConvertPolicy convert_policy) |
| 255 | { |
Michele Di Giorgio | 6997fc9 | 2019-06-18 10:23:22 +0100 | [diff] [blame] | 256 | 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] | 257 | |
| 258 | SimpleTensor<T> dst(TensorShape::broadcast_shape(src1.shape(), src2.shape()), dst_data_type); |
| 259 | arithmetic_operation<T>(op, src1, src2, dst, convert_policy); |
| 260 | return dst; |
| 261 | } |
| 262 | |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 263 | template SimpleTensor<int32_t> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<int32_t> &src1, const SimpleTensor<int32_t> &src2, DataType dst_data_type, |
| 264 | ConvertPolicy convert_policy); |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 265 | template SimpleTensor<int16_t> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<int16_t> &src1, const SimpleTensor<int16_t> &src2, DataType dst_data_type, |
| 266 | ConvertPolicy convert_policy); |
| 267 | 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); |
| 268 | template SimpleTensor<half> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<half> &src1, const SimpleTensor<half> &src2, DataType dst_data_type, ConvertPolicy convert_policy); |
| 269 | template SimpleTensor<float> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<float> &src1, const SimpleTensor<float> &src2, DataType dst_data_type, ConvertPolicy convert_policy); |
| 270 | |
| 271 | } // namespace reference |
| 272 | } // namespace validation |
| 273 | } // namespace test |
| 274 | } // namespace arm_compute |