Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [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 | { |
Michalis Spyrou | 17220e2 | 2018-09-12 13:35:38 +0100 | [diff] [blame] | 51 | _shape = shape; |
| 52 | _policy = policy; |
| 53 | _border_mode = border_mode; |
| 54 | _sampling_policy = sampling_policy; |
| 55 | _data_type = data_type; |
| 56 | _quantization_info = quantization_info; |
Sang-Hoon Park | f025a77 | 2020-05-26 11:11:32 +0100 | [diff] [blame] | 57 | _align_corners = align_corners; |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 58 | |
Sang-Hoon Park | c72dabc | 2020-05-27 13:03:18 +0100 | [diff] [blame] | 59 | generate_scale(shape); |
Gian Marco Iodice | b11e269 | 2017-09-25 11:31:42 +0100 | [diff] [blame] | 60 | |
Sang-Hoon Park | c72dabc | 2020-05-27 13:03:18 +0100 | [diff] [blame] | 61 | std::mt19937 generator(library->seed()); |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 62 | std::uniform_int_distribution<uint8_t> distribution_u8(0, 255); |
Sang-Hoon Park | c72dabc | 2020-05-27 13:03:18 +0100 | [diff] [blame] | 63 | _constant_border_value = static_cast<T>(distribution_u8(generator)); |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 64 | |
Sang-Hoon Park | c72dabc | 2020-05-27 13:03:18 +0100 | [diff] [blame] | 65 | _target = compute_target(shape, data_layout); |
| 66 | _reference = compute_reference(shape); |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | protected: |
Sang-Hoon Park | c72dabc | 2020-05-27 13:03:18 +0100 | [diff] [blame] | 70 | void generate_scale(const TensorShape &shape) |
| 71 | { |
| 72 | static constexpr float _min_scale{ 0.25f }; |
| 73 | static constexpr float _max_scale{ 3.f }; |
| 74 | |
| 75 | constexpr float max_width{ 8192.0f }; |
| 76 | constexpr float max_height{ 6384.0f }; |
Sang-Hoon Park | 3687ee1 | 2020-06-24 13:34:04 +0100 | [diff] [blame] | 77 | const float min_width{ 1.f }; |
| 78 | const float min_height{ 1.f }; |
Sang-Hoon Park | c72dabc | 2020-05-27 13:03:18 +0100 | [diff] [blame] | 79 | |
| 80 | std::mt19937 generator(library->seed()); |
| 81 | std::uniform_real_distribution<float> distribution_float(_min_scale, _max_scale); |
| 82 | |
| 83 | auto generate = [&](size_t input_size, float min_output, float max_output) -> float |
| 84 | { |
| 85 | const float generated_scale = distribution_float(generator); |
| 86 | const float output_size = utility::clamp(static_cast<float>(input_size) * generated_scale, min_output, max_output); |
| 87 | return output_size / input_size; |
| 88 | }; |
| 89 | |
| 90 | // Input shape is always given in NCHW layout. NHWC is dealt by permute in compute_target() |
| 91 | const int idx_width = get_data_layout_dimension_index(DataLayout::NCHW, DataLayoutDimension::WIDTH); |
| 92 | const int idx_height = get_data_layout_dimension_index(DataLayout::NCHW, DataLayoutDimension::HEIGHT); |
| 93 | |
| 94 | _scale_x = generate(shape[idx_width], min_width, max_width); |
| 95 | _scale_y = generate(shape[idx_height], min_height, max_height); |
| 96 | } |
| 97 | |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 98 | template <typename U> |
| 99 | void fill(U &&tensor) |
| 100 | { |
Diego Lopez Recas | 0085429 | 2018-02-22 13:08:01 +0000 | [diff] [blame] | 101 | if(is_data_type_float(_data_type)) |
| 102 | { |
| 103 | library->fill_tensor_uniform(tensor, 0); |
| 104 | } |
Michalis Spyrou | 17220e2 | 2018-09-12 13:35:38 +0100 | [diff] [blame] | 105 | else if(is_data_type_quantized(tensor.data_type())) |
| 106 | { |
| 107 | std::uniform_int_distribution<> distribution(0, 100); |
| 108 | library->fill(tensor, distribution, 0); |
| 109 | } |
Diego Lopez Recas | 0085429 | 2018-02-22 13:08:01 +0000 | [diff] [blame] | 110 | else |
| 111 | { |
| 112 | // Restrict range for float to avoid any floating point issues |
| 113 | std::uniform_real_distribution<> distribution(-5.0f, 5.0f); |
| 114 | library->fill(tensor, distribution, 0); |
| 115 | } |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 116 | } |
| 117 | |
Sang-Hoon Park | c72dabc | 2020-05-27 13:03:18 +0100 | [diff] [blame] | 118 | TensorType compute_target(TensorShape shape, DataLayout data_layout) |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 119 | { |
Georgios Pinitas | 393fa4c | 2018-05-08 15:54:53 +0100 | [diff] [blame] | 120 | // Change shape in case of NHWC. |
| 121 | if(data_layout == DataLayout::NHWC) |
| 122 | { |
| 123 | permute(shape, PermutationVector(2U, 0U, 1U)); |
| 124 | } |
| 125 | |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 126 | // Create tensors |
Sang-Hoon Park | c72dabc | 2020-05-27 13:03:18 +0100 | [diff] [blame] | 127 | 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] | 128 | |
| 129 | const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 130 | const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 131 | |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 132 | TensorShape shape_scaled(shape); |
Sang-Hoon Park | 9e4b899 | 2020-06-22 13:48:56 +0100 | [diff] [blame] | 133 | shape_scaled.set(idx_width, shape[idx_width] * _scale_x, /* apply_dim_correction = */ false); |
| 134 | shape_scaled.set(idx_height, shape[idx_height] * _scale_y, /* apply_dim_correction = */ false); |
Sang-Hoon Park | c72dabc | 2020-05-27 13:03:18 +0100 | [diff] [blame] | 135 | 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] | 136 | |
| 137 | // Create and configure function |
| 138 | FunctionType scale; |
| 139 | |
Manuel Bottini | fc2f6d0 | 2020-08-26 16:28:38 +0100 | [diff] [blame^] | 140 | scale.configure(&src, &dst, ScaleKernelInfo{ _policy, _border_mode, _constant_border_value, _sampling_policy, /* use_padding */ false, _align_corners }); |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 141 | |
| 142 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 143 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 144 | |
| 145 | // Allocate tensors |
| 146 | src.allocator()->allocate(); |
| 147 | dst.allocator()->allocate(); |
| 148 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 149 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 150 | |
| 151 | // Fill tensors |
| 152 | fill(AccessorType(src)); |
| 153 | |
| 154 | // Compute function |
| 155 | scale.run(); |
| 156 | |
| 157 | return dst; |
| 158 | } |
| 159 | |
Sang-Hoon Park | c72dabc | 2020-05-27 13:03:18 +0100 | [diff] [blame] | 160 | SimpleTensor<T> compute_reference(const TensorShape &shape) |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 161 | { |
| 162 | // Create reference |
Sang-Hoon Park | c72dabc | 2020-05-27 13:03:18 +0100 | [diff] [blame] | 163 | SimpleTensor<T> src{ shape, _data_type, 1, _quantization_info }; |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 164 | |
| 165 | // Fill reference |
| 166 | fill(src); |
| 167 | |
Sang-Hoon Park | c72dabc | 2020-05-27 13:03:18 +0100 | [diff] [blame] | 168 | 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] | 169 | } |
| 170 | |
| 171 | TensorType _target{}; |
| 172 | SimpleTensor<T> _reference{}; |
| 173 | TensorShape _shape{}; |
| 174 | InterpolationPolicy _policy{}; |
| 175 | BorderMode _border_mode{}; |
Sang-Hoon Park | c72dabc | 2020-05-27 13:03:18 +0100 | [diff] [blame] | 176 | T _constant_border_value{}; |
Daniil Efremov | 02bf80d | 2017-11-22 00:26:51 +0700 | [diff] [blame] | 177 | SamplingPolicy _sampling_policy{}; |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 178 | DataType _data_type{}; |
Michalis Spyrou | 17220e2 | 2018-09-12 13:35:38 +0100 | [diff] [blame] | 179 | QuantizationInfo _quantization_info{}; |
Sang-Hoon Park | bb123bd | 2020-01-03 10:57:30 +0000 | [diff] [blame] | 180 | bool _align_corners{ false }; |
Sang-Hoon Park | c72dabc | 2020-05-27 13:03:18 +0100 | [diff] [blame] | 181 | float _scale_x{ 1.f }; |
| 182 | float _scale_y{ 1.f }; |
Michalis Spyrou | 17220e2 | 2018-09-12 13:35:38 +0100 | [diff] [blame] | 183 | }; |
| 184 | |
| 185 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 186 | class ScaleValidationQuantizedFixture : public ScaleValidationGenericFixture<TensorType, AccessorType, FunctionType, T> |
| 187 | { |
| 188 | public: |
| 189 | template <typename...> |
Sang-Hoon Park | bb123bd | 2020-01-03 10:57:30 +0000 | [diff] [blame] | 190 | void setup(TensorShape shape, DataType data_type, QuantizationInfo quantization_info, DataLayout data_layout, InterpolationPolicy policy, BorderMode border_mode, SamplingPolicy sampling_policy, |
| 191 | bool align_corners) |
Michalis Spyrou | 17220e2 | 2018-09-12 13:35:38 +0100 | [diff] [blame] | 192 | { |
| 193 | ScaleValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, |
| 194 | data_type, |
| 195 | quantization_info, |
| 196 | data_layout, |
| 197 | policy, |
| 198 | border_mode, |
Sang-Hoon Park | bb123bd | 2020-01-03 10:57:30 +0000 | [diff] [blame] | 199 | sampling_policy, |
| 200 | align_corners); |
Michalis Spyrou | 17220e2 | 2018-09-12 13:35:38 +0100 | [diff] [blame] | 201 | } |
| 202 | }; |
| 203 | template <typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 204 | class ScaleValidationFixture : public ScaleValidationGenericFixture<TensorType, AccessorType, FunctionType, T> |
| 205 | { |
| 206 | public: |
| 207 | template <typename...> |
Sang-Hoon Park | bb123bd | 2020-01-03 10:57:30 +0000 | [diff] [blame] | 208 | 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] | 209 | { |
| 210 | ScaleValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, |
| 211 | data_type, |
| 212 | QuantizationInfo(), |
| 213 | data_layout, |
| 214 | policy, |
| 215 | border_mode, |
Sang-Hoon Park | bb123bd | 2020-01-03 10:57:30 +0000 | [diff] [blame] | 216 | sampling_policy, |
| 217 | align_corners); |
Michalis Spyrou | 17220e2 | 2018-09-12 13:35:38 +0100 | [diff] [blame] | 218 | } |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 219 | }; |
| 220 | } // namespace validation |
| 221 | } // namespace test |
| 222 | } // namespace arm_compute |
steniu01 | f81652d | 2017-09-11 15:29:12 +0100 | [diff] [blame] | 223 | #endif /* ARM_COMPUTE_TEST_SCALE_FIXTURE */ |