Michalis Spyrou | 7930db4 | 2018-11-22 17:36:28 +0000 | [diff] [blame] | 1 | /* |
Matthew Bentham | 945b8da | 2023-07-12 11:54:59 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2023 Arm Limited. |
Michalis Spyrou | 7930db4 | 2018-11-22 17:36:28 +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_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" |
Sang-Hoon Park | 2697fd8 | 2019-10-15 16:49:24 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Michalis Spyrou | 7930db4 | 2018-11-22 17:36:28 +0000 | [diff] [blame] | 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 | { |
Pablo Marquez Tello | 4cb0bd4 | 2023-07-27 18:02:37 +0100 | [diff] [blame] | 45 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T1, typename T2> |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 46 | class ArgMinMaxValidationBaseFixture : public framework::Fixture |
Michalis Spyrou | 7930db4 | 2018-11-22 17:36:28 +0000 | [diff] [blame] | 47 | { |
| 48 | public: |
Pablo Marquez Tello | 4cb0bd4 | 2023-07-27 18:02:37 +0100 | [diff] [blame] | 49 | void setup(TensorShape shape, DataType input_type, DataType output_type, int axis, ReductionOperation op, QuantizationInfo q_info) |
Michalis Spyrou | 7930db4 | 2018-11-22 17:36:28 +0000 | [diff] [blame] | 50 | { |
Pablo Marquez Tello | 4cb0bd4 | 2023-07-27 18:02:37 +0100 | [diff] [blame] | 51 | _target = compute_target(shape, input_type, output_type, axis, op, q_info); |
| 52 | _reference = compute_reference(shape, input_type, output_type, axis, op, q_info); |
Michalis Spyrou | 7930db4 | 2018-11-22 17:36:28 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | protected: |
| 56 | template <typename U> |
| 57 | void fill(U &&tensor) |
| 58 | { |
Michalis Spyrou | b9626ab | 2019-05-13 17:41:01 +0100 | [diff] [blame] | 59 | switch(tensor.data_type()) |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 60 | { |
Michalis Spyrou | b9626ab | 2019-05-13 17:41:01 +0100 | [diff] [blame] | 61 | case DataType::F16: |
| 62 | { |
Giorgio Arena | a8e2aeb | 2021-01-06 11:34:57 +0000 | [diff] [blame] | 63 | arm_compute::utils::uniform_real_distribution_16bit<half> distribution{ -1.0f, 1.0f }; |
Giorgio Arena | 6aeb217 | 2020-12-15 15:45:43 +0000 | [diff] [blame] | 64 | library->fill(tensor, distribution, 0); |
| 65 | break; |
| 66 | } |
| 67 | case DataType::F32: |
| 68 | { |
| 69 | std::uniform_real_distribution<float> distribution(-1.0f, 1.0f); |
Michalis Spyrou | b9626ab | 2019-05-13 17:41:01 +0100 | [diff] [blame] | 70 | library->fill(tensor, distribution, 0); |
| 71 | break; |
| 72 | } |
| 73 | case DataType::S32: |
| 74 | { |
| 75 | std::uniform_int_distribution<int32_t> distribution(-100, 100); |
| 76 | library->fill(tensor, distribution, 0); |
| 77 | break; |
| 78 | } |
| 79 | case DataType::QASYMM8: |
| 80 | { |
| 81 | std::pair<int, int> bounds = get_quantized_bounds(tensor.quantization_info(), -1.0f, 1.0f); |
Pablo Tello | 29cab36 | 2022-03-10 17:05:34 +0000 | [diff] [blame] | 82 | std::uniform_int_distribution<uint32_t> distribution(bounds.first, bounds.second); |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 83 | |
Michalis Spyrou | b9626ab | 2019-05-13 17:41:01 +0100 | [diff] [blame] | 84 | library->fill(tensor, distribution, 0); |
| 85 | break; |
| 86 | } |
Luca Foschiani | ee939fb | 2020-01-28 10:38:07 +0000 | [diff] [blame] | 87 | case DataType::QASYMM8_SIGNED: |
| 88 | { |
| 89 | std::pair<int, int> bounds = get_quantized_qasymm8_signed_bounds(tensor.quantization_info(), -1.0f, 1.0f); |
Pablo Tello | 29cab36 | 2022-03-10 17:05:34 +0000 | [diff] [blame] | 90 | std::uniform_int_distribution<int32_t> distribution(bounds.first, bounds.second); |
Luca Foschiani | ee939fb | 2020-01-28 10:38:07 +0000 | [diff] [blame] | 91 | |
| 92 | library->fill(tensor, distribution, 0); |
| 93 | break; |
| 94 | } |
Michalis Spyrou | b9626ab | 2019-05-13 17:41:01 +0100 | [diff] [blame] | 95 | default: |
| 96 | ARM_COMPUTE_ERROR("DataType for Elementwise Negation Not implemented"); |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 97 | } |
Michalis Spyrou | 7930db4 | 2018-11-22 17:36:28 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Pablo Marquez Tello | 4cb0bd4 | 2023-07-27 18:02:37 +0100 | [diff] [blame] | 100 | TensorType compute_target(TensorShape &src_shape, DataType input_type, DataType output_type, int axis, ReductionOperation op, QuantizationInfo q_info) |
Michalis Spyrou | 7930db4 | 2018-11-22 17:36:28 +0000 | [diff] [blame] | 101 | { |
| 102 | // Create tensors |
Pablo Marquez Tello | 4cb0bd4 | 2023-07-27 18:02:37 +0100 | [diff] [blame] | 103 | TensorType src = create_tensor<TensorType>(src_shape, input_type, 1, q_info); |
| 104 | TensorType dst = create_tensor<TensorType>(compute_output_shape(src_shape, axis), output_type, 1, q_info); |
Michalis Spyrou | 7930db4 | 2018-11-22 17:36:28 +0000 | [diff] [blame] | 105 | |
| 106 | // Create and configure function |
| 107 | FunctionType arg_min_max_layer; |
| 108 | arg_min_max_layer.configure(&src, axis, &dst, op); |
| 109 | |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 110 | ARM_COMPUTE_ASSERT(src.info()->is_resizable()); |
| 111 | ARM_COMPUTE_ASSERT(dst.info()->is_resizable()); |
Michalis Spyrou | 7930db4 | 2018-11-22 17:36:28 +0000 | [diff] [blame] | 112 | |
| 113 | // Allocate tensors |
| 114 | src.allocator()->allocate(); |
| 115 | dst.allocator()->allocate(); |
| 116 | |
Michele Di Giorgio | 4fc10b3 | 2021-04-30 18:30:41 +0100 | [diff] [blame] | 117 | ARM_COMPUTE_ASSERT(!src.info()->is_resizable()); |
| 118 | ARM_COMPUTE_ASSERT(!dst.info()->is_resizable()); |
Michalis Spyrou | 7930db4 | 2018-11-22 17:36:28 +0000 | [diff] [blame] | 119 | |
| 120 | // Fill tensors |
| 121 | fill(AccessorType(src)); |
| 122 | |
| 123 | // Compute function |
| 124 | arg_min_max_layer.run(); |
| 125 | |
| 126 | return dst; |
| 127 | } |
| 128 | |
Pablo Marquez Tello | 4cb0bd4 | 2023-07-27 18:02:37 +0100 | [diff] [blame] | 129 | TensorShape compute_output_shape(const TensorShape &src_shape, int axis) |
| 130 | { |
| 131 | return arm_compute::misc::shape_calculator::compute_reduced_shape(src_shape, axis, false); |
| 132 | } |
| 133 | |
| 134 | SimpleTensor<T2> compute_reference(TensorShape &src_shape, DataType input_type, DataType output_type, int axis, ReductionOperation op, QuantizationInfo q_info) |
Michalis Spyrou | 7930db4 | 2018-11-22 17:36:28 +0000 | [diff] [blame] | 135 | { |
| 136 | // Create reference |
Pablo Marquez Tello | 4cb0bd4 | 2023-07-27 18:02:37 +0100 | [diff] [blame] | 137 | SimpleTensor<T1> src{ src_shape, input_type, 1, q_info }; |
Michalis Spyrou | 7930db4 | 2018-11-22 17:36:28 +0000 | [diff] [blame] | 138 | |
| 139 | // Fill reference |
| 140 | fill(src); |
| 141 | |
Pablo Marquez Tello | 4cb0bd4 | 2023-07-27 18:02:37 +0100 | [diff] [blame] | 142 | return reference::reduction_operation<T1, T2>(src, compute_output_shape(src_shape, axis), axis, op, output_type); |
Michalis Spyrou | 7930db4 | 2018-11-22 17:36:28 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Pablo Marquez Tello | 4cb0bd4 | 2023-07-27 18:02:37 +0100 | [diff] [blame] | 145 | TensorType _target{}; |
| 146 | SimpleTensor<T2> _reference{}; |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 147 | }; |
| 148 | |
Pablo Marquez Tello | 4cb0bd4 | 2023-07-27 18:02:37 +0100 | [diff] [blame] | 149 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T1, typename T2> |
| 150 | class ArgMinMaxValidationQuantizedFixture : public ArgMinMaxValidationBaseFixture<TensorType, AccessorType, FunctionType, T1, T2> |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 151 | { |
| 152 | public: |
Pablo Marquez Tello | 4cb0bd4 | 2023-07-27 18:02:37 +0100 | [diff] [blame] | 153 | void setup(const TensorShape &shape, DataType input_type, DataType output_type, int axis, ReductionOperation op, QuantizationInfo quantization_info) |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 154 | { |
Pablo Marquez Tello | 4cb0bd4 | 2023-07-27 18:02:37 +0100 | [diff] [blame] | 155 | ArgMinMaxValidationBaseFixture<TensorType, AccessorType, FunctionType, T1, T2>::setup(shape, input_type, output_type, axis, op, quantization_info); |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 156 | } |
| 157 | }; |
| 158 | |
Pablo Marquez Tello | 4cb0bd4 | 2023-07-27 18:02:37 +0100 | [diff] [blame] | 159 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T1, typename T2> |
| 160 | class ArgMinMaxValidationFixture : public ArgMinMaxValidationBaseFixture<TensorType, AccessorType, FunctionType, T1, T2> |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 161 | { |
| 162 | public: |
Pablo Marquez Tello | 4cb0bd4 | 2023-07-27 18:02:37 +0100 | [diff] [blame] | 163 | void setup(const TensorShape &shape, DataType input_type, DataType output_type, int axis, ReductionOperation op) |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 164 | { |
Pablo Marquez Tello | 4cb0bd4 | 2023-07-27 18:02:37 +0100 | [diff] [blame] | 165 | ArgMinMaxValidationBaseFixture<TensorType, AccessorType, FunctionType, T1, T2>::setup(shape, input_type, output_type, axis, op, QuantizationInfo()); |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 166 | } |
Michalis Spyrou | 7930db4 | 2018-11-22 17:36:28 +0000 | [diff] [blame] | 167 | }; |
| 168 | } // namespace validation |
| 169 | } // namespace test |
| 170 | } // namespace arm_compute |
| 171 | #endif /* ARM_COMPUTE_TEST_ARG_MIN_MAX_FIXTURE */ |