Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 1 | /* |
Giorgio Arena | b309fc2 | 2021-01-05 09:46:16 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2021 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 | */ |
| 24 | #ifndef ARM_COMPUTE_TEST_ELEMENTWISE_UNARY_FIXTURE |
| 25 | #define ARM_COMPUTE_TEST_ELEMENTWISE_UNARY_FIXTURE |
| 26 | |
| 27 | #include "arm_compute/core/TensorShape.h" |
| 28 | #include "arm_compute/core/Types.h" |
| 29 | #include "tests/AssetsLibrary.h" |
| 30 | #include "tests/Globals.h" |
| 31 | #include "tests/IAccessor.h" |
| 32 | #include "tests/framework/Asserts.h" |
| 33 | #include "tests/framework/Fixture.h" |
| 34 | #include "tests/validation/reference/ElementWiseUnary.h" |
| 35 | |
| 36 | namespace arm_compute |
| 37 | { |
| 38 | namespace test |
| 39 | { |
| 40 | namespace validation |
| 41 | { |
| 42 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 43 | class ElementWiseUnaryValidationFixture : public framework::Fixture |
| 44 | { |
| 45 | public: |
| 46 | template <typename...> |
Sang-Hoon Park | 668ccdc | 2021-02-03 10:32:59 +0000 | [diff] [blame^] | 47 | void setup(TensorShape input_shape, DataType input_data_type, bool in_place, ElementWiseUnary op, bool use_dynamic_shape = false) |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 48 | { |
Sang-Hoon Park | 668ccdc | 2021-02-03 10:32:59 +0000 | [diff] [blame^] | 49 | _op = op; |
| 50 | _target = compute_target(input_shape, input_data_type, in_place); |
| 51 | _reference = compute_reference(input_shape, input_data_type); |
| 52 | _use_dynamic_shape = use_dynamic_shape; |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | protected: |
| 56 | template <typename U> |
Usama Arif | f6e475c | 2019-05-10 12:06:28 +0100 | [diff] [blame] | 57 | void fill(U &&tensor, int i, DataType data_type) |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 58 | { |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 59 | using FloatType = typename std::conditional < std::is_same<T, half>::value || std::is_floating_point<T>::value, T, float >::type; |
Giorgio Arena | 33b103b | 2021-01-08 10:37:15 +0000 | [diff] [blame] | 60 | using FloatDistributionType = typename std::conditional<std::is_same<T, half>::value, arm_compute::utils::uniform_real_distribution_16bit<T>, std::uniform_real_distribution<FloatType>>::type; |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 61 | |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 62 | switch(_op) |
| 63 | { |
| 64 | case ElementWiseUnary::EXP: |
| 65 | { |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 66 | FloatDistributionType distribution{ FloatType(-1.0f), FloatType(1.0f) }; |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 67 | library->fill(tensor, distribution, i); |
| 68 | break; |
| 69 | } |
| 70 | case ElementWiseUnary::RSQRT: |
| 71 | { |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 72 | FloatDistributionType distribution{ FloatType(1.0f), FloatType(2.0f) }; |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 73 | library->fill(tensor, distribution, i); |
| 74 | break; |
| 75 | } |
Manuel Bottini | 6ac5992 | 2019-05-15 14:06:02 +0100 | [diff] [blame] | 76 | case ElementWiseUnary::ABS: |
Usama Arif | f6e475c | 2019-05-10 12:06:28 +0100 | [diff] [blame] | 77 | case ElementWiseUnary::NEG: |
| 78 | { |
| 79 | switch(data_type) |
| 80 | { |
Usama Arif | f6e475c | 2019-05-10 12:06:28 +0100 | [diff] [blame] | 81 | case DataType::F16: |
| 82 | { |
Giorgio Arena | a8e2aeb | 2021-01-06 11:34:57 +0000 | [diff] [blame] | 83 | arm_compute::utils::uniform_real_distribution_16bit<half> distribution{ -2.0f, 2.0f }; |
Giorgio Arena | 6aeb217 | 2020-12-15 15:45:43 +0000 | [diff] [blame] | 84 | library->fill(tensor, distribution, i); |
| 85 | break; |
| 86 | } |
| 87 | case DataType::F32: |
| 88 | { |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 89 | FloatDistributionType distribution{ FloatType(-2.0f), FloatType(2.0f) }; |
Usama Arif | f6e475c | 2019-05-10 12:06:28 +0100 | [diff] [blame] | 90 | library->fill(tensor, distribution, i); |
| 91 | break; |
| 92 | } |
| 93 | case DataType::S32: |
| 94 | { |
| 95 | std::uniform_int_distribution<int32_t> distribution(-100, 100); |
| 96 | library->fill(tensor, distribution, i); |
| 97 | break; |
| 98 | } |
| 99 | default: |
| 100 | ARM_COMPUTE_ERROR("DataType for Elementwise Negation Not implemented"); |
| 101 | } |
| 102 | break; |
| 103 | } |
Usama Arif | c255aa7 | 2019-05-13 16:26:29 +0100 | [diff] [blame] | 104 | case ElementWiseUnary::LOG: |
| 105 | { |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 106 | FloatDistributionType distribution{ FloatType(0.0000001f), FloatType(100.0f) }; |
Usama Arif | c255aa7 | 2019-05-13 16:26:29 +0100 | [diff] [blame] | 107 | library->fill(tensor, distribution, i); |
| 108 | break; |
| 109 | } |
Michalis Spyrou | 0af4418 | 2019-05-17 14:04:47 +0100 | [diff] [blame] | 110 | case ElementWiseUnary::SIN: |
| 111 | { |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 112 | FloatDistributionType distribution{ FloatType(-100.00f), FloatType(100.00f) }; |
Michalis Spyrou | 0af4418 | 2019-05-17 14:04:47 +0100 | [diff] [blame] | 113 | library->fill(tensor, distribution, i); |
| 114 | break; |
| 115 | } |
Usama Arif | 0a5a57a | 2019-05-23 14:20:33 +0100 | [diff] [blame] | 116 | case ElementWiseUnary::ROUND: |
| 117 | { |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 118 | FloatDistributionType distribution{ FloatType(100.0f), FloatType(-100.0f) }; |
Usama Arif | 0a5a57a | 2019-05-23 14:20:33 +0100 | [diff] [blame] | 119 | library->fill(tensor, distribution, i); |
| 120 | break; |
| 121 | } |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 122 | default: |
| 123 | ARM_COMPUTE_ERROR("Not implemented"); |
| 124 | } |
| 125 | } |
| 126 | |
Manuel Bottini | 80feed5 | 2020-06-03 13:20:41 +0100 | [diff] [blame] | 127 | TensorType compute_target(const TensorShape &shape, DataType data_type, bool in_place) |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 128 | { |
| 129 | // Create tensors |
| 130 | TensorType src = create_tensor<TensorType>(shape, data_type); |
| 131 | TensorType dst = create_tensor<TensorType>(shape, data_type); |
| 132 | |
Manuel Bottini | 80feed5 | 2020-06-03 13:20:41 +0100 | [diff] [blame] | 133 | TensorType *actual_dst = in_place ? &src : &dst; |
| 134 | |
Sang-Hoon Park | 668ccdc | 2021-02-03 10:32:59 +0000 | [diff] [blame^] | 135 | // if _use_dynamic_shape is true, this fixture will test scenario for dynamic shapes. |
| 136 | // - At configure time, all input tensors are marked as dynamic using set_tensor_dynamic() |
| 137 | // - After configure, tensors are marked as static for run using set_tensor_static() |
| 138 | // - The tensors with static shape are given to run() |
| 139 | if(_use_dynamic_shape) |
| 140 | { |
| 141 | set_tensor_dynamic(src); |
| 142 | } |
| 143 | |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 144 | // Create and configure function |
| 145 | FunctionType elwiseunary_layer; |
Manuel Bottini | 80feed5 | 2020-06-03 13:20:41 +0100 | [diff] [blame] | 146 | elwiseunary_layer.configure(&src, actual_dst); |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 147 | |
Sang-Hoon Park | 668ccdc | 2021-02-03 10:32:59 +0000 | [diff] [blame^] | 148 | if(_use_dynamic_shape) |
| 149 | { |
| 150 | set_tensor_static(src); |
| 151 | } |
| 152 | |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 153 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 154 | src.allocator()->allocate(); |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 155 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
Manuel Bottini | 80feed5 | 2020-06-03 13:20:41 +0100 | [diff] [blame] | 156 | if(!in_place) |
| 157 | { |
| 158 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 159 | dst.allocator()->allocate(); |
| 160 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 161 | } |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 162 | |
| 163 | // Fill tensors |
Usama Arif | f6e475c | 2019-05-10 12:06:28 +0100 | [diff] [blame] | 164 | fill(AccessorType(src), 0, data_type); |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 165 | |
| 166 | // Compute function |
| 167 | elwiseunary_layer.run(); |
| 168 | |
Manuel Bottini | 80feed5 | 2020-06-03 13:20:41 +0100 | [diff] [blame] | 169 | if(in_place) |
| 170 | { |
| 171 | return src; |
| 172 | } |
| 173 | else |
| 174 | { |
| 175 | return dst; |
| 176 | } |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | SimpleTensor<T> compute_reference(const TensorShape &shape, DataType data_type) |
| 180 | { |
| 181 | // Create reference |
| 182 | SimpleTensor<T> src{ shape, data_type }; |
| 183 | |
| 184 | // Fill reference |
Usama Arif | f6e475c | 2019-05-10 12:06:28 +0100 | [diff] [blame] | 185 | fill(src, 0, data_type); |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 186 | |
| 187 | return reference::elementwise_unary<T>(src, _op); |
| 188 | } |
| 189 | |
| 190 | TensorType _target{}; |
| 191 | SimpleTensor<T> _reference{}; |
| 192 | ElementWiseUnary _op{}; |
Sang-Hoon Park | 668ccdc | 2021-02-03 10:32:59 +0000 | [diff] [blame^] | 193 | bool _use_dynamic_shape{ false }; |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 194 | }; |
| 195 | |
| 196 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 197 | class RsqrtValidationFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T> |
| 198 | { |
| 199 | public: |
| 200 | template <typename...> |
| 201 | void setup(const TensorShape &shape, DataType data_type) |
| 202 | { |
Manuel Bottini | 80feed5 | 2020-06-03 13:20:41 +0100 | [diff] [blame] | 203 | ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, false, ElementWiseUnary::RSQRT); |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 204 | } |
| 205 | }; |
| 206 | |
| 207 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
Sang-Hoon Park | 668ccdc | 2021-02-03 10:32:59 +0000 | [diff] [blame^] | 208 | class RsqrtDynamicShapeValidationFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T> |
| 209 | { |
| 210 | public: |
| 211 | template <typename...> |
| 212 | void setup(const TensorShape &shape, DataType data_type) |
| 213 | { |
| 214 | ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, false, ElementWiseUnary::RSQRT, true); |
| 215 | } |
| 216 | }; |
| 217 | |
| 218 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 219 | class ExpValidationFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T> |
| 220 | { |
| 221 | public: |
| 222 | template <typename...> |
| 223 | void setup(const TensorShape &shape, DataType data_type) |
| 224 | { |
Manuel Bottini | 80feed5 | 2020-06-03 13:20:41 +0100 | [diff] [blame] | 225 | ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, false, ElementWiseUnary::EXP); |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 226 | } |
| 227 | }; |
Usama Arif | f6e475c | 2019-05-10 12:06:28 +0100 | [diff] [blame] | 228 | |
| 229 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 230 | class NegValidationFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T> |
| 231 | { |
| 232 | public: |
| 233 | template <typename...> |
| 234 | void setup(const TensorShape &shape, DataType data_type) |
| 235 | { |
Manuel Bottini | 80feed5 | 2020-06-03 13:20:41 +0100 | [diff] [blame] | 236 | ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, false, ElementWiseUnary::NEG); |
| 237 | } |
| 238 | }; |
| 239 | |
| 240 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 241 | class NegValidationInPlaceFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T> |
| 242 | { |
| 243 | public: |
| 244 | template <typename...> |
| 245 | void setup(const TensorShape &shape, DataType data_type, bool in_place) |
| 246 | { |
| 247 | ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, in_place, ElementWiseUnary::NEG); |
Usama Arif | f6e475c | 2019-05-10 12:06:28 +0100 | [diff] [blame] | 248 | } |
| 249 | }; |
Usama Arif | c255aa7 | 2019-05-13 16:26:29 +0100 | [diff] [blame] | 250 | |
| 251 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 252 | class LogValidationFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T> |
| 253 | { |
| 254 | public: |
| 255 | template <typename...> |
| 256 | void setup(const TensorShape &shape, DataType data_type) |
| 257 | { |
Manuel Bottini | 80feed5 | 2020-06-03 13:20:41 +0100 | [diff] [blame] | 258 | ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, false, ElementWiseUnary::LOG); |
Usama Arif | c255aa7 | 2019-05-13 16:26:29 +0100 | [diff] [blame] | 259 | } |
| 260 | }; |
Manuel Bottini | 6ac5992 | 2019-05-15 14:06:02 +0100 | [diff] [blame] | 261 | |
| 262 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 263 | class AbsValidationFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T> |
| 264 | { |
| 265 | public: |
| 266 | template <typename...> |
| 267 | void setup(const TensorShape &shape, DataType data_type) |
| 268 | { |
Manuel Bottini | 80feed5 | 2020-06-03 13:20:41 +0100 | [diff] [blame] | 269 | ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, false, ElementWiseUnary::ABS); |
Manuel Bottini | 6ac5992 | 2019-05-15 14:06:02 +0100 | [diff] [blame] | 270 | } |
| 271 | }; |
Michalis Spyrou | 0af4418 | 2019-05-17 14:04:47 +0100 | [diff] [blame] | 272 | |
| 273 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 274 | class SinValidationFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T> |
| 275 | { |
| 276 | public: |
| 277 | template <typename...> |
| 278 | void setup(const TensorShape &shape, DataType data_type) |
| 279 | { |
Manuel Bottini | 80feed5 | 2020-06-03 13:20:41 +0100 | [diff] [blame] | 280 | ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, false, ElementWiseUnary::SIN); |
Michalis Spyrou | 0af4418 | 2019-05-17 14:04:47 +0100 | [diff] [blame] | 281 | } |
| 282 | }; |
Usama Arif | 0a5a57a | 2019-05-23 14:20:33 +0100 | [diff] [blame] | 283 | |
| 284 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 285 | class RoundValidationFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T> |
| 286 | { |
| 287 | public: |
| 288 | template <typename...> |
| 289 | void setup(const TensorShape &shape, DataType data_type) |
| 290 | { |
Manuel Bottini | 80feed5 | 2020-06-03 13:20:41 +0100 | [diff] [blame] | 291 | ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, false, ElementWiseUnary::ROUND); |
Usama Arif | 0a5a57a | 2019-05-23 14:20:33 +0100 | [diff] [blame] | 292 | } |
| 293 | }; |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 294 | } // namespace validation |
| 295 | } // namespace test |
| 296 | } // namespace arm_compute |
| 297 | #endif /* ARM_COMPUTE_TEST_ELEMENTWISE_UNARY_FIXTURE */ |