blob: 78195a556ba55613dd8d526f8926757f76d58a6a [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/CL/CLTensor.h"
26#include "arm_compute/runtime/CL/CLTensorAllocator.h"
27#include "arm_compute/runtime/CL/functions/CLFullyConnectedLayer.h"
Moritz Pflanzer69d33412017-08-09 11:45:15 +010028#include "tests/CL/CLAccessor.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.05f); /**< Relative tolerance value for comparing reference's output against implementation's output for DataType:F32 */
47constexpr AbsoluteTolerance<float> abs_tolerance_f32(0.0001f); /**< Absolute tolerance value for comparing reference's output against implementation's output for DataType::F32 */
48RelativeTolerance<half_float::half> tolerance_f16(half(0.2)); /**< Relative tolerance value for comparing reference's output against implementation's output for DataType::F16 */
49constexpr float tolerance_num = 0.07f; /**< Tolerance number */
steniu013e05e4e2017-08-25 17:18:01 +010050
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +000051/** Tolerance for quantized asymmetric operations */
52constexpr AbsoluteTolerance<uint8_t> tolerance_qasymm8(1);
Moritz Pflanzer69d33412017-08-09 11:45:15 +010053
54/** CNN data types */
55const auto CNNDataTypes = framework::dataset::make("DataType",
56{
57 DataType::F16,
58 DataType::F32,
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +000059 DataType::QASYMM8,
Sang-Hoon Parkb66aa3b2020-01-10 14:44:13 +000060 DataType::QASYMM8_SIGNED,
Moritz Pflanzer69d33412017-08-09 11:45:15 +010061});
62
63const auto FullyConnectedParameters = combine(framework::dataset::make("TransposeWeights", { false, true }), framework::dataset::make("ReshapeWeights", { false, true }));
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +010064
65const auto QuantizationData = framework::dataset::make("QuantizationInfo",
66{
67 QuantizationInfo(1.f / 255.f, 10),
68 QuantizationInfo(1.1f, 10),
69});
Giorgio Arena1856ff72020-02-07 13:46:45 +000070
71const auto ActivationFunctionsDataset = framework::dataset::make("ActivationInfo",
72{
73 ActivationLayerInfo(),
74 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
75 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 0.5f),
76 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 0.75f, 0.25f),
77 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH)
78});
79
80const auto ActivationFunctionsQuantizedDataset = framework::dataset::make("ActivationInfo",
81{
82 ActivationLayerInfo(),
83 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
84 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 0.5f),
85 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 0.75f, 0.25f)
86});
Moritz Pflanzer69d33412017-08-09 11:45:15 +010087} // namespace
88
89TEST_SUITE(CL)
90TEST_SUITE(FullyConnectedLayer)
91
Georgios Pinitas358ca202017-12-07 16:47:52 +000092// *INDENT-OFF*
93// clang-format off
94DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(zip(zip(zip(
95 framework::dataset::make("InputInfo", { TensorInfo(TensorShape(9U, 5U, 7U, 3U), 1, DataType::F32), // Mismatching data types
Georgios Pinitas358ca202017-12-07 16:47:52 +000096 TensorInfo(TensorShape(8U, 4U, 6U, 4U), 1, DataType::F32),
97 TensorInfo(TensorShape(8U, 4U, 6U, 4U), 1, DataType::F32),
98 TensorInfo(TensorShape(9U, 5U, 7U, 3U), 1, DataType::F32), // Invalid weights dimensions
99 TensorInfo(TensorShape(9U, 5U, 7U, 3U), 1, DataType::F32), // Wrongly reshaped weights
Georgios Pinitas358ca202017-12-07 16:47:52 +0000100 }),
101 framework::dataset::make("WeightsInfo",{ TensorInfo(TensorShape(315U, 271U), 1, DataType::F16),
Georgios Pinitas358ca202017-12-07 16:47:52 +0000102 TensorInfo(TensorShape(192U, 192U), 1, DataType::F32),
103 TensorInfo(TensorShape(192U, 192U), 1, DataType::F32),
Michalis Spyroud466c2d2018-01-30 10:54:39 +0000104 TensorInfo(TensorShape(217U, 231U), 1, DataType::F32),
Georgios Pinitas358ca202017-12-07 16:47:52 +0000105 TensorInfo(TensorShape(217U, 315U), 1, DataType::F32),
Georgios Pinitas358ca202017-12-07 16:47:52 +0000106 })),
107 framework::dataset::make("BiasInfo",{ TensorInfo(TensorShape(271U), 1, DataType::F32),
Georgios Pinitas358ca202017-12-07 16:47:52 +0000108 TensorInfo(TensorShape(192U), 1, DataType::F32),
109 TensorInfo(TensorShape(192U), 1, DataType::F32),
110 TensorInfo(TensorShape(271U), 1, DataType::F32),
111 TensorInfo(TensorShape(271U), 1, DataType::F32),
Georgios Pinitas358ca202017-12-07 16:47:52 +0000112 })),
113 framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(271U, 3U), 1, DataType::F32),
Georgios Pinitas358ca202017-12-07 16:47:52 +0000114 TensorInfo(TensorShape(192U, 4U), 1, DataType::F32),
115 TensorInfo(TensorShape(192U, 4U), 1, DataType::F32),
116 TensorInfo(TensorShape(271U, 3U), 1, DataType::F32),
117 TensorInfo(TensorShape(271U, 3U), 1, DataType::F32),
Georgios Pinitas358ca202017-12-07 16:47:52 +0000118 })),
Vidhya Sudhan Loganathan0fc25452018-06-18 14:40:56 +0100119 framework::dataset::make("TransposeWeights",{ true, true, false, true, true })),
120 framework::dataset::make("ReshapedWeights",{ false, false, false, false, false})),
121 framework::dataset::make("Expected", { false, true, true, false, false })),
Georgios Pinitas358ca202017-12-07 16:47:52 +0000122 input_info, weights_info, bias_info, output_info, transpose_weights, reshaped_weights, expected)
123{
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100124 // Create Fully Connected layer info
125 FullyConnectedLayerInfo fc_info;
126 fc_info.transpose_weights = transpose_weights;
127 fc_info.are_weights_reshaped = reshaped_weights;
128
Georgios Pinitas358ca202017-12-07 16:47:52 +0000129 Status status = CLFullyConnectedLayer::validate(&input_info.clone()->set_is_resizable(false),
130 &weights_info.clone()->set_is_resizable(false),
131 &bias_info.clone()->set_is_resizable(false),
132 &output_info.clone()->set_is_resizable(false),
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100133 fc_info);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000134 ARM_COMPUTE_EXPECT(bool(status) == expected, framework::LogLevel::ERRORS);
135}
136// clang-format on
137// *INDENT-ON*
138
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100139template <typename T>
Giorgio Arenaa855af12018-07-16 17:20:38 +0100140using CLFullyConnectedLayerFixture = FullyConnectedLayerValidationFixture<CLTensor, CLAccessor, CLFullyConnectedLayer, T>;
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100141
142TEST_SUITE(Float)
143TEST_SUITE(FP16)
Giorgio Arena1856ff72020-02-07 13:46:45 +0000144FIXTURE_DATA_TEST_CASE(RunSmall, CLFullyConnectedLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallFullyConnectedLayerDataset(),
Georgios Pinitas583137c2017-08-31 18:12:42 +0100145 FullyConnectedParameters),
Giorgio Arena1856ff72020-02-07 13:46:45 +0000146 framework::dataset::make("DataType", DataType::F16)),
147 ActivationFunctionsDataset))
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100148{
149 // Validate output
steniu013e05e4e2017-08-25 17:18:01 +0100150 validate(CLAccessor(_target), _reference, tolerance_f16, tolerance_num);
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100151}
Giorgio Arena1856ff72020-02-07 13:46:45 +0000152FIXTURE_DATA_TEST_CASE(RunLarge, CLFullyConnectedLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeFullyConnectedLayerDataset(),
Georgios Pinitas583137c2017-08-31 18:12:42 +0100153 FullyConnectedParameters),
Giorgio Arena1856ff72020-02-07 13:46:45 +0000154 framework::dataset::make("DataType", DataType::F16)),
155 ActivationFunctionsDataset))
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100156{
157 // Validate output
steniu013e05e4e2017-08-25 17:18:01 +0100158 validate(CLAccessor(_target), _reference, tolerance_f16, tolerance_num);
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100159}
160TEST_SUITE_END()
161
162TEST_SUITE(FP32)
Giorgio Arena1856ff72020-02-07 13:46:45 +0000163FIXTURE_DATA_TEST_CASE(RunSmall, CLFullyConnectedLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallFullyConnectedLayerDataset(), FullyConnectedParameters),
164 framework::dataset::make("DataType", DataType::F32)),
165 ActivationFunctionsDataset))
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100166{
167 // Validate output
Michele Di Giorgio419f33a2018-08-27 14:25:24 +0100168 validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0, abs_tolerance_f32);
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100169}
Giorgio Arena1856ff72020-02-07 13:46:45 +0000170FIXTURE_DATA_TEST_CASE(RunLarge, CLFullyConnectedLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeFullyConnectedLayerDataset(), FullyConnectedParameters),
171 framework::dataset::make("DataType", DataType::F32)),
172 ActivationFunctionsDataset))
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100173{
174 // Validate output
Michele Di Giorgio419f33a2018-08-27 14:25:24 +0100175 validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0, abs_tolerance_f32);
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100176}
177TEST_SUITE_END()
178TEST_SUITE_END()
179
180template <typename T>
Giorgio Arenaa855af12018-07-16 17:20:38 +0100181using CLFullyConnectedLayerQuantizedFixture = FullyConnectedLayerValidationQuantizedFixture<CLTensor, CLAccessor, CLFullyConnectedLayer, T>;
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000182
183TEST_SUITE(Quantized)
184TEST_SUITE(QASYMM8)
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100185FIXTURE_DATA_TEST_CASE(RunSmall, CLFullyConnectedLayerQuantizedFixture<uint8_t>, framework::DatasetMode::PRECOMMIT,
Giorgio Arena1856ff72020-02-07 13:46:45 +0000186 combine(combine(combine(combine(datasets::SmallFullyConnectedLayerDataset(), FullyConnectedParameters), framework::dataset::make("DataType", DataType::QASYMM8)), QuantizationData),
187 ActivationFunctionsQuantizedDataset))
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000188{
189 // Validate output
190 validate(CLAccessor(_target), _reference, tolerance_qasymm8);
191}
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100192FIXTURE_DATA_TEST_CASE(RunLarge, CLFullyConnectedLayerQuantizedFixture<uint8_t>, framework::DatasetMode::NIGHTLY,
Giorgio Arena1856ff72020-02-07 13:46:45 +0000193 combine(combine(combine(combine(datasets::LargeFullyConnectedLayerDataset(), FullyConnectedParameters), framework::dataset::make("DataType", DataType::QASYMM8)), QuantizationData),
194 ActivationFunctionsQuantizedDataset))
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000195{
196 // Validate output
197 validate(CLAccessor(_target), _reference, tolerance_qasymm8);
198}
Sang-Hoon Parkb66aa3b2020-01-10 14:44:13 +0000199TEST_SUITE_END() /* QASYMM8 */
200TEST_SUITE(QASYMM8_SIGNED)
201FIXTURE_DATA_TEST_CASE(RunSmall, CLFullyConnectedLayerQuantizedFixture<int8_t>, framework::DatasetMode::PRECOMMIT,
Giorgio Arena1856ff72020-02-07 13:46:45 +0000202 combine(combine(combine(combine(datasets::SmallFullyConnectedLayerDataset(), FullyConnectedParameters), framework::dataset::make("DataType", DataType::QASYMM8_SIGNED)), QuantizationData),
203 ActivationFunctionsQuantizedDataset))
Sang-Hoon Parkb66aa3b2020-01-10 14:44:13 +0000204{
205 // Validate output
206 validate(CLAccessor(_target), _reference, tolerance_qasymm8);
207}
208TEST_SUITE_END() /* QASYMM8_SIGNED */
209TEST_SUITE_END() /* Quantized */
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000210
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100211TEST_SUITE_END()
212TEST_SUITE_END()
213} // namespace validation
214} // namespace test
215} // namespace arm_compute