Compute Library
 21.11
ArithmeticAddition.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2021 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 /** Input data sets **/
47 const auto EmptyActivationFunctionsDataset = framework::dataset::make("ActivationInfo",
48 { ActivationLayerInfo() });
49 const auto ActivationFunctionsDataset = framework::dataset::make("ActivationInfo",
50 {
51  ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 0.75f, 0.25f),
52  ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC, 0.75f, 0.25f)
53 });
54 const auto InPlaceDataSet = framework::dataset::make("InPlace", { false, true });
55 const auto OutOfPlaceDataSet = framework::dataset::make("InPlace", { false });
56 } // namespace
57 
58 TEST_SUITE(CL)
59 TEST_SUITE(ArithmeticAddition)
60 
61 // *INDENT-OFF*
62 // clang-format off
63 DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(
64  framework::dataset::make("Input1Info", { TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
65  TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8), // Invalid data type combination
66  TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), // Mismatching shapes
67  }),
68  framework::dataset::make("Input2Info",{ TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
69  TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::S16),
70  TensorInfo(TensorShape(48U, 11U, 2U), 1, DataType::F32),
71  })),
72  framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
73  TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::U8),
74  TensorInfo(TensorShape(48U, 11U, 2U), 1, DataType::F32),
75  })),
76  framework::dataset::make("Expected", { true, false, false})),
78 {
79  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);
80 }
81 // clang-format on
82 // *INDENT-ON*
83 
84 /** Validate fused activation expecting the following behaviours:
85  *
86  * - Fused activation with float data type should succeed
87  * - Fused activation with quantized data type should fail
88  *
89  */
91 {
92  auto input = TensorInfo{ TensorShape(2U, 2U), 1, DataType::F32 };
93  auto output = TensorInfo{ TensorShape(2U, 2U), 1, DataType::F32 };
94  Status result{};
95 
97 
98  // Fused-activation float type
99  result = CLArithmeticAddition::validate(&input, &input, &output, ConvertPolicy::WRAP, act_info);
100  ARM_COMPUTE_EXPECT(bool(result) == true, framework::LogLevel::ERRORS);
101 
102  // Fused-activation quantized type
103  input.set_data_type(DataType::QASYMM8);
104  output.set_data_type(DataType::QASYMM8);
105  result = CLArithmeticAddition::validate(&input, &input, &output, ConvertPolicy::WRAP, act_info);
106  ARM_COMPUTE_EXPECT(bool(result) == false, framework::LogLevel::ERRORS);
107 }
108 
109 template <typename T>
110 using CLArithmeticAdditionFixture = ArithmeticAdditionValidationFixture<CLTensor, CLAccessor, CLArithmeticAddition, T>;
111 
112 TEST_SUITE(Integer)
113 TEST_SUITE(U8)
114 FIXTURE_DATA_TEST_CASE(RunSmall, CLArithmeticAdditionFixture<uint8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType",
115  DataType::U8)),
116  framework::dataset::make("ConvertPolicy", { ConvertPolicy::SATURATE, ConvertPolicy::WRAP })),
117  OutOfPlaceDataSet))
118 {
119  // Validate output
120  validate(CLAccessor(_target), _reference);
121 }
122 TEST_SUITE_END() // U8
123 
125 FIXTURE_DATA_TEST_CASE(RunSmall, CLArithmeticAdditionFixture<int16_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType",
126  DataType::S16)),
127  framework::dataset::make("ConvertPolicy", { ConvertPolicy::SATURATE, ConvertPolicy::WRAP })),
128  OutOfPlaceDataSet))
129 {
130  // Validate output
131  validate(CLAccessor(_target), _reference);
132 }
133 
135  DataType::S16)),
137  OutOfPlaceDataSet))
138 {
139  // Validate output
140  validate(CLAccessor(_target), _reference);
141 }
142 TEST_SUITE_END() // S16
143 TEST_SUITE_END() // Integer
144 
145 template <typename T>
146 using CLArithmeticAdditionQuantizedFixture = ArithmeticAdditionValidationQuantizedFixture<CLTensor, CLAccessor, CLArithmeticAddition, T>;
147 
148 TEST_SUITE(Quantized)
150 FIXTURE_DATA_TEST_CASE(RunSmall, CLArithmeticAdditionQuantizedFixture<uint8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(combine(combine(combine(datasets::SmallShapes(),
151  framework::dataset::make("DataType", DataType::QASYMM8)),
152  framework::dataset::make("ConvertPolicy", { ConvertPolicy::SATURATE })),
153  framework::dataset::make("Src0QInfo", { QuantizationInfo(5.f / 255.f, 20) })),
154  framework::dataset::make("Src1QInfo", { QuantizationInfo(2.f / 255.f, 10) })),
155  framework::dataset::make("OutQInfo", { QuantizationInfo(1.f / 255.f, 5) })),
156  OutOfPlaceDataSet))
157 {
158  // Validate output
159  validate(CLAccessor(_target), _reference);
160 }
161 template <typename T>
162 using CLArithmeticAdditionBroadcastQuantizedFixture = ArithmeticAdditionValidationQuantizedBroadcastFixture<CLTensor, CLAccessor, CLArithmeticAddition, T>;
164  combine(combine(combine(combine(combine(combine(datasets::SmallShapesBroadcast(),
166  framework::dataset::make("ConvertPolicy", { ConvertPolicy::SATURATE })),
167  framework::dataset::make("Src0QInfo", { QuantizationInfo(5.f / 255.f, 20) })),
168  framework::dataset::make("Src1QInfo", { QuantizationInfo(2.f / 255.f, 10) })),
169  framework::dataset::make("OutQInfo", { QuantizationInfo(1.f / 255.f, 5) })),
170  OutOfPlaceDataSet))
171 {
172  // Validate output
173  validate(CLAccessor(_target), _reference);
174 }
176  combine(combine(combine(combine(combine(combine(datasets::TinyShapesBroadcastInplace(),
178  framework::dataset::make("ConvertPolicy", { ConvertPolicy::SATURATE })),
179  framework::dataset::make("Src0QInfo", { QuantizationInfo(1.f / 255.f, 10) })),
180  framework::dataset::make("Src1QInfo", { QuantizationInfo(1.f / 255.f, 10) })),
181  framework::dataset::make("OutQInfo", { QuantizationInfo(1.f / 255.f, 10) })),
182  InPlaceDataSet))
183 {
184  // Validate output
185  validate(CLAccessor(_target), _reference);
186 }
187 TEST_SUITE_END() // QASYMM8
189 FIXTURE_DATA_TEST_CASE(RunSmall, CLArithmeticAdditionQuantizedFixture<int8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(combine(combine(combine(datasets::SmallShapes(),
190  framework::dataset::make("DataType", DataType::QASYMM8_SIGNED)),
191  framework::dataset::make("ConvertPolicy", { ConvertPolicy::SATURATE })),
192  framework::dataset::make("Src0QInfo", { QuantizationInfo(5.f / 255.f, 10) })),
193  framework::dataset::make("Src1QInfo", { QuantizationInfo(2.f / 255.f, 10) })),
194  framework::dataset::make("OutQInfo", { QuantizationInfo(1.f / 255.f, 5) })),
195  OutOfPlaceDataSet))
196 {
197  // Validate output
198  validate(CLAccessor(_target), _reference);
199 }
200 TEST_SUITE_END() // QASYMM8_SIGNED
202 FIXTURE_DATA_TEST_CASE(RunSmall, CLArithmeticAdditionQuantizedFixture<int16_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(combine(combine(combine(datasets::SmallShapes(),
203  framework::dataset::make("DataType", DataType::QSYMM16)),
204  framework::dataset::make("ConvertPolicy", { ConvertPolicy::SATURATE })),
205  framework::dataset::make("Src0QInfo", { QuantizationInfo(1.f / 32768.f, 0), QuantizationInfo(5.f / 32768.f, 0) })),
206  framework::dataset::make("Src1QInfo", { QuantizationInfo(2.f / 32768.f, 0), QuantizationInfo(5.f / 32768.f, 0) })),
207  framework::dataset::make("OutQInfo", { QuantizationInfo(5.f / 32768.f, 0) })),
208  OutOfPlaceDataSet))
209 {
210  // Validate output
211  validate(CLAccessor(_target), _reference);
212 }
213 TEST_SUITE_END() // QSYMM16
214 TEST_SUITE_END() // Quantized
215 
216 template <typename T>
217 using CLArithmeticAdditionFloatFixture = ArithmeticAdditionValidationFloatFixture<CLTensor, CLAccessor, CLArithmeticAddition, T>;
218 
219 TEST_SUITE(Float)
220 TEST_SUITE(FP16)
221 FIXTURE_DATA_TEST_CASE(RunSmall, CLArithmeticAdditionFloatFixture<half>, framework::DatasetMode::ALL, combine(combine(combine(combine(datasets::SmallShapes(), framework::dataset::make("DataType",
222  DataType::F16)),
223  framework::dataset::make("ConvertPolicy", { ConvertPolicy::SATURATE, ConvertPolicy::WRAP })),
224  EmptyActivationFunctionsDataset),
225  OutOfPlaceDataSet))
226 {
227  // Validate output
228  validate(CLAccessor(_target), _reference);
229 }
230 FIXTURE_DATA_TEST_CASE(RunWithActivation, CLArithmeticAdditionFloatFixture<half>, framework::DatasetMode::ALL, combine(combine(combine(combine(datasets::TinyShapes(),
231  framework::dataset::make("DataType",
232  DataType::F16)),
234  ActivationFunctionsDataset),
235  OutOfPlaceDataSet))
236 {
237  // Validate output
238  validate(CLAccessor(_target), _reference);
239 }
240 TEST_SUITE_END() // FP16
241 
242 TEST_SUITE(FP32)
243 FIXTURE_DATA_TEST_CASE(RunSmall, CLArithmeticAdditionFloatFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(combine(datasets::SmallShapes(),
244  framework::dataset::make("DataType",
245  DataType::F32)),
246  framework::dataset::make("ConvertPolicy", { ConvertPolicy::SATURATE, ConvertPolicy::WRAP })),
247  EmptyActivationFunctionsDataset),
248  InPlaceDataSet))
249 {
250  // Validate output
251  validate(CLAccessor(_target), _reference);
252 }
254  framework::dataset::make("DataType",
255  DataType::F32)),
257  ActivationFunctionsDataset),
258  OutOfPlaceDataSet))
259 {
260  // Validate output
261  validate(CLAccessor(_target), _reference);
262 }
263 
265  DataType::F32)),
267  EmptyActivationFunctionsDataset),
268  OutOfPlaceDataSet))
269 {
270  // Validate output
271  validate(CLAccessor(_target), _reference);
272 }
273 
274 template <typename T>
275 using CLArithmeticAdditionBroadcastFloatFixture = ArithmeticAdditionBroadcastValidationFloatFixture<CLTensor, CLAccessor, CLArithmeticAddition, T>;
276 
280  EmptyActivationFunctionsDataset),
281  OutOfPlaceDataSet))
282 {
283  // Validate output
284  validate(CLAccessor(_target), _reference);
285 }
289  ActivationFunctionsDataset),
290  OutOfPlaceDataSet))
291 {
292  // Validate output
293  validate(CLAccessor(_target), _reference);
294 }
295 
299  EmptyActivationFunctionsDataset),
300  OutOfPlaceDataSet))
301 {
302  // Validate output
303  validate(CLAccessor(_target), _reference);
304 }
305 TEST_SUITE_END() // FP32
306 TEST_SUITE_END() // Float
307 
308 TEST_SUITE_END() // ArithmeticAddition
309 TEST_SUITE_END() // CL
310 } // namespace validation
311 } // namespace test
312 } // namespace arm_compute
Shape of a tensor.
Definition: TensorShape.h:39
quantized, symmetric fixed-point 16-bit number
1 channel, 1 U8 per channel
half_float::half half
16-bit floating point type
Definition: Types.h:48
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.
ArithmeticAdditionValidationQuantizedBroadcastFixture< CLTensor, CLAccessor, CLArithmeticAddition, T > CLArithmeticAdditionBroadcastQuantizedFixture
Status class.
Definition: Error.h:52
Activation Layer Information class.
Definition: Types.h:1509
Copyright (c) 2017-2021 Arm Limited.
1 channel, 1 F16 per channel
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)
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
Accessor implementation for CLTensor objects.
Definition: CLAccessor.h:36
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 opencl::kernels::ClSatur...
TEST_SUITE(U8_to_S8) FIXTURE_DATA_TEST_CASE(RunSmall
validate(CLAccessor(output_state), expected_output)
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
Store the tensor&#39;s metadata.
Definition: TensorInfo.h:43
TEST_CASE(FusedActivation, framework::DatasetMode::ALL)
Validate fused activation expecting the following behaviours:
quantized, asymmetric fixed-point 8-bit number signed
ArithmeticAdditionBroadcastValidationFloatFixture< CLTensor, CLAccessor, CLArithmeticAddition, T > CLArithmeticAdditionBroadcastFloatFixture
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:79
combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::F32)))
Definition: AbsLayer.cpp:65
ConvertPolicy
Policy to handle integer overflow.
Definition: Types.h:391
ArithmeticAdditionValidationFixture< CLTensor, CLAccessor, CLArithmeticAddition, T > CLArithmeticAdditionFixture