blob: 091d9411b71032f2f4de23cef5e8aded4dbd3512 [file] [log] [blame]
Moritz Pflanzer69d33412017-08-09 11:45:15 +01001/*
Michalis Spyrou80943252019-01-10 17:19:50 +00002 * Copyright (c) 2017-2019 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,
Moritz Pflanzer69d33412017-08-09 11:45:15 +010060});
61
62const auto FullyConnectedParameters = combine(framework::dataset::make("TransposeWeights", { false, true }), framework::dataset::make("ReshapeWeights", { false, true }));
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +010063
64const auto QuantizationData = framework::dataset::make("QuantizationInfo",
65{
66 QuantizationInfo(1.f / 255.f, 10),
67 QuantizationInfo(1.1f, 10),
68});
Moritz Pflanzer69d33412017-08-09 11:45:15 +010069} // namespace
70
71TEST_SUITE(CL)
72TEST_SUITE(FullyConnectedLayer)
73
Michalis Spyrou80943252019-01-10 17:19:50 +000074DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(combine(datasets::SmallFullyConnectedLayerDataset(),
Moritz Pflanzer69d33412017-08-09 11:45:15 +010075 FullyConnectedParameters),
76 CNNDataTypes),
77 src_shape, weights_shape, bias_shape, dst_shape, transpose_weights, reshape_weights, data_type)
78{
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010079 const DataType bias_data_type = is_data_type_quantized_asymmetric(data_type) ? DataType::S32 : data_type;
80 const QuantizationInfo quantization_info = is_data_type_quantized_asymmetric(data_type) ? QuantizationInfo(2.f / 255.f, 127) : QuantizationInfo();
Moritz Pflanzer69d33412017-08-09 11:45:15 +010081
82 TensorShape ws(weights_shape);
83
84 // Transpose weights if not done in the function
85 if(!reshape_weights || !transpose_weights)
86 {
87 const size_t shape_x = ws.x();
88 ws.set(0, ws.y());
89 ws.set(1, shape_x);
Moritz Pflanzer69d33412017-08-09 11:45:15 +010090 }
91
92 // Create tensors
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010093 CLTensor src = create_tensor<CLTensor>(src_shape, data_type, 1, quantization_info);
94 CLTensor weights = create_tensor<CLTensor>(ws, data_type, 1, quantization_info);
95 CLTensor bias = create_tensor<CLTensor>(bias_shape, bias_data_type, 1, quantization_info);
96 CLTensor dst = create_tensor<CLTensor>(dst_shape, data_type, 1, quantization_info);
Moritz Pflanzer69d33412017-08-09 11:45:15 +010097
98 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
99 ARM_COMPUTE_EXPECT(weights.info()->is_resizable(), framework::LogLevel::ERRORS);
100 ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS);
101 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
102
Chunosov5124be52017-11-22 20:42:13 +0700103 const QuantizationInfo src_quantization_info = src.info()->quantization_info();
104 const QuantizationInfo weights_quantization_info = weights.info()->quantization_info();
105
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100106 // Create Fully Connected layer info
107 FullyConnectedLayerInfo fc_info;
108 fc_info.transpose_weights = transpose_weights;
109 fc_info.are_weights_reshaped = !reshape_weights;
110
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100111 // Create and configure function.
112 CLFullyConnectedLayer fc;
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100113 fc.configure(&src, &weights, &bias, &dst, fc_info);
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100114
115 // Validate valid region
116 const ValidRegion dst_valid_region = shape_to_valid_region(dst_shape);
117 validate(dst.info()->valid_region(), dst_valid_region);
Chunosov5124be52017-11-22 20:42:13 +0700118
119 // Validate QuantizationInfo
120 ARM_COMPUTE_EXPECT(src.info()->quantization_info() == src_quantization_info, framework::LogLevel::ERRORS);
121 ARM_COMPUTE_EXPECT(weights.info()->quantization_info() == weights_quantization_info, framework::LogLevel::ERRORS);
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100122}
123
Georgios Pinitas358ca202017-12-07 16:47:52 +0000124// *INDENT-OFF*
125// clang-format off
126DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(zip(zip(zip(
127 framework::dataset::make("InputInfo", { TensorInfo(TensorShape(9U, 5U, 7U, 3U), 1, DataType::F32), // Mismatching data types
Georgios Pinitas358ca202017-12-07 16:47:52 +0000128 TensorInfo(TensorShape(8U, 4U, 6U, 4U), 1, DataType::F32),
129 TensorInfo(TensorShape(8U, 4U, 6U, 4U), 1, DataType::F32),
130 TensorInfo(TensorShape(9U, 5U, 7U, 3U), 1, DataType::F32), // Invalid weights dimensions
131 TensorInfo(TensorShape(9U, 5U, 7U, 3U), 1, DataType::F32), // Wrongly reshaped weights
Georgios Pinitas358ca202017-12-07 16:47:52 +0000132 }),
133 framework::dataset::make("WeightsInfo",{ TensorInfo(TensorShape(315U, 271U), 1, DataType::F16),
Georgios Pinitas358ca202017-12-07 16:47:52 +0000134 TensorInfo(TensorShape(192U, 192U), 1, DataType::F32),
135 TensorInfo(TensorShape(192U, 192U), 1, DataType::F32),
Michalis Spyroud466c2d2018-01-30 10:54:39 +0000136 TensorInfo(TensorShape(217U, 231U), 1, DataType::F32),
Georgios Pinitas358ca202017-12-07 16:47:52 +0000137 TensorInfo(TensorShape(217U, 315U), 1, DataType::F32),
Georgios Pinitas358ca202017-12-07 16:47:52 +0000138 })),
139 framework::dataset::make("BiasInfo",{ TensorInfo(TensorShape(271U), 1, DataType::F32),
Georgios Pinitas358ca202017-12-07 16:47:52 +0000140 TensorInfo(TensorShape(192U), 1, DataType::F32),
141 TensorInfo(TensorShape(192U), 1, DataType::F32),
142 TensorInfo(TensorShape(271U), 1, DataType::F32),
143 TensorInfo(TensorShape(271U), 1, DataType::F32),
Georgios Pinitas358ca202017-12-07 16:47:52 +0000144 })),
145 framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(271U, 3U), 1, DataType::F32),
Georgios Pinitas358ca202017-12-07 16:47:52 +0000146 TensorInfo(TensorShape(192U, 4U), 1, DataType::F32),
147 TensorInfo(TensorShape(192U, 4U), 1, DataType::F32),
148 TensorInfo(TensorShape(271U, 3U), 1, DataType::F32),
149 TensorInfo(TensorShape(271U, 3U), 1, DataType::F32),
Georgios Pinitas358ca202017-12-07 16:47:52 +0000150 })),
Vidhya Sudhan Loganathan0fc25452018-06-18 14:40:56 +0100151 framework::dataset::make("TransposeWeights",{ true, true, false, true, true })),
152 framework::dataset::make("ReshapedWeights",{ false, false, false, false, false})),
153 framework::dataset::make("Expected", { false, true, true, false, false })),
Georgios Pinitas358ca202017-12-07 16:47:52 +0000154 input_info, weights_info, bias_info, output_info, transpose_weights, reshaped_weights, expected)
155{
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100156 // Create Fully Connected layer info
157 FullyConnectedLayerInfo fc_info;
158 fc_info.transpose_weights = transpose_weights;
159 fc_info.are_weights_reshaped = reshaped_weights;
160
Georgios Pinitas358ca202017-12-07 16:47:52 +0000161 Status status = CLFullyConnectedLayer::validate(&input_info.clone()->set_is_resizable(false),
162 &weights_info.clone()->set_is_resizable(false),
163 &bias_info.clone()->set_is_resizable(false),
164 &output_info.clone()->set_is_resizable(false),
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100165 fc_info);
Georgios Pinitas358ca202017-12-07 16:47:52 +0000166 ARM_COMPUTE_EXPECT(bool(status) == expected, framework::LogLevel::ERRORS);
167}
168// clang-format on
169// *INDENT-ON*
170
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100171template <typename T>
Giorgio Arenaa855af12018-07-16 17:20:38 +0100172using CLFullyConnectedLayerFixture = FullyConnectedLayerValidationFixture<CLTensor, CLAccessor, CLFullyConnectedLayer, T>;
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100173
174TEST_SUITE(Float)
175TEST_SUITE(FP16)
Georgios Pinitas583137c2017-08-31 18:12:42 +0100176FIXTURE_DATA_TEST_CASE(RunSmall, CLFullyConnectedLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallFullyConnectedLayerDataset(),
177 FullyConnectedParameters),
178 framework::dataset::make("DataType", DataType::F16)))
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100179{
180 // Validate output
steniu013e05e4e2017-08-25 17:18:01 +0100181 validate(CLAccessor(_target), _reference, tolerance_f16, tolerance_num);
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100182}
Georgios Pinitas583137c2017-08-31 18:12:42 +0100183FIXTURE_DATA_TEST_CASE(RunLarge, CLFullyConnectedLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeFullyConnectedLayerDataset(),
184 FullyConnectedParameters),
185 framework::dataset::make("DataType", DataType::F16)))
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100186{
187 // Validate output
steniu013e05e4e2017-08-25 17:18:01 +0100188 validate(CLAccessor(_target), _reference, tolerance_f16, tolerance_num);
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100189}
190TEST_SUITE_END()
191
192TEST_SUITE(FP32)
193FIXTURE_DATA_TEST_CASE(RunSmall, CLFullyConnectedLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallFullyConnectedLayerDataset(), FullyConnectedParameters),
194 framework::dataset::make("DataType", DataType::F32)))
195{
196 // Validate output
Michele Di Giorgio419f33a2018-08-27 14:25:24 +0100197 validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0, abs_tolerance_f32);
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100198}
199FIXTURE_DATA_TEST_CASE(RunLarge, CLFullyConnectedLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeFullyConnectedLayerDataset(), FullyConnectedParameters),
200 framework::dataset::make("DataType", DataType::F32)))
201{
202 // Validate output
Michele Di Giorgio419f33a2018-08-27 14:25:24 +0100203 validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0, abs_tolerance_f32);
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100204}
205TEST_SUITE_END()
206TEST_SUITE_END()
207
208template <typename T>
Giorgio Arenaa855af12018-07-16 17:20:38 +0100209using CLFullyConnectedLayerQuantizedFixture = FullyConnectedLayerValidationQuantizedFixture<CLTensor, CLAccessor, CLFullyConnectedLayer, T>;
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000210
211TEST_SUITE(Quantized)
212TEST_SUITE(QASYMM8)
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100213FIXTURE_DATA_TEST_CASE(RunSmall, CLFullyConnectedLayerQuantizedFixture<uint8_t>, framework::DatasetMode::PRECOMMIT,
214 combine(combine(combine(datasets::SmallFullyConnectedLayerDataset(), FullyConnectedParameters), framework::dataset::make("DataType", DataType::QASYMM8)), QuantizationData))
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000215{
216 // Validate output
217 validate(CLAccessor(_target), _reference, tolerance_qasymm8);
218}
Michele Di Giorgio14cbfb22019-10-23 10:53:10 +0100219FIXTURE_DATA_TEST_CASE(RunLarge, CLFullyConnectedLayerQuantizedFixture<uint8_t>, framework::DatasetMode::NIGHTLY,
220 combine(combine(combine(datasets::LargeFullyConnectedLayerDataset(), FullyConnectedParameters), framework::dataset::make("DataType", DataType::QASYMM8)), QuantizationData))
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000221{
222 // Validate output
223 validate(CLAccessor(_target), _reference, tolerance_qasymm8);
224}
225TEST_SUITE_END()
226TEST_SUITE_END()
227
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100228TEST_SUITE_END()
229TEST_SUITE_END()
230} // namespace validation
231} // namespace test
232} // namespace arm_compute