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...> |
Manuel Bottini | 80feed5 | 2020-06-03 13:20:41 +0100 | [diff] [blame] | 47 | void setup(TensorShape input_shape, DataType input_data_type, bool in_place, ElementWiseUnary op) |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 48 | { |
| 49 | _op = op; |
Manuel Bottini | 80feed5 | 2020-06-03 13:20:41 +0100 | [diff] [blame] | 50 | _target = compute_target(input_shape, input_data_type, in_place); |
Manuel Bottini | ed75326 | 2019-05-15 15:30:47 +0100 | [diff] [blame] | 51 | _reference = compute_reference(input_shape, input_data_type); |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | protected: |
| 55 | template <typename U> |
Usama Arif | f6e475c | 2019-05-10 12:06:28 +0100 | [diff] [blame] | 56 | void fill(U &&tensor, int i, DataType data_type) |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 57 | { |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 58 | 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^] | 59 | 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] | 60 | |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 61 | switch(_op) |
| 62 | { |
| 63 | case ElementWiseUnary::EXP: |
| 64 | { |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 65 | FloatDistributionType distribution{ FloatType(-1.0f), FloatType(1.0f) }; |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 66 | library->fill(tensor, distribution, i); |
| 67 | break; |
| 68 | } |
| 69 | case ElementWiseUnary::RSQRT: |
| 70 | { |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 71 | FloatDistributionType distribution{ FloatType(1.0f), FloatType(2.0f) }; |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 72 | library->fill(tensor, distribution, i); |
| 73 | break; |
| 74 | } |
Manuel Bottini | 6ac5992 | 2019-05-15 14:06:02 +0100 | [diff] [blame] | 75 | case ElementWiseUnary::ABS: |
Usama Arif | f6e475c | 2019-05-10 12:06:28 +0100 | [diff] [blame] | 76 | case ElementWiseUnary::NEG: |
| 77 | { |
| 78 | switch(data_type) |
| 79 | { |
Usama Arif | f6e475c | 2019-05-10 12:06:28 +0100 | [diff] [blame] | 80 | case DataType::F16: |
| 81 | { |
Giorgio Arena | a8e2aeb | 2021-01-06 11:34:57 +0000 | [diff] [blame] | 82 | 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] | 83 | library->fill(tensor, distribution, i); |
| 84 | break; |
| 85 | } |
| 86 | case DataType::F32: |
| 87 | { |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 88 | FloatDistributionType distribution{ FloatType(-2.0f), FloatType(2.0f) }; |
Usama Arif | f6e475c | 2019-05-10 12:06:28 +0100 | [diff] [blame] | 89 | library->fill(tensor, distribution, i); |
| 90 | break; |
| 91 | } |
| 92 | case DataType::S32: |
| 93 | { |
| 94 | std::uniform_int_distribution<int32_t> distribution(-100, 100); |
| 95 | library->fill(tensor, distribution, i); |
| 96 | break; |
| 97 | } |
| 98 | default: |
| 99 | ARM_COMPUTE_ERROR("DataType for Elementwise Negation Not implemented"); |
| 100 | } |
| 101 | break; |
| 102 | } |
Usama Arif | c255aa7 | 2019-05-13 16:26:29 +0100 | [diff] [blame] | 103 | case ElementWiseUnary::LOG: |
| 104 | { |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 105 | FloatDistributionType distribution{ FloatType(0.0000001f), FloatType(100.0f) }; |
Usama Arif | c255aa7 | 2019-05-13 16:26:29 +0100 | [diff] [blame] | 106 | library->fill(tensor, distribution, i); |
| 107 | break; |
| 108 | } |
Michalis Spyrou | 0af4418 | 2019-05-17 14:04:47 +0100 | [diff] [blame] | 109 | case ElementWiseUnary::SIN: |
| 110 | { |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 111 | FloatDistributionType distribution{ FloatType(-100.00f), FloatType(100.00f) }; |
Michalis Spyrou | 0af4418 | 2019-05-17 14:04:47 +0100 | [diff] [blame] | 112 | library->fill(tensor, distribution, i); |
| 113 | break; |
| 114 | } |
Usama Arif | 0a5a57a | 2019-05-23 14:20:33 +0100 | [diff] [blame] | 115 | case ElementWiseUnary::ROUND: |
| 116 | { |
Giorgio Arena | 4bdd177 | 2020-12-17 16:47:07 +0000 | [diff] [blame] | 117 | FloatDistributionType distribution{ FloatType(100.0f), FloatType(-100.0f) }; |
Usama Arif | 0a5a57a | 2019-05-23 14:20:33 +0100 | [diff] [blame] | 118 | library->fill(tensor, distribution, i); |
| 119 | break; |
| 120 | } |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 121 | default: |
| 122 | ARM_COMPUTE_ERROR("Not implemented"); |
| 123 | } |
| 124 | } |
| 125 | |
Manuel Bottini | 80feed5 | 2020-06-03 13:20:41 +0100 | [diff] [blame] | 126 | TensorType compute_target(const TensorShape &shape, DataType data_type, bool in_place) |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 127 | { |
| 128 | // Create tensors |
| 129 | TensorType src = create_tensor<TensorType>(shape, data_type); |
| 130 | TensorType dst = create_tensor<TensorType>(shape, data_type); |
| 131 | |
Manuel Bottini | 80feed5 | 2020-06-03 13:20:41 +0100 | [diff] [blame] | 132 | TensorType *actual_dst = in_place ? &src : &dst; |
| 133 | |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 134 | // Create and configure function |
| 135 | FunctionType elwiseunary_layer; |
Manuel Bottini | 80feed5 | 2020-06-03 13:20:41 +0100 | [diff] [blame] | 136 | elwiseunary_layer.configure(&src, actual_dst); |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 137 | |
| 138 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 139 | src.allocator()->allocate(); |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 140 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
Manuel Bottini | 80feed5 | 2020-06-03 13:20:41 +0100 | [diff] [blame] | 141 | if(!in_place) |
| 142 | { |
| 143 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 144 | dst.allocator()->allocate(); |
| 145 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 146 | } |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 147 | |
| 148 | // Fill tensors |
Usama Arif | f6e475c | 2019-05-10 12:06:28 +0100 | [diff] [blame] | 149 | fill(AccessorType(src), 0, data_type); |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 150 | |
| 151 | // Compute function |
| 152 | elwiseunary_layer.run(); |
| 153 | |
Manuel Bottini | 80feed5 | 2020-06-03 13:20:41 +0100 | [diff] [blame] | 154 | if(in_place) |
| 155 | { |
| 156 | return src; |
| 157 | } |
| 158 | else |
| 159 | { |
| 160 | return dst; |
| 161 | } |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | SimpleTensor<T> compute_reference(const TensorShape &shape, DataType data_type) |
| 165 | { |
| 166 | // Create reference |
| 167 | SimpleTensor<T> src{ shape, data_type }; |
| 168 | |
| 169 | // Fill reference |
Usama Arif | f6e475c | 2019-05-10 12:06:28 +0100 | [diff] [blame] | 170 | fill(src, 0, data_type); |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 171 | |
| 172 | return reference::elementwise_unary<T>(src, _op); |
| 173 | } |
| 174 | |
| 175 | TensorType _target{}; |
| 176 | SimpleTensor<T> _reference{}; |
| 177 | ElementWiseUnary _op{}; |
| 178 | }; |
| 179 | |
| 180 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 181 | class RsqrtValidationFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T> |
| 182 | { |
| 183 | public: |
| 184 | template <typename...> |
| 185 | void setup(const TensorShape &shape, DataType data_type) |
| 186 | { |
Manuel Bottini | 80feed5 | 2020-06-03 13:20:41 +0100 | [diff] [blame] | 187 | ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, false, ElementWiseUnary::RSQRT); |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 188 | } |
| 189 | }; |
| 190 | |
| 191 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 192 | class ExpValidationFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T> |
| 193 | { |
| 194 | public: |
| 195 | template <typename...> |
| 196 | void setup(const TensorShape &shape, DataType data_type) |
| 197 | { |
Manuel Bottini | 80feed5 | 2020-06-03 13:20:41 +0100 | [diff] [blame] | 198 | ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, false, ElementWiseUnary::EXP); |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 199 | } |
| 200 | }; |
Usama Arif | f6e475c | 2019-05-10 12:06:28 +0100 | [diff] [blame] | 201 | |
| 202 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 203 | class NegValidationFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T> |
| 204 | { |
| 205 | public: |
| 206 | template <typename...> |
| 207 | void setup(const TensorShape &shape, DataType data_type) |
| 208 | { |
Manuel Bottini | 80feed5 | 2020-06-03 13:20:41 +0100 | [diff] [blame] | 209 | ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, false, ElementWiseUnary::NEG); |
| 210 | } |
| 211 | }; |
| 212 | |
| 213 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 214 | class NegValidationInPlaceFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T> |
| 215 | { |
| 216 | public: |
| 217 | template <typename...> |
| 218 | void setup(const TensorShape &shape, DataType data_type, bool in_place) |
| 219 | { |
| 220 | 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] | 221 | } |
| 222 | }; |
Usama Arif | c255aa7 | 2019-05-13 16:26:29 +0100 | [diff] [blame] | 223 | |
| 224 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 225 | class LogValidationFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T> |
| 226 | { |
| 227 | public: |
| 228 | template <typename...> |
| 229 | void setup(const TensorShape &shape, DataType data_type) |
| 230 | { |
Manuel Bottini | 80feed5 | 2020-06-03 13:20:41 +0100 | [diff] [blame] | 231 | ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, false, ElementWiseUnary::LOG); |
Usama Arif | c255aa7 | 2019-05-13 16:26:29 +0100 | [diff] [blame] | 232 | } |
| 233 | }; |
Manuel Bottini | 6ac5992 | 2019-05-15 14:06:02 +0100 | [diff] [blame] | 234 | |
| 235 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 236 | class AbsValidationFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T> |
| 237 | { |
| 238 | public: |
| 239 | template <typename...> |
| 240 | void setup(const TensorShape &shape, DataType data_type) |
| 241 | { |
Manuel Bottini | 80feed5 | 2020-06-03 13:20:41 +0100 | [diff] [blame] | 242 | ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, false, ElementWiseUnary::ABS); |
Manuel Bottini | 6ac5992 | 2019-05-15 14:06:02 +0100 | [diff] [blame] | 243 | } |
| 244 | }; |
Michalis Spyrou | 0af4418 | 2019-05-17 14:04:47 +0100 | [diff] [blame] | 245 | |
| 246 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 247 | class SinValidationFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T> |
| 248 | { |
| 249 | public: |
| 250 | template <typename...> |
| 251 | void setup(const TensorShape &shape, DataType data_type) |
| 252 | { |
Manuel Bottini | 80feed5 | 2020-06-03 13:20:41 +0100 | [diff] [blame] | 253 | ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, false, ElementWiseUnary::SIN); |
Michalis Spyrou | 0af4418 | 2019-05-17 14:04:47 +0100 | [diff] [blame] | 254 | } |
| 255 | }; |
Usama Arif | 0a5a57a | 2019-05-23 14:20:33 +0100 | [diff] [blame] | 256 | |
| 257 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 258 | class RoundValidationFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T> |
| 259 | { |
| 260 | public: |
| 261 | template <typename...> |
| 262 | void setup(const TensorShape &shape, DataType data_type) |
| 263 | { |
Manuel Bottini | 80feed5 | 2020-06-03 13:20:41 +0100 | [diff] [blame] | 264 | ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, false, ElementWiseUnary::ROUND); |
Usama Arif | 0a5a57a | 2019-05-23 14:20:33 +0100 | [diff] [blame] | 265 | } |
| 266 | }; |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 267 | } // namespace validation |
| 268 | } // namespace test |
| 269 | } // namespace arm_compute |
| 270 | #endif /* ARM_COMPUTE_TEST_ELEMENTWISE_UNARY_FIXTURE */ |