1 /* 2 * Copyright (c) 2018-2021 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_COMPARISON_FIXTURE 25 #define ARM_COMPUTE_TEST_COMPARISON_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/Comparisons.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 ComparisonValidationGenericFixture : public framework::Fixture 45 { 46 public: 47 template <typename...> setup(ComparisonOperation op,const TensorShape & shape0,const TensorShape & shape1,DataType data_type,QuantizationInfo qinfo0,QuantizationInfo qinfo1)48 void setup(ComparisonOperation op, const TensorShape &shape0, const TensorShape &shape1, DataType data_type, QuantizationInfo qinfo0, QuantizationInfo qinfo1) 49 { 50 _target = compute_target(op, shape0, shape1, data_type, qinfo0, qinfo1); 51 _reference = compute_reference(op, shape0, shape1, data_type, qinfo0, qinfo1); 52 } 53 54 protected: 55 template <typename U> fill(U && tensor,int i)56 void fill(U &&tensor, int i) 57 { 58 library->fill_tensor_uniform(tensor, i); 59 } 60 compute_target(ComparisonOperation op,const TensorShape & shape0,const TensorShape & shape1,DataType data_type,QuantizationInfo qinfo0,QuantizationInfo qinfo1)61 TensorType compute_target(ComparisonOperation op, 62 const TensorShape &shape0, const TensorShape &shape1, DataType data_type, 63 QuantizationInfo qinfo0, QuantizationInfo qinfo1) 64 { 65 // Create tensors 66 TensorType ref_src1 = create_tensor<TensorType>(shape0, data_type, 1, qinfo0); 67 TensorType ref_src2 = create_tensor<TensorType>(shape1, data_type, 1, qinfo1); 68 TensorType dst = create_tensor<TensorType>(TensorShape::broadcast_shape(shape0, shape1), DataType::U8); 69 70 // Create and configure function 71 FunctionType comp_op; 72 comp_op.configure(&ref_src1, &ref_src2, &dst, op); 73 74 ARM_COMPUTE_ASSERT(ref_src1.info()->is_resizable()); 75 ARM_COMPUTE_ASSERT(ref_src2.info()->is_resizable()); 76 ARM_COMPUTE_ASSERT(dst.info()->is_resizable()); 77 78 // Allocate tensors 79 ref_src1.allocator()->allocate(); 80 ref_src2.allocator()->allocate(); 81 dst.allocator()->allocate(); 82 83 ARM_COMPUTE_ASSERT(!ref_src1.info()->is_resizable()); 84 ARM_COMPUTE_ASSERT(!ref_src2.info()->is_resizable()); 85 ARM_COMPUTE_ASSERT(!dst.info()->is_resizable()); 86 87 // Fill tensors 88 fill(AccessorType(ref_src1), 0); 89 fill(AccessorType(ref_src2), 1); 90 91 // Compute function 92 comp_op.run(); 93 94 return dst; 95 } 96 compute_reference(ComparisonOperation op,const TensorShape & shape0,const TensorShape & shape1,DataType data_type,QuantizationInfo qinfo0,QuantizationInfo qinfo1)97 SimpleTensor<uint8_t> compute_reference(ComparisonOperation op, 98 const TensorShape &shape0, const TensorShape &shape1, DataType data_type, 99 QuantizationInfo qinfo0, QuantizationInfo qinfo1) 100 { 101 // Create reference 102 SimpleTensor<T> ref_src1{ shape0, data_type, 1, qinfo0 }; 103 SimpleTensor<T> ref_src2{ shape1, data_type, 1, qinfo1 }; 104 105 // Fill reference 106 fill(ref_src1, 0); 107 fill(ref_src2, 1); 108 109 return reference::compare<T>(op, ref_src1, ref_src2); 110 } 111 112 TensorType _target{}; 113 SimpleTensor<uint8_t> _reference{}; 114 }; 115 116 template <typename TensorType, typename AccessorType, typename FunctionType, typename T> 117 class ComparisonBroadcastValidationFixture : public ComparisonValidationGenericFixture<TensorType, AccessorType, FunctionType, T> 118 { 119 public: 120 template <typename...> setup(ComparisonOperation op,const TensorShape & shape0,const TensorShape & shape1,DataType data_type)121 void setup(ComparisonOperation op, const TensorShape &shape0, const TensorShape &shape1, DataType data_type) 122 { 123 ComparisonValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(op, shape0, shape1, data_type, QuantizationInfo(), QuantizationInfo()); 124 } 125 }; 126 127 template <typename TensorType, typename AccessorType, typename FunctionType, typename T> 128 class ComparisonValidationFixture : public ComparisonValidationGenericFixture<TensorType, AccessorType, FunctionType, T> 129 { 130 public: 131 template <typename...> setup(ComparisonOperation op,const TensorShape & shape,DataType data_type)132 void setup(ComparisonOperation op, const TensorShape &shape, DataType data_type) 133 { 134 ComparisonValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(op, shape, shape, data_type, QuantizationInfo(), QuantizationInfo()); 135 } 136 }; 137 138 template <typename TensorType, typename AccessorType, typename FunctionType, typename T> 139 class ComparisonValidationQuantizedFixture : public ComparisonValidationGenericFixture<TensorType, AccessorType, FunctionType, T> 140 { 141 public: 142 template <typename...> setup(ComparisonOperation op,const TensorShape & shape,DataType data_type,QuantizationInfo qinfo0,QuantizationInfo qinfo1)143 void setup(ComparisonOperation op, const TensorShape &shape, DataType data_type, QuantizationInfo qinfo0, QuantizationInfo qinfo1) 144 145 { 146 ComparisonValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(op, shape, shape, data_type, qinfo0, qinfo1); 147 } 148 }; 149 150 template <typename TensorType, typename AccessorType, typename FunctionType, typename T> 151 class ComparisonQuantizedBroadcastValidationFixture : public ComparisonValidationGenericFixture<TensorType, AccessorType, FunctionType, T> 152 { 153 public: 154 template <typename...> setup(ComparisonOperation op,const TensorShape & shape0,const TensorShape & shape1,DataType data_type,QuantizationInfo qinfo0,QuantizationInfo qinfo1)155 void setup(ComparisonOperation op, const TensorShape &shape0, const TensorShape &shape1, DataType data_type, QuantizationInfo qinfo0, QuantizationInfo qinfo1) 156 { 157 ComparisonValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(op, shape0, shape1, data_type, qinfo0, qinfo1); 158 } 159 }; 160 } // namespace validation 161 } // namespace test 162 } // namespace arm_compute 163 #endif /* ARM_COMPUTE_TEST_COMPARISON_FIXTURE */ 164