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