blob: 83bd2d0a3ada0907dfd0b417f4b1c8e27f8345b1 [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/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
159template <typename T>
160using CLActivationLayerFixture = ActivationValidationFixture<CLTensor, CLAccessor, CLActivationLayer, T>;
161
162TEST_SUITE(Float)
163TEST_SUITE(FP16)
Georgios Pinitas583137c2017-08-31 18:12:42 +0100164FIXTURE_DATA_TEST_CASE(RunSmall, CLActivationLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ActivationDataset),
165 framework::dataset::make("DataType",
166 DataType::F16)))
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100167{
168 // Validate output
169 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
170}
Georgios Pinitas583137c2017-08-31 18:12:42 +0100171FIXTURE_DATA_TEST_CASE(RunLarge, CLActivationLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), ActivationDataset),
172 framework::dataset::make("DataType",
173 DataType::F16)))
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100174{
175 // Validate output
176 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
177}
178TEST_SUITE_END()
179
180TEST_SUITE(FP32)
181FIXTURE_DATA_TEST_CASE(RunSmall, CLActivationLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ActivationDataset), framework::dataset::make("DataType",
182 DataType::F32)))
183{
184 // Validate output
185 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
186}
187FIXTURE_DATA_TEST_CASE(RunLarge, CLActivationLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), ActivationDataset), framework::dataset::make("DataType",
188 DataType::F32)))
189{
190 // Validate output
191 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
192}
193TEST_SUITE_END()
194TEST_SUITE_END()
195
196template <typename T>
197using CLActivationLayerFixedPointFixture = ActivationValidationFixedPointFixture<CLTensor, CLAccessor, CLActivationLayer, T>;
198
199TEST_SUITE(Quantized)
200TEST_SUITE(QS8)
201// We test for fixed point precision [3,5] because [1,2] and [6,7] ranges cause
202// overflowing issues in most of the transcendentals functions.
203FIXTURE_DATA_TEST_CASE(RunSmall, CLActivationLayerFixedPointFixture<int8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallShapes(), ActivationDataset),
204 framework::dataset::make("DataType",
205 DataType::QS8)),
206 framework::dataset::make("FractionalBits", 3, 6)))
207{
208 // Validate output
209 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
210}
211FIXTURE_DATA_TEST_CASE(RunLarge, CLActivationLayerFixedPointFixture<int8_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeShapes(), ActivationDataset),
212 framework::dataset::make("DataType",
213 DataType::QS8)),
214 framework::dataset::make("FractionalBits", 3, 6)))
215{
216 // Validate output
217 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
218}
219TEST_SUITE_END()
220
221TEST_SUITE(QS16)
222// Testing for fixed point position [1,14) as reciprocal limits the maximum fixed point position to 14
223FIXTURE_DATA_TEST_CASE(RunSmall, CLActivationLayerFixedPointFixture<int16_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallShapes(), ActivationDataset),
224 framework::dataset::make("DataType",
225 DataType::QS16)),
226 framework::dataset::make("FractionalBits", 1, 14)))
227{
228 // Validate output
229 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
230}
231FIXTURE_DATA_TEST_CASE(RunLarge, CLActivationLayerFixedPointFixture<int16_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeShapes(), ActivationDataset),
232 framework::dataset::make("DataType",
233 DataType::QS16)),
234 framework::dataset::make("FractionalBits", 1, 14)))
235{
236 // Validate output
237 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
238}
239TEST_SUITE_END()
240TEST_SUITE_END()
241
242TEST_SUITE_END()
243TEST_SUITE_END()
244} // namespace validation
245} // namespace test
246} // namespace arm_compute