blob: d8c22038024fdfc84cdd4bbae188934734b3c38e [file] [log] [blame]
Moritz Pflanzer69d33412017-08-09 11:45:15 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Moritz Pflanzer69d33412017-08-09 11:45:15 +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/NEON/functions/NEFullyConnectedLayer.h"
26#include "arm_compute/runtime/Tensor.h"
27#include "arm_compute/runtime/TensorAllocator.h"
Moritz Pflanzer69d33412017-08-09 11:45:15 +010028#include "tests/NEON/Accessor.h"
29#include "tests/PaddingCalculator.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010030#include "tests/datasets/FullyConnectedLayerDataset.h"
31#include "tests/framework/Asserts.h"
32#include "tests/framework/Macros.h"
33#include "tests/framework/datasets/Datasets.h"
34#include "tests/validation/Validation.h"
35#include "tests/validation/fixtures/FullyConnectedLayerFixture.h"
Moritz Pflanzer69d33412017-08-09 11:45:15 +010036
37namespace arm_compute
38{
39namespace test
40{
41namespace validation
42{
43namespace
44{
45/** Tolerance for float operations */
Michele Di Giorgio419f33a2018-08-27 14:25:24 +010046constexpr RelativeTolerance<float> rel_tolerance_f32(0.01f); /**< Relative tolerance value for comparing reference's output against implementation's output for DataType::F32 */
47constexpr AbsoluteTolerance<float> abs_tolerance_f32(0.001f); /**< Absolute tolerance value for comparing reference's output against implementation's output for DataType::F32 */
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +000048#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Gian Marco Iodice35aea372018-08-24 14:30:36 +010049const AbsoluteTolerance<float> abs_tolerance_f16(0.3f); /**< Absolute tolerance value for comparing reference's output against implementation's output for DataType::F16 */
50const RelativeTolerance<half_float::half> rel_tolerance_f16(half_float::half(0.2f)); /**< Relative tolerance value for comparing reference's output against implementation's output for DataType::F16 */
51constexpr float tolerance_num_f16 = 0.07f; /**< Tolerance number for FP16 */
52#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC*/
Moritz Pflanzer69d33412017-08-09 11:45:15 +010053
Giorgio Arenaa855af12018-07-16 17:20:38 +010054/** Tolerance for quantized asymmetric operations */
55constexpr AbsoluteTolerance<uint8_t> tolerance_qasymm8(1);
Michele Di Giorgio9c700372020-01-08 11:33:44 +000056constexpr AbsoluteTolerance<int8_t> tolerance_qasymm8_signed(1);
Giorgio Arenaa855af12018-07-16 17:20:38 +010057
Moritz Pflanzer69d33412017-08-09 11:45:15 +010058/** CNN data types */
59const auto CNNDataTypes = framework::dataset::make("DataType",
60{
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +000061#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Moritz Pflanzer69d33412017-08-09 11:45:15 +010062 DataType::F16,
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +000063#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Moritz Pflanzer69d33412017-08-09 11:45:15 +010064 DataType::F32,
Moritz Pflanzer69d33412017-08-09 11:45:15 +010065});
66
67const auto FullyConnectedParameters = combine(framework::dataset::make("TransposeWeights", { false, true }), framework::dataset::make("ReshapeWeights", { false, true }));
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +000068
69const auto QuantizationData = framework::dataset::make("QuantizationInfo",
70{
71 QuantizationInfo(1.f / 256.f, 10),
72 QuantizationInfo(1.1f, 10),
73});
SiCongLi2e5fd632020-03-02 15:39:15 +000074const auto EmptyActivationFunctionDataset = framework::dataset::make("ActivationInfo",
75{
76 ActivationLayerInfo(),
77});
78const auto ActivationFunctionsDataset = framework::dataset::make("ActivationInfo",
79{
80 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
81 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 0.5f),
82 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 0.75f, 0.25f),
83 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH),
84});
Giorgio Arena1856ff72020-02-07 13:46:45 +000085
SiCongLi2e5fd632020-03-02 15:39:15 +000086const auto ActivationFunctionsQuantizedDataset = framework::dataset::make("ActivationInfo",
87{
88 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
89 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 0.5f),
90 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 0.75f, 0.25f),
91});
Moritz Pflanzer69d33412017-08-09 11:45:15 +010092} // namespace
93
94TEST_SUITE(NEON)
95TEST_SUITE(FullyConnectedLayer)
96
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000097// *INDENT-OFF*
98// clang-format off
99DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(zip(zip(zip(
100 framework::dataset::make("InputInfo", { TensorInfo(TensorShape(9U, 5U, 7U, 3U), 1, DataType::F32), // Mismatching data types
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000101 TensorInfo(TensorShape(8U, 4U, 6U, 4U), 1, DataType::F32),
102 TensorInfo(TensorShape(8U, 4U, 6U, 4U), 1, DataType::F32),
103 TensorInfo(TensorShape(9U, 5U, 7U, 3U), 1, DataType::F32), // Invalid weights dimensions
104 TensorInfo(TensorShape(9U, 5U, 7U, 3U), 1, DataType::F32), // Wrongly reshaped weights
105 TensorInfo(TensorShape(8U, 4U, 6U, 4U), 1, DataType::F32),
106 }),
107 framework::dataset::make("WeightsInfo",{ TensorInfo(TensorShape(315U, 271U), 1, DataType::F16),
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000108 TensorInfo(TensorShape(192U, 192U), 1, DataType::F32),
109 TensorInfo(TensorShape(192U, 192U), 1, DataType::F32),
110 TensorInfo(TensorShape(217U, 315U), 1, DataType::F32),
111 TensorInfo(TensorShape(217U, 315U), 1, DataType::F32),
112 TensorInfo(TensorShape(192U, 192U), 1, DataType::F32),
113 })),
114 framework::dataset::make("BiasInfo",{ TensorInfo(TensorShape(271U), 1, DataType::F32),
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000115 TensorInfo(TensorShape(192U), 1, DataType::F32),
116 TensorInfo(TensorShape(192U), 1, DataType::F32),
117 TensorInfo(TensorShape(271U), 1, DataType::F32),
118 TensorInfo(TensorShape(271U), 1, DataType::F32),
119 TensorInfo(TensorShape(192U), 1, DataType::F32),
120 })),
121 framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(271U, 3U), 1, DataType::F32),
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000122 TensorInfo(TensorShape(192U, 4U), 1, DataType::F32),
123 TensorInfo(TensorShape(192U, 4U), 1, DataType::F32),
124 TensorInfo(TensorShape(271U, 3U), 1, DataType::F32),
125 TensorInfo(TensorShape(271U, 3U), 1, DataType::F32),
126 TensorInfo(TensorShape(192U, 4U), 1, DataType::F32),
127 })),
Vidhya Sudhan Loganathan0fc25452018-06-18 14:40:56 +0100128 framework::dataset::make("TransposeWeights",{ true, true, false, true, true, true })),
129 framework::dataset::make("ReshapedWeights",{ false, false, false, false, false , false})),
130 framework::dataset::make("Expected", { false, true, true, false, false, true })),
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000131 input_info, weights_info, bias_info, output_info, transpose_weights, reshaped_weights, expected)
132{
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100133 // Create Fully Connected layer info
134 FullyConnectedLayerInfo fc_info;
135 fc_info.transpose_weights = transpose_weights;
136 fc_info.are_weights_reshaped = reshaped_weights;
137
138 Status status = NEFullyConnectedLayer::validate(&input_info.clone()->set_is_resizable(false), &weights_info.clone()->set_is_resizable(false), &bias_info.clone()->set_is_resizable(false), &output_info.clone()->set_is_resizable(false), fc_info);
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000139 ARM_COMPUTE_EXPECT(bool(status) == expected, framework::LogLevel::ERRORS);
140}
141// clang-format on
142// *INDENT-ON*
143
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100144template <typename T>
Giorgio Arenaa855af12018-07-16 17:20:38 +0100145using NEFullyConnectedLayerFixture = FullyConnectedLayerValidationFixture<Tensor, Accessor, NEFullyConnectedLayer, T>;
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100146
147TEST_SUITE(Float)
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000148#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100149TEST_SUITE(FP16)
Giorgio Arena1856ff72020-02-07 13:46:45 +0000150FIXTURE_DATA_TEST_CASE(RunSmall, NEFullyConnectedLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallFullyConnectedLayerDataset(),
Georgios Pinitas583137c2017-08-31 18:12:42 +0100151 FullyConnectedParameters),
Giorgio Arena1856ff72020-02-07 13:46:45 +0000152 framework::dataset::make("DataType", DataType::F16)),
SiCongLi2e5fd632020-03-02 15:39:15 +0000153 EmptyActivationFunctionDataset))
154{
155 // Validate output
156 validate(Accessor(_target), _reference, rel_tolerance_f16, tolerance_num_f16, abs_tolerance_f16);
157}
158FIXTURE_DATA_TEST_CASE(RunWithActivation, NEFullyConnectedLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(
159 combine(datasets::FullyConnectedLayerWithActivationDataset(),
160 FullyConnectedParameters),
161 framework::dataset::make("DataType", DataType::F16)),
162 ActivationFunctionsDataset))
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100163{
164 // Validate output
Gian Marco Iodice35aea372018-08-24 14:30:36 +0100165 validate(Accessor(_target), _reference, rel_tolerance_f16, tolerance_num_f16, abs_tolerance_f16);
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100166}
Giorgio Arena1856ff72020-02-07 13:46:45 +0000167FIXTURE_DATA_TEST_CASE(RunLarge, NEFullyConnectedLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeFullyConnectedLayerDataset(),
Georgios Pinitas583137c2017-08-31 18:12:42 +0100168 FullyConnectedParameters),
Giorgio Arena1856ff72020-02-07 13:46:45 +0000169 framework::dataset::make("DataType", DataType::F16)),
SiCongLi2e5fd632020-03-02 15:39:15 +0000170 EmptyActivationFunctionDataset))
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100171{
172 // Validate output
Gian Marco Iodice35aea372018-08-24 14:30:36 +0100173 validate(Accessor(_target), _reference, rel_tolerance_f16, tolerance_num_f16, abs_tolerance_f16);
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100174}
175TEST_SUITE_END()
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000176#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100177
178TEST_SUITE(FP32)
Giorgio Arena1856ff72020-02-07 13:46:45 +0000179FIXTURE_DATA_TEST_CASE(RunSmall, NEFullyConnectedLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallFullyConnectedLayerDataset(), FullyConnectedParameters),
180 framework::dataset::make("DataType", DataType::F32)),
SiCongLi2e5fd632020-03-02 15:39:15 +0000181 EmptyActivationFunctionDataset))
182{
183 // Validate output
184 validate(Accessor(_target), _reference, rel_tolerance_f32, 0, abs_tolerance_f32);
185}
186FIXTURE_DATA_TEST_CASE(RunWithActivation, NEFullyConnectedLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(
187 combine(datasets::FullyConnectedLayerWithActivationDataset(),
188 FullyConnectedParameters),
189 framework::dataset::make("DataType", DataType::F32)),
190 ActivationFunctionsDataset))
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100191{
192 // Validate output
Michele Di Giorgio419f33a2018-08-27 14:25:24 +0100193 validate(Accessor(_target), _reference, rel_tolerance_f32, 0, abs_tolerance_f32);
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100194}
Giorgio Arena1856ff72020-02-07 13:46:45 +0000195FIXTURE_DATA_TEST_CASE(RunLarge, NEFullyConnectedLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeFullyConnectedLayerDataset(), FullyConnectedParameters),
196 framework::dataset::make("DataType", DataType::F32)),
SiCongLi2e5fd632020-03-02 15:39:15 +0000197 EmptyActivationFunctionDataset))
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100198{
199 // Validate output
Michele Di Giorgio419f33a2018-08-27 14:25:24 +0100200 validate(Accessor(_target), _reference, rel_tolerance_f32, 0, abs_tolerance_f32);
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100201}
202TEST_SUITE_END()
203TEST_SUITE_END()
204
Giorgio Arenaa855af12018-07-16 17:20:38 +0100205template <typename T>
206using NEFullyConnectedLayerQuantizedFixture = FullyConnectedLayerValidationQuantizedFixture<Tensor, Accessor, NEFullyConnectedLayer, T>;
207
208TEST_SUITE(Quantized)
209TEST_SUITE(QASYMM8)
Giorgio Arena1856ff72020-02-07 13:46:45 +0000210FIXTURE_DATA_TEST_CASE(RunSmall, NEFullyConnectedLayerQuantizedFixture<uint8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(
Giorgio Arenaa855af12018-07-16 17:20:38 +0100211 combine(datasets::SmallFullyConnectedLayerDataset(),
212 FullyConnectedParameters),
213 framework::dataset::make("DataType", DataType::QASYMM8)),
Giorgio Arena1856ff72020-02-07 13:46:45 +0000214 QuantizationData),
SiCongLi2e5fd632020-03-02 15:39:15 +0000215 EmptyActivationFunctionDataset))
Giorgio Arenaa855af12018-07-16 17:20:38 +0100216{
217 // Validate output
218 validate(Accessor(_target), _reference, tolerance_qasymm8);
219}
SiCongLi2e5fd632020-03-02 15:39:15 +0000220
221FIXTURE_DATA_TEST_CASE(RunWithActivation, NEFullyConnectedLayerQuantizedFixture<uint8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(
222 combine(datasets::FullyConnectedLayerWithActivationDataset(),
223 FullyConnectedParameters),
224 framework::dataset::make("DataType", DataType::QASYMM8)),
225 QuantizationData),
226 ActivationFunctionsQuantizedDataset))
227{
228 // Validate output
229 validate(Accessor(_target), _reference, tolerance_qasymm8);
230}
231
Giorgio Arena1856ff72020-02-07 13:46:45 +0000232FIXTURE_DATA_TEST_CASE(RunLarge, NEFullyConnectedLayerQuantizedFixture<uint8_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(
Giorgio Arenaa855af12018-07-16 17:20:38 +0100233 combine(datasets::LargeFullyConnectedLayerDataset(),
234 FullyConnectedParameters),
235 framework::dataset::make("DataType", DataType::QASYMM8)),
Giorgio Arena1856ff72020-02-07 13:46:45 +0000236 QuantizationData),
SiCongLi2e5fd632020-03-02 15:39:15 +0000237 EmptyActivationFunctionDataset))
Giorgio Arenaa855af12018-07-16 17:20:38 +0100238{
239 // Validate output
240 validate(Accessor(_target), _reference, tolerance_qasymm8);
241}
242TEST_SUITE_END()
Michele Di Giorgio9c700372020-01-08 11:33:44 +0000243TEST_SUITE(QASYMM8_SIGNED)
Giorgio Arena1856ff72020-02-07 13:46:45 +0000244FIXTURE_DATA_TEST_CASE(RunSmall, NEFullyConnectedLayerQuantizedFixture<int8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(
Michele Di Giorgio9c700372020-01-08 11:33:44 +0000245 combine(datasets::SmallFullyConnectedLayerDataset(),
246 FullyConnectedParameters),
247 framework::dataset::make("DataType", DataType::QASYMM8_SIGNED)),
Giorgio Arena1856ff72020-02-07 13:46:45 +0000248 QuantizationData),
SiCongLi2e5fd632020-03-02 15:39:15 +0000249 EmptyActivationFunctionDataset))
250{
251 // Validate output
252 validate(Accessor(_target), _reference, tolerance_qasymm8_signed);
253}
254
255FIXTURE_DATA_TEST_CASE(RunWithActivation, NEFullyConnectedLayerQuantizedFixture<int8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(
256 combine(datasets::FullyConnectedLayerWithActivationDataset(),
257 FullyConnectedParameters),
258 framework::dataset::make("DataType", DataType::QASYMM8_SIGNED)),
259 QuantizationData),
260 ActivationFunctionsQuantizedDataset))
Michele Di Giorgio9c700372020-01-08 11:33:44 +0000261{
262 // Validate output
263 validate(Accessor(_target), _reference, tolerance_qasymm8_signed);
264}
265TEST_SUITE_END()
Giorgio Arenaa855af12018-07-16 17:20:38 +0100266TEST_SUITE_END()
267
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100268TEST_SUITE_END()
269TEST_SUITE_END()
270} // namespace validation
271} // namespace test
272} // namespace arm_compute