blob: b00a86f6dce60405dcd3cc23efc1ee3ae2efa82a [file] [log] [blame]
Isabella Gottardi1fab09f2017-07-13 15:55:57 +01001/*
Matthew Bentham945b8da2023-07-12 11:54:59 +00002 * Copyright (c) 2017-2023 Arm Limited.
Isabella Gottardi1fab09f2017-07-13 15:55:57 +01003 *
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
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010027#include "tests/framework/Fixture.h"
Michalis Spyrou46da23f2018-04-10 13:41:30 +010028#include "tests/validation/reference/Permute.h"
Georgios Pinitas5a7e7762017-12-01 16:27:29 +000029#include "tests/validation/reference/Scale.h"
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010030
31namespace arm_compute
32{
33namespace test
34{
35namespace validation
36{
37template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Michalis Spyrou17220e22018-09-12 13:35:38 +010038class ScaleValidationGenericFixture : public framework::Fixture
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010039{
40public:
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +000041 void setup(TensorShape shape, DataType data_type, QuantizationInfo quantization_info, DataLayout data_layout, InterpolationPolicy policy, BorderMode border_mode, SamplingPolicy sampling_policy,
Gunes Bayirc4f27432022-09-11 15:59:19 +010042 bool align_corners, bool mixed_layout, QuantizationInfo output_quantization_info)
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010043 {
Gunes Bayirc4f27432022-09-11 15:59:19 +010044 _shape = shape;
45 _policy = policy;
46 _border_mode = border_mode;
47 _sampling_policy = sampling_policy;
48 _data_type = data_type;
49 _input_quantization_info = quantization_info;
50 _output_quantization_info = output_quantization_info;
51 _align_corners = align_corners;
52 _mixed_layout = mixed_layout;
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010053
Sang-Hoon Parkc72dabc2020-05-27 13:03:18 +010054 generate_scale(shape);
Gian Marco Iodiceb11e2692017-09-25 11:31:42 +010055
Pablo Tello29cab362022-03-10 17:05:34 +000056 std::mt19937 generator(library->seed());
57 std::uniform_int_distribution<uint32_t> distribution_u8(0, 255);
Sang-Hoon Parkc72dabc2020-05-27 13:03:18 +010058 _constant_border_value = static_cast<T>(distribution_u8(generator));
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010059
Sang-Hoon Parkc72dabc2020-05-27 13:03:18 +010060 _target = compute_target(shape, data_layout);
61 _reference = compute_reference(shape);
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010062 }
63
64protected:
Manuel Bottinica62c6f2021-03-23 11:50:34 +000065 void mix_layout(FunctionType &layer, TensorType &src, TensorType &dst)
66 {
67 const DataLayout data_layout = src.info()->data_layout();
68 // Test Multi DataLayout graph cases, when the data layout changes after configure
69 src.info()->set_data_layout(data_layout == DataLayout::NCHW ? DataLayout::NHWC : DataLayout::NCHW);
70 dst.info()->set_data_layout(data_layout == DataLayout::NCHW ? DataLayout::NHWC : DataLayout::NCHW);
71
72 // Compute Convolution function
73 layer.run();
74
75 // Reinstating original data layout for the test suite to properly check the values
76 src.info()->set_data_layout(data_layout);
77 dst.info()->set_data_layout(data_layout);
78 }
79
Sang-Hoon Parkc72dabc2020-05-27 13:03:18 +010080 void generate_scale(const TensorShape &shape)
81 {
82 static constexpr float _min_scale{ 0.25f };
83 static constexpr float _max_scale{ 3.f };
84
85 constexpr float max_width{ 8192.0f };
86 constexpr float max_height{ 6384.0f };
Sang-Hoon Park3687ee12020-06-24 13:34:04 +010087 const float min_width{ 1.f };
88 const float min_height{ 1.f };
Sang-Hoon Parkc72dabc2020-05-27 13:03:18 +010089
90 std::mt19937 generator(library->seed());
91 std::uniform_real_distribution<float> distribution_float(_min_scale, _max_scale);
92
93 auto generate = [&](size_t input_size, float min_output, float max_output) -> float
94 {
95 const float generated_scale = distribution_float(generator);
96 const float output_size = utility::clamp(static_cast<float>(input_size) * generated_scale, min_output, max_output);
97 return output_size / input_size;
98 };
99
100 // Input shape is always given in NCHW layout. NHWC is dealt by permute in compute_target()
101 const int idx_width = get_data_layout_dimension_index(DataLayout::NCHW, DataLayoutDimension::WIDTH);
102 const int idx_height = get_data_layout_dimension_index(DataLayout::NCHW, DataLayoutDimension::HEIGHT);
103
104 _scale_x = generate(shape[idx_width], min_width, max_width);
105 _scale_y = generate(shape[idx_height], min_height, max_height);
106 }
107
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100108 template <typename U>
109 void fill(U &&tensor)
110 {
Giorgio Arena4bdd1772020-12-17 16:47:07 +0000111 if(tensor.data_type() == DataType::F32)
Diego Lopez Recas00854292018-02-22 13:08:01 +0000112 {
Giorgio Arena4bdd1772020-12-17 16:47:07 +0000113 std::uniform_real_distribution<float> distribution(-5.0f, 5.0f);
114 library->fill(tensor, distribution, 0);
115 }
116 else if(tensor.data_type() == DataType::F16)
117 {
Giorgio Arena33b103b2021-01-08 10:37:15 +0000118 arm_compute::utils::uniform_real_distribution_16bit<half> distribution{ -5.0f, 5.0f };
Giorgio Arena4bdd1772020-12-17 16:47:07 +0000119 library->fill(tensor, distribution, 0);
Diego Lopez Recas00854292018-02-22 13:08:01 +0000120 }
Michalis Spyrou17220e22018-09-12 13:35:38 +0100121 else if(is_data_type_quantized(tensor.data_type()))
122 {
123 std::uniform_int_distribution<> distribution(0, 100);
124 library->fill(tensor, distribution, 0);
125 }
Diego Lopez Recas00854292018-02-22 13:08:01 +0000126 else
127 {
Giorgio Arena4bdd1772020-12-17 16:47:07 +0000128 library->fill_tensor_uniform(tensor, 0);
Diego Lopez Recas00854292018-02-22 13:08:01 +0000129 }
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100130 }
131
Sang-Hoon Parkc72dabc2020-05-27 13:03:18 +0100132 TensorType compute_target(TensorShape shape, DataLayout data_layout)
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100133 {
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100134 // Change shape in case of NHWC.
135 if(data_layout == DataLayout::NHWC)
136 {
137 permute(shape, PermutationVector(2U, 0U, 1U));
138 }
139
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100140 // Create tensors
Gunes Bayirc4f27432022-09-11 15:59:19 +0100141 TensorType src = create_tensor<TensorType>(shape, _data_type, 1, _input_quantization_info, data_layout);
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100142
143 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
144 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
145
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100146 TensorShape shape_scaled(shape);
Sang-Hoon Park9e4b8992020-06-22 13:48:56 +0100147 shape_scaled.set(idx_width, shape[idx_width] * _scale_x, /* apply_dim_correction = */ false);
148 shape_scaled.set(idx_height, shape[idx_height] * _scale_y, /* apply_dim_correction = */ false);
Gunes Bayirc4f27432022-09-11 15:59:19 +0100149 TensorType dst = create_tensor<TensorType>(shape_scaled, _data_type, 1, _output_quantization_info, data_layout);
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100150
151 // Create and configure function
152 FunctionType scale;
153
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100154 scale.configure(&src, &dst, ScaleKernelInfo{ _policy, _border_mode, _constant_border_value, _sampling_policy, /* use_padding */ false, _align_corners });
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100155
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +0100156 ARM_COMPUTE_ASSERT(src.info()->is_resizable());
157 ARM_COMPUTE_ASSERT(dst.info()->is_resizable());
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100158
Giorgio Arena63825e82021-03-25 14:54:50 +0000159 add_padding_x({ &src, &dst }, data_layout);
160
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100161 // Allocate tensors
162 src.allocator()->allocate();
163 dst.allocator()->allocate();
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +0100164 ARM_COMPUTE_ASSERT(!src.info()->is_resizable());
165 ARM_COMPUTE_ASSERT(!dst.info()->is_resizable());
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100166
167 // Fill tensors
168 fill(AccessorType(src));
169
Manuel Bottinica62c6f2021-03-23 11:50:34 +0000170 if(_mixed_layout)
171 {
172 mix_layout(scale, src, dst);
173 }
174 else
175 {
176 // Compute function
177 scale.run();
178 }
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100179 return dst;
180 }
181
Sang-Hoon Parkc72dabc2020-05-27 13:03:18 +0100182 SimpleTensor<T> compute_reference(const TensorShape &shape)
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100183 {
184 // Create reference
Gunes Bayirc4f27432022-09-11 15:59:19 +0100185 SimpleTensor<T> src{ shape, _data_type, 1, _input_quantization_info };
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100186
187 // Fill reference
188 fill(src);
189
Gunes Bayirc4f27432022-09-11 15:59:19 +0100190 return reference::scale<T>(src, _scale_x, _scale_y, _policy, _border_mode, _constant_border_value, _sampling_policy, /* ceil_policy_scale */ false, _align_corners, _output_quantization_info);
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100191 }
192
193 TensorType _target{};
194 SimpleTensor<T> _reference{};
195 TensorShape _shape{};
196 InterpolationPolicy _policy{};
197 BorderMode _border_mode{};
Sang-Hoon Parkc72dabc2020-05-27 13:03:18 +0100198 T _constant_border_value{};
Daniil Efremov02bf80d2017-11-22 00:26:51 +0700199 SamplingPolicy _sampling_policy{};
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100200 DataType _data_type{};
Gunes Bayirc4f27432022-09-11 15:59:19 +0100201 QuantizationInfo _input_quantization_info{};
202 QuantizationInfo _output_quantization_info{};
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000203 bool _align_corners{ false };
Manuel Bottinica62c6f2021-03-23 11:50:34 +0000204 bool _mixed_layout{ false };
Sang-Hoon Parkc72dabc2020-05-27 13:03:18 +0100205 float _scale_x{ 1.f };
206 float _scale_y{ 1.f };
Michalis Spyrou17220e22018-09-12 13:35:38 +0100207};
208
Manuel Bottinica62c6f2021-03-23 11:50:34 +0000209template <typename TensorType, typename AccessorType, typename FunctionType, typename T, bool mixed_layout = false>
Michalis Spyrou17220e22018-09-12 13:35:38 +0100210class ScaleValidationQuantizedFixture : public ScaleValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
211{
212public:
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000213 void setup(TensorShape shape, DataType data_type, QuantizationInfo quantization_info, DataLayout data_layout, InterpolationPolicy policy, BorderMode border_mode, SamplingPolicy sampling_policy,
214 bool align_corners)
Michalis Spyrou17220e22018-09-12 13:35:38 +0100215 {
216 ScaleValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape,
217 data_type,
218 quantization_info,
219 data_layout,
220 policy,
221 border_mode,
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000222 sampling_policy,
Manuel Bottinica62c6f2021-03-23 11:50:34 +0000223 align_corners,
Gunes Bayirc4f27432022-09-11 15:59:19 +0100224 mixed_layout,
225 quantization_info);
226 }
227};
228template <typename TensorType, typename AccessorType, typename FunctionType, typename T, bool mixed_layout = false>
229class ScaleValidationDifferentOutputQuantizedFixture : public ScaleValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
230{
231public:
Gunes Bayirc4f27432022-09-11 15:59:19 +0100232 void setup(TensorShape shape, DataType data_type, QuantizationInfo input_quantization_info, QuantizationInfo output_quantization_info, DataLayout data_layout, InterpolationPolicy policy,
233 BorderMode border_mode, SamplingPolicy sampling_policy,
234 bool align_corners)
235 {
236 ScaleValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape,
237 data_type,
238 input_quantization_info,
239 data_layout,
240 policy,
241 border_mode,
242 sampling_policy,
243 align_corners,
244 mixed_layout,
245 output_quantization_info);
Michalis Spyrou17220e22018-09-12 13:35:38 +0100246 }
247};
Manuel Bottinica62c6f2021-03-23 11:50:34 +0000248template <typename TensorType, typename AccessorType, typename FunctionType, typename T, bool mixed_layout = false>
Michalis Spyrou17220e22018-09-12 13:35:38 +0100249class ScaleValidationFixture : public ScaleValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
250{
251public:
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000252 void setup(TensorShape shape, DataType data_type, DataLayout data_layout, InterpolationPolicy policy, BorderMode border_mode, SamplingPolicy sampling_policy, bool align_corners)
Michalis Spyrou17220e22018-09-12 13:35:38 +0100253 {
254 ScaleValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape,
255 data_type,
256 QuantizationInfo(),
257 data_layout,
258 policy,
259 border_mode,
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000260 sampling_policy,
Manuel Bottinica62c6f2021-03-23 11:50:34 +0000261 align_corners,
Gunes Bayirc4f27432022-09-11 15:59:19 +0100262 mixed_layout,
263 QuantizationInfo());
Michalis Spyrou17220e22018-09-12 13:35:38 +0100264 }
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100265};
266} // namespace validation
267} // namespace test
268} // namespace arm_compute
steniu01f81652d2017-09-11 15:29:12 +0100269#endif /* ARM_COMPUTE_TEST_SCALE_FIXTURE */