blob: e95db7ca60085e248ce48fcdd73ecd5b5bb0dab1 [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{
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
Michalis Spyrou80943252019-01-10 17:19:50 +000091DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(combine(datasets::SmallShapes(), CNNDataTypes), framework::dataset::make("InPlace", { false, true })),
Moritz Pflanzer572ade72017-07-21 17:36:33 +010092 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
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +0000165/** [CLActivationLayerFixture snippet] **/
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100166template <typename T>
167using CLActivationLayerFixture = ActivationValidationFixture<CLTensor, CLAccessor, CLActivationLayer, T>;
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +0000168/** [CLActivationLayerFixture snippet] **/
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100169
170TEST_SUITE(Float)
171TEST_SUITE(FP16)
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +0000172/** [CLActivationLayer Test snippet] **/
Georgios Pinitas583137c2017-08-31 18:12:42 +0100173FIXTURE_DATA_TEST_CASE(RunSmall, CLActivationLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ActivationDataset),
174 framework::dataset::make("DataType",
175 DataType::F16)))
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100176{
177 // Validate output
178 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
179}
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +0000180/** [CLActivationLayer Test snippet] **/
Georgios Pinitas583137c2017-08-31 18:12:42 +0100181FIXTURE_DATA_TEST_CASE(RunLarge, CLActivationLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), ActivationDataset),
182 framework::dataset::make("DataType",
183 DataType::F16)))
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100184{
185 // Validate output
186 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
187}
Michalis Spyrou80943252019-01-10 17:19:50 +0000188TEST_SUITE_END() // FP16
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100189
190TEST_SUITE(FP32)
191FIXTURE_DATA_TEST_CASE(RunSmall, CLActivationLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallShapes(), ActivationDataset), framework::dataset::make("DataType",
192 DataType::F32)))
193{
194 // Validate output
195 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
196}
197FIXTURE_DATA_TEST_CASE(RunLarge, CLActivationLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeShapes(), ActivationDataset), framework::dataset::make("DataType",
198 DataType::F32)))
199{
200 // Validate output
201 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
202}
Michalis Spyrou80943252019-01-10 17:19:50 +0000203TEST_SUITE_END() // FP32
204TEST_SUITE_END() // Float
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100205
206template <typename T>
Michel Iwaniec66cc12f2017-12-07 17:26:40 +0000207using CLActivationLayerQuantizedFixture = ActivationValidationQuantizedFixture<CLTensor, CLAccessor, CLActivationLayer, T>;
208
Giorgio Arena73023022018-09-04 14:55:55 +0100209const auto QuantizedActivationDataset = combine(combine(framework::dataset::make("InPlace", { false, true }), datasets::ActivationFunctionsQuantized()),
Michel Iwaniec66cc12f2017-12-07 17:26:40 +0000210 framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
211
212TEST_SUITE(Quantized)
213TEST_SUITE(QASYMM8)
214FIXTURE_DATA_TEST_CASE(RunSmall, CLActivationLayerQuantizedFixture<uint8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallShapes(), QuantizedActivationDataset),
215 framework::dataset::make("DataType",
216 DataType::QASYMM8)),
217 framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.1f, 128.0f) })))
218{
219 // Validate output
220 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
221}
222FIXTURE_DATA_TEST_CASE(RunLarge, CLActivationLayerQuantizedFixture<uint8_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeShapes(), QuantizedActivationDataset),
223 framework::dataset::make("DataType",
224 DataType::QASYMM8)),
225 framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.1f, 128.0f) })))
226{
227 // Validate output
228 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
229}
Michalis Spyrou80943252019-01-10 17:19:50 +0000230TEST_SUITE_END() // QASYMM8
231TEST_SUITE_END() // Quantized
Michel Iwaniec66cc12f2017-12-07 17:26:40 +0000232
Michalis Spyrou80943252019-01-10 17:19:50 +0000233TEST_SUITE_END() // ActivationLayer
234TEST_SUITE_END() // CL
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100235} // namespace validation
236} // namespace test
237} // namespace arm_compute