blob: 4f97d7b6c17d1b17a7d94ff2afed89da339923f4 [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,
Vidhya Sudhan Loganathan0fc25452018-06-18 14:40:56 +0100102 DataType::F32
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100103});
104
105/** Input data sets. */
106const auto ActivationDataset = combine(combine(framework::dataset::make("InPlace", { false, true }), datasets::ActivationFunctions()), framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
107} // namespace
108
109TEST_SUITE(CL)
110TEST_SUITE(ActivationLayer)
111
112DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(combine(concat(datasets::SmallShapes(), datasets::LargeShapes()), CNNDataTypes), framework::dataset::make("InPlace", { false, true })),
113 shape, data_type, in_place)
114{
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100115 // Create tensors
Vidhya Sudhan Loganathan0fc25452018-06-18 14:40:56 +0100116 CLTensor src = create_tensor<CLTensor>(shape, data_type, 1);
117 CLTensor dst = create_tensor<CLTensor>(shape, data_type, 1);
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100118
119 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
120 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
121
122 // Create and configure function
123 CLActivationLayer act_layer;
124
125 if(in_place)
126 {
127 act_layer.configure(&src, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::ABS));
128 }
129 else
130 {
131 act_layer.configure(&src, &dst, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::ABS));
132 }
133
134 // Validate valid region
135 const ValidRegion valid_region = shape_to_valid_region(shape);
136 validate(src.info()->valid_region(), valid_region);
137
138 if(!in_place)
139 {
140 validate(dst.info()->valid_region(), valid_region);
141 }
142
143 // Validate padding
144 const int step = 16 / arm_compute::data_size_from_type(data_type);
145 const PaddingSize padding = PaddingCalculator(shape.x(), step).required_padding();
146 validate(src.info()->padding(), padding);
147
148 if(!in_place)
149 {
150 validate(dst.info()->padding(), padding);
151 }
152}
153
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000154// *INDENT-OFF*
155// clang-format off
156DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(
157 framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching data types
Giorgio Arena2995f5b2017-11-29 17:33:59 +0000158 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Window shrink
159 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
160 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QASYMM8),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000161 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QASYMM8), // Unsupported activation
162 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching shapes
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000163 }),
164 framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F16),
165 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32),
Giorgio Arena2995f5b2017-11-29 17:33:59 +0000166 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
167 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QASYMM8),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000168 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QASYMM8),
169 TensorInfo(TensorShape(30U, 11U, 2U), 1, DataType::F32),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000170 })),
171 framework::dataset::make("ActivationInfo", { ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
172 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
Giorgio Arena2995f5b2017-11-29 17:33:59 +0000173 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000174 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU),
Michele Di Giorgioa1f7e332018-01-22 17:26:36 +0000175 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000176 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000177 })),
Vidhya Sudhan Loganathan0fc25452018-06-18 14:40:56 +0100178 framework::dataset::make("Expected", { false, false, true, true, false, false })),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000179 input_info, output_info, act_info, expected)
180{
Giorgio Arena2995f5b2017-11-29 17:33:59 +0000181 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 +0000182}
183// clang-format on
184// *INDENT-ON*
185
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100186template <typename T>
187using CLActivationLayerFixture = ActivationValidationFixture<CLTensor, CLAccessor, CLActivationLayer, T>;
188
189TEST_SUITE(Float)
190TEST_SUITE(FP16)
Georgios Pinitas583137c2017-08-31 18:12:42 +0100191FIXTURE_DATA_TEST_CASE(RunSmall, CLActivationLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ActivationDataset),
192 framework::dataset::make("DataType",
193 DataType::F16)))
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100194{
195 // Validate output
196 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
197}
Georgios Pinitas583137c2017-08-31 18:12:42 +0100198FIXTURE_DATA_TEST_CASE(RunLarge, CLActivationLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), ActivationDataset),
199 framework::dataset::make("DataType",
200 DataType::F16)))
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100201{
202 // Validate output
203 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
204}
205TEST_SUITE_END()
206
207TEST_SUITE(FP32)
208FIXTURE_DATA_TEST_CASE(RunSmall, CLActivationLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ActivationDataset), framework::dataset::make("DataType",
209 DataType::F32)))
210{
211 // Validate output
212 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
213}
214FIXTURE_DATA_TEST_CASE(RunLarge, CLActivationLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), ActivationDataset), framework::dataset::make("DataType",
215 DataType::F32)))
216{
217 // Validate output
218 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
219}
220TEST_SUITE_END()
221TEST_SUITE_END()
222
223template <typename T>
Michel Iwaniec66cc12f2017-12-07 17:26:40 +0000224using CLActivationLayerQuantizedFixture = ActivationValidationQuantizedFixture<CLTensor, CLAccessor, CLActivationLayer, T>;
225
226/** Input data sets. */
Michele Di Giorgioa1f7e332018-01-22 17:26:36 +0000227const auto QuantizedActivationFunctionsDataset = framework::dataset::make("ActivationFunction", { ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU,
228 ActivationLayerInfo::ActivationFunction::RELU,
229 ActivationLayerInfo::ActivationFunction::BOUNDED_RELU
230 });
231
232const auto QuantizedActivationDataset = combine(combine(framework::dataset::make("InPlace", { false, true }), QuantizedActivationFunctionsDataset),
Michel Iwaniec66cc12f2017-12-07 17:26:40 +0000233 framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
234
235TEST_SUITE(Quantized)
236TEST_SUITE(QASYMM8)
237FIXTURE_DATA_TEST_CASE(RunSmall, CLActivationLayerQuantizedFixture<uint8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallShapes(), QuantizedActivationDataset),
238 framework::dataset::make("DataType",
239 DataType::QASYMM8)),
240 framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.1f, 128.0f) })))
241{
242 // Validate output
243 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
244}
245FIXTURE_DATA_TEST_CASE(RunLarge, CLActivationLayerQuantizedFixture<uint8_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeShapes(), QuantizedActivationDataset),
246 framework::dataset::make("DataType",
247 DataType::QASYMM8)),
248 framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.1f, 128.0f) })))
249{
250 // Validate output
251 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
252}
253TEST_SUITE_END()
254TEST_SUITE_END()
255
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100256TEST_SUITE_END()
257TEST_SUITE_END()
258} // namespace validation
259} // namespace test
260} // namespace arm_compute