blob: c0b44bcb5fe4a2c383b0843cc55893805cbe7f71 [file] [log] [blame]
Isabella Gottardi1fab09f2017-07-13 15:55:57 +01001/*
Pablo Tello29cab362022-03-10 17:05:34 +00002 * Copyright (c) 2017-2022 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
27#include "arm_compute/core/TensorShape.h"
28#include "arm_compute/core/Types.h"
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010029#include "tests/AssetsLibrary.h"
30#include "tests/Globals.h"
31#include "tests/IAccessor.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010032#include "tests/framework/Asserts.h"
33#include "tests/framework/Fixture.h"
Michalis Spyrou46da23f2018-04-10 13:41:30 +010034#include "tests/validation/reference/Permute.h"
Georgios Pinitas5a7e7762017-12-01 16:27:29 +000035#include "tests/validation/reference/Scale.h"
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010036
37namespace arm_compute
38{
39namespace test
40{
41namespace validation
42{
43template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Michalis Spyrou17220e22018-09-12 13:35:38 +010044class ScaleValidationGenericFixture : public framework::Fixture
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010045{
46public:
47 template <typename...>
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +000048 void setup(TensorShape shape, DataType data_type, QuantizationInfo quantization_info, DataLayout data_layout, InterpolationPolicy policy, BorderMode border_mode, SamplingPolicy sampling_policy,
Manuel Bottinica62c6f2021-03-23 11:50:34 +000049 bool align_corners, bool mixed_layout)
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010050 {
Michalis Spyrou17220e22018-09-12 13:35:38 +010051 _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 Parkf025a772020-05-26 11:11:32 +010057 _align_corners = align_corners;
Manuel Bottinica62c6f2021-03-23 11:50:34 +000058 _mixed_layout = mixed_layout;
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010059
Sang-Hoon Parkc72dabc2020-05-27 13:03:18 +010060 generate_scale(shape);
Gian Marco Iodiceb11e2692017-09-25 11:31:42 +010061
Pablo Tello29cab362022-03-10 17:05:34 +000062 std::mt19937 generator(library->seed());
63 std::uniform_int_distribution<uint32_t> distribution_u8(0, 255);
Sang-Hoon Parkc72dabc2020-05-27 13:03:18 +010064 _constant_border_value = static_cast<T>(distribution_u8(generator));
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010065
Sang-Hoon Parkc72dabc2020-05-27 13:03:18 +010066 _target = compute_target(shape, data_layout);
67 _reference = compute_reference(shape);
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010068 }
69
70protected:
Manuel Bottinica62c6f2021-03-23 11:50:34 +000071 void mix_layout(FunctionType &layer, TensorType &src, TensorType &dst)
72 {
73 const DataLayout data_layout = src.info()->data_layout();
74 // Test Multi DataLayout graph cases, when the data layout changes after configure
75 src.info()->set_data_layout(data_layout == DataLayout::NCHW ? DataLayout::NHWC : DataLayout::NCHW);
76 dst.info()->set_data_layout(data_layout == DataLayout::NCHW ? DataLayout::NHWC : DataLayout::NCHW);
77
78 // Compute Convolution function
79 layer.run();
80
81 // Reinstating original data layout for the test suite to properly check the values
82 src.info()->set_data_layout(data_layout);
83 dst.info()->set_data_layout(data_layout);
84 }
85
Sang-Hoon Parkc72dabc2020-05-27 13:03:18 +010086 void generate_scale(const TensorShape &shape)
87 {
88 static constexpr float _min_scale{ 0.25f };
89 static constexpr float _max_scale{ 3.f };
90
91 constexpr float max_width{ 8192.0f };
92 constexpr float max_height{ 6384.0f };
Sang-Hoon Park3687ee12020-06-24 13:34:04 +010093 const float min_width{ 1.f };
94 const float min_height{ 1.f };
Sang-Hoon Parkc72dabc2020-05-27 13:03:18 +010095
96 std::mt19937 generator(library->seed());
97 std::uniform_real_distribution<float> distribution_float(_min_scale, _max_scale);
98
99 auto generate = [&](size_t input_size, float min_output, float max_output) -> float
100 {
101 const float generated_scale = distribution_float(generator);
102 const float output_size = utility::clamp(static_cast<float>(input_size) * generated_scale, min_output, max_output);
103 return output_size / input_size;
104 };
105
106 // Input shape is always given in NCHW layout. NHWC is dealt by permute in compute_target()
107 const int idx_width = get_data_layout_dimension_index(DataLayout::NCHW, DataLayoutDimension::WIDTH);
108 const int idx_height = get_data_layout_dimension_index(DataLayout::NCHW, DataLayoutDimension::HEIGHT);
109
110 _scale_x = generate(shape[idx_width], min_width, max_width);
111 _scale_y = generate(shape[idx_height], min_height, max_height);
112 }
113
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100114 template <typename U>
115 void fill(U &&tensor)
116 {
Giorgio Arena4bdd1772020-12-17 16:47:07 +0000117 if(tensor.data_type() == DataType::F32)
Diego Lopez Recas00854292018-02-22 13:08:01 +0000118 {
Giorgio Arena4bdd1772020-12-17 16:47:07 +0000119 std::uniform_real_distribution<float> distribution(-5.0f, 5.0f);
120 library->fill(tensor, distribution, 0);
121 }
122 else if(tensor.data_type() == DataType::F16)
123 {
Giorgio Arena33b103b2021-01-08 10:37:15 +0000124 arm_compute::utils::uniform_real_distribution_16bit<half> distribution{ -5.0f, 5.0f };
Giorgio Arena4bdd1772020-12-17 16:47:07 +0000125 library->fill(tensor, distribution, 0);
Diego Lopez Recas00854292018-02-22 13:08:01 +0000126 }
Michalis Spyrou17220e22018-09-12 13:35:38 +0100127 else if(is_data_type_quantized(tensor.data_type()))
128 {
129 std::uniform_int_distribution<> distribution(0, 100);
130 library->fill(tensor, distribution, 0);
131 }
Diego Lopez Recas00854292018-02-22 13:08:01 +0000132 else
133 {
Giorgio Arena4bdd1772020-12-17 16:47:07 +0000134 library->fill_tensor_uniform(tensor, 0);
Diego Lopez Recas00854292018-02-22 13:08:01 +0000135 }
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100136 }
137
Sang-Hoon Parkc72dabc2020-05-27 13:03:18 +0100138 TensorType compute_target(TensorShape shape, DataLayout data_layout)
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100139 {
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100140 // Change shape in case of NHWC.
141 if(data_layout == DataLayout::NHWC)
142 {
143 permute(shape, PermutationVector(2U, 0U, 1U));
144 }
145
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100146 // Create tensors
Sang-Hoon Parkc72dabc2020-05-27 13:03:18 +0100147 TensorType src = create_tensor<TensorType>(shape, _data_type, 1, _quantization_info, data_layout);
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100148
149 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
150 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
151
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100152 TensorShape shape_scaled(shape);
Sang-Hoon Park9e4b8992020-06-22 13:48:56 +0100153 shape_scaled.set(idx_width, shape[idx_width] * _scale_x, /* apply_dim_correction = */ false);
154 shape_scaled.set(idx_height, shape[idx_height] * _scale_y, /* apply_dim_correction = */ false);
Sang-Hoon Parkc72dabc2020-05-27 13:03:18 +0100155 TensorType dst = create_tensor<TensorType>(shape_scaled, _data_type, 1, _quantization_info, data_layout);
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100156
157 // Create and configure function
158 FunctionType scale;
159
Manuel Bottinifc2f6d02020-08-26 16:28:38 +0100160 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 +0100161
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +0100162 ARM_COMPUTE_ASSERT(src.info()->is_resizable());
163 ARM_COMPUTE_ASSERT(dst.info()->is_resizable());
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100164
Giorgio Arena63825e82021-03-25 14:54:50 +0000165 add_padding_x({ &src, &dst }, data_layout);
166
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100167 // Allocate tensors
168 src.allocator()->allocate();
169 dst.allocator()->allocate();
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +0100170 ARM_COMPUTE_ASSERT(!src.info()->is_resizable());
171 ARM_COMPUTE_ASSERT(!dst.info()->is_resizable());
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100172
173 // Fill tensors
174 fill(AccessorType(src));
175
Manuel Bottinica62c6f2021-03-23 11:50:34 +0000176 if(_mixed_layout)
177 {
178 mix_layout(scale, src, dst);
179 }
180 else
181 {
182 // Compute function
183 scale.run();
184 }
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100185 return dst;
186 }
187
Sang-Hoon Parkc72dabc2020-05-27 13:03:18 +0100188 SimpleTensor<T> compute_reference(const TensorShape &shape)
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100189 {
190 // Create reference
Sang-Hoon Parkc72dabc2020-05-27 13:03:18 +0100191 SimpleTensor<T> src{ shape, _data_type, 1, _quantization_info };
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100192
193 // Fill reference
194 fill(src);
195
Sang-Hoon Parkc72dabc2020-05-27 13:03:18 +0100196 return reference::scale<T>(src, _scale_x, _scale_y, _policy, _border_mode, _constant_border_value, _sampling_policy, /* ceil_policy_scale */ false, _align_corners);
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100197 }
198
199 TensorType _target{};
200 SimpleTensor<T> _reference{};
201 TensorShape _shape{};
202 InterpolationPolicy _policy{};
203 BorderMode _border_mode{};
Sang-Hoon Parkc72dabc2020-05-27 13:03:18 +0100204 T _constant_border_value{};
Daniil Efremov02bf80d2017-11-22 00:26:51 +0700205 SamplingPolicy _sampling_policy{};
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100206 DataType _data_type{};
Michalis Spyrou17220e22018-09-12 13:35:38 +0100207 QuantizationInfo _quantization_info{};
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000208 bool _align_corners{ false };
Manuel Bottinica62c6f2021-03-23 11:50:34 +0000209 bool _mixed_layout{ false };
Sang-Hoon Parkc72dabc2020-05-27 13:03:18 +0100210 float _scale_x{ 1.f };
211 float _scale_y{ 1.f };
Michalis Spyrou17220e22018-09-12 13:35:38 +0100212};
213
Manuel Bottinica62c6f2021-03-23 11:50:34 +0000214template <typename TensorType, typename AccessorType, typename FunctionType, typename T, bool mixed_layout = false>
Michalis Spyrou17220e22018-09-12 13:35:38 +0100215class ScaleValidationQuantizedFixture : public ScaleValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
216{
217public:
218 template <typename...>
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000219 void setup(TensorShape shape, DataType data_type, QuantizationInfo quantization_info, DataLayout data_layout, InterpolationPolicy policy, BorderMode border_mode, SamplingPolicy sampling_policy,
220 bool align_corners)
Michalis Spyrou17220e22018-09-12 13:35:38 +0100221 {
222 ScaleValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape,
223 data_type,
224 quantization_info,
225 data_layout,
226 policy,
227 border_mode,
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000228 sampling_policy,
Manuel Bottinica62c6f2021-03-23 11:50:34 +0000229 align_corners,
230 mixed_layout);
Michalis Spyrou17220e22018-09-12 13:35:38 +0100231 }
232};
Manuel Bottinica62c6f2021-03-23 11:50:34 +0000233template <typename TensorType, typename AccessorType, typename FunctionType, typename T, bool mixed_layout = false>
Michalis Spyrou17220e22018-09-12 13:35:38 +0100234class ScaleValidationFixture : public ScaleValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
235{
236public:
237 template <typename...>
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000238 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 +0100239 {
240 ScaleValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape,
241 data_type,
242 QuantizationInfo(),
243 data_layout,
244 policy,
245 border_mode,
Sang-Hoon Parkbb123bd2020-01-03 10:57:30 +0000246 sampling_policy,
Manuel Bottinica62c6f2021-03-23 11:50:34 +0000247 align_corners,
248 mixed_layout);
Michalis Spyrou17220e22018-09-12 13:35:38 +0100249 }
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100250};
251} // namespace validation
252} // namespace test
253} // namespace arm_compute
steniu01f81652d2017-09-11 15:29:12 +0100254#endif /* ARM_COMPUTE_TEST_SCALE_FIXTURE */