Compute Library
 22.05
ArithmeticAddition.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2022 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"
30 #include "tests/NEON/Accessor.h"
32 #include "tests/datasets/ConvertPolicyDataset.h"
33 #include "tests/datasets/ShapeDatasets.h"
35 #include "tests/framework/Macros.h"
38 #include "tests/validation/fixtures/ArithmeticOperationsFixture.h"
39 
40 namespace arm_compute
41 {
42 namespace test
43 {
44 namespace validation
45 {
46 namespace
47 {
48 #if !defined(__aarch64__) || defined(ENABLE_SVE)
49 constexpr AbsoluteTolerance<float> tolerance_quant(1); /**< Tolerance value for comparing reference's output against implementation's output for quantized data types */
50 #else // !defined(__aarch64__) || defined(ENABLE_SVE)
51 constexpr AbsoluteTolerance<float> tolerance_quant(0);
52 #endif // !defined(__aarch64__) || defined(ENABLE_SVE)
53 const auto InPlaceDataSet = framework::dataset::make("InPlace", { false, true });
54 const auto OutOfPlaceDataSet = framework::dataset::make("InPlace", { false });
55 } // namespace
56 
57 TEST_SUITE(NEON)
58 TEST_SUITE(ArithmeticAddition)
59 
60 template <typename T>
61 using NEArithmeticAdditionFixture = ArithmeticAdditionValidationFixture<Tensor, Accessor, NEArithmeticAddition, T>;
62 
63 // *INDENT-OFF*
64 // clang-format off
66  framework::dataset::make("Input1Info", { TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
67  TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::U8), // Unsupported broadcast
68  TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8), // Invalid data type combination
69  TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),// Mismatching shapes
70  }),
71  framework::dataset::make("Input2Info",{ TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
73  TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::S16),
74  TensorInfo(TensorShape(48U, 11U, 2U), 1, DataType::F32),
75  })),
76  framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
77  TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::S16),
78  TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8),
79  TensorInfo(TensorShape(48U, 11U, 2U), 1, DataType::F32),
80  })),
81  framework::dataset::make("Expected", { true, false, false, false})),
83 {
84  Status s = NEArithmeticAddition::validate(&input1_info.clone()->set_is_resizable(false),
85  &input2_info.clone()->set_is_resizable(false),
86  &output_info.clone()->set_is_resizable(false),
89 }
90 
92  combine(framework::dataset::make("CpuExt", std::string("NEON")),
101  })),
102  combine(framework::dataset::make("CpuExt", std::string("SVE")),
105  DataType::U8,
108  }))),
109  combine(framework::dataset::make("CpuExt", std::string("SVE2")),
113  }))),
115 {
116  using namespace cpu::kernels;
117 
119  cpu_isa.neon = (cpu_ext == "NEON");
120  cpu_isa.sve = (cpu_ext == "SVE");
121  cpu_isa.sve2 = (cpu_ext == "SVE2");
123 
124  const auto *selected_impl = CpuAddKernel::get_implementation(DataTypeISASelectorData{data_type, cpu_isa}, cpu::KernelSelectionType::Preferred);
125 
127 
128  std::string expected = lower_string(cpu_ext) + "_" + cpu_impl_dt(data_type) + "_add";
129  std::string actual = selected_impl->name;
130 
132 }
133 // clang-format on
134 // *INDENT-ON*
135 
137 {
138  // NEArithmeticAddition doesn't use padding, so make sure this is the case.
139  Tensor input1 = create_tensor<Tensor>(TensorShape(15U, 15U), DataType::F32);
140  Tensor input2 = create_tensor<Tensor>(TensorShape(15U, 1U), DataType::F32);
141  Tensor output = create_tensor<Tensor>(TensorShape(15U, 15U), DataType::F32);
142 
144  add.configure(&input1, &input2, &output, ConvertPolicy::WRAP);
145 
146  // Validate padding is zero
147  validate(input1.info()->padding(), PaddingSize());
148  validate(input2.info()->padding(), PaddingSize());
149  validate(output.info()->padding(), PaddingSize());
150 }
151 
152 TEST_SUITE(Integer)
153 TEST_SUITE(U8)
154 FIXTURE_DATA_TEST_CASE(RunSmall, NEArithmeticAdditionFixture<uint8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType",
155  DataType::U8)),
156  framework::dataset::make("ConvertPolicy", { ConvertPolicy::SATURATE, ConvertPolicy::WRAP })),
157  OutOfPlaceDataSet))
158 {
159  // Validate output
160  validate(Accessor(_target), _reference);
161 }
162 TEST_SUITE_END() // U8
163 
165 FIXTURE_DATA_TEST_CASE(RunSmall, NEArithmeticAdditionFixture<int16_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType",
166  DataType::S16)),
167  framework::dataset::make("ConvertPolicy", { ConvertPolicy::SATURATE, ConvertPolicy::WRAP })),
168  OutOfPlaceDataSet))
169 {
170  // Validate output
171  validate(Accessor(_target), _reference);
172 }
173 
175  DataType::S16)),
177  OutOfPlaceDataSet))
178 {
179  // Validate output
180  validate(Accessor(_target), _reference);
181 }
182 TEST_SUITE_END() // S16
183 
185 FIXTURE_DATA_TEST_CASE(RunSmall, NEArithmeticAdditionFixture<int32_t>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType",
186  DataType::S32)),
187  framework::dataset::make("ConvertPolicy", { ConvertPolicy::SATURATE, ConvertPolicy::WRAP })),
188  OutOfPlaceDataSet))
189 {
190  // Validate output
191  validate(Accessor(_target), _reference);
192 }
193 TEST_SUITE_END() // S32
194 TEST_SUITE_END() // Integer
195 
196 TEST_SUITE(Float)
197 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
201  OutOfPlaceDataSet))
202 {
203  // Validate output
204  validate(Accessor(_target), _reference);
205 }
206 TEST_SUITE_END() // F16
207 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
208 
210 FIXTURE_DATA_TEST_CASE(RunSmall, NEArithmeticAdditionFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType",
211  DataType::F32)),
212  framework::dataset::make("ConvertPolicy", { ConvertPolicy::SATURATE, ConvertPolicy::WRAP })),
213  OutOfPlaceDataSet))
214 {
215  // Validate output
216  validate(Accessor(_target), _reference);
217 }
218 
220  DataType::F32)),
222  OutOfPlaceDataSet))
223 {
224  // Validate output
225  validate(Accessor(_target), _reference);
226 }
227 
228 template <typename T>
229 using NEArithmeticAdditionBroadcastFixture = ArithmeticAdditionBroadcastValidationFixture<Tensor, Accessor, NEArithmeticAddition, T>;
230 
234  OutOfPlaceDataSet))
235 {
236  // Validate output
237  validate(Accessor(_target), _reference);
238 }
239 
243  OutOfPlaceDataSet))
244 {
245  // Validate output
246  validate(Accessor(_target), _reference);
247 }
248 TEST_SUITE_END() // F32
249 TEST_SUITE_END() // Float
250 
251 template <typename T>
252 using NEArithmeticAdditionQuantizedFixture = ArithmeticAdditionValidationQuantizedFixture<Tensor, Accessor, NEArithmeticAddition, T>;
253 
254 template <typename T>
255 using NEArithmeticAdditionQuantizedBroadcastFixture = ArithmeticAdditionValidationQuantizedBroadcastFixture<Tensor, Accessor, NEArithmeticAddition, T>;
256 
257 TEST_SUITE(Quantized)
258 TEST_SUITE(QASYMM8)
260  NEArithmeticAdditionQuantizedFixture<uint8_t>,
261  framework::DatasetMode::PRECOMMIT,
262  combine(combine(combine(combine(combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::QASYMM8)),
263  framework::dataset::make("ConvertPolicy", { ConvertPolicy::SATURATE })),
264  framework::dataset::make("Src0QInfo", { QuantizationInfo(5.f / 255.f, 20) })),
265  framework::dataset::make("Src1QInfo", { QuantizationInfo(2.f / 255.f, 10) })),
266  framework::dataset::make("OutQInfo", { QuantizationInfo(1.f / 255.f, 5) })),
267  OutOfPlaceDataSet))
268 {
269  // Validate output
270  validate(Accessor(_target), _reference, tolerance_quant);
271 }
272 TEST_SUITE_END() // QASYMM8
273 
275 FIXTURE_DATA_TEST_CASE(RunSmall,
279  framework::dataset::make("ConvertPolicy", { ConvertPolicy::SATURATE })),
280  framework::dataset::make("Src0QInfo", { QuantizationInfo(0.5f, 20) })),
281  framework::dataset::make("Src1QInfo", { QuantizationInfo(0.5f, 10) })),
282  framework::dataset::make("OutQInfo", { QuantizationInfo(0.5f, 5) })),
283  OutOfPlaceDataSet))
284 {
285  // Validate output
286  validate(Accessor(_target), _reference, tolerance_quant);
287 }
288 
290  datasets::SmallShapesBroadcast(), framework::dataset::make("DataType", DataType::QASYMM8_SIGNED)),
291  framework::dataset::make("ConvertPolicy", { ConvertPolicy::SATURATE })),
292  framework::dataset::make("Src0QInfo", { QuantizationInfo(0.5f, 20) })),
293  framework::dataset::make("Src1QInfo", { QuantizationInfo(0.5f, 10) })),
294  framework::dataset::make("OutQInfo", { QuantizationInfo(0.5f, 5) })),
295  OutOfPlaceDataSet))
296 {
297  // Validate output
298  validate(Accessor(_target), _reference, tolerance_quant);
299 }
300 TEST_SUITE_END() // QASYMM8_SIGNED
301 
303 FIXTURE_DATA_TEST_CASE(RunSmall,
307  framework::dataset::make("ConvertPolicy", { ConvertPolicy::SATURATE })),
308  framework::dataset::make("Src0QInfo", { QuantizationInfo(1.f / 32768.f, 0), QuantizationInfo(5.f / 32768.f, 0) })),
309  framework::dataset::make("Src1QInfo", { QuantizationInfo(2.f / 32768.f, 0), QuantizationInfo(5.f / 32768.f, 0) })),
310  framework::dataset::make("OutQInfo", { QuantizationInfo(5.f / 32768.f, 0) })),
311  OutOfPlaceDataSet))
312 {
313  // Validate output
314  validate(Accessor(_target), _reference, tolerance_quant);
315 }
316 TEST_SUITE_END() // QSYMM16
317 TEST_SUITE_END() // Quantized
318 
319 TEST_SUITE_END() // ArithmeticAddition
320 TEST_SUITE_END() // Neon
321 } // namespace validation
322 } // namespace test
323 } // namespace arm_compute
Retrieve the best implementation available for the given Cpu ISA, ignoring the build flags...
Shape of a tensor.
Definition: TensorShape.h:39
Basic function to run cpu::kernels::CpuAddKernel.
quantized, symmetric fixed-point 16-bit number
static Status validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, ConvertPolicy policy, const ActivationLayerInfo &act_info=ActivationLayerInfo())
Static function to check if given info will lead to a valid configuration of NEArithmeticAddition.
1 channel, 1 U8 per channel
const CpuCastKernel::CastKernel * selected_impl
Definition: Cast.cpp:205
1 channel, 1 F32 per channel
ARM_COMPUTE_EXPECT(has_error==expected, framework::LogLevel::ERRORS)
std::enable_if< is_container< T >::value, ContainerDataset< T > >::type make(std::string name, T &&values)
Helper function to create a ContainerDataset.
Status class.
Definition: Error.h:52
std::string lower_string(const std::string &val)
Lower a given string.
Definition: Utils.cpp:351
void configure(const ITensor *input1, const ITensor *input2, ITensor *output, ConvertPolicy policy, const ActivationLayerInfo &act_info=ActivationLayerInfo())
Initialise the kernel&#39;s inputs, output and conversion policy.
Copyright (c) 2017-2022 Arm Limited.
cpuinfo::CpuIsaInfo cpu_isa
Definition: Cast.cpp:207
std::string cpu_impl_dt(const DataType &data_type)
Returns the suffix string of CPU kernel implementation names based on the given data type...
Definition: Utils.h:1245
1 channel, 1 F16 per channel
ITensorInfo * info() const override
Interface to be implemented by the child class to return the tensor&#39;s metadata.
Definition: Tensor.cpp:33
1 channel, 1 S32 per channel
CPU ISA (Instruction Set Architecture) information.
Definition: CpuIsaInfo.h:37
Quantization information.
DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QASYMM8), TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QASYMM8), TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16), }), framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F16), TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QASYMM8), TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QASYMM8), TensorInfo(TensorShape(30U, 11U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16, QuantizationInfo(1.f/32768.f, 0)), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16, QuantizationInfo(1.f/32768.f, 0)), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16, QuantizationInfo(1.f/32768.f, 0)), })), framework::dataset::make("ActivationInfo", { ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::SQRT), })), framework::dataset::make("Expected", { false, true, true, true, false, false, true, true, false })), input_info, output_info, act_info, expected)
Accessor implementation for Tensor objects.
Definition: Accessor.h:35
DatasetMode
Possible dataset modes.
Definition: DatasetModes.h:40
TEST_SUITE_END() FIXTURE_DATA_TEST_CASE(RunSmall
[CLActivationLayer Test snippet]
quantized, asymmetric fixed-point 8-bit number unsigned
Basic implementation of the tensor interface.
Definition: Tensor.h:37
validate(CLAccessor(output_state), expected_output)
virtual PaddingSize padding() const =0
Padding of tensor.
BorderSize PaddingSize
Container for 2D padding size.
Definition: Types.h:397
ArithmeticAdditionBroadcastValidationFixture< Tensor, Accessor, NEArithmeticAddition, T > NEArithmeticAdditionBroadcastFixture
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
ArithmeticAdditionValidationQuantizedBroadcastFixture< Tensor, Accessor, NEArithmeticAddition, T > NEArithmeticAdditionQuantizedBroadcastFixture
ARM_COMPUTE_ERROR_ON_NULLPTR(selected_impl)
ARM_COMPUTE_EXPECT_EQUAL(expected, actual, framework::LogLevel::ERRORS)
Store the tensor&#39;s metadata.
Definition: TensorInfo.h:43
JoinDataset< T, U > concat(T &&dataset1, U &&dataset2)
Helper function to create a JoinDataset.
Definition: JoinDataset.h:160
TEST_CASE(FusedActivation, framework::DatasetMode::ALL)
Validate fused activation expecting the following behaviours:
quantized, asymmetric fixed-point 8-bit number signed
ArithmeticAdditionValidationFixture< Tensor, Accessor, NEArithmeticAddition, T > NEArithmeticAdditionFixture
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}))
TEST_SUITE(QASYMM8_to_F32) FIXTURE_DATA_TEST_CASE(RunSmall
DataType
Available data types.
Definition: Types.h:79
combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::F32)))
Definition: AbsLayer.cpp:65
ConvertPolicy
Policy to handle integer overflow.
Definition: Types.h:404