1 /*
2 * Copyright (c) 2017-2021 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/CLFullyConnectedLayer.h"
28 #include "tests/CL/CLAccessor.h"
29 #include "tests/PaddingCalculator.h"
30 #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"
36
37 namespace arm_compute
38 {
39 namespace test
40 {
41 namespace validation
42 {
43 namespace
44 {
45 /** Tolerance for float operations */
46 constexpr RelativeTolerance<float> rel_tolerance_f32(0.05f); /**< Relative tolerance value for comparing reference's output against implementation's output for DataType:F32 */
47 constexpr AbsoluteTolerance<float> abs_tolerance_f32(0.0001f); /**< Absolute tolerance value for comparing reference's output against implementation's output for DataType::F32 */
48 RelativeTolerance<half_float::half> tolerance_f16(half(0.2)); /**< Relative tolerance value for comparing reference's output against implementation's output for DataType::F16 */
49 constexpr float tolerance_num = 0.07f; /**< Tolerance number */
50
51 /** Tolerance for quantized asymmetric operations */
52 constexpr AbsoluteTolerance<uint8_t> tolerance_qasymm8(1);
53
54 const auto FullyConnectedParameters = combine(framework::dataset::make("TransposeWeights", { false, true }), framework::dataset::make("ReshapeWeights", { false, true }));
55
56 const auto QuantizationData = framework::dataset::make("QuantizationInfo",
57 {
58 QuantizationInfo(1.f / 255.f, 10),
59 QuantizationInfo(1.1f, 10),
60 });
61
62 const auto ActivationFunctionsDataset = framework::dataset::make("ActivationInfo",
63 {
64 ActivationLayerInfo(),
65 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU),
66 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 0.5f),
67 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 0.75f, 0.25f),
68 ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH)
69 });
70
71 const auto ActivationFunctionsQuantizedDataset = concat(concat(concat(
72 framework::dataset::make("ActivationInfo", ActivationLayerInfo()),
73 framework::dataset::make("ActivationInfo", ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))),
74 framework::dataset::make("ActivationInfo", ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 0.5f))),
75 framework::dataset::make("ActivationInfo", ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 0.75f, 0.25f)));
76 } // namespace
77
78 TEST_SUITE(CL)
TEST_SUITE(FullyConnectedLayer)79 TEST_SUITE(FullyConnectedLayer)
80
81 // *INDENT-OFF*
82 // clang-format off
83 DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(zip(zip(zip(
84 framework::dataset::make("InputInfo", { TensorInfo(TensorShape(9U, 5U, 7U, 3U), 1, DataType::F32), // Mismatching data types
85 TensorInfo(TensorShape(8U, 4U, 6U, 4U), 1, DataType::F32),
86 TensorInfo(TensorShape(8U, 4U, 6U, 4U), 1, DataType::F32),
87 TensorInfo(TensorShape(9U, 5U, 7U, 3U), 1, DataType::F32), // Invalid weights dimensions
88 TensorInfo(TensorShape(9U, 5U, 7U, 3U), 1, DataType::F32), // Wrongly reshaped weights
89 }),
90 framework::dataset::make("WeightsInfo",{ TensorInfo(TensorShape(315U, 271U), 1, DataType::F16),
91 TensorInfo(TensorShape(192U, 192U), 1, DataType::F32),
92 TensorInfo(TensorShape(192U, 192U), 1, DataType::F32),
93 TensorInfo(TensorShape(217U, 231U), 1, DataType::F32),
94 TensorInfo(TensorShape(217U, 315U), 1, DataType::F32),
95 })),
96 framework::dataset::make("BiasInfo",{ TensorInfo(TensorShape(271U), 1, DataType::F32),
97 TensorInfo(TensorShape(192U), 1, DataType::F32),
98 TensorInfo(TensorShape(192U), 1, DataType::F32),
99 TensorInfo(TensorShape(271U), 1, DataType::F32),
100 TensorInfo(TensorShape(271U), 1, DataType::F32),
101 })),
102 framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(271U, 3U), 1, DataType::F32),
103 TensorInfo(TensorShape(192U, 4U), 1, DataType::F32),
104 TensorInfo(TensorShape(192U, 4U), 1, DataType::F32),
105 TensorInfo(TensorShape(271U, 3U), 1, DataType::F32),
106 TensorInfo(TensorShape(271U, 3U), 1, DataType::F32),
107 })),
108 framework::dataset::make("TransposeWeights",{ true, true, false, true, true })),
109 framework::dataset::make("ReshapedWeights",{ false, false, false, false, false})),
110 framework::dataset::make("Expected", { false, true, true, false, false })),
111 input_info, weights_info, bias_info, output_info, transpose_weights, reshaped_weights, expected)
112 {
113 // Create Fully Connected layer info
114 FullyConnectedLayerInfo fc_info;
115 fc_info.transpose_weights = transpose_weights;
116 fc_info.are_weights_reshaped = reshaped_weights;
117
118 Status status = CLFullyConnectedLayer::validate(&input_info.clone()->set_is_resizable(false),
119 &weights_info.clone()->set_is_resizable(false),
120 &bias_info.clone()->set_is_resizable(false),
121 &output_info.clone()->set_is_resizable(false),
122 fc_info);
123 ARM_COMPUTE_EXPECT(bool(status) == expected, framework::LogLevel::ERRORS);
124 }
125 // clang-format on
126 // *INDENT-ON*
127
128 template <typename T>
129 using CLFullyConnectedLayerFixture = FullyConnectedLayerValidationFixture<CLTensor, CLAccessor, CLFullyConnectedLayer, T>;
130 template <typename T>
131 using CLFullyConnectedLayerMixedDataLayoutFixture = FullyConnectedLayerValidationFixture<CLTensor, CLAccessor, CLFullyConnectedLayer, T, true>;
132 template <typename T>
133 using CLFullyConnectedLayerDynamicWeightsFixture = FullyConnectedWithDynamicWeightsFixture<CLTensor, CLAccessor, CLFullyConnectedLayer, T>;
134
135 TEST_SUITE(Float)
TEST_SUITE(FP16)136 TEST_SUITE(FP16)
137 FIXTURE_DATA_TEST_CASE(RunSmall, CLFullyConnectedLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallFullyConnectedLayerDataset(),
138 FullyConnectedParameters),
139 framework::dataset::make("DataType", DataType::F16)),
140 ActivationFunctionsDataset))
141 {
142 // Validate output
143 validate(CLAccessor(_target), _reference, tolerance_f16, tolerance_num);
144 }
145 FIXTURE_DATA_TEST_CASE(RunLarge, CLFullyConnectedLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeFullyConnectedLayerDataset(),
146 FullyConnectedParameters),
147 framework::dataset::make("DataType", DataType::F16)),
148 ActivationFunctionsDataset))
149 {
150 // Validate output
151 validate(CLAccessor(_target), _reference, tolerance_f16, tolerance_num);
152 }
153 TEST_SUITE_END()
154
TEST_SUITE(FP32)155 TEST_SUITE(FP32)
156 FIXTURE_DATA_TEST_CASE(RunSmall, CLFullyConnectedLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallFullyConnectedLayerDataset(), FullyConnectedParameters),
157 framework::dataset::make("DataType", DataType::F32)),
158 ActivationFunctionsDataset))
159 {
160 // Validate output
161 validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0, abs_tolerance_f32);
162 }
163 FIXTURE_DATA_TEST_CASE(RunMixedDataLayout, CLFullyConnectedLayerMixedDataLayoutFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(combine(combine(combine(
164 framework::dataset::make("Input", TensorShape(9U, 5U, 7U)),
165 framework::dataset::make("Weights", TensorShape(315U, 271U))),
166 framework::dataset::make("Biases", TensorShape(271U))),
167 framework::dataset::make("Output", TensorShape(271U))),
168 FullyConnectedParameters),
169 framework::dataset::make("DataType", DataType::F32)),
170 framework::dataset::make("ActivationInfo", ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))))
171 {
172 // Validate output
173 validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0, abs_tolerance_f32);
174 }
175 FIXTURE_DATA_TEST_CASE(RunDynamicWeights, CLFullyConnectedLayerDynamicWeightsFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallFullyConnectedLayerDataset(),
176 framework::dataset::make("DataType", DataType::F32)),
177 framework::dataset::make("ActivationInfo", ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))))
178 {
179 }
180 FIXTURE_DATA_TEST_CASE(RunLarge, CLFullyConnectedLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeFullyConnectedLayerDataset(), FullyConnectedParameters),
181 framework::dataset::make("DataType", DataType::F32)),
182 ActivationFunctionsDataset))
183 {
184 // Validate output
185 validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0, abs_tolerance_f32);
186 }
187 TEST_SUITE_END()
188 TEST_SUITE_END()
189
190 template <typename T>
191 using CLFullyConnectedLayerQuantizedFixture = FullyConnectedLayerValidationQuantizedFixture<CLTensor, CLAccessor, CLFullyConnectedLayer, T>;
192 template <typename T>
193 using CLFullyConnectedLayerQuantizedMixedDataLayoutFixture = FullyConnectedLayerValidationQuantizedFixture<CLTensor, CLAccessor, CLFullyConnectedLayer, T, true>;
194
195 TEST_SUITE(Quantized)
TEST_SUITE(QASYMM8)196 TEST_SUITE(QASYMM8)
197 FIXTURE_DATA_TEST_CASE(RunSmall, CLFullyConnectedLayerQuantizedFixture<uint8_t>, framework::DatasetMode::PRECOMMIT,
198 combine(combine(combine(combine(datasets::SmallFullyConnectedLayerDataset(), FullyConnectedParameters), framework::dataset::make("DataType", DataType::QASYMM8)), QuantizationData),
199 ActivationFunctionsQuantizedDataset))
200 {
201 // Validate output
202 validate(CLAccessor(_target), _reference, tolerance_qasymm8);
203 }
204 FIXTURE_DATA_TEST_CASE(RunMixedDataLayout, CLFullyConnectedLayerQuantizedMixedDataLayoutFixture<uint8_t>, framework::DatasetMode::PRECOMMIT,
205 combine(combine(combine(combine(combine(combine(combine(
206 framework::dataset::make("Input", TensorShape(9U, 5U, 7U)),
207 framework::dataset::make("Weights", TensorShape(315U, 271U))),
208 framework::dataset::make("Biases", TensorShape(271U))),
209 framework::dataset::make("Output", TensorShape(271U))),
210 FullyConnectedParameters),
211 framework::dataset::make("DataType", DataType::QASYMM8)),
212 QuantizationData),
213 framework::dataset::make("ActivationInfo", ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))))
214 {
215 // Validate output
216 validate(CLAccessor(_target), _reference, tolerance_qasymm8);
217 }
218 FIXTURE_DATA_TEST_CASE(RunLarge, CLFullyConnectedLayerQuantizedFixture<uint8_t>, framework::DatasetMode::NIGHTLY,
219 combine(combine(combine(combine(datasets::LargeFullyConnectedLayerDataset(), FullyConnectedParameters), framework::dataset::make("DataType", DataType::QASYMM8)), QuantizationData),
220 ActivationFunctionsQuantizedDataset))
221 {
222 // Validate output
223 validate(CLAccessor(_target), _reference, tolerance_qasymm8);
224 }
225 TEST_SUITE_END() /* QASYMM8 */
TEST_SUITE(QASYMM8_SIGNED)226 TEST_SUITE(QASYMM8_SIGNED)
227 FIXTURE_DATA_TEST_CASE(RunSmall, CLFullyConnectedLayerQuantizedFixture<int8_t>, framework::DatasetMode::PRECOMMIT,
228 combine(combine(combine(combine(datasets::SmallFullyConnectedLayerDataset(), FullyConnectedParameters), framework::dataset::make("DataType", DataType::QASYMM8_SIGNED)), QuantizationData),
229 ActivationFunctionsQuantizedDataset))
230 {
231 // Validate output
232 validate(CLAccessor(_target), _reference, tolerance_qasymm8);
233 }
234 FIXTURE_DATA_TEST_CASE(RunMixedDataLayout, CLFullyConnectedLayerQuantizedMixedDataLayoutFixture<int8_t>, framework::DatasetMode::PRECOMMIT,
235 combine(combine(combine(combine(combine(combine(combine(
236 framework::dataset::make("Input", TensorShape(9U, 5U, 7U)),
237 framework::dataset::make("Weights", TensorShape(315U, 271U))),
238 framework::dataset::make("Biases", TensorShape(271U))),
239 framework::dataset::make("Output", TensorShape(271U))),
240 FullyConnectedParameters),
241 framework::dataset::make("DataType", DataType::QASYMM8_SIGNED)),
242 QuantizationData),
243 framework::dataset::make("ActivationInfo", ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))))
244 {
245 // Validate output
246 validate(CLAccessor(_target), _reference, tolerance_qasymm8);
247 }
248 TEST_SUITE_END() // QASYMM8_SIGNED
249 TEST_SUITE_END() // Quantized
250 TEST_SUITE_END() // FullyConnectedLayer
251 TEST_SUITE_END() // CL
252 } // namespace validation
253 } // namespace test
254 } // namespace arm_compute
255