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