Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 ARM Limited. |
| 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...> |
| 47 | void setup(TensorShape shape, DataType data_type, ElementWiseUnary op) |
| 48 | { |
| 49 | _op = op; |
| 50 | _target = compute_target(shape, data_type); |
| 51 | _reference = compute_reference(shape, data_type); |
| 52 | } |
| 53 | |
| 54 | protected: |
| 55 | template <typename U> |
| 56 | void fill(U &&tensor, int i) |
| 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 | } |
| 72 | default: |
| 73 | ARM_COMPUTE_ERROR("Not implemented"); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | TensorType compute_target(const TensorShape &shape, DataType data_type) |
| 78 | { |
| 79 | // Create tensors |
| 80 | TensorType src = create_tensor<TensorType>(shape, data_type); |
| 81 | TensorType dst = create_tensor<TensorType>(shape, data_type); |
| 82 | |
| 83 | // Create and configure function |
| 84 | FunctionType elwiseunary_layer; |
| 85 | |
| 86 | elwiseunary_layer.configure(&src, &dst); |
| 87 | |
| 88 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 89 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 90 | |
| 91 | // Allocate tensors |
| 92 | src.allocator()->allocate(); |
| 93 | dst.allocator()->allocate(); |
| 94 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 95 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 96 | |
| 97 | // Fill tensors |
| 98 | fill(AccessorType(src), 0); |
| 99 | |
| 100 | // Compute function |
| 101 | elwiseunary_layer.run(); |
| 102 | |
| 103 | return dst; |
| 104 | } |
| 105 | |
| 106 | SimpleTensor<T> compute_reference(const TensorShape &shape, DataType data_type) |
| 107 | { |
| 108 | // Create reference |
| 109 | SimpleTensor<T> src{ shape, data_type }; |
| 110 | |
| 111 | // Fill reference |
| 112 | fill(src, 0); |
| 113 | |
| 114 | return reference::elementwise_unary<T>(src, _op); |
| 115 | } |
| 116 | |
| 117 | TensorType _target{}; |
| 118 | SimpleTensor<T> _reference{}; |
| 119 | ElementWiseUnary _op{}; |
| 120 | }; |
| 121 | |
| 122 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 123 | class RsqrtValidationFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T> |
| 124 | { |
| 125 | public: |
| 126 | template <typename...> |
| 127 | void setup(const TensorShape &shape, DataType data_type) |
| 128 | { |
| 129 | ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, ElementWiseUnary::RSQRT); |
| 130 | } |
| 131 | }; |
| 132 | |
| 133 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 134 | class ExpValidationFixture : public ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T> |
| 135 | { |
| 136 | public: |
| 137 | template <typename...> |
| 138 | void setup(const TensorShape &shape, DataType data_type) |
| 139 | { |
| 140 | ElementWiseUnaryValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, ElementWiseUnary::EXP); |
| 141 | } |
| 142 | }; |
| 143 | } // namespace validation |
| 144 | } // namespace test |
| 145 | } // namespace arm_compute |
| 146 | #endif /* ARM_COMPUTE_TEST_ELEMENTWISE_UNARY_FIXTURE */ |