blob: 8a918b2597e3f202f0b91bda3b5c084ee83c7ed6 [file] [log] [blame]
Moritz Pflanzer572ade72017-07-21 17:36:33 +01001/*
2 * Copyright (c) 2017 ARM Limited.
3 *
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{
46/** Define tolerance of the activation layer.
47 *
48 * @param[in] data_type The data type used.
49 * @param[in] activation The activation function used.
50 *
51 * @return Tolerance depending on the activation function.
52 */
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010053AbsoluteTolerance<float> tolerance(DataType data_type, ActivationLayerInfo::ActivationFunction activation)
Moritz Pflanzer572ade72017-07-21 17:36:33 +010054{
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::QS8:
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010064 return AbsoluteTolerance<float>(5.f);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010065 case DataType::QS16:
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010066 return AbsoluteTolerance<float>(11.f);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010067 case DataType::F16:
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010068 return AbsoluteTolerance<float>(0.01f);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010069 default:
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010070 return AbsoluteTolerance<float>(0.00001f);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010071 }
72 break;
73 default:
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010074 return AbsoluteTolerance<float>(0.f);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010075 }
76}
77
78/** CNN data types */
79const auto CNNDataTypes = framework::dataset::make("DataType",
80{
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +000081#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Moritz Pflanzer572ade72017-07-21 17:36:33 +010082 DataType::F16,
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +000083#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Moritz Pflanzer572ade72017-07-21 17:36:33 +010084 DataType::F32,
85 DataType::QS8,
86 DataType::QS16,
87});
88
89/** Input data sets. */
90const auto ActivationDataset = combine(combine(framework::dataset::make("InPlace", { false, true }), datasets::ActivationFunctions()), framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
91} // namespace
92
93TEST_SUITE(NEON)
94TEST_SUITE(ActivationLayer)
95
96DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(combine(concat(datasets::SmallShapes(), datasets::LargeShapes()), CNNDataTypes), framework::dataset::make("InPlace", { false, true })),
97 shape, data_type, in_place)
98{
99 // Set fixed point position data type allowed
100 const int fixed_point_position = is_data_type_fixed_point(data_type) ? 3 : 0;
101
102 // Create tensors
103 Tensor src = create_tensor<Tensor>(shape, data_type, 1, fixed_point_position);
104 Tensor dst = create_tensor<Tensor>(shape, data_type, 1, fixed_point_position);
105
106 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
107 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
108
109 // Create and configure function
110 NEActivationLayer act_layer;
111
112 if(in_place)
113 {
114 act_layer.configure(&src, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::ABS));
115 }
116 else
117 {
118 act_layer.configure(&src, &dst, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::ABS));
119 }
120
121 // Validate valid region
122 const ValidRegion valid_region = shape_to_valid_region(shape);
123 validate(src.info()->valid_region(), valid_region);
124
125 if(!in_place)
126 {
127 validate(dst.info()->valid_region(), valid_region);
128 }
129
130 // Validate padding
131 const PaddingSize padding = PaddingCalculator(shape.x(), 16).required_padding();
132 validate(src.info()->padding(), padding);
133
134 if(!in_place)
135 {
136 validate(dst.info()->padding(), padding);
137 }
138}
139
Michalis Spyrouafa5d812017-11-30 14:25:57 +0000140// *INDENT-OFF*
141// clang-format off
142DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(
143 framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching data types
144 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
145 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching shapes
146 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QS8, 2), // Mismatching fixed point
147 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QS8, 2),
148 }),
149 framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F16),
150 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
151 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
152 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QS8, 3),
153 TensorInfo(),
154 })),
155 framework::dataset::make("ActivationInfo", { ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
156 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
157 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
158 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
159 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
160 })),
161 framework::dataset::make("Expected", { false, true, false, false, true })),
162 input_info, output_info, act_info, expected)
163{
164 bool is_valid = bool(NEActivationLayer::validate(&input_info.clone()->set_is_resizable(false), &output_info.clone()->set_is_resizable(false), act_info));
165 ARM_COMPUTE_EXPECT(is_valid == expected, framework::LogLevel::ERRORS);
166}
167// clang-format on
168// *INDENT-ON*
169
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100170template <typename T>
171using NEActivationLayerFixture = ActivationValidationFixture<Tensor, Accessor, NEActivationLayer, T>;
172
173TEST_SUITE(Float)
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000174#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100175TEST_SUITE(FP16)
Georgios Pinitas583137c2017-08-31 18:12:42 +0100176FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ActivationDataset),
177 framework::dataset::make("DataType",
178 DataType::F16)))
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100179{
180 // Validate output
181 validate(Accessor(_target), _reference, tolerance(_data_type, _function));
182}
Georgios Pinitas583137c2017-08-31 18:12:42 +0100183FIXTURE_DATA_TEST_CASE(RunLarge, NEActivationLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), ActivationDataset),
184 framework::dataset::make("DataType",
185 DataType::F16)))
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100186{
187 // Validate output
188 validate(Accessor(_target), _reference, tolerance(_data_type, _function));
189}
190TEST_SUITE_END()
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000191#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100192
193TEST_SUITE(FP32)
194FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ActivationDataset), framework::dataset::make("DataType",
195 DataType::F32)))
196{
197 // Validate output
198 validate(Accessor(_target), _reference, tolerance(_data_type, _function));
199}
200FIXTURE_DATA_TEST_CASE(RunLarge, NEActivationLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), ActivationDataset), framework::dataset::make("DataType",
201 DataType::F32)))
202{
203 // Validate output
204 validate(Accessor(_target), _reference, tolerance(_data_type, _function));
205}
206TEST_SUITE_END()
207TEST_SUITE_END()
208
209template <typename T>
210using NEActivationLayerFixedPointFixture = ActivationValidationFixedPointFixture<Tensor, Accessor, NEActivationLayer, T>;
211
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000212TEST_SUITE(FixedPoint)
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100213TEST_SUITE(QS8)
214// We test for fixed point precision [3,5] because [1,2] and [6,7] ranges cause
215// overflowing issues in most of the transcendentals functions.
216FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerFixedPointFixture<int8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallShapes(), ActivationDataset),
217 framework::dataset::make("DataType",
218 DataType::QS8)),
219 framework::dataset::make("FractionalBits", 3, 6)))
220{
221 // Validate output
222 validate(Accessor(_target), _reference, tolerance(_data_type, _function));
223}
224FIXTURE_DATA_TEST_CASE(RunLarge, NEActivationLayerFixedPointFixture<int8_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeShapes(), ActivationDataset),
225 framework::dataset::make("DataType",
226 DataType::QS8)),
227 framework::dataset::make("FractionalBits", 3, 6)))
228{
229 // Validate output
230 validate(Accessor(_target), _reference, tolerance(_data_type, _function));
231}
232TEST_SUITE_END()
233
234TEST_SUITE(QS16)
235// Testing for fixed point position [1,14) as reciprocal limits the maximum fixed point position to 14
236FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerFixedPointFixture<int16_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallShapes(), ActivationDataset),
237 framework::dataset::make("DataType",
238 DataType::QS16)),
239 framework::dataset::make("FractionalBits", 1, 14)))
240{
241 // Validate output
242 validate(Accessor(_target), _reference, tolerance(_data_type, _function));
243}
244FIXTURE_DATA_TEST_CASE(RunLarge, NEActivationLayerFixedPointFixture<int16_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeShapes(), ActivationDataset),
245 framework::dataset::make("DataType",
246 DataType::QS16)),
247 framework::dataset::make("FractionalBits", 1, 14)))
248{
249 // Validate output
250 validate(Accessor(_target), _reference, tolerance(_data_type, _function));
251}
252TEST_SUITE_END()
253TEST_SUITE_END()
254
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000255template <typename T>
256using NEActivationLayerQuantizedFixture = ActivationValidationQuantizedFixture<Tensor, Accessor, NEActivationLayer, T>;
257
258/** Input data sets. */
259const auto QuantizedActivationDataset = combine(combine(framework::dataset::make("InPlace", { false, true }), framework::dataset::make("ActivationFunction", { ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU })),
260 framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
261
262TEST_SUITE(Quantized)
263TEST_SUITE(QASYMM8)
264FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerQuantizedFixture<uint8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallShapes(), QuantizedActivationDataset),
265 framework::dataset::make("DataType",
266 DataType::QASYMM8)),
267 framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.1f, 128.0f) })))
268{
269 // Validate output
270 validate(Accessor(_target), _reference, tolerance(_data_type, _function));
271}
272FIXTURE_DATA_TEST_CASE(RunLarge, NEActivationLayerQuantizedFixture<uint8_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeShapes(), QuantizedActivationDataset),
273 framework::dataset::make("DataType",
274 DataType::QASYMM8)),
275 framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.1f, 128.0f) })))
276{
277 // Validate output
278 validate(Accessor(_target), _reference, tolerance(_data_type, _function));
279}
280TEST_SUITE_END()
281TEST_SUITE_END()
282
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100283TEST_SUITE_END()
284TEST_SUITE_END()
285} // namespace validation
286} // namespace test
287} // namespace arm_compute