blob: 0ef4590d7ecb5f24e4a385a3c7f2b47ede078b2d [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/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);
51
52
Georgios Pinitas3463a8b2018-08-23 13:11:53 +010053/** Define relative tolerance of the activation layer.
Moritz Pflanzer572ade72017-07-21 17:36:33 +010054 *
55 * @param[in] data_type The data type used.
56 * @param[in] activation The activation function used.
57 *
Georgios Pinitas3463a8b2018-08-23 13:11:53 +010058 * @return Relative tolerance depending on the activation function.
Moritz Pflanzer572ade72017-07-21 17:36:33 +010059 */
Georgios Pinitas3463a8b2018-08-23 13:11:53 +010060RelativeTolerance<float> relative_tolerance(DataType data_type, ActivationLayerInfo::ActivationFunction activation)
61{
62 switch(activation)
63 {
64 case ActivationLayerInfo::ActivationFunction::LOGISTIC:
65 case ActivationLayerInfo::ActivationFunction::SOFT_RELU:
Georgios Pinitasfb0fdcd2019-08-22 17:10:04 +010066 case ActivationLayerInfo::ActivationFunction::ELU:
Georgios Pinitas3463a8b2018-08-23 13:11:53 +010067 case ActivationLayerInfo::ActivationFunction::SQRT:
68 case ActivationLayerInfo::ActivationFunction::TANH:
SiCong Lia32e2ae2020-06-08 17:30:51 +010069 case ActivationLayerInfo::ActivationFunction::HARD_SWISH:
Georgios Pinitas3463a8b2018-08-23 13:11:53 +010070 switch(data_type)
71 {
72 case DataType::F16:
73 return RelativeTolerance<float>(0.1f);
74 default:
75 return RelativeTolerance<float>(0.05f);
76 }
Georgios Pinitas3463a8b2018-08-23 13:11:53 +010077 default:
78 return RelativeTolerance<float>(0.f);
79 }
80}
81
82/** Define absolute tolerance of the activation layer.
83 *
84 * @param[in] data_type The data type used.
85 * @param[in] activation The activation function used.
86 *
87 * @return Absolute tolerance depending on the activation function.
88 */
89AbsoluteTolerance<float> absolute_tolerance(DataType data_type, ActivationLayerInfo::ActivationFunction activation)
Moritz Pflanzer572ade72017-07-21 17:36:33 +010090{
91 switch(activation)
92 {
93 case ActivationLayerInfo::ActivationFunction::LOGISTIC:
94 case ActivationLayerInfo::ActivationFunction::SOFT_RELU:
95 case ActivationLayerInfo::ActivationFunction::SQRT:
96 case ActivationLayerInfo::ActivationFunction::TANH:
SiCong Lia32e2ae2020-06-08 17:30:51 +010097 case ActivationLayerInfo::ActivationFunction::HARD_SWISH:
Moritz Pflanzer572ade72017-07-21 17:36:33 +010098 switch(data_type)
99 {
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100100 case DataType::F16:
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +0100101 return AbsoluteTolerance<float>(0.01f);
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100102 default:
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +0100103 return AbsoluteTolerance<float>(0.00001f);
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100104 }
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100105 default:
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +0100106 return AbsoluteTolerance<float>(0.f);
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100107 }
108}
109
Georgios Pinitas151fa872019-06-06 14:44:44 +0100110/** Tolerance for quantized asymmetric operations */
111#if defined(__aarch64__)
112constexpr AbsoluteTolerance<uint8_t> tolerance_qasymm8(0);
113#else // defined(__aarch64__)
114constexpr AbsoluteTolerance<uint8_t> tolerance_qasymm8(1);
115#endif // defined(__aarch64__)
116
giuros01c9573f32019-06-20 10:30:17 +0100117constexpr AbsoluteTolerance<int16_t> tolerance_qsymm16(1);
118
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100119/** CNN data types */
120const auto CNNDataTypes = framework::dataset::make("DataType",
121{
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000122#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100123 DataType::F16,
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000124#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100125 DataType::F32,
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100126});
127
morgolockaa85cdf2020-02-28 15:38:28 +0000128const auto NeonActivationFunctionsDataset = concat(datasets::ActivationFunctions(), framework::dataset::make("ActivationFunction", ActivationLayerInfo::ActivationFunction::HARD_SWISH));
morgolock07df3d42020-02-27 11:46:28 +0000129
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100130/** Input data sets. */
morgolock07df3d42020-02-27 11:46:28 +0000131const 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 +0100132
133template <typename T, REQUIRES_TA(arm_compute::utils::traits::is_floating_point<T>::value)>
134void test_float_sqrt_boundary_value()
135{
136 constexpr auto vector_size = uint32_t{ 16 };
137
138 auto data_type = DataType::F32;
139#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
140 data_type = std::is_same<T, half>::value ? DataType::F16 : data_type;
141#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
142
143 const auto boundary_value_vector = std::vector<T>
144 {
145 std::numeric_limits<T>::min(),
146 T(0),
147 std::numeric_limits<T>::epsilon(),
148 std::numeric_limits<T>::max(),
149 };
150
151 // the following size ensures that the whole logic (vector + left-over) to be tested
152 // using all boundary values iff boundary_value_vecotr.size() is smaller than vector_size.
153 auto shape = TensorShape{ vector_size + boundary_value_vector.size() };
154 auto info = ActivationLayerInfo{ ActivationLayerInfo::ActivationFunction::SQRT };
155 auto src = create_tensor<Tensor>(shape, data_type);
156
157 auto act = NEActivationLayer{};
158 act.configure(&src, nullptr, info);
159 src.allocator()->allocate();
160 library->fill_static_values(Accessor(src), boundary_value_vector);
161 act.run();
162
163 auto reference_src = SimpleTensor<T> { shape, data_type };
164 library->fill_static_values(reference_src, boundary_value_vector);
165 auto reference_dst = reference::activation_layer<T>(reference_src, info);
166
Manuel Bottini1d1bca72020-11-17 13:53:13 +0000167 validate(Accessor(src), reference_dst, tolerance_float_sqrt);
Sang-Hoon Parke7280582020-10-13 23:34:09 +0100168}
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100169} // namespace
170
171TEST_SUITE(NEON)
172TEST_SUITE(ActivationLayer)
173
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000174// *INDENT-OFF*
175// clang-format off
176DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(
177 framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching data types
178 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
179 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching shapes
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000180 }),
181 framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F16),
182 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
183 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000184 })),
185 framework::dataset::make("ActivationInfo", { ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
186 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
187 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000188 })),
Vidhya Sudhan Loganathan0fc25452018-06-18 14:40:56 +0100189 framework::dataset::make("Expected", { false, true, false})),
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000190 input_info, output_info, act_info, expected)
191{
192 bool is_valid = bool(NEActivationLayer::validate(&input_info.clone()->set_is_resizable(false), &output_info.clone()->set_is_resizable(false), act_info));
193 ARM_COMPUTE_EXPECT(is_valid == expected, framework::LogLevel::ERRORS);
194}
195// clang-format on
196// *INDENT-ON*
197
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100198template <typename T>
199using NEActivationLayerFixture = ActivationValidationFixture<Tensor, Accessor, NEActivationLayer, T>;
200
201TEST_SUITE(Float)
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000202#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100203TEST_SUITE(FP16)
Sang-Hoon Parke7280582020-10-13 23:34:09 +0100204TEST_CASE(SqrtBoundaryValue, framework::DatasetMode::ALL)
205{
206 test_float_sqrt_boundary_value<half>();
207}
Sang-Hoon Parka8a7c1d2020-05-12 22:01:23 +0100208FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerFixture<half>, framework::DatasetMode::ALL, combine(combine(datasets::SmallShapes(), 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}
Sang-Hoon Parke7280582020-10-13 23:34:09 +0100215TEST_SUITE_END() // FP16
216#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100217
218TEST_SUITE(FP32)
Sang-Hoon Parke7280582020-10-13 23:34:09 +0100219TEST_CASE(SqrtBoundaryValue, framework::DatasetMode::ALL)
220{
221 test_float_sqrt_boundary_value<float>();
222}
Sang-Hoon Parka8a7c1d2020-05-12 22:01:23 +0100223FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerFixture<float>, framework::DatasetMode::ALL, combine(combine(datasets::SmallShapes(), ActivationDataset), framework::dataset::make("DataType",
224 DataType::F32)))
Pablo Tello7282d562018-06-14 15:35:49 +0100225
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100226{
227 // Validate output
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100228 validate(Accessor(_target), _reference, relative_tolerance(_data_type, _function), 0.f, absolute_tolerance(_data_type, _function));
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100229}
Michalis Spyrouaeebe4a2019-01-09 14:21:03 +0000230TEST_SUITE_END() // FP32
231TEST_SUITE_END() // Float
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100232
233template <typename T>
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000234using NEActivationLayerQuantizedFixture = ActivationValidationQuantizedFixture<Tensor, Accessor, NEActivationLayer, T>;
235
236/** Input data sets. */
Michele Di Giorgiodde3ad92018-01-23 16:55:24 +0000237const auto QuantizedActivationFunctionsDataset = framework::dataset::make("ActivationFunction", { ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU,
Isabella Gottardi03bb5502019-01-31 17:45:07 +0000238 ActivationLayerInfo::ActivationFunction::RELU,
239 ActivationLayerInfo::ActivationFunction::BOUNDED_RELU,
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100240 ActivationLayerInfo::ActivationFunction::LOGISTIC,
241 ActivationLayerInfo::ActivationFunction::TANH
Michele Di Giorgiodde3ad92018-01-23 16:55:24 +0000242 });
morgolockaa85cdf2020-02-28 15:38:28 +0000243
244const auto QuantizedActivationDataset = combine(combine(framework::dataset::make("InPlace", { false }),
245 concat(QuantizedActivationFunctionsDataset, framework::dataset::make("ActivationFunction", ActivationLayerInfo::ActivationFunction::HARD_SWISH))),
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000246 framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
247
248TEST_SUITE(Quantized)
249TEST_SUITE(QASYMM8)
Sang-Hoon Parka8a7c1d2020-05-12 22:01:23 +0100250FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerQuantizedFixture<uint8_t>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallShapes(), QuantizedActivationDataset),
251 framework::dataset::make("DataType",
252 DataType::QASYMM8)),
253 framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.1f, 128.0f) })))
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000254{
255 // Validate output
Georgios Pinitas151fa872019-06-06 14:44:44 +0100256 validate(Accessor(_target), _reference, tolerance_qasymm8);
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000257}
Michalis Spyrouaeebe4a2019-01-09 14:21:03 +0000258TEST_SUITE_END() // QASYMM8
giuros01c9573f32019-06-20 10:30:17 +0100259
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +0000260TEST_SUITE(QASYMM8_SIGNED)
Sang-Hoon Parka8a7c1d2020-05-12 22:01:23 +0100261FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerQuantizedFixture<int8_t>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallShapes(), QuantizedActivationDataset),
262 framework::dataset::make("DataType",
263 DataType::QASYMM8_SIGNED)),
264 framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, 10.0f) })))
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +0000265{
266 // Validate output
267 validate(Accessor(_target), _reference, tolerance_qasymm8);
268}
269TEST_SUITE_END() // QASYMM8_SIGNED
270
giuros01c9573f32019-06-20 10:30:17 +0100271/** Input data sets. */
272const auto Int16QuantizedActivationFunctionsDataset = framework::dataset::make("ActivationFunction", { ActivationLayerInfo::ActivationFunction::LOGISTIC,
273 ActivationLayerInfo::ActivationFunction::TANH
274 });
275const auto Int16QuantizedActivationDataset = combine(combine(framework::dataset::make("InPlace", { false }), Int16QuantizedActivationFunctionsDataset),
276 framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
277
278TEST_SUITE(QSYMM16)
Sang-Hoon Parka8a7c1d2020-05-12 22:01:23 +0100279FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerQuantizedFixture<int16_t>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallShapes(), Int16QuantizedActivationDataset),
280 framework::dataset::make("DataType",
281 DataType::QSYMM16)),
282 framework::dataset::make("QuantizationInfo", { QuantizationInfo(1.f / 32768.f, 0.f) })))
giuros01c9573f32019-06-20 10:30:17 +0100283{
284 // Validate output
285 validate(Accessor(_target), _reference, tolerance_qsymm16);
286}
287TEST_SUITE_END() // QSYMM16
Michalis Spyrouaeebe4a2019-01-09 14:21:03 +0000288TEST_SUITE_END() // Quantized
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000289
Michalis Spyrouaeebe4a2019-01-09 14:21:03 +0000290TEST_SUITE_END() // ActivationLayer
291TEST_SUITE_END() // NEON
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100292} // namespace validation
293} // namespace test
294} // namespace arm_compute