• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2017-2020 Arm Limited.
3  *
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"
28 #include "arm_compute/runtime/RuntimeContext.h"
29 #include "tests/CL/CLAccessor.h"
30 #include "tests/PaddingCalculator.h"
31 #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"
38 
39 namespace arm_compute
40 {
41 namespace test
42 {
43 namespace validation
44 {
45 namespace
46 {
47 constexpr AbsoluteTolerance<float> tolerance_qsymm16(1.f);
48 
49 /** 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  */
tolerance(ActivationLayerInfo::ActivationFunction activation,DataType data_type)56 AbsoluteTolerance<float> tolerance(ActivationLayerInfo::ActivationFunction activation, DataType data_type)
57 {
58     constexpr float epsilon = 1e-6f;
59 
60     switch(activation)
61     {
62         case ActivationLayerInfo::ActivationFunction::LINEAR:
63             return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.2f : epsilon);
64         case ActivationLayerInfo::ActivationFunction::SQUARE:
65             return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.1f : epsilon);
66         case ActivationLayerInfo::ActivationFunction::LOGISTIC:
67             return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.001f : epsilon);
68         case ActivationLayerInfo::ActivationFunction::LEAKY_RELU:
69             return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.00001f : epsilon);
70         case ActivationLayerInfo::ActivationFunction::SOFT_RELU:
71         case ActivationLayerInfo::ActivationFunction::ELU:
72         case ActivationLayerInfo::ActivationFunction::SQRT:
73             return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.01f : 0.00001f);
74         case ActivationLayerInfo::ActivationFunction::TANH:
75             return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.001f : 0.00001f);
76         case ActivationLayerInfo::ActivationFunction::HARD_SWISH:
77             return AbsoluteTolerance<float>(data_type == DataType::F16 ? 0.01f : epsilon);
78         default:
79             return AbsoluteTolerance<float>(epsilon);
80     }
81 }
82 
83 /** CNN data types */
84 const auto CNNDataTypes = framework::dataset::make("DataType",
85 {
86     DataType::F16,
87     DataType::F32
88 });
89 
90 /** Input data sets. */
91 const auto ActivationDataset = combine(combine(framework::dataset::make("InPlace", { false, true }), datasets::ActivationFunctions()), framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
92 
93 } // namespace
94 
95 TEST_SUITE(CL)
TEST_SUITE(ActivationLayer)96 TEST_SUITE(ActivationLayer)
97 // *INDENT-OFF*
98 // clang-format off
99 DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(
100                framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32),     // Mismatching data types
101                                                        TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32),
102                                                        TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
103                                                        TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QASYMM8),
104                                                        TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QASYMM8), // Invalid quantization info
105                                                        TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32),     // Mismatching shapes
106                                                        TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16),
107                                                        TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16),
108                                                        TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16), // Invalid activation function for QSYMM16
109                                                      }),
110                framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F16),
111                                                        TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32),
112                                                        TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32),
113                                                        TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QASYMM8),
114                                                        TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QASYMM8),
115                                                        TensorInfo(TensorShape(30U, 11U, 2U), 1, DataType::F32),
116                                                        TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16, QuantizationInfo(1.f / 32768.f, 0)),
117                                                        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                                                      })),
120                framework::dataset::make("ActivationInfo", { ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
121                                                             ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
122                                                             ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
123                                                             ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU),
124                                                             ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH),
125                                                             ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
126                                                             ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH),
127                                                             ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC),
128                                                             ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::SQRT),
129                                                           })),
130                framework::dataset::make("Expected", { false, true, true, true, false, false, true, true, false })),
131                input_info, output_info, act_info, expected)
132 {
133     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);
134 }
135 
136 // clang-format on
137 // *INDENT-ON*
138 
139 /** [CLActivationLayerFixture snippet] **/
140 template <typename T>
141 using CLActivationLayerFixture = ActivationValidationFixture<CLTensor, CLAccessor, CLActivationLayer, T>;
142 /** [CLActivationLayerFixture snippet] **/
143 
144 TEST_SUITE(Float)
TEST_SUITE(FP16)145 TEST_SUITE(FP16)
146 /** [CLActivationLayer Test snippet] **/
147 FIXTURE_DATA_TEST_CASE(RunSmall, CLActivationLayerFixture<half>, framework::DatasetMode::ALL, combine(combine(datasets::SmallShapes(), ActivationDataset),
148                                                                                                       framework::dataset::make("DataType",
149                                                                                                               DataType::F16)))
150 {
151     // Validate output
152     validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
153 }
154 /** [CLActivationLayer Test snippet] **/
155 TEST_SUITE_END() // FP16
156 
TEST_SUITE(FP32)157 TEST_SUITE(FP32)
158 FIXTURE_DATA_TEST_CASE(RunSmall, CLActivationLayerFixture<float>, framework::DatasetMode::ALL, combine(combine(datasets::SmallShapes(), ActivationDataset), framework::dataset::make("DataType",
159                                                                                                        DataType::F32)))
160 {
161     // Validate output
162     validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
163 }
164 TEST_SUITE_END() // FP32
165 TEST_SUITE_END() // Float
166 
167 template <typename T>
168 using CLActivationLayerQuantizedFixture = ActivationValidationQuantizedFixture<CLTensor, CLAccessor, CLActivationLayer, T>;
169 
170 const auto QuantizedActivationDataset8 = combine(combine(framework::dataset::make("InPlace", { false }),
171                                                          concat(datasets::ActivationFunctionsQuantized(), framework::dataset::make("ActivationFunction", ActivationLayerInfo::ActivationFunction::HARD_SWISH))),
172                                                  framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
173 
174 const auto QuantizedActivationDataset16 = combine(combine(framework::dataset::make("InPlace", { false }),
175                                                           datasets::ActivationFunctionsQuantized()),
176                                                   framework::dataset::make("AlphaBeta", { 0.5f, 1.f }));
177 
178 TEST_SUITE(Quantized)
TEST_SUITE(QASYMM8)179 TEST_SUITE(QASYMM8)
180 FIXTURE_DATA_TEST_CASE(RunSmall, CLActivationLayerQuantizedFixture<uint8_t>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallShapes(), QuantizedActivationDataset8),
181                                                                                                                   framework::dataset::make("DataType",
182                                                                                                                           DataType::QASYMM8)),
183                                                                                                                   framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.1f, 128.0f) })))
184 {
185     // Validate output
186     validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
187 }
188 TEST_SUITE_END() // QASYMM8
TEST_SUITE(QASYMM8_SIGNED)189 TEST_SUITE(QASYMM8_SIGNED)
190 FIXTURE_DATA_TEST_CASE(RunSmall, CLActivationLayerQuantizedFixture<int8_t>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallShapes(), QuantizedActivationDataset8),
191                                                                                                                  framework::dataset::make("DataType",
192                                                                                                                          DataType::QASYMM8_SIGNED)),
193                                                                                                                  framework::dataset::make("QuantizationInfo", { QuantizationInfo(0.1f, 10.0f) })))
194 {
195     // Validate output
196     validate(CLAccessor(_target), _reference, tolerance(_function, _data_type));
197 }
198 TEST_SUITE_END() // QASYMM8_SIGNED
TEST_SUITE(QSYMM16)199 TEST_SUITE(QSYMM16)
200 FIXTURE_DATA_TEST_CASE(RunSmall, CLActivationLayerQuantizedFixture<int16_t>, framework::DatasetMode::ALL, combine(combine(combine(datasets::SmallShapes(), QuantizedActivationDataset16),
201                                                                                                                   framework::dataset::make("DataType",
202                                                                                                                           DataType::QSYMM16)),
203                                                                                                                   framework::dataset::make("QuantizationInfo", { QuantizationInfo(1.f / 32768.f, 0) })))
204 {
205     // Validate output
206     validate(CLAccessor(_target), _reference, tolerance_qsymm16);
207 }
208 TEST_SUITE_END() // QSYMM16
209 TEST_SUITE_END() // Quantized
210 
211 TEST_SUITE_END() // ActivationLayer
212 TEST_SUITE_END() // CL
213 } // namespace validation
214 } // namespace test
215 } // namespace arm_compute
216