blob: a17ad9b269ed84b7da61effe05d3c01ee57a836b [file] [log] [blame]
Moritz Pflanzer572ade72017-07-21 17:36:33 +01001/*
Michalis Spyrou80943252019-01-10 17:19:50 +00002 * Copyright (c) 2017-2019 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"
Georgios Pinitas12833d02019-07-25 13:31:10 +010028#include "arm_compute/runtime/RuntimeContext.h"
Moritz Pflanzer572ade72017-07-21 17:36:33 +010029#include "tests/CL/CLAccessor.h"
30#include "tests/PaddingCalculator.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010031#include "tests/datasets/ActivationFunctionsDataset.h"
32#include "tests/datasets/ShapeDatasets.h"
33#include "tests/framework/Asserts.h"
34#include "tests/framework/Macros.h"
35#include "tests/framework/datasets/Datasets.h"
36#include "tests/validation/Validation.h"
37#include "tests/validation/fixtures/ActivationLayerFixture.h"
Moritz Pflanzer572ade72017-07-21 17:36:33 +010038
39namespace arm_compute
40{
41namespace test
42{
43namespace validation
44{
45namespace
46{
Manuel Bottini30dbeef2019-06-26 16:23:03 +010047constexpr AbsoluteTolerance<float> tolerance_qsymm16(1.f);
48
Moritz Pflanzer572ade72017-07-21 17:36:33 +010049/** Define tolerance of the activation layer.
50 *
51 * @param[in] activation The activation function used.
52 * @param[in] data_type Data type.
53 *
54 * @return Tolerance depending on the activation function.
55 */
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010056AbsoluteTolerance<float> tolerance(ActivationLayerInfo::ActivationFunction activation, DataType data_type)
Moritz Pflanzer572ade72017-07-21 17:36:33 +010057{
Moritz Pflanzerf07f1452017-08-08 17:28:39 +010058 constexpr float epsilon = 1e-6f;
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010059
Moritz Pflanzer572ade72017-07-21 17:36:33 +010060 switch(activation)
61 {
62 case ActivationLayerInfo::ActivationFunction::LINEAR:
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010063 return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.2f : epsilon);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010064 case ActivationLayerInfo::ActivationFunction::SQUARE:
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010065 return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.1f : epsilon);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010066 case ActivationLayerInfo::ActivationFunction::LOGISTIC:
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010067 return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.001f : epsilon);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010068 case ActivationLayerInfo::ActivationFunction::LEAKY_RELU:
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010069 return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.00001f : epsilon);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010070 case ActivationLayerInfo::ActivationFunction::SOFT_RELU:
Georgios Pinitasfb0fdcd2019-08-22 17:10:04 +010071 case ActivationLayerInfo::ActivationFunction::ELU:
Moritz Pflanzer572ade72017-07-21 17:36:33 +010072 case ActivationLayerInfo::ActivationFunction::SQRT:
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010073 return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.01f : 0.00001f);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010074 case ActivationLayerInfo::ActivationFunction::TANH:
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010075 return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.001f : 0.00001f);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010076 default:
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010077 return AbsoluteTolerance<float>(epsilon);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010078 }
79}
80
81/** CNN data types */
82const auto CNNDataTypes = framework::dataset::make("DataType",
83{
84 DataType::F16,
Vidhya Sudhan Loganathan0fc25452018-06-18 14:40:56 +010085 DataType::F32
Moritz Pflanzer572ade72017-07-21 17:36:33 +010086});
87
88/** Input data sets. */
89const auto ActivationDataset = combine(combine(framework::dataset::make("InPlace", { false, true }), datasets::ActivationFunctions()), framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
90} // namespace
91
92TEST_SUITE(CL)
93TEST_SUITE(ActivationLayer)
94
Michalis Spyrou80943252019-01-10 17:19:50 +000095DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(combine(datasets::SmallShapes(), CNNDataTypes), framework::dataset::make("InPlace", { false, true })),
Moritz Pflanzer572ade72017-07-21 17:36:33 +010096 shape, data_type, in_place)
97{
Pablo Tellodb8485a2019-09-24 11:03:47 +010098 // Create context
99 auto ctx = parameters->get_ctx<CLTensor>();
100
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100101 // Create tensors
Pablo Tellodb8485a2019-09-24 11:03:47 +0100102 CLTensor src = create_tensor<CLTensor>(shape, data_type, 1, QuantizationInfo(), DataLayout::NCHW, ctx);
103 CLTensor dst = create_tensor<CLTensor>(shape, data_type, 1, QuantizationInfo(), DataLayout::NCHW, ctx);
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100104
105 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
106 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
107
108 // Create and configure function
Pablo Tellodb8485a2019-09-24 11:03:47 +0100109 CLActivationLayer act_layer(ctx);
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100110
111 if(in_place)
112 {
113 act_layer.configure(&src, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::ABS));
114 }
115 else
116 {
117 act_layer.configure(&src, &dst, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::ABS));
118 }
119
120 // Validate valid region
121 const ValidRegion valid_region = shape_to_valid_region(shape);
122 validate(src.info()->valid_region(), valid_region);
123
124 if(!in_place)
125 {
126 validate(dst.info()->valid_region(), valid_region);
127 }
128
129 // Validate padding
130 const int step = 16 / arm_compute::data_size_from_type(data_type);
131 const PaddingSize padding = PaddingCalculator(shape.x(), step).required_padding();
132 validate(src.info()->padding(), padding);
133
134 if(!in_place)
135 {
136 validate(dst.info()->padding(), padding);
137 }
138}
139
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000140// *INDENT-OFF*
141// clang-format off
142DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(
143 framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching data types
Giorgio Arena2995f5b2017-11-29 17:33:59 +0000144 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Window shrink
145 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
146 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QASYMM8),
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100147 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QASYMM8), // Invalid quantization info
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000148 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching shapes
Manuel Bottini30dbeef2019-06-26 16:23:03 +0100149 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16),
150 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16),
151 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16), // Invalid activation function for QSYMM16
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000152 }),
153 framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F16),
154 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32),
Giorgio Arena2995f5b2017-11-29 17:33:59 +0000155 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
156 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QASYMM8),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000157 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QASYMM8),
158 TensorInfo(TensorShape(30U, 11U, 2U), 1, DataType::F32),
Manuel Bottini30dbeef2019-06-26 16:23:03 +0100159 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16, QuantizationInfo(1.f / 32768.f, 0)),
160 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16, QuantizationInfo(1.f / 32768.f, 0)),
161 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16, QuantizationInfo(1.f / 32768.f, 0)),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000162 })),
163 framework::dataset::make("ActivationInfo", { ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
164 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
Giorgio Arena2995f5b2017-11-29 17:33:59 +0000165 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000166 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU),
Michele Di Giorgioa1f7e332018-01-22 17:26:36 +0000167 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000168 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
Manuel Bottini30dbeef2019-06-26 16:23:03 +0100169 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH),
170 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC),
171 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::SQRT),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000172 })),
Manuel Bottini30dbeef2019-06-26 16:23:03 +0100173 framework::dataset::make("Expected", { false, false, true, true, false, false, true, true, false })),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000174 input_info, output_info, act_info, expected)
175{
Giorgio Arena2995f5b2017-11-29 17:33:59 +0000176 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 +0000177}
178// clang-format on
179// *INDENT-ON*
180
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +0000181/** [CLActivationLayerFixture snippet] **/
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100182template <typename T>
183using CLActivationLayerFixture = ActivationValidationFixture<CLTensor, CLAccessor, CLActivationLayer, T>;
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +0000184/** [CLActivationLayerFixture snippet] **/
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100185
186TEST_SUITE(Float)
187TEST_SUITE(FP16)
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +0000188/** [CLActivationLayer Test snippet] **/
Georgios Pinitas583137c2017-08-31 18:12:42 +0100189FIXTURE_DATA_TEST_CASE(RunSmall, CLActivationLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ActivationDataset),
190 framework::dataset::make("DataType",
191 DataType::F16)))
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100192{
193 // Validate output
194 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
195}
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +0000196/** [CLActivationLayer Test snippet] **/
Georgios Pinitas583137c2017-08-31 18:12:42 +0100197FIXTURE_DATA_TEST_CASE(RunLarge, CLActivationLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), ActivationDataset),
198 framework::dataset::make("DataType",
199 DataType::F16)))
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100200{
201 // Validate output
202 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
203}
Michalis Spyrou80943252019-01-10 17:19:50 +0000204TEST_SUITE_END() // FP16
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100205
206TEST_SUITE(FP32)
207FIXTURE_DATA_TEST_CASE(RunSmall, CLActivationLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ActivationDataset), framework::dataset::make("DataType",
208 DataType::F32)))
209{
210 // Validate output
211 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
212}
213FIXTURE_DATA_TEST_CASE(RunLarge, CLActivationLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), ActivationDataset), framework::dataset::make("DataType",
214 DataType::F32)))
215{
216 // Validate output
217 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
218}
Michalis Spyrou80943252019-01-10 17:19:50 +0000219TEST_SUITE_END() // FP32
220TEST_SUITE_END() // Float
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100221
222template <typename T>
Michel Iwaniec66cc12f2017-12-07 17:26:40 +0000223using CLActivationLayerQuantizedFixture = ActivationValidationQuantizedFixture<CLTensor, CLAccessor, CLActivationLayer, T>;
224
Georgios Pinitas151fa872019-06-06 14:44:44 +0100225const auto QuantizedActivationDataset = combine(combine(framework::dataset::make("InPlace", { false }), datasets::ActivationFunctionsQuantized()),
Michel Iwaniec66cc12f2017-12-07 17:26:40 +0000226 framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
227
228TEST_SUITE(Quantized)
229TEST_SUITE(QASYMM8)
230FIXTURE_DATA_TEST_CASE(RunSmall, CLActivationLayerQuantizedFixture<uint8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallShapes(), QuantizedActivationDataset),
231 framework::dataset::make("DataType",
232 DataType::QASYMM8)),
233 framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.1f, 128.0f) })))
234{
235 // Validate output
236 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
237}
238FIXTURE_DATA_TEST_CASE(RunLarge, CLActivationLayerQuantizedFixture<uint8_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeShapes(), QuantizedActivationDataset),
239 framework::dataset::make("DataType",
240 DataType::QASYMM8)),
241 framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.1f, 128.0f) })))
242{
243 // Validate output
244 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
245}
Michalis Spyrou80943252019-01-10 17:19:50 +0000246TEST_SUITE_END() // QASYMM8
Manuel Bottini30dbeef2019-06-26 16:23:03 +0100247TEST_SUITE(QSYMM16)
248FIXTURE_DATA_TEST_CASE(RunSmall, CLActivationLayerQuantizedFixture<int16_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallShapes(), QuantizedActivationDataset),
249 framework::dataset::make("DataType",
250 DataType::QSYMM16)),
251 framework::dataset::make("QuantizationInfo", { QuantizationInfo(1.f / 32768.f, 0) })))
252{
253 // Validate output
254 validate(CLAccessor(_target), _reference, tolerance_qsymm16);
255}
256FIXTURE_DATA_TEST_CASE(RunLarge, CLActivationLayerQuantizedFixture<int16_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeShapes(), QuantizedActivationDataset),
257 framework::dataset::make("DataType",
258 DataType::QSYMM16)),
259 framework::dataset::make("QuantizationInfo", { QuantizationInfo(1.f / 32768.f, 0) })))
260{
261 // Validate output
262 validate(CLAccessor(_target), _reference, tolerance_qsymm16);
263}
264TEST_SUITE_END() // QSYMM16
Michalis Spyrou80943252019-01-10 17:19:50 +0000265TEST_SUITE_END() // Quantized
Michel Iwaniec66cc12f2017-12-07 17:26:40 +0000266
Michalis Spyrou80943252019-01-10 17:19:50 +0000267TEST_SUITE_END() // ActivationLayer
268TEST_SUITE_END() // CL
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100269} // namespace validation
270} // namespace test
271} // namespace arm_compute