blob: 1ddf03a74c2d07d9920afb1f34efc6adecdb893d [file] [log] [blame]
Isabella Gottardi1fab09f2017-07-13 15:55:57 +01001/*
2 * Copyright (c) 2017 ARM Limited.
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#include "arm_compute/core/Helpers.h"
25#include "arm_compute/core/Types.h"
26#include "arm_compute/runtime/CL/functions/CLScale.h"
27#include "arm_compute/runtime/Tensor.h"
28#include "arm_compute/runtime/TensorAllocator.h"
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010029#include "tests/CL/CLAccessor.h"
30#include "tests/PaddingCalculator.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010031#include "tests/datasets/BorderModeDataset.h"
32#include "tests/datasets/ShapeDatasets.h"
33#include "tests/framework/Asserts.h"
34#include "tests/framework/Macros.h"
35#include "tests/framework/datasets/Datasets.h"
36#include "tests/validation/Helpers.h"
37#include "tests/validation/Validation.h"
38#include "tests/validation/fixtures/ScaleFixture.h"
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010039
40namespace arm_compute
41{
42namespace test
43{
44namespace validation
45{
46namespace
47{
Georgios Pinitas583137c2017-08-31 18:12:42 +010048/** CNN data types */
49const auto ScaleDataTypes = framework::dataset::make("DataType",
50{
51 DataType::U8,
52 DataType::S16,
53 DataType::F16,
54 DataType::F32,
55});
56
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010057/** Tolerance */
Georgios Pinitas99d4f4a2017-09-19 16:51:42 +010058constexpr AbsoluteTolerance<uint8_t> tolerance_u8(1);
59constexpr AbsoluteTolerance<int16_t> tolerance_s16(1);
steniu01f81652d2017-09-11 15:29:12 +010060RelativeTolerance<float> tolerance_f32(0.05);
Georgios Pinitas583137c2017-08-31 18:12:42 +010061RelativeTolerance<half> tolerance_f16(half(0.1));
Moritz Pflanzerff1c3602017-09-22 12:41:25 +010062
63constexpr float tolerance_num_f32(0.01f);
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010064} // namespace
65
66TEST_SUITE(CL)
67TEST_SUITE(Scale)
68
steniu01f81652d2017-09-11 15:29:12 +010069DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(combine(combine(concat(datasets::MediumShapes(), datasets::LargeShapes()), ScaleDataTypes),
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010070 framework::dataset::make("InterpolationPolicy", { InterpolationPolicy::NEAREST_NEIGHBOR, InterpolationPolicy::BILINEAR })),
71 datasets::BorderModes()),
72 shape, data_type, policy, border_mode)
73{
74 std::mt19937 generator(library->seed());
75 std::uniform_real_distribution<float> distribution_float(0.25, 2);
76 const float scale_x = distribution_float(generator);
77 const float scale_y = distribution_float(generator);
78 std::uniform_int_distribution<uint8_t> distribution_u8(0, 255);
79 uint8_t constant_border_value = distribution_u8(generator);
80
81 // Create tensors
82 CLTensor src = create_tensor<CLTensor>(shape, data_type);
83 TensorShape shape_scaled(shape);
84 shape_scaled.set(0, shape[0] * scale_x);
85 shape_scaled.set(1, shape[1] * scale_y);
86 CLTensor dst = create_tensor<CLTensor>(shape_scaled, data_type);
87
88 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
89 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
90
91 // Create and configure function
92 CLScale clscale;
93 clscale.configure(&src, &dst, policy, border_mode, constant_border_value);
94
Daniil Efremov7a49c792017-11-14 21:25:34 +070095 // Get border size depending on border mode
96 const BorderSize border_size(border_mode == BorderMode::UNDEFINED ? 0 : 1);
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010097
Daniil Efremov7a49c792017-11-14 21:25:34 +070098 // Validate valid region
99 const ValidRegion dst_valid_region = calculate_valid_region_scale(*(src.info()), shape_scaled, policy, border_size, (border_mode == BorderMode::UNDEFINED));
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100100 validate(dst.info()->valid_region(), dst_valid_region);
101
102 // Validate padding
103 PaddingCalculator calculator(shape_scaled.x(), 4);
104 calculator.set_border_mode(border_mode);
105
Daniil Efremov7a49c792017-11-14 21:25:34 +0700106 const PaddingSize read_padding(border_size);
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100107 const PaddingSize write_padding = calculator.required_padding(PaddingCalculator::Option::EXCLUDE_BORDER);
108 validate(src.info()->padding(), read_padding);
109 validate(dst.info()->padding(), write_padding);
110}
111
112template <typename T>
113using CLScaleFixture = ScaleValidationFixture<CLTensor, CLAccessor, CLScale, T>;
114
Georgios Pinitas583137c2017-08-31 18:12:42 +0100115TEST_SUITE(Float)
116TEST_SUITE(FP32)
117FIXTURE_DATA_TEST_CASE(RunSmall, CLScaleFixture<float>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::F32)),
118 framework::dataset::make("InterpolationPolicy", { InterpolationPolicy::NEAREST_NEIGHBOR, InterpolationPolicy::BILINEAR })),
119 datasets::BorderModes()))
120{
121 //Create valid region
122 TensorInfo src_info(_shape, 1, _data_type);
123 const ValidRegion valid_region = calculate_valid_region_scale(src_info, _reference.shape(), _policy, BorderSize(1), (_border_mode == BorderMode::UNDEFINED));
124
125 // Validate output
Moritz Pflanzerff1c3602017-09-22 12:41:25 +0100126 validate(CLAccessor(_target), _reference, valid_region, tolerance_f32, tolerance_num_f32);
Georgios Pinitas583137c2017-08-31 18:12:42 +0100127}
128FIXTURE_DATA_TEST_CASE(RunLarge, CLScaleFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeShapes(), framework::dataset::make("DataType", DataType::F32)),
129 framework::dataset::make("InterpolationPolicy", { InterpolationPolicy::NEAREST_NEIGHBOR, InterpolationPolicy::BILINEAR })),
130 datasets::BorderModes()))
131{
132 //Create valid region
133 TensorInfo src_info(_shape, 1, _data_type);
134 const ValidRegion valid_region = calculate_valid_region_scale(src_info, _reference.shape(), _policy, BorderSize(1), (_border_mode == BorderMode::UNDEFINED));
135
136 // Validate output
Moritz Pflanzerff1c3602017-09-22 12:41:25 +0100137 validate(CLAccessor(_target), _reference, valid_region, tolerance_f32, tolerance_num_f32);
Georgios Pinitas583137c2017-08-31 18:12:42 +0100138}
139TEST_SUITE_END()
140TEST_SUITE(FP16)
141FIXTURE_DATA_TEST_CASE(RunSmall, CLScaleFixture<half>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::F16)),
142 framework::dataset::make("InterpolationPolicy", { InterpolationPolicy::NEAREST_NEIGHBOR, InterpolationPolicy::BILINEAR })),
143 datasets::BorderModes()))
144{
145 //Create valid region
146 TensorInfo src_info(_shape, 1, _data_type);
147 const ValidRegion valid_region = calculate_valid_region_scale(src_info, _reference.shape(), _policy, BorderSize(1), (_border_mode == BorderMode::UNDEFINED));
148
149 // Validate output
150 validate(CLAccessor(_target), _reference, valid_region, tolerance_f16);
151}
152FIXTURE_DATA_TEST_CASE(RunLarge, CLScaleFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeShapes(), framework::dataset::make("DataType",
153 DataType::F16)),
154 framework::dataset::make("InterpolationPolicy", { InterpolationPolicy::NEAREST_NEIGHBOR, InterpolationPolicy::BILINEAR })),
155 datasets::BorderModes()))
156{
157 //Create valid region
158 TensorInfo src_info(_shape, 1, _data_type);
159 const ValidRegion valid_region = calculate_valid_region_scale(src_info, _reference.shape(), _policy, BorderSize(1), (_border_mode == BorderMode::UNDEFINED));
160
161 // Validate output
162 validate(CLAccessor(_target), _reference, valid_region, tolerance_f16);
163}
164TEST_SUITE_END()
165TEST_SUITE_END()
166
167TEST_SUITE(Integer)
168TEST_SUITE(U8)
169FIXTURE_DATA_TEST_CASE(RunSmall, CLScaleFixture<uint8_t>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::U8)),
170 framework::dataset::make("InterpolationPolicy", { InterpolationPolicy::NEAREST_NEIGHBOR, InterpolationPolicy::BILINEAR })),
171 datasets::BorderModes()))
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100172{
173 //Create valid region
174 TensorInfo src_info(_shape, 1, _data_type);
175 const ValidRegion valid_region = calculate_valid_region_scale(src_info, _reference.shape(), _policy, BorderSize(1), (_border_mode == BorderMode::UNDEFINED));
176
177 // Validate output
Georgios Pinitas99d4f4a2017-09-19 16:51:42 +0100178 validate(CLAccessor(_target), _reference, valid_region, tolerance_u8);
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100179}
Georgios Pinitas583137c2017-08-31 18:12:42 +0100180FIXTURE_DATA_TEST_CASE(RunLarge, CLScaleFixture<uint8_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeShapes(), framework::dataset::make("DataType", DataType::U8)),
181 framework::dataset::make("InterpolationPolicy", { InterpolationPolicy::NEAREST_NEIGHBOR, InterpolationPolicy::BILINEAR })),
182 datasets::BorderModes()))
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100183{
184 //Create valid region
185 TensorInfo src_info(_shape, 1, _data_type);
186 const ValidRegion valid_region = calculate_valid_region_scale(src_info, _reference.shape(), _policy, BorderSize(1), (_border_mode == BorderMode::UNDEFINED));
187
188 // Validate output
Georgios Pinitas99d4f4a2017-09-19 16:51:42 +0100189 validate(CLAccessor(_target), _reference, valid_region, tolerance_u8);
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100190}
191TEST_SUITE_END()
Georgios Pinitas583137c2017-08-31 18:12:42 +0100192TEST_SUITE(S16)
193FIXTURE_DATA_TEST_CASE(RunSmall, CLScaleFixture<int16_t>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::S16)),
194 framework::dataset::make("InterpolationPolicy", { InterpolationPolicy::NEAREST_NEIGHBOR, InterpolationPolicy::BILINEAR })),
195 datasets::BorderModes()))
196{
197 //Create valid region
198 TensorInfo src_info(_shape, 1, _data_type);
199 const ValidRegion valid_region = calculate_valid_region_scale(src_info, _reference.shape(), _policy, BorderSize(1), (_border_mode == BorderMode::UNDEFINED));
200
201 // Validate output
Georgios Pinitas99d4f4a2017-09-19 16:51:42 +0100202 validate(CLAccessor(_target), _reference, valid_region, tolerance_s16);
Georgios Pinitas583137c2017-08-31 18:12:42 +0100203}
204FIXTURE_DATA_TEST_CASE(RunLarge, CLScaleFixture<int16_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeShapes(), framework::dataset::make("DataType", DataType::S16)),
205 framework::dataset::make("InterpolationPolicy", { InterpolationPolicy::NEAREST_NEIGHBOR, InterpolationPolicy::BILINEAR })),
206 datasets::BorderModes()))
207{
208 //Create valid region
209 TensorInfo src_info(_shape, 1, _data_type);
210 const ValidRegion valid_region = calculate_valid_region_scale(src_info, _reference.shape(), _policy, BorderSize(1), (_border_mode == BorderMode::UNDEFINED));
211
212 // Validate output
Georgios Pinitas99d4f4a2017-09-19 16:51:42 +0100213 validate(CLAccessor(_target), _reference, valid_region, tolerance_s16);
Georgios Pinitas583137c2017-08-31 18:12:42 +0100214}
215TEST_SUITE_END()
216TEST_SUITE_END()
217
218TEST_SUITE_END()
Isabella Gottardi1fab09f2017-07-13 15:55:57 +0100219TEST_SUITE_END()
220} // namespace validation
221} // namespace test
222} // namespace arm_compute