blob: 1174a055c7cc01307708d88e28e2a4095d33c963 [file] [log] [blame]
Moritz Pflanzer572ade72017-07-21 17:36:33 +01001/*
Michalis Spyrouaeebe4a2019-01-09 14:21:03 +00002 * Copyright (c) 2017-2019 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"
26#include "arm_compute/runtime/Tensor.h"
27#include "arm_compute/runtime/TensorAllocator.h"
Moritz Pflanzer572ade72017-07-21 17:36:33 +010028#include "tests/NEON/Accessor.h"
29#include "tests/PaddingCalculator.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010030#include "tests/datasets/ActivationFunctionsDataset.h"
31#include "tests/datasets/ShapeDatasets.h"
32#include "tests/framework/Asserts.h"
33#include "tests/framework/Macros.h"
34#include "tests/framework/datasets/Datasets.h"
35#include "tests/validation/Validation.h"
36#include "tests/validation/fixtures/ActivationLayerFixture.h"
Moritz Pflanzer572ade72017-07-21 17:36:33 +010037
38namespace arm_compute
39{
40namespace test
41{
42namespace validation
43{
44namespace
45{
Georgios Pinitas3463a8b2018-08-23 13:11:53 +010046/** Define relative tolerance of the activation layer.
Moritz Pflanzer572ade72017-07-21 17:36:33 +010047 *
48 * @param[in] data_type The data type used.
49 * @param[in] activation The activation function used.
50 *
Georgios Pinitas3463a8b2018-08-23 13:11:53 +010051 * @return Relative tolerance depending on the activation function.
Moritz Pflanzer572ade72017-07-21 17:36:33 +010052 */
Georgios Pinitas3463a8b2018-08-23 13:11:53 +010053RelativeTolerance<float> relative_tolerance(DataType data_type, ActivationLayerInfo::ActivationFunction activation)
54{
55 switch(activation)
56 {
57 case ActivationLayerInfo::ActivationFunction::LOGISTIC:
58 case ActivationLayerInfo::ActivationFunction::SOFT_RELU:
59 case ActivationLayerInfo::ActivationFunction::SQRT:
60 case ActivationLayerInfo::ActivationFunction::TANH:
61 switch(data_type)
62 {
63 case DataType::F16:
64 return RelativeTolerance<float>(0.1f);
65 default:
66 return RelativeTolerance<float>(0.05f);
67 }
Georgios Pinitas3463a8b2018-08-23 13:11:53 +010068 default:
69 return RelativeTolerance<float>(0.f);
70 }
71}
72
73/** Define absolute tolerance of the activation layer.
74 *
75 * @param[in] data_type The data type used.
76 * @param[in] activation The activation function used.
77 *
78 * @return Absolute tolerance depending on the activation function.
79 */
80AbsoluteTolerance<float> absolute_tolerance(DataType data_type, ActivationLayerInfo::ActivationFunction activation)
Moritz Pflanzer572ade72017-07-21 17:36:33 +010081{
82 switch(activation)
83 {
84 case ActivationLayerInfo::ActivationFunction::LOGISTIC:
85 case ActivationLayerInfo::ActivationFunction::SOFT_RELU:
86 case ActivationLayerInfo::ActivationFunction::SQRT:
87 case ActivationLayerInfo::ActivationFunction::TANH:
88 switch(data_type)
89 {
Moritz Pflanzer572ade72017-07-21 17:36:33 +010090 case DataType::F16:
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010091 return AbsoluteTolerance<float>(0.01f);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010092 default:
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010093 return AbsoluteTolerance<float>(0.00001f);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010094 }
Moritz Pflanzer572ade72017-07-21 17:36:33 +010095 default:
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010096 return AbsoluteTolerance<float>(0.f);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010097 }
98}
99
Georgios Pinitas151fa872019-06-06 14:44:44 +0100100/** Tolerance for quantized asymmetric operations */
101#if defined(__aarch64__)
102constexpr AbsoluteTolerance<uint8_t> tolerance_qasymm8(0);
103#else // defined(__aarch64__)
104constexpr AbsoluteTolerance<uint8_t> tolerance_qasymm8(1);
105#endif // defined(__aarch64__)
106
giuros01c9573f32019-06-20 10:30:17 +0100107constexpr AbsoluteTolerance<int16_t> tolerance_qsymm16(1);
108
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100109/** CNN data types */
110const auto CNNDataTypes = framework::dataset::make("DataType",
111{
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000112#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100113 DataType::F16,
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000114#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100115 DataType::F32,
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100116});
117
118/** Input data sets. */
119const auto ActivationDataset = combine(combine(framework::dataset::make("InPlace", { false, true }), datasets::ActivationFunctions()), framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
120} // namespace
121
122TEST_SUITE(NEON)
123TEST_SUITE(ActivationLayer)
124
Michalis Spyrou5c9f0c42019-01-16 14:48:48 +0000125DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(combine(datasets::SmallShapes(), CNNDataTypes), framework::dataset::make("InPlace", { false, true })),
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100126 shape, data_type, in_place)
127{
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100128 // Create tensors
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100129 Tensor src = create_tensor<Tensor>(shape, data_type, 1);
130 Tensor dst = create_tensor<Tensor>(shape, data_type, 1);
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100131
132 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
133 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
134
135 // Create and configure function
136 NEActivationLayer act_layer;
137
138 if(in_place)
139 {
140 act_layer.configure(&src, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::ABS));
141 }
142 else
143 {
144 act_layer.configure(&src, &dst, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::ABS));
145 }
146
147 // Validate valid region
148 const ValidRegion valid_region = shape_to_valid_region(shape);
149 validate(src.info()->valid_region(), valid_region);
150
151 if(!in_place)
152 {
153 validate(dst.info()->valid_region(), valid_region);
154 }
155
156 // Validate padding
Georgios Pinitas5a594532018-12-03 14:30:05 +0000157 validate(src.info()->padding(), PaddingSize());
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100158 if(!in_place)
159 {
Georgios Pinitas5a594532018-12-03 14:30:05 +0000160 validate(dst.info()->padding(), PaddingSize());
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100161 }
162}
163
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000164// *INDENT-OFF*
165// clang-format off
166DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(
167 framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching data types
168 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
169 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching shapes
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000170 }),
171 framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F16),
172 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
173 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000174 })),
175 framework::dataset::make("ActivationInfo", { ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
176 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
177 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000178 })),
Vidhya Sudhan Loganathan0fc25452018-06-18 14:40:56 +0100179 framework::dataset::make("Expected", { false, true, false})),
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000180 input_info, output_info, act_info, expected)
181{
182 bool is_valid = bool(NEActivationLayer::validate(&input_info.clone()->set_is_resizable(false), &output_info.clone()->set_is_resizable(false), act_info));
183 ARM_COMPUTE_EXPECT(is_valid == expected, framework::LogLevel::ERRORS);
184}
185// clang-format on
186// *INDENT-ON*
187
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100188template <typename T>
189using NEActivationLayerFixture = ActivationValidationFixture<Tensor, Accessor, NEActivationLayer, T>;
190
191TEST_SUITE(Float)
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000192#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100193TEST_SUITE(FP16)
Georgios Pinitas583137c2017-08-31 18:12:42 +0100194FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ActivationDataset),
195 framework::dataset::make("DataType",
196 DataType::F16)))
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100197{
198 // Validate output
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100199 validate(Accessor(_target), _reference, relative_tolerance(_data_type, _function), 0.f, absolute_tolerance(_data_type, _function));
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100200}
Georgios Pinitas583137c2017-08-31 18:12:42 +0100201FIXTURE_DATA_TEST_CASE(RunLarge, NEActivationLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), 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}
208TEST_SUITE_END()
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000209#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100210
211TEST_SUITE(FP32)
212FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ActivationDataset), framework::dataset::make("DataType",
213 DataType::F32)))
Pablo Tello7282d562018-06-14 15:35:49 +0100214
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100215{
216 // Validate output
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100217 validate(Accessor(_target), _reference, relative_tolerance(_data_type, _function), 0.f, absolute_tolerance(_data_type, _function));
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100218}
Pablo Tello7282d562018-06-14 15:35:49 +0100219FIXTURE_DATA_TEST_CASE(RunLarge, NEActivationLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), ActivationDataset),
220 framework::dataset::make("DataType", DataType::F32)))
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100221{
222 // Validate output
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100223 validate(Accessor(_target), _reference, relative_tolerance(_data_type, _function), 0.f, absolute_tolerance(_data_type, _function));
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100224}
Michalis Spyrouaeebe4a2019-01-09 14:21:03 +0000225TEST_SUITE_END() // FP32
226TEST_SUITE_END() // Float
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100227
228template <typename T>
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000229using NEActivationLayerQuantizedFixture = ActivationValidationQuantizedFixture<Tensor, Accessor, NEActivationLayer, T>;
230
231/** Input data sets. */
Michele Di Giorgiodde3ad92018-01-23 16:55:24 +0000232const auto QuantizedActivationFunctionsDataset = framework::dataset::make("ActivationFunction", { ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU,
Isabella Gottardi03bb5502019-01-31 17:45:07 +0000233 ActivationLayerInfo::ActivationFunction::RELU,
234 ActivationLayerInfo::ActivationFunction::BOUNDED_RELU,
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100235 ActivationLayerInfo::ActivationFunction::LOGISTIC,
236 ActivationLayerInfo::ActivationFunction::TANH
Michele Di Giorgiodde3ad92018-01-23 16:55:24 +0000237 });
Georgios Pinitas151fa872019-06-06 14:44:44 +0100238const auto QuantizedActivationDataset = combine(combine(framework::dataset::make("InPlace", { false }), QuantizedActivationFunctionsDataset),
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000239 framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
240
241TEST_SUITE(Quantized)
242TEST_SUITE(QASYMM8)
243FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerQuantizedFixture<uint8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallShapes(), QuantizedActivationDataset),
244 framework::dataset::make("DataType",
245 DataType::QASYMM8)),
246 framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.1f, 128.0f) })))
247{
248 // Validate output
Georgios Pinitas151fa872019-06-06 14:44:44 +0100249 validate(Accessor(_target), _reference, tolerance_qasymm8);
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000250}
251FIXTURE_DATA_TEST_CASE(RunLarge, NEActivationLayerQuantizedFixture<uint8_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeShapes(), QuantizedActivationDataset),
252 framework::dataset::make("DataType",
253 DataType::QASYMM8)),
254 framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.1f, 128.0f) })))
255{
256 // Validate output
Georgios Pinitas151fa872019-06-06 14:44:44 +0100257 validate(Accessor(_target), _reference, tolerance_qasymm8);
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000258}
Michalis Spyrouaeebe4a2019-01-09 14:21:03 +0000259TEST_SUITE_END() // QASYMM8
giuros01c9573f32019-06-20 10:30:17 +0100260
261/** Input data sets. */
262const auto Int16QuantizedActivationFunctionsDataset = framework::dataset::make("ActivationFunction", { ActivationLayerInfo::ActivationFunction::LOGISTIC,
263 ActivationLayerInfo::ActivationFunction::TANH
264 });
265const auto Int16QuantizedActivationDataset = combine(combine(framework::dataset::make("InPlace", { false }), Int16QuantizedActivationFunctionsDataset),
266 framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
267
268TEST_SUITE(QSYMM16)
269FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerQuantizedFixture<int16_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallShapes(), Int16QuantizedActivationDataset),
270 framework::dataset::make("DataType",
271 DataType::QSYMM16)),
272 framework::dataset::make("QuantizationInfo", { QuantizationInfo(1.f / 32768.f, 0.f) })))
273{
274 // Validate output
275 validate(Accessor(_target), _reference, tolerance_qsymm16);
276}
277FIXTURE_DATA_TEST_CASE(RunLarge, NEActivationLayerQuantizedFixture<int16_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeShapes(), Int16QuantizedActivationDataset),
278 framework::dataset::make("DataType",
279 DataType::QSYMM16)),
280 framework::dataset::make("QuantizationInfo", { QuantizationInfo(1.f / 32768.f, 0.f) })))
281{
282 // Validate output
283 validate(Accessor(_target), _reference, tolerance_qsymm16);
284}
285TEST_SUITE_END() // QSYMM16
Michalis Spyrouaeebe4a2019-01-09 14:21:03 +0000286TEST_SUITE_END() // Quantized
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000287
Michalis Spyrouaeebe4a2019-01-09 14:21:03 +0000288TEST_SUITE_END() // ActivationLayer
289TEST_SUITE_END() // NEON
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100290} // namespace validation
291} // namespace test
292} // namespace arm_compute