blob: b294ff6b6b682031386fba629e052ee04f8e9ee9 [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{
Georgios Pinitas3463a8b2018-08-23 13:11:53 +010050/** Define relative tolerance of the activation layer.
Moritz Pflanzer572ade72017-07-21 17:36:33 +010051 *
52 * @param[in] data_type The data type used.
53 * @param[in] activation The activation function used.
54 *
Georgios Pinitas3463a8b2018-08-23 13:11:53 +010055 * @return Relative tolerance depending on the activation function.
Moritz Pflanzer572ade72017-07-21 17:36:33 +010056 */
Georgios Pinitas3463a8b2018-08-23 13:11:53 +010057RelativeTolerance<float> relative_tolerance(DataType data_type, ActivationLayerInfo::ActivationFunction activation)
58{
59 switch(activation)
60 {
61 case ActivationLayerInfo::ActivationFunction::LOGISTIC:
62 case ActivationLayerInfo::ActivationFunction::SOFT_RELU:
Georgios Pinitasfb0fdcd2019-08-22 17:10:04 +010063 case ActivationLayerInfo::ActivationFunction::ELU:
Georgios Pinitas3463a8b2018-08-23 13:11:53 +010064 case ActivationLayerInfo::ActivationFunction::SQRT:
65 case ActivationLayerInfo::ActivationFunction::TANH:
SiCong Lia32e2ae2020-06-08 17:30:51 +010066 case ActivationLayerInfo::ActivationFunction::HARD_SWISH:
Georgios Pinitas3463a8b2018-08-23 13:11:53 +010067 switch(data_type)
68 {
69 case DataType::F16:
70 return RelativeTolerance<float>(0.1f);
71 default:
72 return RelativeTolerance<float>(0.05f);
73 }
Georgios Pinitas3463a8b2018-08-23 13:11:53 +010074 default:
75 return RelativeTolerance<float>(0.f);
76 }
77}
78
79/** Define absolute tolerance of the activation layer.
80 *
81 * @param[in] data_type The data type used.
82 * @param[in] activation The activation function used.
83 *
84 * @return Absolute tolerance depending on the activation function.
85 */
86AbsoluteTolerance<float> absolute_tolerance(DataType data_type, ActivationLayerInfo::ActivationFunction activation)
Moritz Pflanzer572ade72017-07-21 17:36:33 +010087{
88 switch(activation)
89 {
90 case ActivationLayerInfo::ActivationFunction::LOGISTIC:
91 case ActivationLayerInfo::ActivationFunction::SOFT_RELU:
92 case ActivationLayerInfo::ActivationFunction::SQRT:
93 case ActivationLayerInfo::ActivationFunction::TANH:
SiCong Lia32e2ae2020-06-08 17:30:51 +010094 case ActivationLayerInfo::ActivationFunction::HARD_SWISH:
Moritz Pflanzer572ade72017-07-21 17:36:33 +010095 switch(data_type)
96 {
Moritz Pflanzer572ade72017-07-21 17:36:33 +010097 case DataType::F16:
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010098 return AbsoluteTolerance<float>(0.01f);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010099 default:
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +0100100 return AbsoluteTolerance<float>(0.00001f);
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100101 }
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100102 default:
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +0100103 return AbsoluteTolerance<float>(0.f);
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100104 }
105}
106
Georgios Pinitas151fa872019-06-06 14:44:44 +0100107/** Tolerance for quantized asymmetric operations */
108#if defined(__aarch64__)
109constexpr AbsoluteTolerance<uint8_t> tolerance_qasymm8(0);
110#else // defined(__aarch64__)
111constexpr AbsoluteTolerance<uint8_t> tolerance_qasymm8(1);
112#endif // defined(__aarch64__)
113
giuros01c9573f32019-06-20 10:30:17 +0100114constexpr AbsoluteTolerance<int16_t> tolerance_qsymm16(1);
115
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100116/** CNN data types */
117const auto CNNDataTypes = framework::dataset::make("DataType",
118{
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000119#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100120 DataType::F16,
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000121#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100122 DataType::F32,
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100123});
124
morgolockaa85cdf2020-02-28 15:38:28 +0000125const auto NeonActivationFunctionsDataset = concat(datasets::ActivationFunctions(), framework::dataset::make("ActivationFunction", ActivationLayerInfo::ActivationFunction::HARD_SWISH));
morgolock07df3d42020-02-27 11:46:28 +0000126
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100127/** Input data sets. */
morgolock07df3d42020-02-27 11:46:28 +0000128const 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 +0100129
130template <typename T, REQUIRES_TA(arm_compute::utils::traits::is_floating_point<T>::value)>
131void test_float_sqrt_boundary_value()
132{
133 constexpr auto vector_size = uint32_t{ 16 };
134
135 auto data_type = DataType::F32;
136#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
137 data_type = std::is_same<T, half>::value ? DataType::F16 : data_type;
138#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
139
140 const auto boundary_value_vector = std::vector<T>
141 {
142 std::numeric_limits<T>::min(),
143 T(0),
144 std::numeric_limits<T>::epsilon(),
145 std::numeric_limits<T>::max(),
146 };
147
148 // the following size ensures that the whole logic (vector + left-over) to be tested
149 // using all boundary values iff boundary_value_vecotr.size() is smaller than vector_size.
150 auto shape = TensorShape{ vector_size + boundary_value_vector.size() };
151 auto info = ActivationLayerInfo{ ActivationLayerInfo::ActivationFunction::SQRT };
152 auto src = create_tensor<Tensor>(shape, data_type);
153
154 auto act = NEActivationLayer{};
155 act.configure(&src, nullptr, info);
156 src.allocator()->allocate();
157 library->fill_static_values(Accessor(src), boundary_value_vector);
158 act.run();
159
160 auto reference_src = SimpleTensor<T> { shape, data_type };
161 library->fill_static_values(reference_src, boundary_value_vector);
162 auto reference_dst = reference::activation_layer<T>(reference_src, info);
163
164 validate(Accessor(src), reference_dst);
165}
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100166} // namespace
167
168TEST_SUITE(NEON)
169TEST_SUITE(ActivationLayer)
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)
Sang-Hoon Parke7280582020-10-13 23:34:09 +0100201TEST_CASE(SqrtBoundaryValue, framework::DatasetMode::ALL)
202{
203 test_float_sqrt_boundary_value<half>();
204}
Sang-Hoon Parka8a7c1d2020-05-12 22:01:23 +0100205FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerFixture<half>, framework::DatasetMode::ALL, combine(combine(datasets::SmallShapes(), ActivationDataset),
206 framework::dataset::make("DataType",
207 DataType::F16)))
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100208{
209 // Validate output
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100210 validate(Accessor(_target), _reference, relative_tolerance(_data_type, _function), 0.f, absolute_tolerance(_data_type, _function));
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100211}
Sang-Hoon Parke7280582020-10-13 23:34:09 +0100212TEST_SUITE_END() // FP16
213#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100214
215TEST_SUITE(FP32)
Sang-Hoon Parke7280582020-10-13 23:34:09 +0100216TEST_CASE(SqrtBoundaryValue, framework::DatasetMode::ALL)
217{
218 test_float_sqrt_boundary_value<float>();
219}
Sang-Hoon Parka8a7c1d2020-05-12 22:01:23 +0100220FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerFixture<float>, framework::DatasetMode::ALL, combine(combine(datasets::SmallShapes(), ActivationDataset), framework::dataset::make("DataType",
221 DataType::F32)))
Pablo Tello7282d562018-06-14 15:35:49 +0100222
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100223{
224 // Validate output
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100225 validate(Accessor(_target), _reference, relative_tolerance(_data_type, _function), 0.f, absolute_tolerance(_data_type, _function));
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100226}
Michalis Spyrouaeebe4a2019-01-09 14:21:03 +0000227TEST_SUITE_END() // FP32
228TEST_SUITE_END() // Float
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100229
230template <typename T>
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000231using NEActivationLayerQuantizedFixture = ActivationValidationQuantizedFixture<Tensor, Accessor, NEActivationLayer, T>;
232
233/** Input data sets. */
Michele Di Giorgiodde3ad92018-01-23 16:55:24 +0000234const auto QuantizedActivationFunctionsDataset = framework::dataset::make("ActivationFunction", { ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU,
Isabella Gottardi03bb5502019-01-31 17:45:07 +0000235 ActivationLayerInfo::ActivationFunction::RELU,
236 ActivationLayerInfo::ActivationFunction::BOUNDED_RELU,
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100237 ActivationLayerInfo::ActivationFunction::LOGISTIC,
238 ActivationLayerInfo::ActivationFunction::TANH
Michele Di Giorgiodde3ad92018-01-23 16:55:24 +0000239 });
morgolockaa85cdf2020-02-28 15:38:28 +0000240
241const auto QuantizedActivationDataset = combine(combine(framework::dataset::make("InPlace", { false }),
242 concat(QuantizedActivationFunctionsDataset, framework::dataset::make("ActivationFunction", ActivationLayerInfo::ActivationFunction::HARD_SWISH))),
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000243 framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
244
245TEST_SUITE(Quantized)
246TEST_SUITE(QASYMM8)
Sang-Hoon Parka8a7c1d2020-05-12 22:01:23 +0100247FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerQuantizedFixture<uint8_t>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallShapes(), QuantizedActivationDataset),
248 framework::dataset::make("DataType",
249 DataType::QASYMM8)),
250 framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.1f, 128.0f) })))
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000251{
252 // Validate output
Georgios Pinitas151fa872019-06-06 14:44:44 +0100253 validate(Accessor(_target), _reference, tolerance_qasymm8);
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000254}
Michalis Spyrouaeebe4a2019-01-09 14:21:03 +0000255TEST_SUITE_END() // QASYMM8
giuros01c9573f32019-06-20 10:30:17 +0100256
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +0000257TEST_SUITE(QASYMM8_SIGNED)
Sang-Hoon Parka8a7c1d2020-05-12 22:01:23 +0100258FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerQuantizedFixture<int8_t>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallShapes(), QuantizedActivationDataset),
259 framework::dataset::make("DataType",
260 DataType::QASYMM8_SIGNED)),
261 framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.5f, 10.0f) })))
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +0000262{
263 // Validate output
264 validate(Accessor(_target), _reference, tolerance_qasymm8);
265}
266TEST_SUITE_END() // QASYMM8_SIGNED
267
giuros01c9573f32019-06-20 10:30:17 +0100268/** Input data sets. */
269const auto Int16QuantizedActivationFunctionsDataset = framework::dataset::make("ActivationFunction", { ActivationLayerInfo::ActivationFunction::LOGISTIC,
270 ActivationLayerInfo::ActivationFunction::TANH
271 });
272const auto Int16QuantizedActivationDataset = combine(combine(framework::dataset::make("InPlace", { false }), Int16QuantizedActivationFunctionsDataset),
273 framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
274
275TEST_SUITE(QSYMM16)
Sang-Hoon Parka8a7c1d2020-05-12 22:01:23 +0100276FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerQuantizedFixture<int16_t>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallShapes(), Int16QuantizedActivationDataset),
277 framework::dataset::make("DataType",
278 DataType::QSYMM16)),
279 framework::dataset::make("QuantizationInfo", { QuantizationInfo(1.f / 32768.f, 0.f) })))
giuros01c9573f32019-06-20 10:30:17 +0100280{
281 // Validate output
282 validate(Accessor(_target), _reference, tolerance_qsymm16);
283}
284TEST_SUITE_END() // QSYMM16
Michalis Spyrouaeebe4a2019-01-09 14:21:03 +0000285TEST_SUITE_END() // Quantized
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000286
Michalis Spyrouaeebe4a2019-01-09 14:21:03 +0000287TEST_SUITE_END() // ActivationLayer
288TEST_SUITE_END() // NEON
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100289} // namespace validation
290} // namespace test
291} // namespace arm_compute