blob: 9b2cad1db258f51f0697de99e4efca4f5edbe444 [file] [log] [blame]
Moritz Pflanzer572ade72017-07-21 17:36:33 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * 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"
Sang-Hoon Parke7280582020-10-13 23:34:09 +010025#include "arm_compute/core/utils/misc/Requires.h"
26#include "arm_compute/core/utils/misc/Traits.h"
Moritz Pflanzer572ade72017-07-21 17:36:33 +010027#include "arm_compute/runtime/NEON/functions/NEActivationLayer.h"
Georgios Pinitas12833d02019-07-25 13:31:10 +010028#include "arm_compute/runtime/RuntimeContext.h"
Moritz Pflanzer572ade72017-07-21 17:36:33 +010029#include "arm_compute/runtime/Tensor.h"
30#include "arm_compute/runtime/TensorAllocator.h"
Moritz Pflanzer572ade72017-07-21 17:36:33 +010031#include "tests/NEON/Accessor.h"
32#include "tests/PaddingCalculator.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010033#include "tests/datasets/ActivationFunctionsDataset.h"
34#include "tests/datasets/ShapeDatasets.h"
35#include "tests/framework/Asserts.h"
36#include "tests/framework/Macros.h"
37#include "tests/framework/datasets/Datasets.h"
38#include "tests/validation/Validation.h"
39#include "tests/validation/fixtures/ActivationLayerFixture.h"
Moritz Pflanzer572ade72017-07-21 17:36:33 +010040
41namespace arm_compute
42{
43namespace test
44{
45namespace validation
46{
47namespace
48{
Georgios Pinitas3463a8b2018-08-23 13:11:53 +010049/** Define relative tolerance of the activation layer.
Moritz Pflanzer572ade72017-07-21 17:36:33 +010050 *
51 * @param[in] data_type The data type used.
52 * @param[in] activation The activation function used.
53 *
Georgios Pinitas3463a8b2018-08-23 13:11:53 +010054 * @return Relative tolerance depending on the activation function.
Moritz Pflanzer572ade72017-07-21 17:36:33 +010055 */
Georgios Pinitas3463a8b2018-08-23 13:11:53 +010056RelativeTolerance<float> relative_tolerance(DataType data_type, ActivationLayerInfo::ActivationFunction activation)
57{
58 switch(activation)
59 {
60 case ActivationLayerInfo::ActivationFunction::LOGISTIC:
61 case ActivationLayerInfo::ActivationFunction::SOFT_RELU:
Georgios Pinitasfb0fdcd2019-08-22 17:10:04 +010062 case ActivationLayerInfo::ActivationFunction::ELU:
Georgios Pinitas3463a8b2018-08-23 13:11:53 +010063 case ActivationLayerInfo::ActivationFunction::SQRT:
64 case ActivationLayerInfo::ActivationFunction::TANH:
SiCong Lia32e2ae2020-06-08 17:30:51 +010065 case ActivationLayerInfo::ActivationFunction::HARD_SWISH:
Georgios Pinitas3463a8b2018-08-23 13:11:53 +010066 switch(data_type)
67 {
68 case DataType::F16:
69 return RelativeTolerance<float>(0.1f);
70 default:
71 return RelativeTolerance<float>(0.05f);
72 }
Georgios Pinitas3463a8b2018-08-23 13:11:53 +010073 default:
74 return RelativeTolerance<float>(0.f);
75 }
76}
77
78/** Define absolute tolerance of the activation layer.
79 *
80 * @param[in] data_type The data type used.
81 * @param[in] activation The activation function used.
82 *
83 * @return Absolute tolerance depending on the activation function.
84 */
85AbsoluteTolerance<float> absolute_tolerance(DataType data_type, ActivationLayerInfo::ActivationFunction activation)
Moritz Pflanzer572ade72017-07-21 17:36:33 +010086{
87 switch(activation)
88 {
89 case ActivationLayerInfo::ActivationFunction::LOGISTIC:
90 case ActivationLayerInfo::ActivationFunction::SOFT_RELU:
91 case ActivationLayerInfo::ActivationFunction::SQRT:
92 case ActivationLayerInfo::ActivationFunction::TANH:
SiCong Lia32e2ae2020-06-08 17:30:51 +010093 case ActivationLayerInfo::ActivationFunction::HARD_SWISH:
Moritz Pflanzer572ade72017-07-21 17:36:33 +010094 switch(data_type)
95 {
Moritz Pflanzer572ade72017-07-21 17:36:33 +010096 case DataType::F16:
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010097 return AbsoluteTolerance<float>(0.01f);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010098 default:
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010099 return AbsoluteTolerance<float>(0.00001f);
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100100 }
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100101 default:
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +0100102 return AbsoluteTolerance<float>(0.f);
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100103 }
104}
105
Georgios Pinitas151fa872019-06-06 14:44:44 +0100106/** Tolerance for quantized asymmetric operations */
107#if defined(__aarch64__)
108constexpr AbsoluteTolerance<uint8_t> tolerance_qasymm8(0);
109#else // defined(__aarch64__)
110constexpr AbsoluteTolerance<uint8_t> tolerance_qasymm8(1);
111#endif // defined(__aarch64__)
112
giuros01c9573f32019-06-20 10:30:17 +0100113constexpr AbsoluteTolerance<int16_t> tolerance_qsymm16(1);
114
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100115/** CNN data types */
116const auto CNNDataTypes = framework::dataset::make("DataType",
117{
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000118#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100119 DataType::F16,
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000120#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100121 DataType::F32,
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100122});
123
morgolockaa85cdf2020-02-28 15:38:28 +0000124const auto NeonActivationFunctionsDataset = concat(datasets::ActivationFunctions(), framework::dataset::make("ActivationFunction", ActivationLayerInfo::ActivationFunction::HARD_SWISH));
morgolock07df3d42020-02-27 11:46:28 +0000125
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100126/** Input data sets. */
morgolock07df3d42020-02-27 11:46:28 +0000127const 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 +0100128
129template <typename T, REQUIRES_TA(arm_compute::utils::traits::is_floating_point<T>::value)>
130void test_float_sqrt_boundary_value()
131{
132 constexpr auto vector_size = uint32_t{ 16 };
133
134 auto data_type = DataType::F32;
135#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
136 data_type = std::is_same<T, half>::value ? DataType::F16 : data_type;
137#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
138
139 const auto boundary_value_vector = std::vector<T>
140 {
141 std::numeric_limits<T>::min(),
142 T(0),
143 std::numeric_limits<T>::epsilon(),
144 std::numeric_limits<T>::max(),
145 };
146
147 // the following size ensures that the whole logic (vector + left-over) to be tested
148 // using all boundary values iff boundary_value_vecotr.size() is smaller than vector_size.
149 auto shape = TensorShape{ vector_size + boundary_value_vector.size() };
150 auto info = ActivationLayerInfo{ ActivationLayerInfo::ActivationFunction::SQRT };
151 auto src = create_tensor<Tensor>(shape, data_type);
152
153 auto act = NEActivationLayer{};
154 act.configure(&src, nullptr, info);
155 src.allocator()->allocate();
156 library->fill_static_values(Accessor(src), boundary_value_vector);
157 act.run();
158
159 auto reference_src = SimpleTensor<T> { shape, data_type };
160 library->fill_static_values(reference_src, boundary_value_vector);
161 auto reference_dst = reference::activation_layer<T>(reference_src, info);
162
163 validate(Accessor(src), reference_dst);
164}
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100165} // namespace
166
167TEST_SUITE(NEON)
168TEST_SUITE(ActivationLayer)
169
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000170// *INDENT-OFF*
171// clang-format off
172DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(
173 framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching data types
174 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
175 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching shapes
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000176 }),
177 framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F16),
178 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
179 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000180 })),
181 framework::dataset::make("ActivationInfo", { ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
182 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
183 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000184 })),
Vidhya Sudhan Loganathan0fc25452018-06-18 14:40:56 +0100185 framework::dataset::make("Expected", { false, true, false})),
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000186 input_info, output_info, act_info, expected)
187{
188 bool is_valid = bool(NEActivationLayer::validate(&input_info.clone()->set_is_resizable(false), &output_info.clone()->set_is_resizable(false), act_info));
189 ARM_COMPUTE_EXPECT(is_valid == expected, framework::LogLevel::ERRORS);
190}
191// clang-format on
192// *INDENT-ON*
193
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100194template <typename T>
195using NEActivationLayerFixture = ActivationValidationFixture<Tensor, Accessor, NEActivationLayer, T>;
196
197TEST_SUITE(Float)
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000198#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100199TEST_SUITE(FP16)
Sang-Hoon Parke7280582020-10-13 23:34:09 +0100200TEST_CASE(SqrtBoundaryValue, framework::DatasetMode::ALL)
201{
202 test_float_sqrt_boundary_value<half>();
203}
Sang-Hoon Parka8a7c1d2020-05-12 22:01:23 +0100204FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerFixture<half>, framework::DatasetMode::ALL, combine(combine(datasets::SmallShapes(), ActivationDataset),
205 framework::dataset::make("DataType",
206 DataType::F16)))
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100207{
208 // Validate output
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100209 validate(Accessor(_target), _reference, relative_tolerance(_data_type, _function), 0.f, absolute_tolerance(_data_type, _function));
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100210}
Sang-Hoon Parke7280582020-10-13 23:34:09 +0100211TEST_SUITE_END() // FP16
212#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100213
214TEST_SUITE(FP32)
Sang-Hoon Parke7280582020-10-13 23:34:09 +0100215TEST_CASE(SqrtBoundaryValue, framework::DatasetMode::ALL)
216{
217 test_float_sqrt_boundary_value<float>();
218}
Sang-Hoon Parka8a7c1d2020-05-12 22:01:23 +0100219FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerFixture<float>, framework::DatasetMode::ALL, 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}
Michalis Spyrouaeebe4a2019-01-09 14:21:03 +0000226TEST_SUITE_END() // FP32
227TEST_SUITE_END() // Float
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100228
229template <typename T>
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000230using NEActivationLayerQuantizedFixture = ActivationValidationQuantizedFixture<Tensor, Accessor, NEActivationLayer, T>;
231
232/** Input data sets. */
Michele Di Giorgiodde3ad92018-01-23 16:55:24 +0000233const auto QuantizedActivationFunctionsDataset = framework::dataset::make("ActivationFunction", { ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU,
Isabella Gottardi03bb5502019-01-31 17:45:07 +0000234 ActivationLayerInfo::ActivationFunction::RELU,
235 ActivationLayerInfo::ActivationFunction::BOUNDED_RELU,
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100236 ActivationLayerInfo::ActivationFunction::LOGISTIC,
237 ActivationLayerInfo::ActivationFunction::TANH
Michele Di Giorgiodde3ad92018-01-23 16:55:24 +0000238 });
morgolockaa85cdf2020-02-28 15:38:28 +0000239
240const auto QuantizedActivationDataset = combine(combine(framework::dataset::make("InPlace", { false }),
241 concat(QuantizedActivationFunctionsDataset, framework::dataset::make("ActivationFunction", ActivationLayerInfo::ActivationFunction::HARD_SWISH))),
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000242 framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
243
244TEST_SUITE(Quantized)
245TEST_SUITE(QASYMM8)
Sang-Hoon Parka8a7c1d2020-05-12 22:01:23 +0100246FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerQuantizedFixture<uint8_t>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallShapes(), QuantizedActivationDataset),
247 framework::dataset::make("DataType",
248 DataType::QASYMM8)),
249 framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.1f, 128.0f) })))
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000250{
251 // Validate output
Georgios Pinitas151fa872019-06-06 14:44:44 +0100252 validate(Accessor(_target), _reference, tolerance_qasymm8);
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000253}
Michalis Spyrouaeebe4a2019-01-09 14:21:03 +0000254TEST_SUITE_END() // QASYMM8
giuros01c9573f32019-06-20 10:30:17 +0100255
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +0000256TEST_SUITE(QASYMM8_SIGNED)
Sang-Hoon Parka8a7c1d2020-05-12 22:01:23 +0100257FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerQuantizedFixture<int8_t>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallShapes(), QuantizedActivationDataset),
258 framework::dataset::make("DataType",
259 DataType::QASYMM8_SIGNED)),
260 framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, 10.0f) })))
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +0000261{
262 // Validate output
263 validate(Accessor(_target), _reference, tolerance_qasymm8);
264}
265TEST_SUITE_END() // QASYMM8_SIGNED
266
giuros01c9573f32019-06-20 10:30:17 +0100267/** Input data sets. */
268const auto Int16QuantizedActivationFunctionsDataset = framework::dataset::make("ActivationFunction", { ActivationLayerInfo::ActivationFunction::LOGISTIC,
269 ActivationLayerInfo::ActivationFunction::TANH
270 });
271const auto Int16QuantizedActivationDataset = combine(combine(framework::dataset::make("InPlace", { false }), Int16QuantizedActivationFunctionsDataset),
272 framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
273
274TEST_SUITE(QSYMM16)
Sang-Hoon Parka8a7c1d2020-05-12 22:01:23 +0100275FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerQuantizedFixture<int16_t>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallShapes(), Int16QuantizedActivationDataset),
276 framework::dataset::make("DataType",
277 DataType::QSYMM16)),
278 framework::dataset::make("QuantizationInfo", { QuantizationInfo(1.f / 32768.f, 0.f) })))
giuros01c9573f32019-06-20 10:30:17 +0100279{
280 // Validate output
281 validate(Accessor(_target), _reference, tolerance_qsymm16);
282}
283TEST_SUITE_END() // QSYMM16
Michalis Spyrouaeebe4a2019-01-09 14:21:03 +0000284TEST_SUITE_END() // Quantized
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000285
Michalis Spyrouaeebe4a2019-01-09 14:21:03 +0000286TEST_SUITE_END() // ActivationLayer
287TEST_SUITE_END() // NEON
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100288} // namespace validation
289} // namespace test
290} // namespace arm_compute