Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 1 | /* |
Sang-Hoon Park | bb123bd | 2020-01-03 10:57:30 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 ARM Limited. |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +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_SCALE_FIXTURE |
| 25 | #define ARM_COMPUTE_TEST_SCALE_FIXTURE |
| 26 | |
| 27 | #include "arm_compute/core/TensorShape.h" |
| 28 | #include "arm_compute/core/Types.h" |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 29 | #include "tests/AssetsLibrary.h" |
| 30 | #include "tests/Globals.h" |
| 31 | #include "tests/IAccessor.h" |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 32 | #include "tests/framework/Asserts.h" |
| 33 | #include "tests/framework/Fixture.h" |
Michalis Spyrou | 46da23f | 2018-04-10 13:41:30 +0100 | [diff] [blame] | 34 | #include "tests/validation/reference/Permute.h" |
Georgios Pinitas | 5a7e776 | 2017-12-01 16:27:29 +0000 | [diff] [blame] | 35 | #include "tests/validation/reference/Scale.h" |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 36 | |
| 37 | namespace arm_compute |
| 38 | { |
| 39 | namespace test |
| 40 | { |
| 41 | namespace validation |
| 42 | { |
| 43 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
Michalis Spyrou | 17220e2 | 2018-09-12 13:35:38 +0100 | [diff] [blame] | 44 | class ScaleValidationGenericFixture : public framework::Fixture |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 45 | { |
| 46 | public: |
| 47 | template <typename...> |
Sang-Hoon Park | bb123bd | 2020-01-03 10:57:30 +0000 | [diff] [blame] | 48 | void setup(TensorShape shape, DataType data_type, QuantizationInfo quantization_info, DataLayout data_layout, InterpolationPolicy policy, BorderMode border_mode, SamplingPolicy sampling_policy, |
| 49 | bool align_corners) |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 50 | { |
Gian Marco Iodice | b11e269 | 2017-09-25 11:31:42 +0100 | [diff] [blame] | 51 | constexpr float max_width = 8192.0f; |
| 52 | constexpr float max_height = 6384.0f; |
| 53 | |
Michalis Spyrou | 17220e2 | 2018-09-12 13:35:38 +0100 | [diff] [blame] | 54 | _shape = shape; |
| 55 | _policy = policy; |
| 56 | _border_mode = border_mode; |
| 57 | _sampling_policy = sampling_policy; |
| 58 | _data_type = data_type; |
| 59 | _quantization_info = quantization_info; |
Sang-Hoon Park | bb123bd | 2020-01-03 10:57:30 +0000 | [diff] [blame] | 60 | _align_corners = align_corners; |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 61 | |
Gian Marco Iodice | b11e269 | 2017-09-25 11:31:42 +0100 | [diff] [blame] | 62 | std::mt19937 generator(library->seed()); |
| 63 | std::uniform_real_distribution<float> distribution_float(0.25, 3); |
| 64 | float scale_x = distribution_float(generator); |
| 65 | float scale_y = distribution_float(generator); |
| 66 | |
Georgios Pinitas | 393fa4c | 2018-05-08 15:54:53 +0100 | [diff] [blame] | 67 | const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 68 | const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 69 | |
| 70 | scale_x = ((shape[idx_width] * scale_x) > max_width) ? (max_width / shape[idx_width]) : scale_x; |
| 71 | scale_y = ((shape[idx_height] * scale_y) > max_height) ? (max_height / shape[idx_height]) : scale_y; |
Gian Marco Iodice | b11e269 | 2017-09-25 11:31:42 +0100 | [diff] [blame] | 72 | |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 73 | std::uniform_int_distribution<uint8_t> distribution_u8(0, 255); |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 74 | T constant_border_value = static_cast<T>(distribution_u8(generator)); |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 75 | |
Michalis Spyrou | 17220e2 | 2018-09-12 13:35:38 +0100 | [diff] [blame] | 76 | _target = compute_target(shape, data_layout, scale_x, scale_y, policy, border_mode, constant_border_value, sampling_policy, quantization_info); |
| 77 | _reference = compute_reference(shape, scale_x, scale_y, policy, border_mode, constant_border_value, sampling_policy, quantization_info); |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | protected: |
| 81 | template <typename U> |
| 82 | void fill(U &&tensor) |
| 83 | { |
Diego Lopez Recas | 0085429 | 2018-02-22 13:08:01 +0000 | [diff] [blame] | 84 | if(is_data_type_float(_data_type)) |
| 85 | { |
| 86 | library->fill_tensor_uniform(tensor, 0); |
| 87 | } |
Michalis Spyrou | 17220e2 | 2018-09-12 13:35:38 +0100 | [diff] [blame] | 88 | else if(is_data_type_quantized(tensor.data_type())) |
| 89 | { |
| 90 | std::uniform_int_distribution<> distribution(0, 100); |
| 91 | library->fill(tensor, distribution, 0); |
| 92 | } |
Diego Lopez Recas | 0085429 | 2018-02-22 13:08:01 +0000 | [diff] [blame] | 93 | else |
| 94 | { |
| 95 | // Restrict range for float to avoid any floating point issues |
| 96 | std::uniform_real_distribution<> distribution(-5.0f, 5.0f); |
| 97 | library->fill(tensor, distribution, 0); |
| 98 | } |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 99 | } |
| 100 | |
Georgios Pinitas | 393fa4c | 2018-05-08 15:54:53 +0100 | [diff] [blame] | 101 | TensorType compute_target(TensorShape shape, DataLayout data_layout, const float scale_x, const float scale_y, |
Michalis Spyrou | 17220e2 | 2018-09-12 13:35:38 +0100 | [diff] [blame] | 102 | InterpolationPolicy policy, BorderMode border_mode, T constant_border_value, SamplingPolicy sampling_policy, |
| 103 | QuantizationInfo quantization_info) |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 104 | { |
Georgios Pinitas | 393fa4c | 2018-05-08 15:54:53 +0100 | [diff] [blame] | 105 | // Change shape in case of NHWC. |
| 106 | if(data_layout == DataLayout::NHWC) |
| 107 | { |
| 108 | permute(shape, PermutationVector(2U, 0U, 1U)); |
| 109 | } |
| 110 | |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 111 | // Create tensors |
Michalis Spyrou | 17220e2 | 2018-09-12 13:35:38 +0100 | [diff] [blame] | 112 | TensorType src = create_tensor<TensorType>(shape, _data_type, 1, quantization_info, data_layout); |
Georgios Pinitas | 393fa4c | 2018-05-08 15:54:53 +0100 | [diff] [blame] | 113 | |
| 114 | const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 115 | const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 116 | |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 117 | TensorShape shape_scaled(shape); |
Georgios Pinitas | 393fa4c | 2018-05-08 15:54:53 +0100 | [diff] [blame] | 118 | shape_scaled.set(idx_width, shape[idx_width] * scale_x); |
| 119 | shape_scaled.set(idx_height, shape[idx_height] * scale_y); |
Michalis Spyrou | 17220e2 | 2018-09-12 13:35:38 +0100 | [diff] [blame] | 120 | TensorType dst = create_tensor<TensorType>(shape_scaled, _data_type, 1, quantization_info, data_layout); |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 121 | |
| 122 | // Create and configure function |
| 123 | FunctionType scale; |
| 124 | |
Sang-Hoon Park | bb123bd | 2020-01-03 10:57:30 +0000 | [diff] [blame] | 125 | scale.configure(&src, &dst, policy, border_mode, constant_border_value, sampling_policy, /* use_padding */ true, _align_corners); |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 126 | |
| 127 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 128 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 129 | |
| 130 | // Allocate tensors |
| 131 | src.allocator()->allocate(); |
| 132 | dst.allocator()->allocate(); |
| 133 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 134 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 135 | |
| 136 | // Fill tensors |
| 137 | fill(AccessorType(src)); |
| 138 | |
| 139 | // Compute function |
| 140 | scale.run(); |
| 141 | |
| 142 | return dst; |
| 143 | } |
| 144 | |
| 145 | SimpleTensor<T> compute_reference(const TensorShape &shape, const float scale_x, const float scale_y, |
Michalis Spyrou | 17220e2 | 2018-09-12 13:35:38 +0100 | [diff] [blame] | 146 | InterpolationPolicy policy, BorderMode border_mode, T constant_border_value, SamplingPolicy sampling_policy, |
| 147 | QuantizationInfo quantization_info) |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 148 | { |
| 149 | // Create reference |
Michalis Spyrou | 17220e2 | 2018-09-12 13:35:38 +0100 | [diff] [blame] | 150 | SimpleTensor<T> src{ shape, _data_type, 1, quantization_info }; |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 151 | |
| 152 | // Fill reference |
| 153 | fill(src); |
| 154 | |
Sang-Hoon Park | bb123bd | 2020-01-03 10:57:30 +0000 | [diff] [blame] | 155 | return reference::scale<T>(src, scale_x, scale_y, policy, border_mode, constant_border_value, sampling_policy, /* ceil_policy_scale */ false, _align_corners); |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | TensorType _target{}; |
| 159 | SimpleTensor<T> _reference{}; |
| 160 | TensorShape _shape{}; |
| 161 | InterpolationPolicy _policy{}; |
| 162 | BorderMode _border_mode{}; |
Daniil Efremov | 02bf80d | 2017-11-22 00:26:51 +0700 | [diff] [blame] | 163 | SamplingPolicy _sampling_policy{}; |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 164 | DataType _data_type{}; |
Michalis Spyrou | 17220e2 | 2018-09-12 13:35:38 +0100 | [diff] [blame] | 165 | QuantizationInfo _quantization_info{}; |
Sang-Hoon Park | bb123bd | 2020-01-03 10:57:30 +0000 | [diff] [blame] | 166 | bool _align_corners{ false }; |
Michalis Spyrou | 17220e2 | 2018-09-12 13:35:38 +0100 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 170 | class ScaleValidationQuantizedFixture : public ScaleValidationGenericFixture<TensorType, AccessorType, FunctionType, T> |
| 171 | { |
| 172 | public: |
| 173 | template <typename...> |
Sang-Hoon Park | bb123bd | 2020-01-03 10:57:30 +0000 | [diff] [blame] | 174 | void setup(TensorShape shape, DataType data_type, QuantizationInfo quantization_info, DataLayout data_layout, InterpolationPolicy policy, BorderMode border_mode, SamplingPolicy sampling_policy, |
| 175 | bool align_corners) |
Michalis Spyrou | 17220e2 | 2018-09-12 13:35:38 +0100 | [diff] [blame] | 176 | { |
| 177 | ScaleValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, |
| 178 | data_type, |
| 179 | quantization_info, |
| 180 | data_layout, |
| 181 | policy, |
| 182 | border_mode, |
Sang-Hoon Park | bb123bd | 2020-01-03 10:57:30 +0000 | [diff] [blame] | 183 | sampling_policy, |
| 184 | align_corners); |
Michalis Spyrou | 17220e2 | 2018-09-12 13:35:38 +0100 | [diff] [blame] | 185 | } |
| 186 | }; |
| 187 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 188 | class ScaleValidationFixture : public ScaleValidationGenericFixture<TensorType, AccessorType, FunctionType, T> |
| 189 | { |
| 190 | public: |
| 191 | template <typename...> |
Sang-Hoon Park | bb123bd | 2020-01-03 10:57:30 +0000 | [diff] [blame] | 192 | void setup(TensorShape shape, DataType data_type, DataLayout data_layout, InterpolationPolicy policy, BorderMode border_mode, SamplingPolicy sampling_policy, bool align_corners) |
Michalis Spyrou | 17220e2 | 2018-09-12 13:35:38 +0100 | [diff] [blame] | 193 | { |
| 194 | ScaleValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, |
| 195 | data_type, |
| 196 | QuantizationInfo(), |
| 197 | data_layout, |
| 198 | policy, |
| 199 | border_mode, |
Sang-Hoon Park | bb123bd | 2020-01-03 10:57:30 +0000 | [diff] [blame] | 200 | sampling_policy, |
| 201 | align_corners); |
Michalis Spyrou | 17220e2 | 2018-09-12 13:35:38 +0100 | [diff] [blame] | 202 | } |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 203 | }; |
| 204 | } // namespace validation |
| 205 | } // namespace test |
| 206 | } // namespace arm_compute |
steniu01 | f81652d | 2017-09-11 15:29:12 +0100 | [diff] [blame] | 207 | #endif /* ARM_COMPUTE_TEST_SCALE_FIXTURE */ |