Compute Library
 22.11
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(combine(framework::dataset::make("CpuExt", std::string("NEON")),
101  })),
102  framework::dataset::make("CanUseFixedpoint", {true, false})),
103  combine(combine(framework::dataset::make("CpuExt", std::string("SVE")),
106  DataType::U8,
109  })),
110  framework::dataset::make("CanUseFixedpoint", {true, false}))),
111  combine(combine(framework::dataset::make("CpuExt", std::string("SVE2")),
115  })),
116  framework::dataset::make("CanUseFixedpoint", {true, false}))),
117  cpu_ext, data_type, can_use_fixedpoint)
118 {
119  using namespace cpu::kernels;
120 
122  cpu_isa.neon = (cpu_ext == "NEON");
123  cpu_isa.sve = (cpu_ext == "SVE");
124  cpu_isa.sve2 = (cpu_ext == "SVE2");
126 
127  const auto *selected_impl = CpuAddKernel::get_implementation(CpuAddKernelDataTypeISASelectorData{data_type, cpu_isa, can_use_fixedpoint}, cpu::KernelSelectionType::Preferred);
128 
130 
131  bool qasymm8_any = (data_type == DataType::QASYMM8 || data_type == DataType::QASYMM8_SIGNED);
132 
133  std::string expected;
134  if(qasymm8_any && can_use_fixedpoint)
135  {
136  expected = "neon_" + cpu_impl_dt(data_type) + "_add_fixedpoint";
137  }
138  else
139  {
140  expected = lower_string(cpu_ext) + "_" + cpu_impl_dt(data_type) + "_add";
141  }
142 
143  std::string actual = selected_impl->name;
144 
146 }
147 // clang-format on
148 // *INDENT-ON*
149 
151 {
152  // NEArithmeticAddition doesn't use padding, so make sure this is the case.
153  Tensor input1 = create_tensor<Tensor>(TensorShape(15U, 15U), DataType::F32);
154  Tensor input2 = create_tensor<Tensor>(TensorShape(15U, 1U), DataType::F32);
155  Tensor output = create_tensor<Tensor>(TensorShape(15U, 15U), DataType::F32);
156 
158  add.configure(&input1, &input2, &output, ConvertPolicy::WRAP);
159 
160  // Validate padding is zero
161  validate(input1.info()->padding(), PaddingSize());
162  validate(input2.info()->padding(), PaddingSize());
163  validate(output.info()->padding(), PaddingSize());
164 }
165 
166 TEST_SUITE(Integer)
167 TEST_SUITE(U8)
168 FIXTURE_DATA_TEST_CASE(RunSmall, NEArithmeticAdditionFixture<uint8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType",
169  DataType::U8)),
170  framework::dataset::make("ConvertPolicy", { ConvertPolicy::SATURATE, ConvertPolicy::WRAP })),
171  OutOfPlaceDataSet))
172 {
173  // Validate output
174  validate(Accessor(_target), _reference);
175 }
176 TEST_SUITE_END() // U8
177 
179 FIXTURE_DATA_TEST_CASE(RunSmall, NEArithmeticAdditionFixture<int16_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType",
180  DataType::S16)),
181  framework::dataset::make("ConvertPolicy", { ConvertPolicy::SATURATE, ConvertPolicy::WRAP })),
182  OutOfPlaceDataSet))
183 {
184  // Validate output
185  validate(Accessor(_target), _reference);
186 }
187 
189  DataType::S16)),
191  OutOfPlaceDataSet))
192 {
193  // Validate output
194  validate(Accessor(_target), _reference);
195 }
196 TEST_SUITE_END() // S16
197 
199 FIXTURE_DATA_TEST_CASE(RunSmall, NEArithmeticAdditionFixture<int32_t>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType",
200  DataType::S32)),
201  framework::dataset::make("ConvertPolicy", { ConvertPolicy::SATURATE, ConvertPolicy::WRAP })),
202  OutOfPlaceDataSet))
203 {
204  // Validate output
205  validate(Accessor(_target), _reference);
206 }
207 TEST_SUITE_END() // S32
208 TEST_SUITE_END() // Integer
209 
210 TEST_SUITE(Float)
211 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
215  OutOfPlaceDataSet))
216 {
217  // Validate output
218  validate(Accessor(_target), _reference);
219 }
220 TEST_SUITE_END() // F16
221 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
222 
224 FIXTURE_DATA_TEST_CASE(RunSmall, NEArithmeticAdditionFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType",
225  DataType::F32)),
226  framework::dataset::make("ConvertPolicy", { ConvertPolicy::SATURATE, ConvertPolicy::WRAP })),
227  OutOfPlaceDataSet))
228 {
229  // Validate output
230  validate(Accessor(_target), _reference);
231 }
232 
234  DataType::F32)),
236  OutOfPlaceDataSet))
237 {
238  // Validate output
239  validate(Accessor(_target), _reference);
240 }
241 
242 template <typename T>
243 using NEArithmeticAdditionBroadcastFixture = ArithmeticAdditionBroadcastValidationFixture<Tensor, Accessor, NEArithmeticAddition, T>;
244 
248  OutOfPlaceDataSet))
249 {
250  // Validate output
251  validate(Accessor(_target), _reference);
252 }
253 
257  OutOfPlaceDataSet))
258 {
259  // Validate output
260  validate(Accessor(_target), _reference);
261 }
262 TEST_SUITE_END() // F32
263 TEST_SUITE_END() // Float
264 
265 template <typename T>
266 using NEArithmeticAdditionQuantizedFixture = ArithmeticAdditionValidationQuantizedFixture<Tensor, Accessor, NEArithmeticAddition, T>;
267 
268 template <typename T>
269 using NEArithmeticAdditionQuantizedBroadcastFixture = ArithmeticAdditionValidationQuantizedBroadcastFixture<Tensor, Accessor, NEArithmeticAddition, T>;
270 
271 TEST_SUITE(Quantized)
272 TEST_SUITE(QASYMM8)
274  NEArithmeticAdditionQuantizedFixture<uint8_t>,
275  framework::DatasetMode::PRECOMMIT,
276  combine(combine(combine(combine(combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::QASYMM8)),
277  framework::dataset::make("ConvertPolicy", { ConvertPolicy::SATURATE })),
278  framework::dataset::make("Src0QInfo", { QuantizationInfo(5.f / 255.f, 20) })),
279  framework::dataset::make("Src1QInfo", { QuantizationInfo(2.f / 255.f, 10) })),
280  framework::dataset::make("OutQInfo", { QuantizationInfo(1.f / 255.f, 5) })),
281  OutOfPlaceDataSet))
282 {
283  // Validate output
284  validate(Accessor(_target), _reference, tolerance_quant);
285 }
286 TEST_SUITE_END() // QASYMM8
287 
289 FIXTURE_DATA_TEST_CASE(RunSmall,
293  framework::dataset::make("ConvertPolicy", { ConvertPolicy::SATURATE })),
294  framework::dataset::make("Src0QInfo", { QuantizationInfo(0.5f, 20) })),
295  framework::dataset::make("Src1QInfo", { QuantizationInfo(0.5f, 10) })),
296  framework::dataset::make("OutQInfo", { QuantizationInfo(0.5f, 5) })),
297  OutOfPlaceDataSet))
298 {
299  // Validate output
300  validate(Accessor(_target), _reference, tolerance_quant);
301 }
302 
304  datasets::SmallShapesBroadcast(), framework::dataset::make("DataType", DataType::QASYMM8_SIGNED)),
305  framework::dataset::make("ConvertPolicy", { ConvertPolicy::SATURATE })),
306  framework::dataset::make("Src0QInfo", { QuantizationInfo(0.5f, 20) })),
307  framework::dataset::make("Src1QInfo", { QuantizationInfo(0.5f, 10) })),
308  framework::dataset::make("OutQInfo", { QuantizationInfo(0.5f, 5) })),
309  OutOfPlaceDataSet))
310 {
311  // Validate output
312  validate(Accessor(_target), _reference, tolerance_quant);
313 }
314 TEST_SUITE_END() // QASYMM8_SIGNED
315 
317 FIXTURE_DATA_TEST_CASE(RunSmall,
321  framework::dataset::make("ConvertPolicy", { ConvertPolicy::SATURATE })),
322  framework::dataset::make("Src0QInfo", { QuantizationInfo(1.f / 32768.f, 0), QuantizationInfo(5.f / 32768.f, 0) })),
323  framework::dataset::make("Src1QInfo", { QuantizationInfo(2.f / 32768.f, 0), QuantizationInfo(5.f / 32768.f, 0) })),
324  framework::dataset::make("OutQInfo", { QuantizationInfo(5.f / 32768.f, 0) })),
325  OutOfPlaceDataSet))
326 {
327  // Validate output
328  validate(Accessor(_target), _reference, tolerance_quant);
329 }
330 TEST_SUITE_END() // QSYMM16
331 TEST_SUITE_END() // Quantized
332 
333 TEST_SUITE_END() // ArithmeticAddition
334 TEST_SUITE_END() // Neon
335 } // namespace validation
336 } // namespace test
337 } // 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:353
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