blob: fbd7185a48745245e74886117a603ad85a18ed2b [file] [log] [blame]
Giorgio Arena50f9fd72017-06-19 17:05:30 +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
25#include "CL/CLAccessor.h"
26#include "CL/Helper.h"
27#include "Globals.h"
28#include "PaddingCalculator.h"
29#include "TensorLibrary.h"
30#include "TypePrinter.h"
31#include "Utils.h"
32#include "validation/Datasets.h"
33#include "validation/Reference.h"
34#include "validation/Validation.h"
35#include "validation/ValidationUserConfiguration.h"
36
37#include "arm_compute/core/Helpers.h"
38#include "arm_compute/core/Types.h"
39#include "arm_compute/runtime/CL/CLSubTensor.h"
40#include "arm_compute/runtime/CL/CLTensor.h"
41#include "arm_compute/runtime/CL/CLTensorAllocator.h"
42#include "arm_compute/runtime/CL/functions/CLSobel3x3.h"
43
44#include "boost_wrapper.h"
45
46#include <random>
47#include <string>
48
49using namespace arm_compute;
50using namespace arm_compute::test;
51using namespace arm_compute::test::cl;
52using namespace arm_compute::test::validation;
53
54namespace
55{
SiCong Li7a035752017-06-28 15:27:02 +010056constexpr unsigned int filter_size = 3; /** Size of the kernel/filter in number of elements. */
57constexpr BorderSize border_size(filter_size / 2); /** Border size of the kernel/filter around its central element. */
58
Giorgio Arena50f9fd72017-06-19 17:05:30 +010059/** Compute CL Sobel 3x3 function.
60 *
61 * @param[in] shape Shape of the input and output tensors.
62 * @param[in] border_mode BorderMode used by the input tensor
63 * @param[in] constant_border_value Constant to use if @p border_mode == CONSTANT
64 *
65 * @return Computed output tensor.
66 */
67std::pair<CLTensor, CLTensor> compute_sobel_3x3(const TensorShape &shape, BorderMode border_mode, uint8_t constant_border_value)
68{
69 // Create tensors
70 CLTensor src = create_tensor(shape, DataType::U8);
71 CLTensor dst_x = create_tensor(shape, DataType::S16);
72 CLTensor dst_y = create_tensor(shape, DataType::S16);
73
74 src.info()->set_format(Format::U8);
75 dst_x.info()->set_format(Format::S16);
76 dst_y.info()->set_format(Format::S16);
77
78 // Create sobel image configure function
79 CLSobel3x3 sobel_3x3;
80 sobel_3x3.configure(&src, &dst_x, &dst_y, border_mode, constant_border_value);
81
82 // Allocate tensors
83 src.allocator()->allocate();
84 dst_x.allocator()->allocate();
85 dst_y.allocator()->allocate();
86
87 BOOST_TEST(!src.info()->is_resizable());
88 BOOST_TEST(!dst_x.info()->is_resizable());
89 BOOST_TEST(!dst_y.info()->is_resizable());
90
91 // Fill tensors
92 library->fill_tensor_uniform(CLAccessor(src), 0);
93
94 // Compute function
95 sobel_3x3.run();
96
97 return std::make_pair(std::move(dst_x), std::move(dst_y));
98}
99} // namespace
100
101#ifndef DOXYGEN_SKIP_THIS
102BOOST_AUTO_TEST_SUITE(CL)
103BOOST_AUTO_TEST_SUITE(Sobel3x3)
104
105BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit") * boost::unit_test::label("nightly"))
106BOOST_DATA_TEST_CASE(Configuration, (SmallShapes() + LargeShapes()) * BorderModes(), shape, border_mode)
107{
108 // Create tensors
109 CLTensor src = create_tensor(shape, DataType::U8);
110 CLTensor dst_x = create_tensor(shape, DataType::S16);
111 CLTensor dst_y = create_tensor(shape, DataType::S16);
112
113 src.info()->set_format(Format::U8);
114 dst_x.info()->set_format(Format::S16);
115 dst_y.info()->set_format(Format::S16);
116
117 BOOST_TEST(src.info()->is_resizable());
118 BOOST_TEST(dst_x.info()->is_resizable());
119 BOOST_TEST(dst_y.info()->is_resizable());
120
121 // Create sobel 3x3 configure function
122 CLSobel3x3 sobel_3x3;
123 sobel_3x3.configure(&src, &dst_x, &dst_y, border_mode);
124
125 // Validate valid region
126 const ValidRegion src_valid_region = shape_to_valid_region(shape);
SiCong Li7a035752017-06-28 15:27:02 +0100127 const ValidRegion dst_valid_region = shape_to_valid_region(shape, border_mode == BorderMode::UNDEFINED, border_size);
Giorgio Arena50f9fd72017-06-19 17:05:30 +0100128
129 validate(src.info()->valid_region(), src_valid_region);
130 validate(dst_x.info()->valid_region(), dst_valid_region);
131 validate(dst_y.info()->valid_region(), dst_valid_region);
132
133 // Validate padding
134 PaddingCalculator calculator(shape.x(), 8);
135
136 calculator.set_border_mode(border_mode);
137 calculator.set_border_size(1);
138
139 const PaddingSize dst_padding = calculator.required_padding();
140
141 calculator.set_accessed_elements(16);
142 calculator.set_access_offset(-1);
143
144 const PaddingSize src_padding = calculator.required_padding();
145
146 validate(src.info()->padding(), src_padding);
147 validate(dst_x.info()->padding(), dst_padding);
148 validate(dst_y.info()->padding(), dst_padding);
149}
150
151BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit"))
152BOOST_DATA_TEST_CASE(RunSmall, SmallShapes() * BorderModes(), shape, border_mode)
153{
154 uint8_t constant_border_value = 0;
155
156 // Generate a random constant value if border_mode is constant
157 if(border_mode == BorderMode::CONSTANT)
158 {
159 std::mt19937 gen(user_config.seed.get());
160 std::uniform_int_distribution<uint8_t> distribution(0, 255);
161 constant_border_value = distribution(gen);
162 }
163
164 // Compute function
165 std::pair<CLTensor, CLTensor> dst = compute_sobel_3x3(shape, border_mode, constant_border_value);
166
167 // Compute reference
168 std::pair<RawTensor, RawTensor> ref_dst = Reference::compute_reference_sobel_3x3(shape, border_mode, constant_border_value);
169
170 // Calculate valid region
SiCong Li7a035752017-06-28 15:27:02 +0100171 const ValidRegion valid_region = shape_to_valid_region(shape, border_mode == BorderMode::UNDEFINED, border_size);
Giorgio Arena50f9fd72017-06-19 17:05:30 +0100172
173 // Validate output
174 validate(CLAccessor(dst.first), ref_dst.first, valid_region);
175 validate(CLAccessor(dst.second), ref_dst.second, valid_region);
176}
177
178BOOST_TEST_DECORATOR(*boost::unit_test::label("nightly"))
179BOOST_DATA_TEST_CASE(RunLarge, LargeShapes() * BorderModes(), shape, border_mode)
180{
181 uint8_t constant_border_value = 0;
182
183 // Generate a random constant value if border_mode is constant
184 if(border_mode == BorderMode::CONSTANT)
185 {
186 std::mt19937 gen(user_config.seed.get());
187 std::uniform_int_distribution<uint8_t> distribution(0, 255);
188 constant_border_value = distribution(gen);
189 }
190
191 // Compute function
192 std::pair<CLTensor, CLTensor> dst = compute_sobel_3x3(shape, border_mode, constant_border_value);
193
194 // Compute reference
195 std::pair<RawTensor, RawTensor> ref_dst = Reference::compute_reference_sobel_3x3(shape, border_mode, constant_border_value);
196
197 // Calculate valid region
SiCong Li7a035752017-06-28 15:27:02 +0100198 const ValidRegion valid_region = shape_to_valid_region(shape, border_mode == BorderMode::UNDEFINED, border_size);
Giorgio Arena50f9fd72017-06-19 17:05:30 +0100199
200 // Validate output
201 validate(CLAccessor(dst.first), ref_dst.first, valid_region);
202 validate(CLAccessor(dst.second), ref_dst.second, valid_region);
203}
204
205BOOST_AUTO_TEST_SUITE_END()
206BOOST_AUTO_TEST_SUITE_END()
207#endif