blob: f9f7451ed783f75b0e197c228af7d47d75d7d8fa [file] [log] [blame]
Moritz Pflanzer572ade72017-07-21 17:36:33 +01001/*
morgolock07df3d42020-02-27 11:46:28 +00002 * Copyright (c) 2017-2020 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"
25#include "arm_compute/runtime/NEON/functions/NEActivationLayer.h"
Georgios Pinitas12833d02019-07-25 13:31:10 +010026#include "arm_compute/runtime/RuntimeContext.h"
Moritz Pflanzer572ade72017-07-21 17:36:33 +010027#include "arm_compute/runtime/Tensor.h"
28#include "arm_compute/runtime/TensorAllocator.h"
Moritz Pflanzer572ade72017-07-21 17:36:33 +010029#include "tests/NEON/Accessor.h"
30#include "tests/PaddingCalculator.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010031#include "tests/datasets/ActivationFunctionsDataset.h"
32#include "tests/datasets/ShapeDatasets.h"
33#include "tests/framework/Asserts.h"
34#include "tests/framework/Macros.h"
35#include "tests/framework/datasets/Datasets.h"
36#include "tests/validation/Validation.h"
37#include "tests/validation/fixtures/ActivationLayerFixture.h"
Moritz Pflanzer572ade72017-07-21 17:36:33 +010038
39namespace arm_compute
40{
41namespace test
42{
43namespace validation
44{
45namespace
46{
Georgios Pinitas3463a8b2018-08-23 13:11:53 +010047/** Define relative tolerance of the activation layer.
Moritz Pflanzer572ade72017-07-21 17:36:33 +010048 *
49 * @param[in] data_type The data type used.
50 * @param[in] activation The activation function used.
51 *
Georgios Pinitas3463a8b2018-08-23 13:11:53 +010052 * @return Relative tolerance depending on the activation function.
Moritz Pflanzer572ade72017-07-21 17:36:33 +010053 */
Georgios Pinitas3463a8b2018-08-23 13:11:53 +010054RelativeTolerance<float> relative_tolerance(DataType data_type, ActivationLayerInfo::ActivationFunction activation)
55{
56 switch(activation)
57 {
58 case ActivationLayerInfo::ActivationFunction::LOGISTIC:
59 case ActivationLayerInfo::ActivationFunction::SOFT_RELU:
Georgios Pinitasfb0fdcd2019-08-22 17:10:04 +010060 case ActivationLayerInfo::ActivationFunction::ELU:
Georgios Pinitas3463a8b2018-08-23 13:11:53 +010061 case ActivationLayerInfo::ActivationFunction::SQRT:
62 case ActivationLayerInfo::ActivationFunction::TANH:
63 switch(data_type)
64 {
65 case DataType::F16:
66 return RelativeTolerance<float>(0.1f);
67 default:
68 return RelativeTolerance<float>(0.05f);
69 }
Georgios Pinitas3463a8b2018-08-23 13:11:53 +010070 default:
71 return RelativeTolerance<float>(0.f);
72 }
73}
74
75/** Define absolute tolerance of the activation layer.
76 *
77 * @param[in] data_type The data type used.
78 * @param[in] activation The activation function used.
79 *
80 * @return Absolute tolerance depending on the activation function.
81 */
82AbsoluteTolerance<float> absolute_tolerance(DataType data_type, ActivationLayerInfo::ActivationFunction activation)
Moritz Pflanzer572ade72017-07-21 17:36:33 +010083{
84 switch(activation)
85 {
86 case ActivationLayerInfo::ActivationFunction::LOGISTIC:
87 case ActivationLayerInfo::ActivationFunction::SOFT_RELU:
88 case ActivationLayerInfo::ActivationFunction::SQRT:
89 case ActivationLayerInfo::ActivationFunction::TANH:
90 switch(data_type)
91 {
Moritz Pflanzer572ade72017-07-21 17:36:33 +010092 case DataType::F16:
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010093 return AbsoluteTolerance<float>(0.01f);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010094 default:
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010095 return AbsoluteTolerance<float>(0.00001f);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010096 }
Moritz Pflanzer572ade72017-07-21 17:36:33 +010097 default:
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010098 return AbsoluteTolerance<float>(0.f);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010099 }
100}
101
Georgios Pinitas151fa872019-06-06 14:44:44 +0100102/** Tolerance for quantized asymmetric operations */
103#if defined(__aarch64__)
104constexpr AbsoluteTolerance<uint8_t> tolerance_qasymm8(0);
105#else // defined(__aarch64__)
106constexpr AbsoluteTolerance<uint8_t> tolerance_qasymm8(1);
107#endif // defined(__aarch64__)
108
giuros01c9573f32019-06-20 10:30:17 +0100109constexpr AbsoluteTolerance<int16_t> tolerance_qsymm16(1);
110
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100111/** CNN data types */
112const auto CNNDataTypes = framework::dataset::make("DataType",
113{
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000114#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100115 DataType::F16,
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000116#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100117 DataType::F32,
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100118});
119
morgolockaa85cdf2020-02-28 15:38:28 +0000120const auto NeonActivationFunctionsDataset = concat(datasets::ActivationFunctions(), framework::dataset::make("ActivationFunction", ActivationLayerInfo::ActivationFunction::HARD_SWISH));
morgolock07df3d42020-02-27 11:46:28 +0000121
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100122/** Input data sets. */
morgolock07df3d42020-02-27 11:46:28 +0000123const auto ActivationDataset = combine(combine(framework::dataset::make("InPlace", { false, true }), NeonActivationFunctionsDataset), framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100124} // namespace
125
126TEST_SUITE(NEON)
127TEST_SUITE(ActivationLayer)
128
Michalis Spyrou5c9f0c42019-01-16 14:48:48 +0000129DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(combine(datasets::SmallShapes(), CNNDataTypes), framework::dataset::make("InPlace", { false, true })),
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100130 shape, data_type, in_place)
131{
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100132 // Create tensors
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100133 Tensor src = create_tensor<Tensor>(shape, data_type, 1);
134 Tensor dst = create_tensor<Tensor>(shape, data_type, 1);
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100135
136 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
137 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
138
Georgios Pinitas12833d02019-07-25 13:31:10 +0100139 // Create context
140 RuntimeContext ctx;
141
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100142 // Create and configure function
Georgios Pinitas12833d02019-07-25 13:31:10 +0100143 NEActivationLayer act_layer(&ctx);
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100144
145 if(in_place)
146 {
147 act_layer.configure(&src, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::ABS));
148 }
149 else
150 {
151 act_layer.configure(&src, &dst, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::ABS));
152 }
153
154 // Validate valid region
155 const ValidRegion valid_region = shape_to_valid_region(shape);
156 validate(src.info()->valid_region(), valid_region);
157
158 if(!in_place)
159 {
160 validate(dst.info()->valid_region(), valid_region);
161 }
162
163 // Validate padding
Georgios Pinitas5a594532018-12-03 14:30:05 +0000164 validate(src.info()->padding(), PaddingSize());
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100165 if(!in_place)
166 {
Georgios Pinitas5a594532018-12-03 14:30:05 +0000167 validate(dst.info()->padding(), PaddingSize());
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100168 }
169}
170
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000171// *INDENT-OFF*
172// clang-format off
173DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(
174 framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching data types
175 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
176 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching shapes
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000177 }),
178 framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F16),
179 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
180 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000181 })),
182 framework::dataset::make("ActivationInfo", { ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
183 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
184 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000185 })),
Vidhya Sudhan Loganathan0fc25452018-06-18 14:40:56 +0100186 framework::dataset::make("Expected", { false, true, false})),
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000187 input_info, output_info, act_info, expected)
188{
189 bool is_valid = bool(NEActivationLayer::validate(&input_info.clone()->set_is_resizable(false), &output_info.clone()->set_is_resizable(false), act_info));
190 ARM_COMPUTE_EXPECT(is_valid == expected, framework::LogLevel::ERRORS);
191}
192// clang-format on
193// *INDENT-ON*
194
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100195template <typename T>
196using NEActivationLayerFixture = ActivationValidationFixture<Tensor, Accessor, NEActivationLayer, T>;
197
198TEST_SUITE(Float)
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000199#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100200TEST_SUITE(FP16)
Georgios Pinitas583137c2017-08-31 18:12:42 +0100201FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ActivationDataset),
202 framework::dataset::make("DataType",
203 DataType::F16)))
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100204{
205 // Validate output
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100206 validate(Accessor(_target), _reference, relative_tolerance(_data_type, _function), 0.f, absolute_tolerance(_data_type, _function));
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100207}
Georgios Pinitas583137c2017-08-31 18:12:42 +0100208FIXTURE_DATA_TEST_CASE(RunLarge, NEActivationLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), ActivationDataset),
209 framework::dataset::make("DataType",
210 DataType::F16)))
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100211{
212 // Validate output
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100213 validate(Accessor(_target), _reference, relative_tolerance(_data_type, _function), 0.f, absolute_tolerance(_data_type, _function));
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100214}
215TEST_SUITE_END()
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000216#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100217
218TEST_SUITE(FP32)
219FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ActivationDataset), framework::dataset::make("DataType",
220 DataType::F32)))
Pablo Tello7282d562018-06-14 15:35:49 +0100221
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100222{
223 // Validate output
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100224 validate(Accessor(_target), _reference, relative_tolerance(_data_type, _function), 0.f, absolute_tolerance(_data_type, _function));
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100225}
Pablo Tello7282d562018-06-14 15:35:49 +0100226FIXTURE_DATA_TEST_CASE(RunLarge, NEActivationLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), ActivationDataset),
227 framework::dataset::make("DataType", DataType::F32)))
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100228{
229 // Validate output
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100230 validate(Accessor(_target), _reference, relative_tolerance(_data_type, _function), 0.f, absolute_tolerance(_data_type, _function));
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100231}
Michalis Spyrouaeebe4a2019-01-09 14:21:03 +0000232TEST_SUITE_END() // FP32
233TEST_SUITE_END() // Float
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100234
235template <typename T>
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000236using NEActivationLayerQuantizedFixture = ActivationValidationQuantizedFixture<Tensor, Accessor, NEActivationLayer, T>;
237
238/** Input data sets. */
Michele Di Giorgiodde3ad92018-01-23 16:55:24 +0000239const auto QuantizedActivationFunctionsDataset = framework::dataset::make("ActivationFunction", { ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU,
Isabella Gottardi03bb5502019-01-31 17:45:07 +0000240 ActivationLayerInfo::ActivationFunction::RELU,
241 ActivationLayerInfo::ActivationFunction::BOUNDED_RELU,
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100242 ActivationLayerInfo::ActivationFunction::LOGISTIC,
243 ActivationLayerInfo::ActivationFunction::TANH
Michele Di Giorgiodde3ad92018-01-23 16:55:24 +0000244 });
morgolockaa85cdf2020-02-28 15:38:28 +0000245const auto NeonActivationFunctionsDataset = concat(datasets::ActivationFunctions(), framework::dataset::make("ActivationFunction", ActivationLayerInfo::ActivationFunction::HARD_SWISH));
246
247const auto QuantizedActivationDataset = combine(combine(framework::dataset::make("InPlace", { false }),
248 concat(QuantizedActivationFunctionsDataset, framework::dataset::make("ActivationFunction", ActivationLayerInfo::ActivationFunction::HARD_SWISH))),
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000249 framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
250
251TEST_SUITE(Quantized)
252TEST_SUITE(QASYMM8)
253FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerQuantizedFixture<uint8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallShapes(), QuantizedActivationDataset),
254 framework::dataset::make("DataType",
255 DataType::QASYMM8)),
256 framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.1f, 128.0f) })))
257{
258 // Validate output
Georgios Pinitas151fa872019-06-06 14:44:44 +0100259 validate(Accessor(_target), _reference, tolerance_qasymm8);
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000260}
261FIXTURE_DATA_TEST_CASE(RunLarge, NEActivationLayerQuantizedFixture<uint8_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeShapes(), QuantizedActivationDataset),
262 framework::dataset::make("DataType",
263 DataType::QASYMM8)),
264 framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.1f, 128.0f) })))
265{
266 // Validate output
Georgios Pinitas151fa872019-06-06 14:44:44 +0100267 validate(Accessor(_target), _reference, tolerance_qasymm8);
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000268}
Michalis Spyrouaeebe4a2019-01-09 14:21:03 +0000269TEST_SUITE_END() // QASYMM8
giuros01c9573f32019-06-20 10:30:17 +0100270
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +0000271TEST_SUITE(QASYMM8_SIGNED)
272FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerQuantizedFixture<int8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallShapes(), QuantizedActivationDataset),
273 framework::dataset::make("DataType",
274 DataType::QASYMM8_SIGNED)),
275 framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, 10.0f) })))
276{
277 // Validate output
278 validate(Accessor(_target), _reference, tolerance_qasymm8);
279}
280FIXTURE_DATA_TEST_CASE(RunLarge, NEActivationLayerQuantizedFixture<int8_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeShapes(), QuantizedActivationDataset),
281 framework::dataset::make("DataType",
282 DataType::QASYMM8_SIGNED)),
283 framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, 10.0f) })))
284{
285 // Validate output
286 validate(Accessor(_target), _reference, tolerance_qasymm8);
287}
288TEST_SUITE_END() // QASYMM8_SIGNED
289
giuros01c9573f32019-06-20 10:30:17 +0100290/** Input data sets. */
291const auto Int16QuantizedActivationFunctionsDataset = framework::dataset::make("ActivationFunction", { ActivationLayerInfo::ActivationFunction::LOGISTIC,
292 ActivationLayerInfo::ActivationFunction::TANH
293 });
294const auto Int16QuantizedActivationDataset = combine(combine(framework::dataset::make("InPlace", { false }), Int16QuantizedActivationFunctionsDataset),
295 framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
296
297TEST_SUITE(QSYMM16)
298FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerQuantizedFixture<int16_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallShapes(), Int16QuantizedActivationDataset),
299 framework::dataset::make("DataType",
300 DataType::QSYMM16)),
301 framework::dataset::make("QuantizationInfo", { QuantizationInfo(1.f / 32768.f, 0.f) })))
302{
303 // Validate output
304 validate(Accessor(_target), _reference, tolerance_qsymm16);
305}
306FIXTURE_DATA_TEST_CASE(RunLarge, NEActivationLayerQuantizedFixture<int16_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeShapes(), Int16QuantizedActivationDataset),
307 framework::dataset::make("DataType",
308 DataType::QSYMM16)),
309 framework::dataset::make("QuantizationInfo", { QuantizationInfo(1.f / 32768.f, 0.f) })))
310{
311 // Validate output
312 validate(Accessor(_target), _reference, tolerance_qsymm16);
313}
314TEST_SUITE_END() // QSYMM16
Michalis Spyrouaeebe4a2019-01-09 14:21:03 +0000315TEST_SUITE_END() // Quantized
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000316
Michalis Spyrouaeebe4a2019-01-09 14:21:03 +0000317TEST_SUITE_END() // ActivationLayer
318TEST_SUITE_END() // NEON
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100319} // namespace validation
320} // namespace test
321} // namespace arm_compute