blob: 577603d07d528d3e8ddfbaede29a6437d45d3fd0 [file] [log] [blame]
Moritz Pflanzer572ade72017-07-21 17:36:33 +01001/*
Giorgio Arenac5a61392021-01-06 15:13:08 +00002 * Copyright (c) 2017-2021 Arm Limited.
Moritz Pflanzer572ade72017-07-21 17:36:33 +01003 *
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"
Sang-Hoon Parke7280582020-10-13 23:34:09 +010025#include "arm_compute/core/utils/misc/Traits.h"
Moritz Pflanzer572ade72017-07-21 17:36:33 +010026#include "arm_compute/runtime/NEON/functions/NEActivationLayer.h"
Georgios Pinitas12833d02019-07-25 13:31:10 +010027#include "arm_compute/runtime/RuntimeContext.h"
Moritz Pflanzer572ade72017-07-21 17:36:33 +010028#include "arm_compute/runtime/Tensor.h"
29#include "arm_compute/runtime/TensorAllocator.h"
Moritz Pflanzer572ade72017-07-21 17:36:33 +010030#include "tests/NEON/Accessor.h"
31#include "tests/PaddingCalculator.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010032#include "tests/datasets/ActivationFunctionsDataset.h"
33#include "tests/datasets/ShapeDatasets.h"
34#include "tests/framework/Asserts.h"
35#include "tests/framework/Macros.h"
36#include "tests/framework/datasets/Datasets.h"
37#include "tests/validation/Validation.h"
38#include "tests/validation/fixtures/ActivationLayerFixture.h"
Moritz Pflanzer572ade72017-07-21 17:36:33 +010039
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010040#include "support/Requires.h"
41
Moritz Pflanzer572ade72017-07-21 17:36:33 +010042namespace arm_compute
43{
44namespace test
45{
46namespace validation
47{
48namespace
49{
Manuel Bottini1d1bca72020-11-17 13:53:13 +000050RelativeTolerance<float> tolerance_float_sqrt(0.0001f);
Sang-Hoon Parkadd8e812020-11-25 11:46:03 +000051
Georgios Pinitas3463a8b2018-08-23 13:11:53 +010052/** Define relative tolerance of the activation layer.
Moritz Pflanzer572ade72017-07-21 17:36:33 +010053 *
54 * @param[in] data_type The data type used.
55 * @param[in] activation The activation function used.
56 *
Georgios Pinitas3463a8b2018-08-23 13:11:53 +010057 * @return Relative tolerance depending on the activation function.
Moritz Pflanzer572ade72017-07-21 17:36:33 +010058 */
Georgios Pinitas3463a8b2018-08-23 13:11:53 +010059RelativeTolerance<float> relative_tolerance(DataType data_type, ActivationLayerInfo::ActivationFunction activation)
60{
61 switch(activation)
62 {
63 case ActivationLayerInfo::ActivationFunction::LOGISTIC:
Georgios Pinitasfb0fdcd2019-08-22 17:10:04 +010064 case ActivationLayerInfo::ActivationFunction::ELU:
Georgios Pinitas3463a8b2018-08-23 13:11:53 +010065 case ActivationLayerInfo::ActivationFunction::SQRT:
66 case ActivationLayerInfo::ActivationFunction::TANH:
SiCong Lia32e2ae2020-06-08 17:30:51 +010067 case ActivationLayerInfo::ActivationFunction::HARD_SWISH:
Georgios Pinitas3463a8b2018-08-23 13:11:53 +010068 switch(data_type)
69 {
70 case DataType::F16:
Michalis Spyrouaa51a5b2020-11-22 00:49:42 +000071#if defined(__ARM_FEATURE_SVE)
72 return RelativeTolerance<float>(0.25f);
73#else // !defined(__ARM_FEATURE_SVE)
Georgios Pinitas3463a8b2018-08-23 13:11:53 +010074 return RelativeTolerance<float>(0.1f);
Michalis Spyrouaa51a5b2020-11-22 00:49:42 +000075#endif // defined(__ARM_FEATURE_SVE)
Georgios Pinitas3463a8b2018-08-23 13:11:53 +010076 default:
77 return RelativeTolerance<float>(0.05f);
78 }
Michalis Spyrouaa51a5b2020-11-22 00:49:42 +000079 case ActivationLayerInfo::ActivationFunction::SOFT_RELU:
80 switch(data_type)
81 {
82 case DataType::F16:
83#if defined(__ARM_FEATURE_SVE)
84 return RelativeTolerance<float>(0.9f);
85#else // !defined(__ARM_FEATURE_SVE)
86 return RelativeTolerance<float>(0.01f);
87#endif // defined(__ARM_FEATURE_SVE)
88 default:
89 return RelativeTolerance<float>(0.00001f);
90 }
Georgios Pinitas3463a8b2018-08-23 13:11:53 +010091 default:
92 return RelativeTolerance<float>(0.f);
93 }
94}
95
96/** Define absolute tolerance of the activation layer.
97 *
98 * @param[in] data_type The data type used.
99 * @param[in] activation The activation function used.
100 *
101 * @return Absolute tolerance depending on the activation function.
102 */
103AbsoluteTolerance<float> absolute_tolerance(DataType data_type, ActivationLayerInfo::ActivationFunction activation)
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100104{
105 switch(activation)
106 {
107 case ActivationLayerInfo::ActivationFunction::LOGISTIC:
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100108 case ActivationLayerInfo::ActivationFunction::SQRT:
109 case ActivationLayerInfo::ActivationFunction::TANH:
SiCong Lia32e2ae2020-06-08 17:30:51 +0100110 case ActivationLayerInfo::ActivationFunction::HARD_SWISH:
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100111 switch(data_type)
112 {
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100113 case DataType::F16:
Michalis Spyrouaa51a5b2020-11-22 00:49:42 +0000114#if defined(__ARM_FEATURE_SVE)
115 return AbsoluteTolerance<float>(0.25f);
116#else // !defined(__ARM_FEATURE_SVE)
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +0100117 return AbsoluteTolerance<float>(0.01f);
Michalis Spyrouaa51a5b2020-11-22 00:49:42 +0000118#endif // defined(__ARM_FEATURE_SVE)
119 default:
120 return AbsoluteTolerance<float>(0.00001f);
121 }
122 case ActivationLayerInfo::ActivationFunction::SOFT_RELU:
123 switch(data_type)
124 {
125 case DataType::F16:
126#if defined(__ARM_FEATURE_SVE)
127 return AbsoluteTolerance<float>(0.9f);
128#else // !defined(__ARM_FEATURE_SVE)
129 return AbsoluteTolerance<float>(0.01f);
130#endif // defined(__ARM_FEATURE_SVE)
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100131 default:
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +0100132 return AbsoluteTolerance<float>(0.00001f);
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100133 }
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100134 default:
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +0100135 return AbsoluteTolerance<float>(0.f);
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100136 }
137}
138
Michele Di Giorgio7e5a8652021-01-12 13:49:07 +0000139/** Define absolute tolerance of the activation layer for qasymm8.
140 *
141 * @param[in] activation The activation function used.
142 *
143 * @return Absolute tolerance depending on the activation function.
144 */
145AbsoluteTolerance<uint8_t> tolerance_qasymm8(ActivationLayerInfo::ActivationFunction activation)
146{
147 switch(activation)
148 {
149 case ActivationLayerInfo::ActivationFunction::LOGISTIC:
150 case ActivationLayerInfo::ActivationFunction::SQRT:
151 case ActivationLayerInfo::ActivationFunction::TANH:
152 case ActivationLayerInfo::ActivationFunction::HARD_SWISH:
153 case ActivationLayerInfo::ActivationFunction::SOFT_RELU:
154 case ActivationLayerInfo::ActivationFunction::LEAKY_RELU:
155 return AbsoluteTolerance<uint8_t>(1);
156 default:
157 return AbsoluteTolerance<uint8_t>(0);
158 }
159}
Georgios Pinitas151fa872019-06-06 14:44:44 +0100160
giuros01c9573f32019-06-20 10:30:17 +0100161constexpr AbsoluteTolerance<int16_t> tolerance_qsymm16(1);
162
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100163/** CNN data types */
164const auto CNNDataTypes = framework::dataset::make("DataType",
165{
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000166#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100167 DataType::F16,
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000168#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100169 DataType::F32,
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100170});
171
morgolockaa85cdf2020-02-28 15:38:28 +0000172const auto NeonActivationFunctionsDataset = concat(datasets::ActivationFunctions(), framework::dataset::make("ActivationFunction", ActivationLayerInfo::ActivationFunction::HARD_SWISH));
morgolock07df3d42020-02-27 11:46:28 +0000173
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100174/** Input data sets. */
morgolock07df3d42020-02-27 11:46:28 +0000175const auto ActivationDataset = combine(combine(framework::dataset::make("InPlace", { false, true }), NeonActivationFunctionsDataset), framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
Sang-Hoon Parke7280582020-10-13 23:34:09 +0100176
Giorgio Arenac5a61392021-01-06 15:13:08 +0000177template <typename T, ARM_COMPUTE_REQUIRES_TA(arm_compute::utils::traits::is_floating_point<T>::value)>
Sang-Hoon Parke7280582020-10-13 23:34:09 +0100178void test_float_sqrt_boundary_value()
179{
180 constexpr auto vector_size = uint32_t{ 16 };
181
182 auto data_type = DataType::F32;
183#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
184 data_type = std::is_same<T, half>::value ? DataType::F16 : data_type;
185#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
186
187 const auto boundary_value_vector = std::vector<T>
188 {
189 std::numeric_limits<T>::min(),
190 T(0),
191 std::numeric_limits<T>::epsilon(),
192 std::numeric_limits<T>::max(),
193 };
194
195 // the following size ensures that the whole logic (vector + left-over) to be tested
196 // using all boundary values iff boundary_value_vecotr.size() is smaller than vector_size.
197 auto shape = TensorShape{ vector_size + boundary_value_vector.size() };
198 auto info = ActivationLayerInfo{ ActivationLayerInfo::ActivationFunction::SQRT };
199 auto src = create_tensor<Tensor>(shape, data_type);
200
201 auto act = NEActivationLayer{};
202 act.configure(&src, nullptr, info);
203 src.allocator()->allocate();
204 library->fill_static_values(Accessor(src), boundary_value_vector);
205 act.run();
206
207 auto reference_src = SimpleTensor<T> { shape, data_type };
208 library->fill_static_values(reference_src, boundary_value_vector);
209 auto reference_dst = reference::activation_layer<T>(reference_src, info);
210
Manuel Bottini1d1bca72020-11-17 13:53:13 +0000211 validate(Accessor(src), reference_dst, tolerance_float_sqrt);
Sang-Hoon Parke7280582020-10-13 23:34:09 +0100212}
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100213} // namespace
214
215TEST_SUITE(NEON)
216TEST_SUITE(ActivationLayer)
217
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000218// *INDENT-OFF*
219// clang-format off
220DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(
221 framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching data types
222 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
223 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching shapes
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000224 }),
225 framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F16),
226 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
227 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000228 })),
229 framework::dataset::make("ActivationInfo", { ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
230 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
231 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000232 })),
Vidhya Sudhan Loganathan0fc25452018-06-18 14:40:56 +0100233 framework::dataset::make("Expected", { false, true, false})),
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000234 input_info, output_info, act_info, expected)
235{
236 bool is_valid = bool(NEActivationLayer::validate(&input_info.clone()->set_is_resizable(false), &output_info.clone()->set_is_resizable(false), act_info));
237 ARM_COMPUTE_EXPECT(is_valid == expected, framework::LogLevel::ERRORS);
238}
239// clang-format on
240// *INDENT-ON*
241
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100242template <typename T>
243using NEActivationLayerFixture = ActivationValidationFixture<Tensor, Accessor, NEActivationLayer, T>;
244
245TEST_SUITE(Float)
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000246#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100247TEST_SUITE(FP16)
Sang-Hoon Parke7280582020-10-13 23:34:09 +0100248TEST_CASE(SqrtBoundaryValue, framework::DatasetMode::ALL)
249{
250 test_float_sqrt_boundary_value<half>();
251}
Sang-Hoon Parka8a7c1d2020-05-12 22:01:23 +0100252FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerFixture<half>, framework::DatasetMode::ALL, combine(combine(datasets::SmallShapes(), ActivationDataset),
253 framework::dataset::make("DataType",
254 DataType::F16)))
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100255{
256 // Validate output
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100257 validate(Accessor(_target), _reference, relative_tolerance(_data_type, _function), 0.f, absolute_tolerance(_data_type, _function));
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100258}
Sang-Hoon Parke7280582020-10-13 23:34:09 +0100259TEST_SUITE_END() // FP16
260#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100261
262TEST_SUITE(FP32)
Sang-Hoon Parke7280582020-10-13 23:34:09 +0100263TEST_CASE(SqrtBoundaryValue, framework::DatasetMode::ALL)
264{
265 test_float_sqrt_boundary_value<float>();
266}
Sang-Hoon Parka8a7c1d2020-05-12 22:01:23 +0100267FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerFixture<float>, framework::DatasetMode::ALL, combine(combine(datasets::SmallShapes(), ActivationDataset), framework::dataset::make("DataType",
268 DataType::F32)))
Pablo Tello7282d562018-06-14 15:35:49 +0100269
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100270{
271 // Validate output
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100272 validate(Accessor(_target), _reference, relative_tolerance(_data_type, _function), 0.f, absolute_tolerance(_data_type, _function));
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100273}
Michalis Spyrouaeebe4a2019-01-09 14:21:03 +0000274TEST_SUITE_END() // FP32
275TEST_SUITE_END() // Float
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100276
277template <typename T>
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000278using NEActivationLayerQuantizedFixture = ActivationValidationQuantizedFixture<Tensor, Accessor, NEActivationLayer, T>;
279
280/** Input data sets. */
Sang-Hoon Parkadd8e812020-11-25 11:46:03 +0000281const auto QuantizedActivationFunctionsDataset = framework::dataset::make("ActivationFunction",
282{
283 ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU,
284 ActivationLayerInfo::ActivationFunction::RELU,
285 ActivationLayerInfo::ActivationFunction::BOUNDED_RELU,
286 ActivationLayerInfo::ActivationFunction::LOGISTIC,
287 ActivationLayerInfo::ActivationFunction::TANH,
288 ActivationLayerInfo::ActivationFunction::LEAKY_RELU,
289});
morgolockaa85cdf2020-02-28 15:38:28 +0000290
291const auto QuantizedActivationDataset = combine(combine(framework::dataset::make("InPlace", { false }),
292 concat(QuantizedActivationFunctionsDataset, framework::dataset::make("ActivationFunction", ActivationLayerInfo::ActivationFunction::HARD_SWISH))),
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000293 framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
294
295TEST_SUITE(Quantized)
296TEST_SUITE(QASYMM8)
Sang-Hoon Parka8a7c1d2020-05-12 22:01:23 +0100297FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerQuantizedFixture<uint8_t>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallShapes(), QuantizedActivationDataset),
298 framework::dataset::make("DataType",
299 DataType::QASYMM8)),
300 framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.1f, 128.0f) })))
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000301{
302 // Validate output
Michele Di Giorgio7e5a8652021-01-12 13:49:07 +0000303 validate(Accessor(_target), _reference, tolerance_qasymm8(_function));
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000304}
Michalis Spyrouaeebe4a2019-01-09 14:21:03 +0000305TEST_SUITE_END() // QASYMM8
giuros01c9573f32019-06-20 10:30:17 +0100306
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +0000307TEST_SUITE(QASYMM8_SIGNED)
Sang-Hoon Parka8a7c1d2020-05-12 22:01:23 +0100308FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerQuantizedFixture<int8_t>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallShapes(), QuantizedActivationDataset),
309 framework::dataset::make("DataType",
310 DataType::QASYMM8_SIGNED)),
311 framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, 10.0f) })))
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +0000312{
313 // Validate output
Michele Di Giorgio7e5a8652021-01-12 13:49:07 +0000314 validate(Accessor(_target), _reference, tolerance_qasymm8(_function));
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +0000315}
316TEST_SUITE_END() // QASYMM8_SIGNED
317
giuros01c9573f32019-06-20 10:30:17 +0100318/** Input data sets. */
319const auto Int16QuantizedActivationFunctionsDataset = framework::dataset::make("ActivationFunction", { ActivationLayerInfo::ActivationFunction::LOGISTIC,
320 ActivationLayerInfo::ActivationFunction::TANH
321 });
322const auto Int16QuantizedActivationDataset = combine(combine(framework::dataset::make("InPlace", { false }), Int16QuantizedActivationFunctionsDataset),
323 framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
324
325TEST_SUITE(QSYMM16)
Sang-Hoon Parka8a7c1d2020-05-12 22:01:23 +0100326FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerQuantizedFixture<int16_t>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallShapes(), Int16QuantizedActivationDataset),
327 framework::dataset::make("DataType",
328 DataType::QSYMM16)),
329 framework::dataset::make("QuantizationInfo", { QuantizationInfo(1.f / 32768.f, 0.f) })))
giuros01c9573f32019-06-20 10:30:17 +0100330{
331 // Validate output
332 validate(Accessor(_target), _reference, tolerance_qsymm16);
333}
334TEST_SUITE_END() // QSYMM16
Michalis Spyrouaeebe4a2019-01-09 14:21:03 +0000335TEST_SUITE_END() // Quantized
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000336
Michalis Spyrouaeebe4a2019-01-09 14:21:03 +0000337TEST_SUITE_END() // ActivationLayer
Sheri Zhangac6499a2021-02-10 15:32:38 +0000338TEST_SUITE_END() // Neon
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100339} // namespace validation
340} // namespace test
341} // namespace arm_compute