blob: 4cd64a2a99cc4611cae978c2f5204744b0493c60 [file] [log] [blame]
Anthony Barbierdbdab852017-06-23 15:42:00 +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 "CL/CLAccessor.h"
25#include "CL/Helper.h"
26#include "Globals.h"
27#include "TensorLibrary.h"
28#include "TypePrinter.h"
29#include "Utils.h"
30#include "validation/Datasets.h"
31#include "validation/Reference.h"
32#include "validation/Validation.h"
33
34#include "arm_compute/core/Helpers.h"
35#include "arm_compute/core/Types.h"
36#include "arm_compute/runtime/CL/CLSubTensor.h"
37#include "arm_compute/runtime/CL/CLTensor.h"
38#include "arm_compute/runtime/CL/CLTensorAllocator.h"
39#include "arm_compute/runtime/CL/functions/CLBitwiseAnd.h"
40
41#include "boost_wrapper.h"
42
43#include <random>
44#include <string>
45
46using namespace arm_compute;
47using namespace arm_compute::test;
48using namespace arm_compute::test::cl;
49using namespace arm_compute::test::validation;
50
51namespace
52{
53/** Compute Neon bitwise and function.
54 *
55 * @param[in] shape Shape of the input and output tensors.
56 *
57 * @return Computed output tensor.
58 */
59CLTensor compute_bitwise_and(const TensorShape &shape)
60{
61 // Create tensors
62 CLTensor src1 = create_tensor(shape, DataType::U8);
63 CLTensor src2 = create_tensor(shape, DataType::U8);
64 CLTensor dst = create_tensor(shape, DataType::U8);
65
66 // Create and configure function
67 CLBitwiseAnd band;
68 band.configure(&src1, &src2, &dst);
69
70 // Allocate tensors
71 src1.allocator()->allocate();
72 src2.allocator()->allocate();
73 dst.allocator()->allocate();
74
75 BOOST_TEST(!src1.info()->is_resizable());
76 BOOST_TEST(!src2.info()->is_resizable());
77 BOOST_TEST(!dst.info()->is_resizable());
78
79 // Fill tensors
80 library->fill_tensor_uniform(CLAccessor(src1), 0);
81 library->fill_tensor_uniform(CLAccessor(src2), 1);
82
83 // Compute function
84 band.run();
85
86 return dst;
87}
88
89/** Compute OpenCL bitwise and function that splits the input and output in two subtensor.
90 *
91 * @param[in] shape Shape of the input and output tensors.
92 *
93 * @return Computed output tensor.
94 */
95CLTensor compute_bitwise_and_subtensor(const TensorShape &shape)
96{
97 // Create tensors
98 CLTensor src1 = create_tensor(shape, DataType::U8);
99 CLTensor src2 = create_tensor(shape, DataType::U8);
100 CLTensor dst = create_tensor(shape, DataType::U8);
101
102 // Create SubTensors
103 int coord_z = shape.z() / 2;
104 TensorShape sub_shape = shape;
105 sub_shape.set(2, coord_z);
106
107 CLSubTensor src1_sub1(&src1, sub_shape, Coordinates());
108 CLSubTensor src1_sub2(&src1, sub_shape, Coordinates(0, 0, coord_z));
109 CLSubTensor src2_sub1(&src2, sub_shape, Coordinates());
110 CLSubTensor src2_sub2(&src2, sub_shape, Coordinates(0, 0, coord_z));
111 CLSubTensor dst_sub1(&dst, sub_shape, Coordinates());
112 CLSubTensor dst_sub2(&dst, sub_shape, Coordinates(0, 0, coord_z));
113
114 // Create and configure function
115 CLBitwiseAnd band1, band2;
116 band1.configure(&src1_sub1, &src2_sub1, &dst_sub1);
117 band2.configure(&src1_sub2, &src2_sub2, &dst_sub2);
118
119 // Allocate tensors
120 src1.allocator()->allocate();
121 src2.allocator()->allocate();
122 dst.allocator()->allocate();
123
124 BOOST_TEST(!src1.info()->is_resizable());
125 BOOST_TEST(!src2.info()->is_resizable());
126 BOOST_TEST(!dst.info()->is_resizable());
127
128 // Fill tensors
129 std::uniform_int_distribution<> distribution(0, 255);
130 library->fill(CLAccessor(src1), distribution, 0);
131 library->fill(CLAccessor(src2), distribution, 1);
132
133 // Compute function
134 band1.run();
135 band2.run();
136
137 return dst;
138}
139} // namespace
140
141#ifndef DOXYGEN_SKIP_THIS
142BOOST_AUTO_TEST_SUITE(CL)
143BOOST_AUTO_TEST_SUITE(BitwiseAnd)
144
145BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit") * boost::unit_test::label("nightly"))
146BOOST_DATA_TEST_CASE(Configuration, SmallShapes() + LargeShapes(), shape)
147{
148 // Create tensors
149 CLTensor src1 = create_tensor(shape, DataType::U8);
150 CLTensor src2 = create_tensor(shape, DataType::U8);
151 CLTensor dst = create_tensor(shape, DataType::U8);
152
153 BOOST_TEST(src1.info()->is_resizable());
154 BOOST_TEST(src2.info()->is_resizable());
155 BOOST_TEST(dst.info()->is_resizable());
156
157 // Create and configure function
158 CLBitwiseAnd band;
159 band.configure(&src1, &src2, &dst);
160
161 // Validate valid region
162 const ValidRegion valid_region = shape_to_valid_region(shape);
163 validate(src1.info()->valid_region(), valid_region);
164 validate(src2.info()->valid_region(), valid_region);
165 validate(dst.info()->valid_region(), valid_region);
166
167 // Validate padding
168 const PaddingSize padding(0, required_padding(shape.x(), 16), 0, 0);
169 validate(src1.info()->padding(), padding);
170 validate(src2.info()->padding(), padding);
171 validate(dst.info()->padding(), padding);
172}
173
174BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit"))
175BOOST_DATA_TEST_CASE(RunSmall, SmallShapes(), shape)
176{
177 // Compute function
178 CLTensor dst = compute_bitwise_and(shape);
179
180 // Compute reference
181 RawTensor ref_dst = Reference::compute_reference_bitwise_and(shape);
182
183 // Validate output
184 validate(CLAccessor(dst), ref_dst);
185}
186
187BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit"))
188BOOST_AUTO_TEST_CASE(RunSubTensor)
189{
190 // Create shape
191 TensorShape shape(27U, 35U, 8U, 2U);
192
193 // Compute function
194 CLTensor dst = compute_bitwise_and_subtensor(shape);
195
196 // Compute reference
197 RawTensor ref_dst = Reference::compute_reference_bitwise_and(shape);
198
199 // Validate output
200 validate(CLAccessor(dst), ref_dst);
201}
202
203BOOST_TEST_DECORATOR(*boost::unit_test::label("nightly"))
204BOOST_DATA_TEST_CASE(RunLarge, LargeShapes(), shape)
205{
206 // Compute function
207 CLTensor dst = compute_bitwise_and(shape);
208
209 // Compute reference
210 RawTensor ref_dst = Reference::compute_reference_bitwise_and(shape);
211
212 // Validate output
213 validate(CLAccessor(dst), ref_dst);
214}
215
216BOOST_AUTO_TEST_SUITE_END()
217BOOST_AUTO_TEST_SUITE_END()
218#endif