blob: fd203ccb7e144f4d92385a40f48e94cfe4938c61 [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"
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{
Manuel Bottini30dbeef2019-06-26 16:23:03 +010046constexpr AbsoluteTolerance<float> tolerance_qsymm16(1.f);
47
Moritz Pflanzer572ade72017-07-21 17:36:33 +010048/** Define tolerance of the activation layer.
49 *
50 * @param[in] activation The activation function used.
51 * @param[in] data_type Data type.
52 *
53 * @return Tolerance depending on the activation function.
54 */
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010055AbsoluteTolerance<float> tolerance(ActivationLayerInfo::ActivationFunction activation, DataType data_type)
Moritz Pflanzer572ade72017-07-21 17:36:33 +010056{
Moritz Pflanzerf07f1452017-08-08 17:28:39 +010057 constexpr float epsilon = 1e-6f;
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010058
Moritz Pflanzer572ade72017-07-21 17:36:33 +010059 switch(activation)
60 {
61 case ActivationLayerInfo::ActivationFunction::LINEAR:
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010062 return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.2f : epsilon);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010063 case ActivationLayerInfo::ActivationFunction::SQUARE:
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010064 return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.1f : epsilon);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010065 case ActivationLayerInfo::ActivationFunction::LOGISTIC:
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010066 return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.001f : epsilon);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010067 case ActivationLayerInfo::ActivationFunction::LEAKY_RELU:
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010068 return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.00001f : epsilon);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010069 case ActivationLayerInfo::ActivationFunction::SOFT_RELU:
70 case ActivationLayerInfo::ActivationFunction::SQRT:
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010071 return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.01f : 0.00001f);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010072 case ActivationLayerInfo::ActivationFunction::TANH:
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010073 return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.001f : 0.00001f);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010074 default:
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010075 return AbsoluteTolerance<float>(epsilon);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010076 }
77}
78
79/** CNN data types */
80const auto CNNDataTypes = framework::dataset::make("DataType",
81{
82 DataType::F16,
Vidhya Sudhan Loganathan0fc25452018-06-18 14:40:56 +010083 DataType::F32
Moritz Pflanzer572ade72017-07-21 17:36:33 +010084});
85
86/** Input data sets. */
87const auto ActivationDataset = combine(combine(framework::dataset::make("InPlace", { false, true }), datasets::ActivationFunctions()), framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
88} // namespace
89
90TEST_SUITE(CL)
91TEST_SUITE(ActivationLayer)
92
Michalis Spyrou80943252019-01-10 17:19:50 +000093DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(combine(datasets::SmallShapes(), CNNDataTypes), framework::dataset::make("InPlace", { false, true })),
Moritz Pflanzer572ade72017-07-21 17:36:33 +010094 shape, data_type, in_place)
95{
Moritz Pflanzer572ade72017-07-21 17:36:33 +010096 // Create tensors
Vidhya Sudhan Loganathan0fc25452018-06-18 14:40:56 +010097 CLTensor src = create_tensor<CLTensor>(shape, data_type, 1);
98 CLTensor dst = create_tensor<CLTensor>(shape, data_type, 1);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010099
100 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
101 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
102
103 // Create and configure function
104 CLActivationLayer act_layer;
105
106 if(in_place)
107 {
108 act_layer.configure(&src, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::ABS));
109 }
110 else
111 {
112 act_layer.configure(&src, &dst, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::ABS));
113 }
114
115 // Validate valid region
116 const ValidRegion valid_region = shape_to_valid_region(shape);
117 validate(src.info()->valid_region(), valid_region);
118
119 if(!in_place)
120 {
121 validate(dst.info()->valid_region(), valid_region);
122 }
123
124 // Validate padding
125 const int step = 16 / arm_compute::data_size_from_type(data_type);
126 const PaddingSize padding = PaddingCalculator(shape.x(), step).required_padding();
127 validate(src.info()->padding(), padding);
128
129 if(!in_place)
130 {
131 validate(dst.info()->padding(), padding);
132 }
133}
134
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000135// *INDENT-OFF*
136// clang-format off
137DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(
138 framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching data types
Giorgio Arena2995f5b2017-11-29 17:33:59 +0000139 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Window shrink
140 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
141 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QASYMM8),
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100142 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QASYMM8), // Invalid quantization info
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000143 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching shapes
Manuel Bottini30dbeef2019-06-26 16:23:03 +0100144 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16),
145 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16),
146 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16), // Invalid activation function for QSYMM16
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000147 }),
148 framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F16),
149 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32),
Giorgio Arena2995f5b2017-11-29 17:33:59 +0000150 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
151 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QASYMM8),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000152 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QASYMM8),
153 TensorInfo(TensorShape(30U, 11U, 2U), 1, DataType::F32),
Manuel Bottini30dbeef2019-06-26 16:23:03 +0100154 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16, QuantizationInfo(1.f / 32768.f, 0)),
155 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16, QuantizationInfo(1.f / 32768.f, 0)),
156 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16, QuantizationInfo(1.f / 32768.f, 0)),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000157 })),
158 framework::dataset::make("ActivationInfo", { ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
159 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
Giorgio Arena2995f5b2017-11-29 17:33:59 +0000160 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000161 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU),
Michele Di Giorgioa1f7e332018-01-22 17:26:36 +0000162 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000163 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
Manuel Bottini30dbeef2019-06-26 16:23:03 +0100164 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH),
165 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC),
166 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::SQRT),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000167 })),
Manuel Bottini30dbeef2019-06-26 16:23:03 +0100168 framework::dataset::make("Expected", { false, false, true, true, false, false, true, true, false })),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000169 input_info, output_info, act_info, expected)
170{
Giorgio Arena2995f5b2017-11-29 17:33:59 +0000171 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 +0000172}
173// clang-format on
174// *INDENT-ON*
175
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +0000176/** [CLActivationLayerFixture snippet] **/
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100177template <typename T>
178using CLActivationLayerFixture = ActivationValidationFixture<CLTensor, CLAccessor, CLActivationLayer, T>;
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +0000179/** [CLActivationLayerFixture snippet] **/
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100180
181TEST_SUITE(Float)
182TEST_SUITE(FP16)
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +0000183/** [CLActivationLayer Test snippet] **/
Georgios Pinitas583137c2017-08-31 18:12:42 +0100184FIXTURE_DATA_TEST_CASE(RunSmall, CLActivationLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ActivationDataset),
185 framework::dataset::make("DataType",
186 DataType::F16)))
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100187{
188 // Validate output
189 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
190}
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +0000191/** [CLActivationLayer Test snippet] **/
Georgios Pinitas583137c2017-08-31 18:12:42 +0100192FIXTURE_DATA_TEST_CASE(RunLarge, CLActivationLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), ActivationDataset),
193 framework::dataset::make("DataType",
194 DataType::F16)))
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100195{
196 // Validate output
197 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
198}
Michalis Spyrou80943252019-01-10 17:19:50 +0000199TEST_SUITE_END() // FP16
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100200
201TEST_SUITE(FP32)
202FIXTURE_DATA_TEST_CASE(RunSmall, CLActivationLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ActivationDataset), framework::dataset::make("DataType",
203 DataType::F32)))
204{
205 // Validate output
206 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
207}
208FIXTURE_DATA_TEST_CASE(RunLarge, CLActivationLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), ActivationDataset), framework::dataset::make("DataType",
209 DataType::F32)))
210{
211 // Validate output
212 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
213}
Michalis Spyrou80943252019-01-10 17:19:50 +0000214TEST_SUITE_END() // FP32
215TEST_SUITE_END() // Float
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100216
217template <typename T>
Michel Iwaniec66cc12f2017-12-07 17:26:40 +0000218using CLActivationLayerQuantizedFixture = ActivationValidationQuantizedFixture<CLTensor, CLAccessor, CLActivationLayer, T>;
219
Georgios Pinitas151fa872019-06-06 14:44:44 +0100220const auto QuantizedActivationDataset = combine(combine(framework::dataset::make("InPlace", { false }), datasets::ActivationFunctionsQuantized()),
Michel Iwaniec66cc12f2017-12-07 17:26:40 +0000221 framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
222
223TEST_SUITE(Quantized)
224TEST_SUITE(QASYMM8)
225FIXTURE_DATA_TEST_CASE(RunSmall, CLActivationLayerQuantizedFixture<uint8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallShapes(), QuantizedActivationDataset),
226 framework::dataset::make("DataType",
227 DataType::QASYMM8)),
228 framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.1f, 128.0f) })))
229{
230 // Validate output
231 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
232}
233FIXTURE_DATA_TEST_CASE(RunLarge, CLActivationLayerQuantizedFixture<uint8_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeShapes(), QuantizedActivationDataset),
234 framework::dataset::make("DataType",
235 DataType::QASYMM8)),
236 framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.1f, 128.0f) })))
237{
238 // Validate output
239 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
240}
Michalis Spyrou80943252019-01-10 17:19:50 +0000241TEST_SUITE_END() // QASYMM8
Manuel Bottini30dbeef2019-06-26 16:23:03 +0100242TEST_SUITE(QSYMM16)
243FIXTURE_DATA_TEST_CASE(RunSmall, CLActivationLayerQuantizedFixture<int16_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallShapes(), QuantizedActivationDataset),
244 framework::dataset::make("DataType",
245 DataType::QSYMM16)),
246 framework::dataset::make("QuantizationInfo", { QuantizationInfo(1.f / 32768.f, 0) })))
247{
248 // Validate output
249 validate(CLAccessor(_target), _reference, tolerance_qsymm16);
250}
251FIXTURE_DATA_TEST_CASE(RunLarge, CLActivationLayerQuantizedFixture<int16_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeShapes(), QuantizedActivationDataset),
252 framework::dataset::make("DataType",
253 DataType::QSYMM16)),
254 framework::dataset::make("QuantizationInfo", { QuantizationInfo(1.f / 32768.f, 0) })))
255{
256 // Validate output
257 validate(CLAccessor(_target), _reference, tolerance_qsymm16);
258}
259TEST_SUITE_END() // QSYMM16
Michalis Spyrou80943252019-01-10 17:19:50 +0000260TEST_SUITE_END() // Quantized
Michel Iwaniec66cc12f2017-12-07 17:26:40 +0000261
Michalis Spyrou80943252019-01-10 17:19:50 +0000262TEST_SUITE_END() // ActivationLayer
263TEST_SUITE_END() // CL
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100264} // namespace validation
265} // namespace test
266} // namespace arm_compute