Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 1 | /* |
Manuel Bottini | c58f0ad | 2020-08-07 16:49:15 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2020 Arm Limited. |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [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_REDUCE_MEAN_FIXTURE |
| 25 | #define ARM_COMPUTE_TEST_REDUCE_MEAN_FIXTURE |
| 26 | |
| 27 | #include "arm_compute/core/TensorShape.h" |
| 28 | #include "arm_compute/core/Types.h" |
Manuel Bottini | c58f0ad | 2020-08-07 16:49:15 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [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" |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 36 | #include "tests/validation/Helpers.h" |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 37 | #include "tests/validation/reference/ReductionOperation.h" |
| 38 | #include "tests/validation/reference/ReshapeLayer.h" |
| 39 | |
| 40 | namespace arm_compute |
| 41 | { |
| 42 | namespace test |
| 43 | { |
| 44 | namespace validation |
| 45 | { |
| 46 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 47 | class ReduceMeanValidationFixture : public framework::Fixture |
| 48 | { |
| 49 | public: |
| 50 | template <typename...> |
Manuel Bottini | c58f0ad | 2020-08-07 16:49:15 +0100 | [diff] [blame] | 51 | void setup(TensorShape shape, DataType data_type, Coordinates axis, bool keep_dims, QuantizationInfo quantization_info_input, QuantizationInfo quantization_info_output) |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 52 | { |
Manuel Bottini | c58f0ad | 2020-08-07 16:49:15 +0100 | [diff] [blame] | 53 | _target = compute_target(shape, data_type, axis, keep_dims, quantization_info_input, quantization_info_output); |
| 54 | _reference = compute_reference(shape, data_type, axis, keep_dims, quantization_info_input, quantization_info_output); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | protected: |
| 58 | template <typename U> |
| 59 | void fill(U &&tensor) |
| 60 | { |
| 61 | if(!is_data_type_quantized(tensor.data_type())) |
| 62 | { |
| 63 | std::uniform_real_distribution<> distribution(-1.0f, 1.0f); |
| 64 | library->fill(tensor, distribution, 0); |
| 65 | } |
| 66 | else |
| 67 | { |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 68 | std::pair<int, int> bounds = get_quantized_bounds(tensor.quantization_info(), -1.0f, 1.0f); |
| 69 | std::uniform_int_distribution<> distribution(bounds.first, bounds.second); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 70 | |
| 71 | library->fill(tensor, distribution, 0); |
| 72 | } |
| 73 | } |
| 74 | |
Manuel Bottini | c58f0ad | 2020-08-07 16:49:15 +0100 | [diff] [blame] | 75 | TensorType compute_target(TensorShape &src_shape, DataType data_type, Coordinates axis, bool keep_dims, QuantizationInfo quantization_info_input, QuantizationInfo quantization_info_output) |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 76 | { |
| 77 | // Create tensors |
Manuel Bottini | c58f0ad | 2020-08-07 16:49:15 +0100 | [diff] [blame] | 78 | TensorType src = create_tensor<TensorType>(src_shape, data_type, 1, quantization_info_input); |
| 79 | TensorShape dst_shape = arm_compute::misc::shape_calculator::calculate_reduce_mean_shape(src.info(), axis, keep_dims); |
| 80 | TensorType dst = create_tensor<TensorType>(dst_shape, data_type, 1, quantization_info_output); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 81 | |
| 82 | // Create and configure function |
| 83 | FunctionType reduction_mean; |
| 84 | reduction_mean.configure(&src, axis, keep_dims, &dst); |
| 85 | |
| 86 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 87 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 88 | |
| 89 | // Allocate tensors |
| 90 | src.allocator()->allocate(); |
| 91 | dst.allocator()->allocate(); |
| 92 | |
| 93 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 94 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 95 | |
| 96 | // Fill tensors |
| 97 | fill(AccessorType(src)); |
| 98 | |
| 99 | // Compute function |
| 100 | reduction_mean.run(); |
| 101 | |
| 102 | return dst; |
| 103 | } |
| 104 | |
Manuel Bottini | c58f0ad | 2020-08-07 16:49:15 +0100 | [diff] [blame] | 105 | SimpleTensor<T> compute_reference(TensorShape &src_shape, DataType data_type, Coordinates axis, bool keep_dims, QuantizationInfo quantization_info_input, QuantizationInfo quantization_info_output) |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 106 | { |
| 107 | // Create reference |
Manuel Bottini | c58f0ad | 2020-08-07 16:49:15 +0100 | [diff] [blame] | 108 | SimpleTensor<T> src{ src_shape, data_type, 1, quantization_info_input }; |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 109 | |
| 110 | // Fill reference |
| 111 | fill(src); |
| 112 | |
| 113 | SimpleTensor<T> out; |
| 114 | for(unsigned int i = 0; i < axis.num_dimensions(); ++i) |
| 115 | { |
| 116 | TensorShape output_shape = i == 0 ? src_shape : out.shape(); |
| 117 | output_shape.set(axis[i], 1); |
Manuel Bottini | c58f0ad | 2020-08-07 16:49:15 +0100 | [diff] [blame] | 118 | out = reference::reduction_operation<T, T>(i == 0 ? src : out, output_shape, axis[i], ReductionOperation::MEAN_SUM, quantization_info_output); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | if(!keep_dims) |
| 122 | { |
| 123 | TensorShape output_shape = src_shape; |
Michalis Spyrou | 96f8461 | 2018-10-24 14:01:04 +0100 | [diff] [blame] | 124 | std::sort(axis.begin(), axis.begin() + axis.num_dimensions()); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 125 | for(unsigned int i = 0; i < axis.num_dimensions(); ++i) |
| 126 | { |
Michalis Spyrou | 96f8461 | 2018-10-24 14:01:04 +0100 | [diff] [blame] | 127 | output_shape.remove_dimension(axis[i] - i); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | out = reference::reshape_layer(out, output_shape); |
| 131 | } |
| 132 | return out; |
| 133 | } |
| 134 | |
| 135 | TensorType _target{}; |
| 136 | SimpleTensor<T> _reference{}; |
| 137 | }; |
| 138 | |
| 139 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 140 | class ReduceMeanQuantizedFixture : public ReduceMeanValidationFixture<TensorType, AccessorType, FunctionType, T> |
| 141 | { |
| 142 | public: |
| 143 | template <typename...> |
Manuel Bottini | c58f0ad | 2020-08-07 16:49:15 +0100 | [diff] [blame] | 144 | void setup(TensorShape shape, DataType data_type, Coordinates axis, bool keep_dims, QuantizationInfo quantization_info_input, QuantizationInfo quantization_info_output) |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 145 | { |
Manuel Bottini | c58f0ad | 2020-08-07 16:49:15 +0100 | [diff] [blame] | 146 | ReduceMeanValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, axis, keep_dims, quantization_info_input, quantization_info_output); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 147 | } |
| 148 | }; |
| 149 | |
| 150 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 151 | class ReduceMeanFixture : public ReduceMeanValidationFixture<TensorType, AccessorType, FunctionType, T> |
| 152 | { |
| 153 | public: |
| 154 | template <typename...> |
| 155 | void setup(TensorShape shape, DataType data_type, Coordinates axis, bool keep_dims) |
| 156 | { |
Manuel Bottini | c58f0ad | 2020-08-07 16:49:15 +0100 | [diff] [blame] | 157 | ReduceMeanValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, axis, keep_dims, QuantizationInfo(), QuantizationInfo()); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 158 | } |
| 159 | }; |
| 160 | } // namespace validation |
| 161 | } // namespace test |
| 162 | } // namespace arm_compute |
| 163 | #endif /* ARM_COMPUTE_TEST_REDUCE_MEAN_FIXTURE */ |