blob: 5b16b0621c014c1865a8ea10ff82b388a389afb9 [file] [log] [blame]
Moritz Pflanzer572ade72017-07-21 17:36:33 +01001/*
Michele Di Giorgiodde3ad92018-01-23 16:55:24 +00002 * Copyright (c) 2017-2018 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 }
68 break;
69 default:
70 return RelativeTolerance<float>(0.f);
71 }
72}
73
74/** Define absolute tolerance of the activation layer.
75 *
76 * @param[in] data_type The data type used.
77 * @param[in] activation The activation function used.
78 *
79 * @return Absolute tolerance depending on the activation function.
80 */
81AbsoluteTolerance<float> absolute_tolerance(DataType data_type, ActivationLayerInfo::ActivationFunction activation)
Moritz Pflanzer572ade72017-07-21 17:36:33 +010082{
83 switch(activation)
84 {
85 case ActivationLayerInfo::ActivationFunction::LOGISTIC:
86 case ActivationLayerInfo::ActivationFunction::SOFT_RELU:
87 case ActivationLayerInfo::ActivationFunction::SQRT:
88 case ActivationLayerInfo::ActivationFunction::TANH:
89 switch(data_type)
90 {
Moritz Pflanzer572ade72017-07-21 17:36:33 +010091 case DataType::F16:
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010092 return AbsoluteTolerance<float>(0.01f);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010093 default:
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010094 return AbsoluteTolerance<float>(0.00001f);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010095 }
96 break;
97 default:
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010098 return AbsoluteTolerance<float>(0.f);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010099 }
100}
101
102/** CNN data types */
103const auto CNNDataTypes = framework::dataset::make("DataType",
104{
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000105#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100106 DataType::F16,
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000107#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100108 DataType::F32,
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100109});
110
111/** Input data sets. */
112const auto ActivationDataset = combine(combine(framework::dataset::make("InPlace", { false, true }), datasets::ActivationFunctions()), framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
113} // namespace
114
115TEST_SUITE(NEON)
116TEST_SUITE(ActivationLayer)
117
118DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(combine(concat(datasets::SmallShapes(), datasets::LargeShapes()), CNNDataTypes), framework::dataset::make("InPlace", { false, true })),
119 shape, data_type, in_place)
120{
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100121 // Create tensors
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +0100122 Tensor src = create_tensor<Tensor>(shape, data_type, 1);
123 Tensor dst = create_tensor<Tensor>(shape, data_type, 1);
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100124
125 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
126 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
127
128 // Create and configure function
129 NEActivationLayer act_layer;
130
131 if(in_place)
132 {
133 act_layer.configure(&src, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::ABS));
134 }
135 else
136 {
137 act_layer.configure(&src, &dst, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::ABS));
138 }
139
140 // Validate valid region
141 const ValidRegion valid_region = shape_to_valid_region(shape);
142 validate(src.info()->valid_region(), valid_region);
143
144 if(!in_place)
145 {
146 validate(dst.info()->valid_region(), valid_region);
147 }
148
149 // Validate padding
150 const PaddingSize padding = PaddingCalculator(shape.x(), 16).required_padding();
151 validate(src.info()->padding(), padding);
152
153 if(!in_place)
154 {
155 validate(dst.info()->padding(), padding);
156 }
157}
158
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000159// *INDENT-OFF*
160// clang-format off
161DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(
162 framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching data types
163 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
164 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching shapes
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000165 }),
166 framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F16),
167 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
168 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000169 })),
170 framework::dataset::make("ActivationInfo", { ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
171 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
172 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000173 })),
Vidhya Sudhan Loganathan0fc25452018-06-18 14:40:56 +0100174 framework::dataset::make("Expected", { false, true, false})),
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000175 input_info, output_info, act_info, expected)
176{
177 bool is_valid = bool(NEActivationLayer::validate(&input_info.clone()->set_is_resizable(false), &output_info.clone()->set_is_resizable(false), act_info));
178 ARM_COMPUTE_EXPECT(is_valid == expected, framework::LogLevel::ERRORS);
179}
180// clang-format on
181// *INDENT-ON*
182
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100183template <typename T>
184using NEActivationLayerFixture = ActivationValidationFixture<Tensor, Accessor, NEActivationLayer, T>;
185
186TEST_SUITE(Float)
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000187#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100188TEST_SUITE(FP16)
Georgios Pinitas583137c2017-08-31 18:12:42 +0100189FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ActivationDataset),
190 framework::dataset::make("DataType",
191 DataType::F16)))
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100192{
193 // Validate output
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100194 validate(Accessor(_target), _reference, relative_tolerance(_data_type, _function), 0.f, absolute_tolerance(_data_type, _function));
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100195}
Georgios Pinitas583137c2017-08-31 18:12:42 +0100196FIXTURE_DATA_TEST_CASE(RunLarge, NEActivationLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), ActivationDataset),
197 framework::dataset::make("DataType",
198 DataType::F16)))
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100199{
200 // Validate output
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100201 validate(Accessor(_target), _reference, relative_tolerance(_data_type, _function), 0.f, absolute_tolerance(_data_type, _function));
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100202}
203TEST_SUITE_END()
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000204#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100205
206TEST_SUITE(FP32)
207FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ActivationDataset), framework::dataset::make("DataType",
208 DataType::F32)))
Pablo Tello7282d562018-06-14 15:35:49 +0100209
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100210{
211 // Validate output
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100212 validate(Accessor(_target), _reference, relative_tolerance(_data_type, _function), 0.f, absolute_tolerance(_data_type, _function));
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100213}
Pablo Tello7282d562018-06-14 15:35:49 +0100214FIXTURE_DATA_TEST_CASE(RunLarge, NEActivationLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), ActivationDataset),
215 framework::dataset::make("DataType", DataType::F32)))
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100216{
217 // Validate output
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100218 validate(Accessor(_target), _reference, relative_tolerance(_data_type, _function), 0.f, absolute_tolerance(_data_type, _function));
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100219}
220TEST_SUITE_END()
221TEST_SUITE_END()
222
223template <typename T>
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000224using NEActivationLayerQuantizedFixture = ActivationValidationQuantizedFixture<Tensor, Accessor, NEActivationLayer, T>;
225
226/** Input data sets. */
Michele Di Giorgiodde3ad92018-01-23 16:55:24 +0000227const auto QuantizedActivationFunctionsDataset = framework::dataset::make("ActivationFunction", { ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU,
228 ActivationLayerInfo::ActivationFunction::RELU
229 });
230
231const auto QuantizedActivationDataset = combine(combine(framework::dataset::make("InPlace", { false, true }), QuantizedActivationFunctionsDataset),
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000232 framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
233
234TEST_SUITE(Quantized)
235TEST_SUITE(QASYMM8)
236FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerQuantizedFixture<uint8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallShapes(), QuantizedActivationDataset),
237 framework::dataset::make("DataType",
238 DataType::QASYMM8)),
239 framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.1f, 128.0f) })))
240{
241 // Validate output
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100242 validate(Accessor(_target), _reference, relative_tolerance(_data_type, _function), 0.f, absolute_tolerance(_data_type, _function));
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000243}
244FIXTURE_DATA_TEST_CASE(RunLarge, NEActivationLayerQuantizedFixture<uint8_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeShapes(), QuantizedActivationDataset),
245 framework::dataset::make("DataType",
246 DataType::QASYMM8)),
247 framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.1f, 128.0f) })))
248{
249 // Validate output
Georgios Pinitas3463a8b2018-08-23 13:11:53 +0100250 validate(Accessor(_target), _reference, relative_tolerance(_data_type, _function), 0.f, absolute_tolerance(_data_type, _function));
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000251}
252TEST_SUITE_END()
253TEST_SUITE_END()
254
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100255TEST_SUITE_END()
256TEST_SUITE_END()
257} // namespace validation
258} // namespace test
259} // namespace arm_compute