1 /* 2 * Copyright (c) 2018-2022 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_ARG_MIN_MAX_FIXTURE 25 #define ARM_COMPUTE_TEST_ARG_MIN_MAX_FIXTURE 26 27 #include "arm_compute/core/TensorShape.h" 28 #include "arm_compute/core/Types.h" 29 #include "arm_compute/core/utils/misc/ShapeCalculator.h" 30 #include "arm_compute/runtime/Tensor.h" 31 #include "tests/AssetsLibrary.h" 32 #include "tests/Globals.h" 33 #include "tests/IAccessor.h" 34 #include "tests/framework/Asserts.h" 35 #include "tests/framework/Fixture.h" 36 #include "tests/validation/Helpers.h" 37 #include "tests/validation/reference/ReductionOperation.h" 38 39 namespace arm_compute 40 { 41 namespace test 42 { 43 namespace validation 44 { 45 template <typename TensorType, typename AccessorType, typename FunctionType, typename T> 46 class ArgMinMaxValidationBaseFixture : public framework::Fixture 47 { 48 public: 49 template <typename...> setup(TensorShape shape,DataType data_type,int axis,ReductionOperation op,QuantizationInfo q_info)50 void setup(TensorShape shape, DataType data_type, int axis, ReductionOperation op, QuantizationInfo q_info) 51 { 52 _target = compute_target(shape, data_type, axis, op, q_info); 53 _reference = compute_reference(shape, data_type, axis, op, q_info); 54 } 55 56 protected: 57 template <typename U> fill(U && tensor)58 void fill(U &&tensor) 59 { 60 switch(tensor.data_type()) 61 { 62 case DataType::F16: 63 { 64 arm_compute::utils::uniform_real_distribution_16bit<half> distribution{ -1.0f, 1.0f }; 65 library->fill(tensor, distribution, 0); 66 break; 67 } 68 case DataType::F32: 69 { 70 std::uniform_real_distribution<float> distribution(-1.0f, 1.0f); 71 library->fill(tensor, distribution, 0); 72 break; 73 } 74 case DataType::S32: 75 { 76 std::uniform_int_distribution<int32_t> distribution(-100, 100); 77 library->fill(tensor, distribution, 0); 78 break; 79 } 80 case DataType::QASYMM8: 81 { 82 std::pair<int, int> bounds = get_quantized_bounds(tensor.quantization_info(), -1.0f, 1.0f); 83 std::uniform_int_distribution<uint32_t> distribution(bounds.first, bounds.second); 84 85 library->fill(tensor, distribution, 0); 86 break; 87 } 88 case DataType::QASYMM8_SIGNED: 89 { 90 std::pair<int, int> bounds = get_quantized_qasymm8_signed_bounds(tensor.quantization_info(), -1.0f, 1.0f); 91 std::uniform_int_distribution<int32_t> distribution(bounds.first, bounds.second); 92 93 library->fill(tensor, distribution, 0); 94 break; 95 } 96 default: 97 ARM_COMPUTE_ERROR("DataType for Elementwise Negation Not implemented"); 98 } 99 } 100 compute_target(TensorShape & src_shape,DataType data_type,int axis,ReductionOperation op,QuantizationInfo q_info)101 TensorType compute_target(TensorShape &src_shape, DataType data_type, int axis, ReductionOperation op, QuantizationInfo q_info) 102 { 103 // Create tensors 104 TensorType src = create_tensor<TensorType>(src_shape, data_type, 1, q_info); 105 TensorType dst; 106 107 // Create and configure function 108 FunctionType arg_min_max_layer; 109 arg_min_max_layer.configure(&src, axis, &dst, op); 110 111 ARM_COMPUTE_ASSERT(src.info()->is_resizable()); 112 ARM_COMPUTE_ASSERT(dst.info()->is_resizable()); 113 114 // Allocate tensors 115 src.allocator()->allocate(); 116 dst.allocator()->allocate(); 117 118 ARM_COMPUTE_ASSERT(!src.info()->is_resizable()); 119 ARM_COMPUTE_ASSERT(!dst.info()->is_resizable()); 120 121 // Fill tensors 122 fill(AccessorType(src)); 123 124 // Compute function 125 arg_min_max_layer.run(); 126 127 return dst; 128 } 129 compute_reference(TensorShape & src_shape,DataType data_type,int axis,ReductionOperation op,QuantizationInfo q_info)130 SimpleTensor<int32_t> compute_reference(TensorShape &src_shape, DataType data_type, int axis, ReductionOperation op, QuantizationInfo q_info) 131 { 132 // Create reference 133 SimpleTensor<T> src{ src_shape, data_type, 1, q_info }; 134 135 // Fill reference 136 fill(src); 137 138 TensorShape output_shape = arm_compute::misc::shape_calculator::compute_reduced_shape(src_shape, axis, false); 139 return reference::reduction_operation<T, int32_t>(src, output_shape, axis, op); 140 } 141 142 TensorType _target{}; 143 SimpleTensor<int32_t> _reference{}; 144 }; 145 146 template <typename TensorType, typename AccessorType, typename FunctionType, typename T> 147 class ArgMinMaxValidationQuantizedFixture : public ArgMinMaxValidationBaseFixture<TensorType, AccessorType, FunctionType, T> 148 { 149 public: 150 template <typename...> setup(const TensorShape & shape,DataType data_type,int axis,ReductionOperation op,QuantizationInfo quantization_info)151 void setup(const TensorShape &shape, DataType data_type, int axis, ReductionOperation op, QuantizationInfo quantization_info) 152 { 153 ArgMinMaxValidationBaseFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, axis, op, quantization_info); 154 } 155 }; 156 157 template <typename TensorType, typename AccessorType, typename FunctionType, typename T> 158 class ArgMinMaxValidationFixture : public ArgMinMaxValidationBaseFixture<TensorType, AccessorType, FunctionType, T> 159 { 160 public: 161 template <typename...> setup(const TensorShape & shape,DataType data_type,int axis,ReductionOperation op)162 void setup(const TensorShape &shape, DataType data_type, int axis, ReductionOperation op) 163 { 164 ArgMinMaxValidationBaseFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, axis, op, QuantizationInfo()); 165 } 166 }; 167 } // namespace validation 168 } // namespace test 169 } // namespace arm_compute 170 #endif /* ARM_COMPUTE_TEST_ARG_MIN_MAX_FIXTURE */ 171