blob: 45b23edd2772ffe20a1a477ae30f284538316a7e [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:
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010064 return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.001f : epsilon);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010065 case ActivationLayerInfo::ActivationFunction::LEAKY_RELU:
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010066 return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.00001f : epsilon);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010067 case ActivationLayerInfo::ActivationFunction::SOFT_RELU:
68 case ActivationLayerInfo::ActivationFunction::SQRT:
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010069 return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.01f : 0.00001f);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010070 case ActivationLayerInfo::ActivationFunction::TANH:
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010071 return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.001f : 0.00001f);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010072 default:
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010073 return AbsoluteTolerance<float>(epsilon);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010074 }
75}
76
77/** CNN data types */
78const auto CNNDataTypes = framework::dataset::make("DataType",
79{
80 DataType::F16,
Vidhya Sudhan Loganathan0fc25452018-06-18 14:40:56 +010081 DataType::F32
Moritz Pflanzer572ade72017-07-21 17:36:33 +010082});
83
84/** Input data sets. */
85const auto ActivationDataset = combine(combine(framework::dataset::make("InPlace", { false, true }), datasets::ActivationFunctions()), framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
86} // namespace
87
88TEST_SUITE(CL)
89TEST_SUITE(ActivationLayer)
90
91DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(combine(concat(datasets::SmallShapes(), datasets::LargeShapes()), CNNDataTypes), framework::dataset::make("InPlace", { false, true })),
92 shape, data_type, in_place)
93{
Moritz Pflanzer572ade72017-07-21 17:36:33 +010094 // Create tensors
Vidhya Sudhan Loganathan0fc25452018-06-18 14:40:56 +010095 CLTensor src = create_tensor<CLTensor>(shape, data_type, 1);
96 CLTensor dst = create_tensor<CLTensor>(shape, data_type, 1);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010097
98 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
99 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
100
101 // Create and configure function
102 CLActivationLayer act_layer;
103
104 if(in_place)
105 {
106 act_layer.configure(&src, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::ABS));
107 }
108 else
109 {
110 act_layer.configure(&src, &dst, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::ABS));
111 }
112
113 // Validate valid region
114 const ValidRegion valid_region = shape_to_valid_region(shape);
115 validate(src.info()->valid_region(), valid_region);
116
117 if(!in_place)
118 {
119 validate(dst.info()->valid_region(), valid_region);
120 }
121
122 // Validate padding
123 const int step = 16 / arm_compute::data_size_from_type(data_type);
124 const PaddingSize padding = PaddingCalculator(shape.x(), step).required_padding();
125 validate(src.info()->padding(), padding);
126
127 if(!in_place)
128 {
129 validate(dst.info()->padding(), padding);
130 }
131}
132
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000133// *INDENT-OFF*
134// clang-format off
135DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(
136 framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching data types
Giorgio Arena2995f5b2017-11-29 17:33:59 +0000137 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Window shrink
138 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
139 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QASYMM8),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000140 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QASYMM8), // Unsupported activation
141 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching shapes
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000142 }),
143 framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F16),
144 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32),
Giorgio Arena2995f5b2017-11-29 17:33:59 +0000145 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
146 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QASYMM8),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000147 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QASYMM8),
148 TensorInfo(TensorShape(30U, 11U, 2U), 1, DataType::F32),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000149 })),
150 framework::dataset::make("ActivationInfo", { ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
151 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
Giorgio Arena2995f5b2017-11-29 17:33:59 +0000152 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000153 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU),
Michele Di Giorgioa1f7e332018-01-22 17:26:36 +0000154 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000155 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000156 })),
Vidhya Sudhan Loganathan0fc25452018-06-18 14:40:56 +0100157 framework::dataset::make("Expected", { false, false, true, true, false, false })),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000158 input_info, output_info, act_info, expected)
159{
Giorgio Arena2995f5b2017-11-29 17:33:59 +0000160 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 +0000161}
162// clang-format on
163// *INDENT-ON*
164
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100165template <typename T>
166using CLActivationLayerFixture = ActivationValidationFixture<CLTensor, CLAccessor, CLActivationLayer, T>;
167
168TEST_SUITE(Float)
169TEST_SUITE(FP16)
Georgios Pinitas583137c2017-08-31 18:12:42 +0100170FIXTURE_DATA_TEST_CASE(RunSmall, CLActivationLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ActivationDataset),
171 framework::dataset::make("DataType",
172 DataType::F16)))
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100173{
174 // Validate output
175 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
176}
Georgios Pinitas583137c2017-08-31 18:12:42 +0100177FIXTURE_DATA_TEST_CASE(RunLarge, CLActivationLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), ActivationDataset),
178 framework::dataset::make("DataType",
179 DataType::F16)))
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100180{
181 // Validate output
182 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
183}
184TEST_SUITE_END()
185
186TEST_SUITE(FP32)
187FIXTURE_DATA_TEST_CASE(RunSmall, CLActivationLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ActivationDataset), framework::dataset::make("DataType",
188 DataType::F32)))
189{
190 // Validate output
191 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
192}
193FIXTURE_DATA_TEST_CASE(RunLarge, CLActivationLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), ActivationDataset), framework::dataset::make("DataType",
194 DataType::F32)))
195{
196 // Validate output
197 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
198}
199TEST_SUITE_END()
200TEST_SUITE_END()
201
202template <typename T>
Michel Iwaniec66cc12f2017-12-07 17:26:40 +0000203using CLActivationLayerQuantizedFixture = ActivationValidationQuantizedFixture<CLTensor, CLAccessor, CLActivationLayer, T>;
204
205/** Input data sets. */
Michele Di Giorgioa1f7e332018-01-22 17:26:36 +0000206const auto QuantizedActivationFunctionsDataset = framework::dataset::make("ActivationFunction", { ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU,
207 ActivationLayerInfo::ActivationFunction::RELU,
208 ActivationLayerInfo::ActivationFunction::BOUNDED_RELU
209 });
210
211const auto QuantizedActivationDataset = combine(combine(framework::dataset::make("InPlace", { false, true }), QuantizedActivationFunctionsDataset),
Michel Iwaniec66cc12f2017-12-07 17:26:40 +0000212 framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
213
214TEST_SUITE(Quantized)
215TEST_SUITE(QASYMM8)
216FIXTURE_DATA_TEST_CASE(RunSmall, CLActivationLayerQuantizedFixture<uint8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallShapes(), QuantizedActivationDataset),
217 framework::dataset::make("DataType",
218 DataType::QASYMM8)),
219 framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.1f, 128.0f) })))
220{
221 // Validate output
222 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
223}
224FIXTURE_DATA_TEST_CASE(RunLarge, CLActivationLayerQuantizedFixture<uint8_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeShapes(), QuantizedActivationDataset),
225 framework::dataset::make("DataType",
226 DataType::QASYMM8)),
227 framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.1f, 128.0f) })))
228{
229 // Validate output
230 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
231}
232TEST_SUITE_END()
233TEST_SUITE_END()
234
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100235TEST_SUITE_END()
236TEST_SUITE_END()
237} // namespace validation
238} // namespace test
239} // namespace arm_compute