blob: 5fd0855ded0bdffdca2148e1deacfa3607a1f631 [file] [log] [blame]
Moritz Pflanzer572ade72017-07-21 17:36:33 +01001/*
Michele Di Giorgioa1f7e332018-01-22 17:26:36 +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/CL/CLTensor.h"
26#include "arm_compute/runtime/CL/CLTensorAllocator.h"
27#include "arm_compute/runtime/CL/functions/CLActivationLayer.h"
Moritz Pflanzer572ade72017-07-21 17:36:33 +010028#include "tests/CL/CLAccessor.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] activation The activation function used.
49 * @param[in] data_type Data type.
50 *
51 * @return Tolerance depending on the activation function.
52 */
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010053AbsoluteTolerance<float> tolerance(ActivationLayerInfo::ActivationFunction activation, DataType data_type)
Moritz Pflanzer572ade72017-07-21 17:36:33 +010054{
Moritz Pflanzerf07f1452017-08-08 17:28:39 +010055 constexpr float epsilon = 1e-6f;
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010056
Moritz Pflanzer572ade72017-07-21 17:36:33 +010057 switch(activation)
58 {
59 case ActivationLayerInfo::ActivationFunction::LINEAR:
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010060 return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.2f : epsilon);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010061 case ActivationLayerInfo::ActivationFunction::SQUARE:
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010062 return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.1f : epsilon);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010063 case ActivationLayerInfo::ActivationFunction::LOGISTIC:
64 if(is_data_type_fixed_point(data_type))
65 {
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010066 return AbsoluteTolerance<float>(5.f);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010067 }
68 else
69 {
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010070 return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.001f : epsilon);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010071 }
72 case ActivationLayerInfo::ActivationFunction::LEAKY_RELU:
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010073 return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.00001f : epsilon);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010074 case ActivationLayerInfo::ActivationFunction::SOFT_RELU:
75 case ActivationLayerInfo::ActivationFunction::SQRT:
76 if(is_data_type_fixed_point(data_type))
77 {
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010078 return AbsoluteTolerance<float>(5.f);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010079 }
80 else
81 {
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010082 return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.01f : 0.00001f);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010083 }
84 case ActivationLayerInfo::ActivationFunction::TANH:
85 if(is_data_type_fixed_point(data_type))
86 {
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010087 return AbsoluteTolerance<float>(5.f);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010088 }
89 else
90 {
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010091 return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.001f : 0.00001f);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010092 }
93 default:
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010094 return AbsoluteTolerance<float>(epsilon);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010095 }
96}
97
98/** CNN data types */
99const auto CNNDataTypes = framework::dataset::make("DataType",
100{
101 DataType::F16,
102 DataType::F32,
103 DataType::QS8,
104 DataType::QS16,
105});
106
107/** Input data sets. */
108const auto ActivationDataset = combine(combine(framework::dataset::make("InPlace", { false, true }), datasets::ActivationFunctions()), framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
109} // namespace
110
111TEST_SUITE(CL)
112TEST_SUITE(ActivationLayer)
113
114DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(combine(concat(datasets::SmallShapes(), datasets::LargeShapes()), CNNDataTypes), framework::dataset::make("InPlace", { false, true })),
115 shape, data_type, in_place)
116{
117 // Set fixed point position data type allowed
118 const int fixed_point_position = is_data_type_fixed_point(data_type) ? 3 : 0;
119
120 // Create tensors
121 CLTensor src = create_tensor<CLTensor>(shape, data_type, 1, fixed_point_position);
122 CLTensor dst = create_tensor<CLTensor>(shape, data_type, 1, fixed_point_position);
123
124 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
125 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
126
127 // Create and configure function
128 CLActivationLayer act_layer;
129
130 if(in_place)
131 {
132 act_layer.configure(&src, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::ABS));
133 }
134 else
135 {
136 act_layer.configure(&src, &dst, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::ABS));
137 }
138
139 // Validate valid region
140 const ValidRegion valid_region = shape_to_valid_region(shape);
141 validate(src.info()->valid_region(), valid_region);
142
143 if(!in_place)
144 {
145 validate(dst.info()->valid_region(), valid_region);
146 }
147
148 // Validate padding
149 const int step = 16 / arm_compute::data_size_from_type(data_type);
150 const PaddingSize padding = PaddingCalculator(shape.x(), step).required_padding();
151 validate(src.info()->padding(), padding);
152
153 if(!in_place)
154 {
155 validate(dst.info()->padding(), padding);
156 }
157}
158
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +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
Giorgio Arena2995f5b2017-11-29 17:33:59 +0000163 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Window shrink
164 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
165 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QASYMM8),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000166 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QASYMM8), // Unsupported activation
167 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching shapes
168 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QS8, 2), // Mismatching fixed point
Giorgio Arena2995f5b2017-11-29 17:33:59 +0000169 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QS8, 2),
170 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QS8, 2),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000171 }),
172 framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F16),
173 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32),
Giorgio Arena2995f5b2017-11-29 17:33:59 +0000174 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
175 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QASYMM8),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000176 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QASYMM8),
177 TensorInfo(TensorShape(30U, 11U, 2U), 1, DataType::F32),
178 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QS8, 3),
Giorgio Arena2995f5b2017-11-29 17:33:59 +0000179 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QS8, 2),
180 TensorInfo(),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000181 })),
182 framework::dataset::make("ActivationInfo", { ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
183 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
Giorgio Arena2995f5b2017-11-29 17:33:59 +0000184 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000185 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU),
Michele Di Giorgioa1f7e332018-01-22 17:26:36 +0000186 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000187 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
188 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
189 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
Giorgio Arena2995f5b2017-11-29 17:33:59 +0000190 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000191 })),
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000192 framework::dataset::make("Expected", { false, false, true, true, false, false, false, true, true })),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000193 input_info, output_info, act_info, expected)
194{
Giorgio Arena2995f5b2017-11-29 17:33:59 +0000195 ARM_COMPUTE_EXPECT(bool(CLActivationLayer::validate(&input_info.clone()->set_is_resizable(false), (output_info.total_size() == 0) ? nullptr : &output_info.clone()->set_is_resizable(false), act_info)) == expected, framework::LogLevel::ERRORS);
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000196}
197// clang-format on
198// *INDENT-ON*
199
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100200template <typename T>
201using CLActivationLayerFixture = ActivationValidationFixture<CLTensor, CLAccessor, CLActivationLayer, T>;
202
203TEST_SUITE(Float)
204TEST_SUITE(FP16)
Georgios Pinitas583137c2017-08-31 18:12:42 +0100205FIXTURE_DATA_TEST_CASE(RunSmall, CLActivationLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ActivationDataset),
206 framework::dataset::make("DataType",
207 DataType::F16)))
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100208{
209 // Validate output
210 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
211}
Georgios Pinitas583137c2017-08-31 18:12:42 +0100212FIXTURE_DATA_TEST_CASE(RunLarge, CLActivationLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), ActivationDataset),
213 framework::dataset::make("DataType",
214 DataType::F16)))
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100215{
216 // Validate output
217 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
218}
219TEST_SUITE_END()
220
221TEST_SUITE(FP32)
222FIXTURE_DATA_TEST_CASE(RunSmall, CLActivationLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ActivationDataset), framework::dataset::make("DataType",
223 DataType::F32)))
224{
225 // Validate output
226 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
227}
228FIXTURE_DATA_TEST_CASE(RunLarge, CLActivationLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), ActivationDataset), framework::dataset::make("DataType",
229 DataType::F32)))
230{
231 // Validate output
232 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
233}
234TEST_SUITE_END()
235TEST_SUITE_END()
236
237template <typename T>
238using CLActivationLayerFixedPointFixture = ActivationValidationFixedPointFixture<CLTensor, CLAccessor, CLActivationLayer, T>;
239
Michel Iwaniec66cc12f2017-12-07 17:26:40 +0000240TEST_SUITE(FixedPoint)
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100241TEST_SUITE(QS8)
242// We test for fixed point precision [3,5] because [1,2] and [6,7] ranges cause
243// overflowing issues in most of the transcendentals functions.
Anthony Barbier1c0d0ff2018-01-31 13:05:09 +0000244FIXTURE_DATA_TEST_CASE(RunTiny, CLActivationLayerFixedPointFixture<int8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::TinyShapes(), ActivationDataset),
245 framework::dataset::make("DataType",
246 DataType::QS8)),
247 framework::dataset::make("FractionalBits", 3, 6)))
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100248{
249 // Validate output
250 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
251}
Anthony Barbier1c0d0ff2018-01-31 13:05:09 +0000252FIXTURE_DATA_TEST_CASE(RunSmall, CLActivationLayerFixedPointFixture<int8_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::SmallShapes(), ActivationDataset),
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100253 framework::dataset::make("DataType",
254 DataType::QS8)),
255 framework::dataset::make("FractionalBits", 3, 6)))
256{
257 // Validate output
258 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
259}
260TEST_SUITE_END()
261
262TEST_SUITE(QS16)
263// Testing for fixed point position [1,14) as reciprocal limits the maximum fixed point position to 14
Anthony Barbier1c0d0ff2018-01-31 13:05:09 +0000264FIXTURE_DATA_TEST_CASE(RunTiny, CLActivationLayerFixedPointFixture<int16_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::TinyShapes(), ActivationDataset),
265 framework::dataset::make("DataType",
266 DataType::QS16)),
267 framework::dataset::make("FractionalBits", 1, 14)))
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100268{
269 // Validate output
270 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
271}
Anthony Barbier1c0d0ff2018-01-31 13:05:09 +0000272FIXTURE_DATA_TEST_CASE(RunSmall, CLActivationLayerFixedPointFixture<int16_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::SmallShapes(), ActivationDataset),
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100273 framework::dataset::make("DataType",
274 DataType::QS16)),
275 framework::dataset::make("FractionalBits", 1, 14)))
276{
277 // Validate output
278 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
279}
280TEST_SUITE_END()
281TEST_SUITE_END()
282
Michel Iwaniec66cc12f2017-12-07 17:26:40 +0000283template <typename T>
284using CLActivationLayerQuantizedFixture = ActivationValidationQuantizedFixture<CLTensor, CLAccessor, CLActivationLayer, T>;
285
286/** Input data sets. */
Michele Di Giorgioa1f7e332018-01-22 17:26:36 +0000287const auto QuantizedActivationFunctionsDataset = framework::dataset::make("ActivationFunction", { ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU,
288 ActivationLayerInfo::ActivationFunction::RELU,
289 ActivationLayerInfo::ActivationFunction::BOUNDED_RELU
290 });
291
292const auto QuantizedActivationDataset = combine(combine(framework::dataset::make("InPlace", { false, true }), QuantizedActivationFunctionsDataset),
Michel Iwaniec66cc12f2017-12-07 17:26:40 +0000293 framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
294
295TEST_SUITE(Quantized)
296TEST_SUITE(QASYMM8)
297FIXTURE_DATA_TEST_CASE(RunSmall, CLActivationLayerQuantizedFixture<uint8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallShapes(), QuantizedActivationDataset),
298 framework::dataset::make("DataType",
299 DataType::QASYMM8)),
300 framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.1f, 128.0f) })))
301{
302 // Validate output
303 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
304}
305FIXTURE_DATA_TEST_CASE(RunLarge, CLActivationLayerQuantizedFixture<uint8_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeShapes(), QuantizedActivationDataset),
306 framework::dataset::make("DataType",
307 DataType::QASYMM8)),
308 framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.1f, 128.0f) })))
309{
310 // Validate output
311 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
312}
313TEST_SUITE_END()
314TEST_SUITE_END()
315
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100316TEST_SUITE_END()
317TEST_SUITE_END()
318} // namespace validation
319} // namespace test
320} // namespace arm_compute