blob: 2be02ec4d645154fd288ff3938ea9d100876e1db [file] [log] [blame]
Isabella Gottardi1fab09f2017-07-13 15:55:57 +01001/*
Diego Lopez Recas00854292018-02-22 13:08:01 +00002 * Copyright (c) 2017-2018 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...>
Michalis Spyrou17220e22018-09-12 13:35:38 +010048 void setup(TensorShape shape, DataType data_type, QuantizationInfo quantization_info, DataLayout data_layout, InterpolationPolicy policy, BorderMode border_mode, SamplingPolicy sampling_policy)
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010049 {
Gian Marco Iodiceb11e2692017-09-25 11:31:42 +010050 constexpr float max_width = 8192.0f;
51 constexpr float max_height = 6384.0f;
52
Michalis Spyrou17220e22018-09-12 13:35:38 +010053 _shape = shape;
54 _policy = policy;
55 _border_mode = border_mode;
56 _sampling_policy = sampling_policy;
57 _data_type = data_type;
58 _quantization_info = quantization_info;
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010059
Gian Marco Iodiceb11e2692017-09-25 11:31:42 +010060 std::mt19937 generator(library->seed());
61 std::uniform_real_distribution<float> distribution_float(0.25, 3);
62 float scale_x = distribution_float(generator);
63 float scale_y = distribution_float(generator);
64
Georgios Pinitas393fa4c2018-05-08 15:54:53 +010065 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
66 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
67
68 scale_x = ((shape[idx_width] * scale_x) > max_width) ? (max_width / shape[idx_width]) : scale_x;
69 scale_y = ((shape[idx_height] * scale_y) > max_height) ? (max_height / shape[idx_height]) : scale_y;
Gian Marco Iodiceb11e2692017-09-25 11:31:42 +010070
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010071 std::uniform_int_distribution<uint8_t> distribution_u8(0, 255);
Georgios Pinitas583137c2017-08-31 18:12:42 +010072 T constant_border_value = static_cast<T>(distribution_u8(generator));
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010073
Michalis Spyrou17220e22018-09-12 13:35:38 +010074 _target = compute_target(shape, data_layout, scale_x, scale_y, policy, border_mode, constant_border_value, sampling_policy, quantization_info);
75 _reference = compute_reference(shape, scale_x, scale_y, policy, border_mode, constant_border_value, sampling_policy, quantization_info);
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010076 }
77
78protected:
79 template <typename U>
80 void fill(U &&tensor)
81 {
Diego Lopez Recas00854292018-02-22 13:08:01 +000082 if(is_data_type_float(_data_type))
83 {
84 library->fill_tensor_uniform(tensor, 0);
85 }
Michalis Spyrou17220e22018-09-12 13:35:38 +010086 else if(is_data_type_quantized(tensor.data_type()))
87 {
88 std::uniform_int_distribution<> distribution(0, 100);
89 library->fill(tensor, distribution, 0);
90 }
Diego Lopez Recas00854292018-02-22 13:08:01 +000091 else
92 {
93 // Restrict range for float to avoid any floating point issues
94 std::uniform_real_distribution<> distribution(-5.0f, 5.0f);
95 library->fill(tensor, distribution, 0);
96 }
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010097 }
98
Georgios Pinitas393fa4c2018-05-08 15:54:53 +010099 TensorType compute_target(TensorShape shape, DataLayout data_layout, const float scale_x, const float scale_y,
Michalis Spyrou17220e22018-09-12 13:35:38 +0100100 InterpolationPolicy policy, BorderMode border_mode, T constant_border_value, SamplingPolicy sampling_policy,
101 QuantizationInfo quantization_info)
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100102 {
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100103 // Change shape in case of NHWC.
104 if(data_layout == DataLayout::NHWC)
105 {
106 permute(shape, PermutationVector(2U, 0U, 1U));
107 }
108
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100109 // Create tensors
Michalis Spyrou17220e22018-09-12 13:35:38 +0100110 TensorType src = create_tensor<TensorType>(shape, _data_type, 1, quantization_info, data_layout);
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100111
112 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
113 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
114
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100115 TensorShape shape_scaled(shape);
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100116 shape_scaled.set(idx_width, shape[idx_width] * scale_x);
117 shape_scaled.set(idx_height, shape[idx_height] * scale_y);
Michalis Spyrou17220e22018-09-12 13:35:38 +0100118 TensorType dst = create_tensor<TensorType>(shape_scaled, _data_type, 1, quantization_info, data_layout);
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100119
120 // Create and configure function
121 FunctionType scale;
122
Daniil Efremov02bf80d2017-11-22 00:26:51 +0700123 scale.configure(&src, &dst, policy, border_mode, constant_border_value, sampling_policy);
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100124
125 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
126 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
127
128 // Allocate tensors
129 src.allocator()->allocate();
130 dst.allocator()->allocate();
131 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
132 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
133
134 // Fill tensors
135 fill(AccessorType(src));
136
137 // Compute function
138 scale.run();
139
140 return dst;
141 }
142
143 SimpleTensor<T> compute_reference(const TensorShape &shape, const float scale_x, const float scale_y,
Michalis Spyrou17220e22018-09-12 13:35:38 +0100144 InterpolationPolicy policy, BorderMode border_mode, T constant_border_value, SamplingPolicy sampling_policy,
145 QuantizationInfo quantization_info)
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100146 {
147 // Create reference
Michalis Spyrou17220e22018-09-12 13:35:38 +0100148 SimpleTensor<T> src{ shape, _data_type, 1, quantization_info };
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100149
150 // Fill reference
151 fill(src);
152
Daniil Efremov02bf80d2017-11-22 00:26:51 +0700153 return reference::scale<T>(src, scale_x, scale_y, policy, border_mode, constant_border_value, sampling_policy);
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100154 }
155
156 TensorType _target{};
157 SimpleTensor<T> _reference{};
158 TensorShape _shape{};
159 InterpolationPolicy _policy{};
160 BorderMode _border_mode{};
Daniil Efremov02bf80d2017-11-22 00:26:51 +0700161 SamplingPolicy _sampling_policy{};
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100162 DataType _data_type{};
Michalis Spyrou17220e22018-09-12 13:35:38 +0100163 QuantizationInfo _quantization_info{};
164};
165
166template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
167class ScaleValidationQuantizedFixture : public ScaleValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
168{
169public:
170 template <typename...>
171 void setup(TensorShape shape, DataType data_type, QuantizationInfo quantization_info, DataLayout data_layout, InterpolationPolicy policy, BorderMode border_mode, SamplingPolicy sampling_policy)
172 {
173 ScaleValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape,
174 data_type,
175 quantization_info,
176 data_layout,
177 policy,
178 border_mode,
179 sampling_policy);
180 }
181};
182template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
183class ScaleValidationFixture : public ScaleValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
184{
185public:
186 template <typename...>
187 void setup(TensorShape shape, DataType data_type, DataLayout data_layout, InterpolationPolicy policy, BorderMode border_mode, SamplingPolicy sampling_policy)
188 {
189 ScaleValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(shape,
190 data_type,
191 QuantizationInfo(),
192 data_layout,
193 policy,
194 border_mode,
195 sampling_policy);
196 }
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100197};
198} // namespace validation
199} // namespace test
200} // namespace arm_compute
steniu01f81652d2017-09-11 15:29:12 +0100201#endif /* ARM_COMPUTE_TEST_SCALE_FIXTURE */