Georgios Pinitas | cbf39c6 | 2018-09-10 15:07:45 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017-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_ARITHMETIC_OPERATIONS_FIXTURE |
| 25 | #define ARM_COMPUTE_TEST_ARITHMETIC_OPERATIONS_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/Helpers.h" |
| 35 | #include "tests/validation/reference/ArithmeticOperations.h" |
| 36 | |
| 37 | namespace arm_compute |
| 38 | { |
| 39 | namespace test |
| 40 | { |
| 41 | namespace validation |
| 42 | { |
| 43 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 44 | class ArithmeticOperationGenericFixture : public framework::Fixture |
| 45 | { |
| 46 | public: |
| 47 | template <typename...> |
| 48 | void setup(reference::ArithmeticOperation op, const TensorShape &shape0, const TensorShape &shape1, |
| 49 | DataType data_type0, DataType data_type1, DataType output_data_type, ConvertPolicy convert_policy, |
| 50 | QuantizationInfo qinfo0, QuantizationInfo qinfo1, QuantizationInfo qinfo_out) |
| 51 | { |
| 52 | _op = op; |
| 53 | _target = compute_target(shape0, shape1, data_type0, data_type1, output_data_type, convert_policy, qinfo0, qinfo1, qinfo_out); |
| 54 | _reference = compute_reference(shape0, shape1, data_type0, data_type1, output_data_type, convert_policy, qinfo0, qinfo1, qinfo_out); |
| 55 | } |
| 56 | |
| 57 | protected: |
| 58 | template <typename U> |
| 59 | void fill(U &&tensor, int i) |
| 60 | { |
| 61 | library->fill_tensor_uniform(tensor, i); |
| 62 | } |
| 63 | |
| 64 | TensorType compute_target(const TensorShape &shape0, const TensorShape &shape1, DataType data_type0, DataType data_type1, DataType output_data_type, ConvertPolicy convert_policy, |
| 65 | QuantizationInfo qinfo0, QuantizationInfo qinfo1, QuantizationInfo qinfo_out) |
| 66 | { |
| 67 | // Create tensors |
| 68 | TensorType ref_src1 = create_tensor<TensorType>(shape0, data_type0, 1, qinfo0); |
| 69 | TensorType ref_src2 = create_tensor<TensorType>(shape1, data_type1, 1, qinfo1); |
| 70 | TensorType dst = create_tensor<TensorType>(TensorShape::broadcast_shape(shape0, shape1), output_data_type, 1, qinfo_out); |
| 71 | |
| 72 | // Create and configure function |
| 73 | FunctionType arith_op; |
| 74 | arith_op.configure(&ref_src1, &ref_src2, &dst, convert_policy); |
| 75 | |
| 76 | ARM_COMPUTE_EXPECT(ref_src1.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 77 | ARM_COMPUTE_EXPECT(ref_src2.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 78 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 79 | |
| 80 | // Allocate tensors |
| 81 | ref_src1.allocator()->allocate(); |
| 82 | ref_src2.allocator()->allocate(); |
| 83 | dst.allocator()->allocate(); |
| 84 | |
| 85 | ARM_COMPUTE_EXPECT(!ref_src1.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 86 | ARM_COMPUTE_EXPECT(!ref_src2.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 87 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 88 | |
| 89 | // Fill tensors |
| 90 | fill(AccessorType(ref_src1), 0); |
| 91 | fill(AccessorType(ref_src2), 1); |
| 92 | |
| 93 | // Compute function |
| 94 | arith_op.run(); |
| 95 | |
| 96 | return dst; |
| 97 | } |
| 98 | |
| 99 | SimpleTensor<T> compute_reference(const TensorShape &shape0, const TensorShape &shape1, |
| 100 | DataType data_type0, DataType data_type1, DataType output_data_type, ConvertPolicy convert_policy, |
| 101 | QuantizationInfo qinfo0, QuantizationInfo qinfo1, QuantizationInfo qinfo_out) |
| 102 | { |
| 103 | // Create reference |
| 104 | SimpleTensor<T> ref_src1{ shape0, data_type0, 1, qinfo0 }; |
| 105 | SimpleTensor<T> ref_src2{ shape1, data_type1, 1, qinfo1 }; |
| 106 | SimpleTensor<T> ref_dst{ TensorShape::broadcast_shape(shape0, shape1), output_data_type, 1, qinfo_out }; |
| 107 | |
| 108 | // Fill reference |
| 109 | fill(ref_src1, 0); |
| 110 | fill(ref_src2, 1); |
| 111 | |
| 112 | return reference::arithmetic_operation<T>(_op, ref_src1, ref_src2, ref_dst, convert_policy); |
| 113 | } |
| 114 | |
| 115 | TensorType _target{}; |
| 116 | SimpleTensor<T> _reference{}; |
| 117 | reference::ArithmeticOperation _op{ reference::ArithmeticOperation::ADD }; |
| 118 | }; |
| 119 | |
| 120 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 121 | class ArithmeticAdditionBroadcastValidationFixture : public ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T> |
| 122 | { |
| 123 | public: |
| 124 | template <typename...> |
| 125 | void setup(const TensorShape &shape0, const TensorShape &shape1, DataType data_type0, DataType data_type1, DataType output_data_type, ConvertPolicy convert_policy) |
| 126 | { |
| 127 | ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(reference::ArithmeticOperation::ADD, shape0, shape1, data_type0, data_type1, |
| 128 | output_data_type, convert_policy, QuantizationInfo(), QuantizationInfo(), QuantizationInfo()); |
| 129 | } |
| 130 | }; |
| 131 | |
| 132 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 133 | class ArithmeticAdditionValidationFixture : public ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T> |
| 134 | { |
| 135 | public: |
| 136 | template <typename...> |
| 137 | void setup(const TensorShape &shape, DataType data_type0, DataType data_type1, DataType output_data_type, ConvertPolicy convert_policy) |
| 138 | { |
| 139 | ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(reference::ArithmeticOperation::ADD, shape, shape, data_type0, data_type1, |
| 140 | output_data_type, convert_policy, QuantizationInfo(), QuantizationInfo(), QuantizationInfo()); |
| 141 | } |
| 142 | }; |
| 143 | |
| 144 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 145 | class ArithmeticAdditionValidationQuantizedFixture : public ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T> |
| 146 | { |
| 147 | public: |
| 148 | template <typename...> |
| 149 | void setup(const TensorShape &shape, DataType data_type0, DataType data_type1, DataType output_data_type, ConvertPolicy convert_policy, |
| 150 | QuantizationInfo qinfo0, QuantizationInfo qinfo1, QuantizationInfo qinfo_out) |
| 151 | |
| 152 | { |
| 153 | ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(reference::ArithmeticOperation::ADD, shape, shape, data_type0, data_type1, |
| 154 | output_data_type, convert_policy, qinfo0, qinfo1, qinfo_out); |
| 155 | } |
| 156 | }; |
| 157 | |
| 158 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 159 | class ArithmeticSubtractionBroadcastValidationFixture : public ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T> |
| 160 | { |
| 161 | public: |
| 162 | template <typename...> |
| 163 | void setup(const TensorShape &shape0, const TensorShape &shape1, DataType data_type0, DataType data_type1, DataType output_data_type, ConvertPolicy convert_policy) |
| 164 | { |
| 165 | ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(reference::ArithmeticOperation::SUB, shape0, shape1, |
| 166 | data_type0, data_type1, output_data_type, convert_policy, |
| 167 | QuantizationInfo(), QuantizationInfo(), QuantizationInfo()); |
| 168 | } |
| 169 | }; |
| 170 | |
| 171 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 172 | class ArithmeticSubtractionValidationFixture : public ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T> |
| 173 | { |
| 174 | public: |
| 175 | template <typename...> |
| 176 | void setup(const TensorShape &shape, DataType data_type0, DataType data_type1, DataType output_data_type, ConvertPolicy convert_policy) |
| 177 | { |
| 178 | ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(reference::ArithmeticOperation::SUB, shape, shape, |
| 179 | data_type0, data_type1, output_data_type, convert_policy, |
| 180 | QuantizationInfo(), QuantizationInfo(), QuantizationInfo()); |
| 181 | } |
| 182 | }; |
| 183 | |
| 184 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 185 | class ArithmeticSubtractionValidationQuantizedFixture : public ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T> |
| 186 | { |
| 187 | public: |
| 188 | template <typename...> |
| 189 | void setup(const TensorShape &shape, DataType data_type0, DataType data_type1, DataType output_data_type, ConvertPolicy convert_policy, |
| 190 | QuantizationInfo qinfo0, QuantizationInfo qinfo1, QuantizationInfo qinfo_out) |
| 191 | |
| 192 | { |
| 193 | ArithmeticOperationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(reference::ArithmeticOperation::SUB, shape, shape, |
| 194 | data_type0, data_type1, output_data_type, |
| 195 | convert_policy, qinfo0, qinfo1, qinfo_out); |
| 196 | } |
| 197 | }; |
| 198 | } // namespace validation |
| 199 | } // namespace test |
| 200 | } // namespace arm_compute |
| 201 | #endif /* ARM_COMPUTE_TEST_ARITHMETIC_OPERATIONS_FIXTURE */ |