blob: f58f3a8338cb7ce37bdb9cee181987a0da26c6bd [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{
81#ifdef ARM_COMPUTE_ENABLE_FP16
82 DataType::F16,
83#endif /* ARM_COMPUTE_ENABLE_FP16 */
84 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
140template <typename T>
141using NEActivationLayerFixture = ActivationValidationFixture<Tensor, Accessor, NEActivationLayer, T>;
142
143TEST_SUITE(Float)
144#ifdef ARM_COMPUTE_ENABLE_FP16
145TEST_SUITE(FP16)
Georgios Pinitas583137c2017-08-31 18:12:42 +0100146FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ActivationDataset),
147 framework::dataset::make("DataType",
148 DataType::F16)))
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100149{
150 // Validate output
151 validate(Accessor(_target), _reference, tolerance(_data_type, _function));
152}
Georgios Pinitas583137c2017-08-31 18:12:42 +0100153FIXTURE_DATA_TEST_CASE(RunLarge, NEActivationLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), ActivationDataset),
154 framework::dataset::make("DataType",
155 DataType::F16)))
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100156{
157 // Validate output
158 validate(Accessor(_target), _reference, tolerance(_data_type, _function));
159}
160TEST_SUITE_END()
161#endif /* ARM_COMPUTE_ENABLE_FP16 */
162
163TEST_SUITE(FP32)
164FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ActivationDataset), framework::dataset::make("DataType",
165 DataType::F32)))
166{
167 // Validate output
168 validate(Accessor(_target), _reference, tolerance(_data_type, _function));
169}
170FIXTURE_DATA_TEST_CASE(RunLarge, NEActivationLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), ActivationDataset), framework::dataset::make("DataType",
171 DataType::F32)))
172{
173 // Validate output
174 validate(Accessor(_target), _reference, tolerance(_data_type, _function));
175}
176TEST_SUITE_END()
177TEST_SUITE_END()
178
179template <typename T>
180using NEActivationLayerFixedPointFixture = ActivationValidationFixedPointFixture<Tensor, Accessor, NEActivationLayer, T>;
181
182TEST_SUITE(Quantized)
183TEST_SUITE(QS8)
184// We test for fixed point precision [3,5] because [1,2] and [6,7] ranges cause
185// overflowing issues in most of the transcendentals functions.
186FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerFixedPointFixture<int8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallShapes(), ActivationDataset),
187 framework::dataset::make("DataType",
188 DataType::QS8)),
189 framework::dataset::make("FractionalBits", 3, 6)))
190{
191 // Validate output
192 validate(Accessor(_target), _reference, tolerance(_data_type, _function));
193}
194FIXTURE_DATA_TEST_CASE(RunLarge, NEActivationLayerFixedPointFixture<int8_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeShapes(), ActivationDataset),
195 framework::dataset::make("DataType",
196 DataType::QS8)),
197 framework::dataset::make("FractionalBits", 3, 6)))
198{
199 // Validate output
200 validate(Accessor(_target), _reference, tolerance(_data_type, _function));
201}
202TEST_SUITE_END()
203
204TEST_SUITE(QS16)
205// Testing for fixed point position [1,14) as reciprocal limits the maximum fixed point position to 14
206FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerFixedPointFixture<int16_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallShapes(), ActivationDataset),
207 framework::dataset::make("DataType",
208 DataType::QS16)),
209 framework::dataset::make("FractionalBits", 1, 14)))
210{
211 // Validate output
212 validate(Accessor(_target), _reference, tolerance(_data_type, _function));
213}
214FIXTURE_DATA_TEST_CASE(RunLarge, NEActivationLayerFixedPointFixture<int16_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeShapes(), ActivationDataset),
215 framework::dataset::make("DataType",
216 DataType::QS16)),
217 framework::dataset::make("FractionalBits", 1, 14)))
218{
219 // Validate output
220 validate(Accessor(_target), _reference, tolerance(_data_type, _function));
221}
222TEST_SUITE_END()
223TEST_SUITE_END()
224
225TEST_SUITE_END()
226TEST_SUITE_END()
227} // namespace validation
228} // namespace test
229} // namespace arm_compute