blob: bc2fe603fd6b23e97b0a9cb970e9ab7a34d278dd [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"
28#include "framework/Asserts.h"
29#include "framework/Macros.h"
30#include "framework/datasets/Datasets.h"
31#include "tests/NEON/Accessor.h"
32#include "tests/PaddingCalculator.h"
33#include "tests/datasets_new/ActivationFunctionsDataset.h"
34#include "tests/datasets_new/ShapeDatasets.h"
35#include "tests/validation_new/Validation.h"
36#include "tests/validation_new/fixtures/ActivationLayerFixture.h"
37#include "tests/validation_new/half.h"
38
39namespace arm_compute
40{
41namespace test
42{
43namespace validation
44{
45namespace
46{
47/** Define tolerance of the activation layer.
48 *
49 * @param[in] data_type The data type used.
50 * @param[in] activation The activation function used.
51 *
52 * @return Tolerance depending on the activation function.
53 */
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010054AbsoluteTolerance<float> tolerance(DataType data_type, ActivationLayerInfo::ActivationFunction activation)
Moritz Pflanzer572ade72017-07-21 17:36:33 +010055{
56 switch(activation)
57 {
58 case ActivationLayerInfo::ActivationFunction::LOGISTIC:
59 case ActivationLayerInfo::ActivationFunction::SOFT_RELU:
60 case ActivationLayerInfo::ActivationFunction::SQRT:
61 case ActivationLayerInfo::ActivationFunction::TANH:
62 switch(data_type)
63 {
64 case DataType::QS8:
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010065 return AbsoluteTolerance<float>(5.f);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010066 case DataType::QS16:
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010067 return AbsoluteTolerance<float>(11.f);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010068 case DataType::F16:
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010069 return AbsoluteTolerance<float>(0.01f);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010070 default:
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010071 return AbsoluteTolerance<float>(0.00001f);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010072 }
73 break;
74 default:
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010075 return AbsoluteTolerance<float>(0.f);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010076 }
77}
78
79/** CNN data types */
80const auto CNNDataTypes = framework::dataset::make("DataType",
81{
82#ifdef ARM_COMPUTE_ENABLE_FP16
83 DataType::F16,
84#endif /* ARM_COMPUTE_ENABLE_FP16 */
85 DataType::F32,
86 DataType::QS8,
87 DataType::QS16,
88});
89
90/** Input data sets. */
91const auto ActivationDataset = combine(combine(framework::dataset::make("InPlace", { false, true }), datasets::ActivationFunctions()), framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
92} // namespace
93
94TEST_SUITE(NEON)
95TEST_SUITE(ActivationLayer)
96
97DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(combine(concat(datasets::SmallShapes(), datasets::LargeShapes()), CNNDataTypes), framework::dataset::make("InPlace", { false, true })),
98 shape, data_type, in_place)
99{
100 // Set fixed point position data type allowed
101 const int fixed_point_position = is_data_type_fixed_point(data_type) ? 3 : 0;
102
103 // Create tensors
104 Tensor src = create_tensor<Tensor>(shape, data_type, 1, fixed_point_position);
105 Tensor dst = create_tensor<Tensor>(shape, data_type, 1, fixed_point_position);
106
107 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
108 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
109
110 // Create and configure function
111 NEActivationLayer act_layer;
112
113 if(in_place)
114 {
115 act_layer.configure(&src, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::ABS));
116 }
117 else
118 {
119 act_layer.configure(&src, &dst, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::ABS));
120 }
121
122 // Validate valid region
123 const ValidRegion valid_region = shape_to_valid_region(shape);
124 validate(src.info()->valid_region(), valid_region);
125
126 if(!in_place)
127 {
128 validate(dst.info()->valid_region(), valid_region);
129 }
130
131 // Validate padding
132 const PaddingSize padding = PaddingCalculator(shape.x(), 16).required_padding();
133 validate(src.info()->padding(), padding);
134
135 if(!in_place)
136 {
137 validate(dst.info()->padding(), padding);
138 }
139}
140
141template <typename T>
142using NEActivationLayerFixture = ActivationValidationFixture<Tensor, Accessor, NEActivationLayer, T>;
143
144TEST_SUITE(Float)
145#ifdef ARM_COMPUTE_ENABLE_FP16
146TEST_SUITE(FP16)
147FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerFixture<half_float::half>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ActivationDataset),
148 framework::dataset::make("DataType",
149 DataType::F16)))
150{
151 // Validate output
152 validate(Accessor(_target), _reference, tolerance(_data_type, _function));
153}
154FIXTURE_DATA_TEST_CASE(RunLarge, NEActivationLayerFixture<half_float::half>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), ActivationDataset),
155 framework::dataset::make("DataType",
156 DataType::F16)))
157{
158 // Validate output
159 validate(Accessor(_target), _reference, tolerance(_data_type, _function));
160}
161TEST_SUITE_END()
162#endif /* ARM_COMPUTE_ENABLE_FP16 */
163
164TEST_SUITE(FP32)
165FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ActivationDataset), framework::dataset::make("DataType",
166 DataType::F32)))
167{
168 // Validate output
169 validate(Accessor(_target), _reference, tolerance(_data_type, _function));
170}
171FIXTURE_DATA_TEST_CASE(RunLarge, NEActivationLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), ActivationDataset), framework::dataset::make("DataType",
172 DataType::F32)))
173{
174 // Validate output
175 validate(Accessor(_target), _reference, tolerance(_data_type, _function));
176}
177TEST_SUITE_END()
178TEST_SUITE_END()
179
180template <typename T>
181using NEActivationLayerFixedPointFixture = ActivationValidationFixedPointFixture<Tensor, Accessor, NEActivationLayer, T>;
182
183TEST_SUITE(Quantized)
184TEST_SUITE(QS8)
185// We test for fixed point precision [3,5] because [1,2] and [6,7] ranges cause
186// overflowing issues in most of the transcendentals functions.
187FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerFixedPointFixture<int8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallShapes(), ActivationDataset),
188 framework::dataset::make("DataType",
189 DataType::QS8)),
190 framework::dataset::make("FractionalBits", 3, 6)))
191{
192 // Validate output
193 validate(Accessor(_target), _reference, tolerance(_data_type, _function));
194}
195FIXTURE_DATA_TEST_CASE(RunLarge, NEActivationLayerFixedPointFixture<int8_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeShapes(), ActivationDataset),
196 framework::dataset::make("DataType",
197 DataType::QS8)),
198 framework::dataset::make("FractionalBits", 3, 6)))
199{
200 // Validate output
201 validate(Accessor(_target), _reference, tolerance(_data_type, _function));
202}
203TEST_SUITE_END()
204
205TEST_SUITE(QS16)
206// Testing for fixed point position [1,14) as reciprocal limits the maximum fixed point position to 14
207FIXTURE_DATA_TEST_CASE(RunSmall, NEActivationLayerFixedPointFixture<int16_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallShapes(), ActivationDataset),
208 framework::dataset::make("DataType",
209 DataType::QS16)),
210 framework::dataset::make("FractionalBits", 1, 14)))
211{
212 // Validate output
213 validate(Accessor(_target), _reference, tolerance(_data_type, _function));
214}
215FIXTURE_DATA_TEST_CASE(RunLarge, NEActivationLayerFixedPointFixture<int16_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeShapes(), ActivationDataset),
216 framework::dataset::make("DataType",
217 DataType::QS16)),
218 framework::dataset::make("FractionalBits", 1, 14)))
219{
220 // Validate output
221 validate(Accessor(_target), _reference, tolerance(_data_type, _function));
222}
223TEST_SUITE_END()
224TEST_SUITE_END()
225
226TEST_SUITE_END()
227TEST_SUITE_END()
228} // namespace validation
229} // namespace test
230} // namespace arm_compute