Compute Library
 20.02.1
ArithmeticAddition.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2019 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/Types.h"
28 #include "tests/CL/CLAccessor.h"
30 #include "tests/datasets/ConvertPolicyDataset.h"
31 #include "tests/datasets/ShapeDatasets.h"
33 #include "tests/framework/Macros.h"
36 #include "tests/validation/fixtures/ArithmeticOperationsFixture.h"
37 
38 namespace arm_compute
39 {
40 namespace test
41 {
42 namespace validation
43 {
44 namespace
45 {
46 constexpr unsigned int num_elems_processed_per_iteration = 16;
47 /** Input data sets **/
48 const auto ArithmeticAdditionU8Dataset = combine(combine(framework::dataset::make("DataType", DataType::U8), framework::dataset::make("DataType", DataType::U8)), framework::dataset::make("DataType",
49  DataType::U8));
50 const auto ArithmeticAdditionQASYMM8Dataset = combine(combine(framework::dataset::make("DataType", DataType::QASYMM8), framework::dataset::make("DataType", DataType::QASYMM8)),
51  framework::dataset::make("DataType",
53 const auto ArithmeticAdditionQASYMM8SignedDataset = combine(combine(framework::dataset::make("DataType", DataType::QASYMM8_SIGNED), framework::dataset::make("DataType", DataType::QASYMM8_SIGNED)),
54  framework::dataset::make("DataType",
56 const auto ArithmeticAdditionQSYMM16Dataset = combine(combine(framework::dataset::make("DataType", DataType::QSYMM16), framework::dataset::make("DataType", DataType::QSYMM16)),
57  framework::dataset::make("DataType",
59 const auto ArithmeticAdditionS16Dataset = combine(combine(framework::dataset::make("DataType", { DataType::U8, DataType::S16 }), framework::dataset::make("DataType", DataType::S16)),
61 const auto ArithmeticAdditionFP16Dataset = combine(combine(framework::dataset::make("DataType", DataType::F16), framework::dataset::make("DataType", DataType::F16)),
63 const auto ArithmeticAdditionFP32Dataset = combine(combine(framework::dataset::make("DataType", DataType::F32), framework::dataset::make("DataType", DataType::F32)),
65 } // namespace
66 
67 TEST_SUITE(CL)
68 TEST_SUITE(ArithmeticAddition)
69 
70 // *INDENT-OFF*
71 // clang-format off
72 DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(
73  framework::dataset::make("Input1Info", { TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8),
74  TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8),
75  TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::U8), // Window shrink
76  TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8), // Invalid data type combination
77  TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Mismatching shapes
78  }),
79  framework::dataset::make("Input2Info",{ TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8),
80  TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8),
81  TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::U8),
82  TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::S16),
83  TensorInfo(TensorShape(48U, 11U, 2U), 1, DataType::F32),
84  })),
85  framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::S16),
86  TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8),
87  TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::U8),
88  TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8),
89  TensorInfo(TensorShape(48U, 11U, 2U), 1, DataType::F32),
90  })),
91  framework::dataset::make("Expected", { true, true, false, false, false})),
92  input1_info, input2_info, output_info, expected)
93 {
94  ARM_COMPUTE_EXPECT(bool(CLArithmeticAddition::validate(&input1_info.clone()->set_is_resizable(false), &input2_info.clone()->set_is_resizable(false), &output_info.clone()->set_is_resizable(false), ConvertPolicy::WRAP)) == expected, framework::LogLevel::ERRORS);
95 }
96 // clang-format on
97 // *INDENT-ON*
98 
99 template <typename T>
100 using CLArithmeticAdditionFixture = ArithmeticAdditionValidationFixture<CLTensor, CLAccessor, CLArithmeticAddition, T>;
101 
102 TEST_SUITE(Integer)
103 TEST_SUITE(U8)
104 DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(datasets::SmallShapes(), framework::dataset::make("ConvertPolicy", { ConvertPolicy::SATURATE, ConvertPolicy::WRAP })),
105  shape, policy)
106 {
107  // Create tensors
108  CLTensor ref_src1 = create_tensor<CLTensor>(shape, DataType::U8);
109  CLTensor ref_src2 = create_tensor<CLTensor>(shape, DataType::U8);
110  CLTensor dst = create_tensor<CLTensor>(shape, DataType::U8);
111 
112  // Create and Configure function
115 
116  // Validate valid region
119 
120  // Validate padding
122  validate(ref_src1.info()->padding(), padding);
125 }
126 
127 FIXTURE_DATA_TEST_CASE(RunSmall, CLArithmeticAdditionFixture<uint8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ArithmeticAdditionU8Dataset),
129 {
130  // Validate output
131  validate(CLAccessor(_target), _reference);
132 }
133 TEST_SUITE_END() // U8
134 
135 TEST_SUITE(S16)
136 DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType", { DataType::U8, DataType::S16 })),
139 {
140  // Create tensors
141  CLTensor ref_src1 = create_tensor<CLTensor>(shape, data_type);
142  CLTensor ref_src2 = create_tensor<CLTensor>(shape, DataType::S16);
143  CLTensor dst = create_tensor<CLTensor>(shape, DataType::S16);
144 
145  // Create and Configure function
148 
149  // Validate valid region
152 
153  // Validate padding
155  validate(ref_src1.info()->padding(), padding);
158 }
159 
160 FIXTURE_DATA_TEST_CASE(RunSmall, CLArithmeticAdditionFixture<int16_t>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ArithmeticAdditionS16Dataset),
162 {
163  // Validate output
164  validate(CLAccessor(_target), _reference);
165 }
166 
167 FIXTURE_DATA_TEST_CASE(RunLarge, CLArithmeticAdditionFixture<int16_t>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), ArithmeticAdditionS16Dataset),
169 {
170  // Validate output
171  validate(CLAccessor(_target), _reference);
172 }
173 TEST_SUITE_END() // S16
174 TEST_SUITE_END() // Integer
175 
176 template <typename T>
177 using CLArithmeticAdditionQuantizedFixture = ArithmeticAdditionValidationQuantizedFixture<CLTensor, CLAccessor, CLArithmeticAddition, T>;
178 
179 TEST_SUITE(Quantized)
180 TEST_SUITE(QASYMM8)
181 DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(datasets::SmallShapes(), framework::dataset::make("ConvertPolicy", { ConvertPolicy::SATURATE })),
182  shape, policy)
183 {
184  // Create tensors
185  CLTensor ref_src1 = create_tensor<CLTensor>(shape, DataType::QASYMM8);
186  CLTensor ref_src2 = create_tensor<CLTensor>(shape, DataType::QASYMM8);
187  CLTensor dst = create_tensor<CLTensor>(shape, DataType::QASYMM8);
188 
189  // Create and Configure function
192 
193  // Validate valid region
196 
197  // Validate padding
199  validate(ref_src1.info()->padding(), padding);
202 }
203 
204 FIXTURE_DATA_TEST_CASE(RunSmall, CLArithmeticAdditionQuantizedFixture<uint8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(combine(combine(datasets::SmallShapes(),
205  ArithmeticAdditionQASYMM8Dataset),
206  framework::dataset::make("ConvertPolicy", { ConvertPolicy::SATURATE })),
207  framework::dataset::make("Src0QInfo", { QuantizationInfo(5.f / 255.f, 20) })),
208  framework::dataset::make("Src1QInfo", { QuantizationInfo(2.f / 255.f, 10) })),
209  framework::dataset::make("OutQInfo", { QuantizationInfo(1.f / 255.f, 5) })))
210 {
211  // Validate output
212  validate(CLAccessor(_target), _reference);
213 }
214 TEST_SUITE_END() // QASYMM8
215 TEST_SUITE(QASYMM8_SIGNED)
216 DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(datasets::SmallShapes(), framework::dataset::make("ConvertPolicy", { ConvertPolicy::SATURATE })),
217  shape, policy)
218 {
219  // Create tensors
220  CLTensor ref_src1 = create_tensor<CLTensor>(shape, DataType::QASYMM8_SIGNED);
221  CLTensor ref_src2 = create_tensor<CLTensor>(shape, DataType::QASYMM8_SIGNED);
222  CLTensor dst = create_tensor<CLTensor>(shape, DataType::QASYMM8_SIGNED);
223 
224  // Create and Configure function
225  CLArithmeticAddition add;
227 
228  // Validate valid region
229  const ValidRegion valid_region = shape_to_valid_region(shape);
231 
232  // Validate padding
233  const PaddingSize padding = PaddingCalculator(shape.x(), num_elems_processed_per_iteration).required_padding();
234  validate(ref_src1.info()->padding(), padding);
237 }
238 
239 FIXTURE_DATA_TEST_CASE(RunSmall, CLArithmeticAdditionQuantizedFixture<int8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(combine(combine(datasets::SmallShapes(),
240  ArithmeticAdditionQASYMM8SignedDataset),
241  framework::dataset::make("ConvertPolicy", { ConvertPolicy::SATURATE })),
242  framework::dataset::make("Src0QInfo", { QuantizationInfo(5.f / 255.f, 10) })),
243  framework::dataset::make("Src1QInfo", { QuantizationInfo(2.f / 255.f, 10) })),
244  framework::dataset::make("OutQInfo", { QuantizationInfo(1.f / 255.f, 5) })))
245 {
246  // Validate output
247  validate(CLAccessor(_target), _reference);
248 }
249 TEST_SUITE_END() // QASYMM8_SIGNED
250 TEST_SUITE(QSYMM16)
251 DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(datasets::SmallShapes(), framework::dataset::make("ConvertPolicy", { ConvertPolicy::SATURATE })),
252  shape, policy)
253 {
254  // Create tensors
255  CLTensor ref_src1 = create_tensor<CLTensor>(shape, DataType::QSYMM16);
256  CLTensor ref_src2 = create_tensor<CLTensor>(shape, DataType::QSYMM16);
257  CLTensor dst = create_tensor<CLTensor>(shape, DataType::QSYMM16);
258 
259  // Create and Configure function
260  CLArithmeticAddition add;
262 
263  // Validate valid region
264  const ValidRegion valid_region = shape_to_valid_region(shape);
266 
267  // Validate padding
268  const PaddingSize padding = PaddingCalculator(shape.x(), num_elems_processed_per_iteration).required_padding();
269  validate(ref_src1.info()->padding(), padding);
272 }
273 
274 FIXTURE_DATA_TEST_CASE(RunSmall, CLArithmeticAdditionQuantizedFixture<int16_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(combine(combine(datasets::SmallShapes(),
275  ArithmeticAdditionQSYMM16Dataset),
276  framework::dataset::make("ConvertPolicy", { ConvertPolicy::SATURATE })),
277  framework::dataset::make("Src0QInfo", { QuantizationInfo(1.f / 32768.f, 0), QuantizationInfo(5.f / 32768.f, 0) })),
278  framework::dataset::make("Src1QInfo", { QuantizationInfo(2.f / 32768.f, 0), QuantizationInfo(5.f / 32768.f, 0) })),
279  framework::dataset::make("OutQInfo", { QuantizationInfo(5.f / 32768.f, 0) })))
280 {
281  // Validate output
282  validate(CLAccessor(_target), _reference);
283 }
284 TEST_SUITE_END() // QSYMM16
285 TEST_SUITE_END() // Quantized
286 
287 TEST_SUITE(Float)
288 TEST_SUITE(FP16)
289 FIXTURE_DATA_TEST_CASE(RunSmall, CLArithmeticAdditionFixture<half>, framework::DatasetMode::ALL, combine(combine(datasets::SmallShapes(), ArithmeticAdditionFP16Dataset),
290  framework::dataset::make("ConvertPolicy", { ConvertPolicy::SATURATE, ConvertPolicy::WRAP })))
291 {
292  // Validate output
293  validate(CLAccessor(_target), _reference);
294 }
295 TEST_SUITE_END() // FP16
296 
297 TEST_SUITE(FP32)
298 DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(datasets::SmallShapes(), framework::dataset::make("ConvertPolicy", { ConvertPolicy::SATURATE, ConvertPolicy::WRAP })),
299  shape, policy)
300 {
301  // Create tensors
302  CLTensor ref_src1 = create_tensor<CLTensor>(shape, DataType::F32);
303  CLTensor ref_src2 = create_tensor<CLTensor>(shape, DataType::F32);
304  CLTensor dst = create_tensor<CLTensor>(shape, DataType::F32);
305 
306  // Create and Configure function
307  CLArithmeticAddition add;
309 
310  // Validate valid region
311  const ValidRegion valid_region = shape_to_valid_region(shape);
313 
314  // Validate padding
315  const PaddingSize padding = PaddingCalculator(shape.x(), num_elems_processed_per_iteration).required_padding();
316  validate(ref_src1.info()->padding(), padding);
319 }
320 
321 FIXTURE_DATA_TEST_CASE(RunSmall, CLArithmeticAdditionFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ArithmeticAdditionFP32Dataset),
323 {
324  // Validate output
325  validate(CLAccessor(_target), _reference);
326 }
327 
328 FIXTURE_DATA_TEST_CASE(RunLarge, CLArithmeticAdditionFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), ArithmeticAdditionFP32Dataset),
330 {
331  // Validate output
332  validate(CLAccessor(_target), _reference);
333 }
334 
335 template <typename T>
336 using CLArithmeticAdditionBroadcastFixture = ArithmeticAdditionBroadcastValidationFixture<CLTensor, CLAccessor, CLArithmeticAddition, T>;
337 
339  ArithmeticAdditionFP32Dataset),
341 {
342  // Validate output
343  validate(CLAccessor(_target), _reference);
344 }
345 
347  ArithmeticAdditionFP32Dataset),
349 {
350  // Validate output
351  validate(CLAccessor(_target), _reference);
352 }
353 TEST_SUITE_END() // FP32
354 TEST_SUITE_END() // Float
355 
356 TEST_SUITE_END() // ArithmeticAddition
357 TEST_SUITE_END() // CL
358 } // namespace validation
359 } // namespace test
360 } // namespace arm_compute
static Status validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, ConvertPolicy policy)
Static function to check if given info will lead to a valid configuration of CLSaturatedArithmeticOpe...
Shape of a tensor.
Definition: TensorShape.h:39
quantized, symmetric fixed-point 16-bit number
TensorInfo * info() const override
Interface to be implemented by the child class to return the tensor's metadata.
Definition: CLTensor.cpp:41
Container for 2D border size.
Definition: Types.h:269
ValidRegion valid_region() const override
Valid region of the tensor.
Definition: TensorInfo.h:303
1 channel, 1 U8 per channel
half_float::half half
16-bit floating point type
Definition: Types.h:45
1 channel, 1 F32 per channel
ARM_COMPUTE_EXPECT(has_error==expected, framework::LogLevel::ERRORS)
Basic function to run CLSaturatedArithmeticOperationKernel for addition.
void configure(ICLTensor *input1, ICLTensor *input2, ICLTensor *output, ConvertPolicy policy)
Initialise the kernel's inputs, output and conversion policy.
std::enable_if< is_container< T >::value, ContainerDataset< T > >::type make(std::string name, T &&values)
Helper function to create a ContainerDataset.
PaddingSize padding() const override
Padding of tensor.
Definition: TensorInfo.h:277
Copyright (c) 2017-2020 ARM Limited.
1 channel, 1 F16 per channel
Quantization information.
DatasetMode
Possible dataset modes.
Definition: DatasetModes.h:40
quantized, asymmetric fixed-point 8-bit number unsigned
Accessor implementation for CLTensor objects.
Definition: CLAccessor.h:35
BorderSize PaddingSize
Container for 2D padding size.
Definition: Types.h:356
1 channel, 1 S16 per channel
FIXTURE_DATA_TEST_CASE(RunSmall, CLAbsLayerFixture< half >, framework::DatasetMode::PRECOMMIT, combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::F16)))
Definition: AbsLayer.cpp:50
validate(dst.info() ->valid_region(), valid_region)
TEST_SUITE_END() FIXTURE_DATA_TEST_CASE(RunSmall
Input data sets.
unsigned int num_elems_processed_per_iteration
Store the tensor's metadata.
Definition: TensorInfo.h:45
quantized, asymmetric fixed-point 8-bit number signed
Container for valid region of a window.
Definition: Types.h:184
zip(zip(framework::dataset::make("Weights", { TensorInfo(TensorShape(32U, 13U, 2U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U, 1U), 1, DataType::F32), }), framework::dataset::make("MVBGInfo",{ TensorInfo(TensorShape(2U), 1, DataType::F32), TensorInfo(TensorShape(2U), 1, DataType::F16), TensorInfo(TensorShape(5U), 1, DataType::F32), })), framework::dataset::make("Expected", { true, false, false}))
DataType
Available data types.
Definition: Types.h:75
ArithmeticAdditionBroadcastValidationFixture< CLTensor, CLAccessor, CLArithmeticAddition, T > CLArithmeticAdditionBroadcastFixture
combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::F32)))
Definition: AbsLayer.cpp:65
ConvertPolicy
Policy to handle overflow.
Definition: Types.h:359
TEST_SUITE(U8_to_S8) DATA_TEST_CASE(Configuration
ValidRegion shape_to_valid_region(const TensorShape &a_shape, bool border_undefined=false, BorderSize border_size=BorderSize(0))
Create a valid region based on tensor shape, border mode and border size.
Definition: Utils.h:224
ArithmeticAdditionValidationFixture< CLTensor, CLAccessor, CLArithmeticAddition, T > CLArithmeticAdditionFixture
Calculate required padding.
DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(datasets::SmallShapes(), AbsoluteDifferenceU8Dataset), shape, data_type0, data_type1, output_data_type)
Basic implementation of the OpenCL tensor interface.
Definition: CLTensor.h:41