blob: 6feb15ad349713aaf1b45446e7e74a2de78d6882 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +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 "Globals.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010025#include "NEON/NEAccessor.h"
Moritz Pflanzer5b512292017-06-21 15:54:07 +010026#include "PaddingCalculator.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#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/NEON/functions/NEArithmeticAddition.h"
37#include "arm_compute/runtime/Tensor.h"
38#include "arm_compute/runtime/TensorAllocator.h"
39
40#include "boost_wrapper.h"
41
42#include <random>
43#include <string>
44
45using namespace arm_compute;
46using namespace arm_compute::test;
47using namespace arm_compute::test::neon;
48using namespace arm_compute::test::validation;
49
50namespace
51{
52/** Compute Neon arithmetic addition function.
53 *
Michele Di Giorgio81f0d152017-07-11 15:00:52 +010054 * @param[in] shape Shape of the input and output tensors.
55 * @param[in] dt_in0 Data type of first input tensor.
56 * @param[in] dt_in1 Data type of second input tensor.
57 * @param[in] dt_out Data type of the output tensor.
58 * @param[in] policy Overflow policy of the operation.
59 * @param[in] fixed_point_position (Optional) Fixed point position that expresses the number of bits for the fractional part of the number when the tensor's data type is QS8 or QS16 (default = 0).
Anthony Barbier6ff3b192017-09-04 18:44:23 +010060 *
61 * @return Computed output tensor.
62 */
Michele Di Giorgio81f0d152017-07-11 15:00:52 +010063Tensor compute_arithmetic_addition(const TensorShape &shape, DataType dt_in0, DataType dt_in1, DataType dt_out, ConvertPolicy policy, int fixed_point_position = 0)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010064{
65 // Create tensors
Michele Di Giorgio81f0d152017-07-11 15:00:52 +010066 Tensor src1 = create_tensor<Tensor>(shape, dt_in0, 1, fixed_point_position);
67 Tensor src2 = create_tensor<Tensor>(shape, dt_in1, 1, fixed_point_position);
68 Tensor dst = create_tensor<Tensor>(shape, dt_out, 1, fixed_point_position);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010069
70 // Create and configure function
71 NEArithmeticAddition add;
72 add.configure(&src1, &src2, &dst, policy);
73
74 // Allocate tensors
75 src1.allocator()->allocate();
76 src2.allocator()->allocate();
77 dst.allocator()->allocate();
78
79 BOOST_TEST(!src1.info()->is_resizable());
80 BOOST_TEST(!src2.info()->is_resizable());
81 BOOST_TEST(!dst.info()->is_resizable());
82
83 // Fill tensors
84 library->fill_tensor_uniform(NEAccessor(src1), 0);
85 library->fill_tensor_uniform(NEAccessor(src2), 1);
86
87 // Compute function
88 add.run();
89
90 return dst;
91}
92
93void validate_configuration(const Tensor &src1, const Tensor &src2, Tensor &dst, TensorShape shape, ConvertPolicy policy)
94{
95 BOOST_TEST(src1.info()->is_resizable());
96 BOOST_TEST(src2.info()->is_resizable());
97 BOOST_TEST(dst.info()->is_resizable());
98
99 // Create and configure function
100 NEArithmeticAddition add;
101 add.configure(&src1, &src2, &dst, policy);
102
103 // Validate valid region
104 const ValidRegion valid_region = shape_to_valid_region(shape);
105 validate(src1.info()->valid_region(), valid_region);
106 validate(src2.info()->valid_region(), valid_region);
107 validate(dst.info()->valid_region(), valid_region);
108
109 // Validate padding
Moritz Pflanzer2509fba2017-06-23 14:15:03 +0100110 const PaddingSize padding = PaddingCalculator(shape.x(), 16).required_padding();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100111 validate(src1.info()->padding(), padding);
112 validate(src2.info()->padding(), padding);
113 validate(dst.info()->padding(), padding);
114}
115} // namespace
116
117#ifndef DOXYGEN_SKIP_THIS
118BOOST_AUTO_TEST_SUITE(NEON)
119BOOST_AUTO_TEST_SUITE(ArithmeticAddition)
120
121BOOST_AUTO_TEST_SUITE(U8)
122BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit") * boost::unit_test::label("nightly"))
123BOOST_DATA_TEST_CASE(Configuration, (SmallShapes() + LargeShapes()) * boost::unit_test::data::make({ ConvertPolicy::SATURATE, ConvertPolicy::WRAP }),
124 shape, policy)
125{
126 // Create tensors
Moritz Pflanzer94450f12017-06-30 12:48:43 +0100127 Tensor src1 = create_tensor<Tensor>(shape, DataType::U8);
128 Tensor src2 = create_tensor<Tensor>(shape, DataType::U8);
129 Tensor dst = create_tensor<Tensor>(shape, DataType::U8);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100130
131 validate_configuration(src1, src2, dst, shape, policy);
132}
133BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit"))
134BOOST_DATA_TEST_CASE(RunSmall, SmallShapes() * boost::unit_test::data::make({ ConvertPolicy::SATURATE, ConvertPolicy::WRAP }),
135 shape, policy)
136{
137 // Compute function
138 Tensor dst = compute_arithmetic_addition(shape, DataType::U8, DataType::U8, DataType::U8, policy);
139
140 // Compute reference
141 RawTensor ref_dst = Reference::compute_reference_arithmetic_addition(shape, DataType::U8, DataType::U8, DataType::U8, policy);
142
143 // Validate output
144 validate(NEAccessor(dst), ref_dst);
145}
146BOOST_AUTO_TEST_SUITE_END()
147
148BOOST_AUTO_TEST_SUITE(S16)
149BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit") * boost::unit_test::label("nightly"))
150BOOST_DATA_TEST_CASE(Configuration, (SmallShapes() + LargeShapes()) * boost::unit_test::data::make({ DataType::U8, DataType::S16 }) * boost::unit_test::data::make({ ConvertPolicy::SATURATE, ConvertPolicy::WRAP }),
151 shape, dt, policy)
152{
153 // Create tensors
Moritz Pflanzer94450f12017-06-30 12:48:43 +0100154 Tensor src1 = create_tensor<Tensor>(shape, dt);
155 Tensor src2 = create_tensor<Tensor>(shape, DataType::S16);
156 Tensor dst = create_tensor<Tensor>(shape, DataType::S16);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100157
158 validate_configuration(src1, src2, dst, shape, policy);
159}
160BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit"))
161BOOST_DATA_TEST_CASE(RunSmall, SmallShapes() * boost::unit_test::data::make({ DataType::U8, DataType::S16 }) * boost::unit_test::data::make({ ConvertPolicy::SATURATE, ConvertPolicy::WRAP }),
162 shape, dt, policy)
163{
164 // Compute function
165 Tensor dst = compute_arithmetic_addition(shape, dt, DataType::S16, DataType::S16, policy);
166
167 // Compute reference
168 RawTensor ref_dst = Reference::compute_reference_arithmetic_addition(shape, dt, DataType::S16, DataType::S16, policy);
169
170 // Validate output
171 validate(NEAccessor(dst), ref_dst);
172}
173BOOST_TEST_DECORATOR(*boost::unit_test::label("nightly"))
174BOOST_DATA_TEST_CASE(RunLarge, LargeShapes() * boost::unit_test::data::make({ DataType::U8, DataType::S16 }) * boost::unit_test::data::make({ ConvertPolicy::SATURATE, ConvertPolicy::WRAP }),
175 shape, dt, policy)
176{
177 // Compute function
178 Tensor dst = compute_arithmetic_addition(shape, dt, DataType::S16, DataType::S16, policy);
179
180 // Compute reference
181 RawTensor ref_dst = Reference::compute_reference_arithmetic_addition(shape, dt, DataType::S16, DataType::S16, policy);
182
183 // Validate output
184 validate(NEAccessor(dst), ref_dst);
185}
186BOOST_AUTO_TEST_SUITE_END()
187
Michele Di Giorgio81f0d152017-07-11 15:00:52 +0100188BOOST_AUTO_TEST_SUITE(Quantized)
189BOOST_AUTO_TEST_SUITE(QS8)
190BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit"))
191BOOST_DATA_TEST_CASE(RunSmall, SmallShapes() * ConvertPolicies() * boost::unit_test::data::xrange(1, 7),
192 shape, policy, fixed_point_position)
193{
194 // Compute function
195 Tensor dst = compute_arithmetic_addition(shape, DataType::QS8, DataType::QS8, DataType::QS8, policy, fixed_point_position);
196
197 // Compute reference
198 RawTensor ref_dst = Reference::compute_reference_arithmetic_addition(shape, DataType::QS8, DataType::QS8, DataType::QS8, policy, fixed_point_position);
199
200 // Validate output
201 validate(NEAccessor(dst), ref_dst);
202}
203BOOST_TEST_DECORATOR(*boost::unit_test::label("nightly"))
204BOOST_DATA_TEST_CASE(RunLarge, LargeShapes() * ConvertPolicies() * boost::unit_test::data::xrange(1, 7),
205 shape, policy, fixed_point_position)
206{
207 // Compute function
208 Tensor dst = compute_arithmetic_addition(shape, DataType::QS8, DataType::QS8, DataType::QS8, policy, fixed_point_position);
209
210 // Compute reference
211 RawTensor ref_dst = Reference::compute_reference_arithmetic_addition(shape, DataType::QS8, DataType::QS8, DataType::QS8, policy, fixed_point_position);
212
213 // Validate output
214 validate(NEAccessor(dst), ref_dst);
215}
216BOOST_AUTO_TEST_SUITE_END()
217
218BOOST_AUTO_TEST_SUITE(QS16)
219BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit"))
220BOOST_DATA_TEST_CASE(RunSmall, SmallShapes() * ConvertPolicies() * boost::unit_test::data::xrange(1, 15),
221 shape, policy, fixed_point_position)
222{
223 // Compute function
224 Tensor dst = compute_arithmetic_addition(shape, DataType::QS16, DataType::QS16, DataType::QS16, policy, fixed_point_position);
225
226 // Compute reference
227 RawTensor ref_dst = Reference::compute_reference_arithmetic_addition(shape, DataType::QS16, DataType::QS16, DataType::QS16, policy, fixed_point_position);
228
229 // Validate output
230 validate(NEAccessor(dst), ref_dst);
231}
232BOOST_TEST_DECORATOR(*boost::unit_test::label("nightly"))
233BOOST_DATA_TEST_CASE(RunLarge, LargeShapes() * ConvertPolicies() * boost::unit_test::data::xrange(1, 15),
234 shape, policy, fixed_point_position)
235{
236 // Compute function
237 Tensor dst = compute_arithmetic_addition(shape, DataType::QS16, DataType::QS16, DataType::QS16, policy, fixed_point_position);
238
239 // Compute reference
240 RawTensor ref_dst = Reference::compute_reference_arithmetic_addition(shape, DataType::QS16, DataType::QS16, DataType::QS16, policy, fixed_point_position);
241
242 // Validate output
243 validate(NEAccessor(dst), ref_dst);
244}
245BOOST_AUTO_TEST_SUITE_END()
246BOOST_AUTO_TEST_SUITE_END()
247
Pablo Tellod1b0ecc2017-07-11 11:27:04 +0100248#ifdef ARM_COMPUTE_ENABLE_FP16
249BOOST_AUTO_TEST_SUITE(F16)
250BOOST_DATA_TEST_CASE(RunSmall, SmallShapes(), shape)
251{
252 // Compute function
253 Tensor dst = compute_arithmetic_addition(shape, DataType::F16, DataType::F16, DataType::F16, ConvertPolicy::WRAP);
254
255 // Compute reference
256 RawTensor ref_dst = Reference::compute_reference_arithmetic_addition(shape, DataType::F16, DataType::F16, DataType::F16, ConvertPolicy::WRAP);
257
258 // Validate output
259 validate(NEAccessor(dst), ref_dst);
260}
261BOOST_AUTO_TEST_SUITE_END()
262#endif /* ARM_COMPUTE_ENABLE_FP16 */
263
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100264BOOST_AUTO_TEST_SUITE(F32)
265BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit") * boost::unit_test::label("nightly"))
266BOOST_DATA_TEST_CASE(Configuration, (SmallShapes() + LargeShapes()) * boost::unit_test::data::make({ ConvertPolicy::SATURATE, ConvertPolicy::WRAP }),
267 shape, policy)
268{
269 // Create tensors
Moritz Pflanzer94450f12017-06-30 12:48:43 +0100270 Tensor src1 = create_tensor<Tensor>(shape, DataType::F32);
271 Tensor src2 = create_tensor<Tensor>(shape, DataType::F32);
272 Tensor dst = create_tensor<Tensor>(shape, DataType::F32);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100273
274 validate_configuration(src1, src2, dst, shape, policy);
275}
276BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit"))
277BOOST_DATA_TEST_CASE(RunSmall, SmallShapes(), shape)
278{
279 // Compute function
280 Tensor dst = compute_arithmetic_addition(shape, DataType::F32, DataType::F32, DataType::F32, ConvertPolicy::WRAP);
281
282 // Compute reference
283 RawTensor ref_dst = Reference::compute_reference_arithmetic_addition(shape, DataType::F32, DataType::F32, DataType::F32, ConvertPolicy::WRAP);
284
285 // Validate output
286 validate(NEAccessor(dst), ref_dst);
287}
288BOOST_TEST_DECORATOR(*boost::unit_test::label("nightly"))
289BOOST_DATA_TEST_CASE(RunLarge, LargeShapes() * boost::unit_test::data::make({ ConvertPolicy::SATURATE, ConvertPolicy::WRAP }),
290 shape, policy)
291{
292 // Compute function
293 Tensor dst = compute_arithmetic_addition(shape, DataType::F32, DataType::F32, DataType::F32, policy);
294
295 // Compute reference
296 RawTensor ref_dst = Reference::compute_reference_arithmetic_addition(shape, DataType::F32, DataType::F32, DataType::F32, policy);
297
298 // Validate output
299 validate(NEAccessor(dst), ref_dst);
300}
301BOOST_AUTO_TEST_SUITE_END()
302
303BOOST_AUTO_TEST_SUITE_END()
304BOOST_AUTO_TEST_SUITE_END()
Anthony Barbierac69aa12017-07-03 17:39:37 +0100305#endif /* DOXYGEN_SKIP_THIS */