blob: 133b39d15458fffb341396c9fd5fe0b56459c99f [file] [log] [blame]
Moritz Pflanzer572ade72017-07-21 17:36:33 +01001/*
Murray Kornelsen926f5022022-07-13 21:22:39 -04002 * Copyright (c) 2017-2020, 2022 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:
Murray Kornelsen926f5022022-07-13 21:22:39 -040073 case ActivationLayerInfo::ActivationFunction::GELU:
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010074 return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.01f : 0.00001f);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010075 case ActivationLayerInfo::ActivationFunction::TANH:
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010076 return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.001f : 0.00001f);
morgolock6b3865a2020-03-04 14:57:46 +000077 case ActivationLayerInfo::ActivationFunction::HARD_SWISH:
78 return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.01f : epsilon);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010079 default:
Moritz Pflanzer6106a4d2017-08-02 09:42:27 +010080 return AbsoluteTolerance<float>(epsilon);
Moritz Pflanzer572ade72017-07-21 17:36:33 +010081 }
82}
83
84/** CNN data types */
85const auto CNNDataTypes = framework::dataset::make("DataType",
86{
87 DataType::F16,
Vidhya Sudhan Loganathan0fc25452018-06-18 14:40:56 +010088 DataType::F32
Moritz Pflanzer572ade72017-07-21 17:36:33 +010089});
90
91/** Input data sets. */
92const auto ActivationDataset = combine(combine(framework::dataset::make("InPlace", { false, true }), datasets::ActivationFunctions()), framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
Giorgio Arenad304adb2020-10-02 10:20:11 +010093
Moritz Pflanzer572ade72017-07-21 17:36:33 +010094} // namespace
95
96TEST_SUITE(CL)
97TEST_SUITE(ActivationLayer)
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +000098// *INDENT-OFF*
99// clang-format off
100DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(
101 framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching data types
Giorgio Arenad304adb2020-10-02 10:20:11 +0100102 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32),
Giorgio Arena2995f5b2017-11-29 17:33:59 +0000103 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
104 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QASYMM8),
Georgios Pinitas4b3fba12019-06-04 17:31:46 +0100105 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QASYMM8), // Invalid quantization info
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000106 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), // Mismatching shapes
Manuel Bottini30dbeef2019-06-26 16:23:03 +0100107 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16),
108 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16),
109 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16), // Invalid activation function for QSYMM16
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000110 }),
111 framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F16),
112 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32),
Giorgio Arena2995f5b2017-11-29 17:33:59 +0000113 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
114 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QASYMM8),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000115 TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QASYMM8),
116 TensorInfo(TensorShape(30U, 11U, 2U), 1, DataType::F32),
Manuel Bottini30dbeef2019-06-26 16:23:03 +0100117 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16, QuantizationInfo(1.f / 32768.f, 0)),
118 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16, QuantizationInfo(1.f / 32768.f, 0)),
119 TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16, QuantizationInfo(1.f / 32768.f, 0)),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000120 })),
121 framework::dataset::make("ActivationInfo", { ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
122 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
Giorgio Arena2995f5b2017-11-29 17:33:59 +0000123 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000124 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU),
Michele Di Giorgioa1f7e332018-01-22 17:26:36 +0000125 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000126 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
Manuel Bottini30dbeef2019-06-26 16:23:03 +0100127 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH),
128 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC),
129 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::SQRT),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000130 })),
Giorgio Arenad304adb2020-10-02 10:20:11 +0100131 framework::dataset::make("Expected", { false, true, true, true, false, false, true, true, false })),
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000132 input_info, output_info, act_info, expected)
133{
Giorgio Arena2995f5b2017-11-29 17:33:59 +0000134 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 +0000135}
Giorgio Arenad304adb2020-10-02 10:20:11 +0100136
Georgios Pinitasf9d3a0a2017-11-03 19:01:44 +0000137// clang-format on
138// *INDENT-ON*
139
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +0000140/** [CLActivationLayerFixture snippet] **/
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100141template <typename T>
142using CLActivationLayerFixture = ActivationValidationFixture<CLTensor, CLAccessor, CLActivationLayer, T>;
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +0000143/** [CLActivationLayerFixture snippet] **/
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100144
145TEST_SUITE(Float)
146TEST_SUITE(FP16)
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +0000147/** [CLActivationLayer Test snippet] **/
Sang-Hoon Parka8a7c1d2020-05-12 22:01:23 +0100148FIXTURE_DATA_TEST_CASE(RunSmall, CLActivationLayerFixture<half>, framework::DatasetMode::ALL, combine(combine(datasets::SmallShapes(), ActivationDataset),
149 framework::dataset::make("DataType",
150 DataType::F16)))
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100151{
152 // Validate output
153 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
154}
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +0000155/** [CLActivationLayer Test snippet] **/
Michalis Spyrou80943252019-01-10 17:19:50 +0000156TEST_SUITE_END() // FP16
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100157
158TEST_SUITE(FP32)
Sang-Hoon Parka8a7c1d2020-05-12 22:01:23 +0100159FIXTURE_DATA_TEST_CASE(RunSmall, CLActivationLayerFixture<float>, framework::DatasetMode::ALL, combine(combine(datasets::SmallShapes(), ActivationDataset), framework::dataset::make("DataType",
160 DataType::F32)))
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100161{
162 // Validate output
163 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
164}
Michalis Spyrou80943252019-01-10 17:19:50 +0000165TEST_SUITE_END() // FP32
166TEST_SUITE_END() // Float
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100167
168template <typename T>
Michel Iwaniec66cc12f2017-12-07 17:26:40 +0000169using CLActivationLayerQuantizedFixture = ActivationValidationQuantizedFixture<CLTensor, CLAccessor, CLActivationLayer, T>;
170
morgolock6b3865a2020-03-04 14:57:46 +0000171const auto QuantizedActivationDataset8 = combine(combine(framework::dataset::make("InPlace", { false }),
Sang-Hoon Parkadd8e812020-11-25 11:46:03 +0000172 concat(datasets::ActivationFunctionsQuantized(),
173 framework::dataset::make("ActivationFunction",
174{ ActivationLayerInfo::ActivationFunction::HARD_SWISH, ActivationLayerInfo::ActivationFunction::LEAKY_RELU }))),
175framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
morgolock6b3865a2020-03-04 14:57:46 +0000176
177const auto QuantizedActivationDataset16 = combine(combine(framework::dataset::make("InPlace", { false }),
178 datasets::ActivationFunctionsQuantized()),
179 framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
Michel Iwaniec66cc12f2017-12-07 17:26:40 +0000180
181TEST_SUITE(Quantized)
182TEST_SUITE(QASYMM8)
Sang-Hoon Parka8a7c1d2020-05-12 22:01:23 +0100183FIXTURE_DATA_TEST_CASE(RunSmall, CLActivationLayerQuantizedFixture<uint8_t>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallShapes(), QuantizedActivationDataset8),
184 framework::dataset::make("DataType",
185 DataType::QASYMM8)),
186 framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.1f, 128.0f) })))
Michel Iwaniec66cc12f2017-12-07 17:26:40 +0000187{
188 // Validate output
189 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
190}
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000191TEST_SUITE_END() // QASYMM8
192TEST_SUITE(QASYMM8_SIGNED)
Sang-Hoon Parka8a7c1d2020-05-12 22:01:23 +0100193FIXTURE_DATA_TEST_CASE(RunSmall, CLActivationLayerQuantizedFixture<int8_t>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallShapes(), QuantizedActivationDataset8),
194 framework::dataset::make("DataType",
195 DataType::QASYMM8_SIGNED)),
196 framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.1f, 10.0f) })))
Michel Iwaniec66cc12f2017-12-07 17:26:40 +0000197{
198 // Validate output
199 validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
200}
Michele Di Giorgiocbbed282019-12-20 13:26:08 +0000201TEST_SUITE_END() // QASYMM8_SIGNED
Manuel Bottini30dbeef2019-06-26 16:23:03 +0100202TEST_SUITE(QSYMM16)
Sang-Hoon Parka8a7c1d2020-05-12 22:01:23 +0100203FIXTURE_DATA_TEST_CASE(RunSmall, CLActivationLayerQuantizedFixture<int16_t>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallShapes(), QuantizedActivationDataset16),
204 framework::dataset::make("DataType",
205 DataType::QSYMM16)),
206 framework::dataset::make("QuantizationInfo", { QuantizationInfo(1.f / 32768.f, 0) })))
Manuel Bottini30dbeef2019-06-26 16:23:03 +0100207{
208 // Validate output
209 validate(CLAccessor(_target), _reference, tolerance_qsymm16);
210}
211TEST_SUITE_END() // QSYMM16
Michalis Spyrou80943252019-01-10 17:19:50 +0000212TEST_SUITE_END() // Quantized
Michel Iwaniec66cc12f2017-12-07 17:26:40 +0000213
Michalis Spyrou80943252019-01-10 17:19:50 +0000214TEST_SUITE_END() // ActivationLayer
215TEST_SUITE_END() // CL
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100216} // namespace validation
217} // namespace test
218} // namespace arm_compute