blob: 069d8a73ac8a9286763247c948cf053901ec3b63 [file] [log] [blame]
Moritz Pflanzer69d33412017-08-09 11:45:15 +01001/*
Michalis Spyroud466c2d2018-01-30 10:54:39 +00002 * Copyright (c) 2017-2018 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 */
steniu01f81652d2017-09-11 15:29:12 +010046RelativeTolerance<float> tolerance_f32(0.05f);
47RelativeTolerance<half_float::half> tolerance_f16(half(0.2));
48constexpr float tolerance_num = 0.07f; /**< Tolerance number */
steniu013e05e4e2017-08-25 17:18:01 +010049
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +000050/** Tolerance for quantized asymmetric operations */
51constexpr AbsoluteTolerance<uint8_t> tolerance_qasymm8(1);
Moritz Pflanzer69d33412017-08-09 11:45:15 +010052
53/** CNN data types */
54const auto CNNDataTypes = framework::dataset::make("DataType",
55{
56 DataType::F16,
57 DataType::F32,
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +000058 DataType::QASYMM8,
Moritz Pflanzer69d33412017-08-09 11:45:15 +010059});
60
61const auto FullyConnectedParameters = combine(framework::dataset::make("TransposeWeights", { false, true }), framework::dataset::make("ReshapeWeights", { false, true }));
62} // namespace
63
64TEST_SUITE(CL)
65TEST_SUITE(FullyConnectedLayer)
66
67DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(combine(framework::dataset::concat(datasets::SmallFullyConnectedLayerDataset(), datasets::LargeFullyConnectedLayerDataset()),
68 FullyConnectedParameters),
69 CNNDataTypes),
70 src_shape, weights_shape, bias_shape, dst_shape, transpose_weights, reshape_weights, data_type)
71{
72 // Set fixed point position data type allowed
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +000073 const int fixed_point_position = is_data_type_fixed_point(data_type) ? 3 : 0;
74 const DataType bias_data_type = is_data_type_quantized_asymmetric(data_type) ? DataType::S32 : data_type;
75 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 +010076
77 TensorShape ws(weights_shape);
78
79 // Transpose weights if not done in the function
80 if(!reshape_weights || !transpose_weights)
81 {
82 const size_t shape_x = ws.x();
83 ws.set(0, ws.y());
84 ws.set(1, shape_x);
Moritz Pflanzer69d33412017-08-09 11:45:15 +010085 }
86
87 // Create tensors
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +000088 CLTensor src = create_tensor<CLTensor>(src_shape, data_type, 1, fixed_point_position, quantization_info);
89 CLTensor weights = create_tensor<CLTensor>(ws, data_type, 1, fixed_point_position, quantization_info);
90 CLTensor bias = create_tensor<CLTensor>(bias_shape, bias_data_type, 1, fixed_point_position, quantization_info);
91 CLTensor dst = create_tensor<CLTensor>(dst_shape, data_type, 1, fixed_point_position, quantization_info);
Moritz Pflanzer69d33412017-08-09 11:45:15 +010092
93 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
94 ARM_COMPUTE_EXPECT(weights.info()->is_resizable(), framework::LogLevel::ERRORS);
95 ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS);
96 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
97
Chunosov5124be52017-11-22 20:42:13 +070098 const QuantizationInfo src_quantization_info = src.info()->quantization_info();
99 const QuantizationInfo weights_quantization_info = weights.info()->quantization_info();
100
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100101 // Create and configure function.
102 CLFullyConnectedLayer fc;
103 fc.configure(&src, &weights, &bias, &dst, transpose_weights, !reshape_weights);
104
105 // Validate valid region
106 const ValidRegion dst_valid_region = shape_to_valid_region(dst_shape);
107 validate(dst.info()->valid_region(), dst_valid_region);
Chunosov5124be52017-11-22 20:42:13 +0700108
109 // Validate QuantizationInfo
110 ARM_COMPUTE_EXPECT(src.info()->quantization_info() == src_quantization_info, framework::LogLevel::ERRORS);
111 ARM_COMPUTE_EXPECT(weights.info()->quantization_info() == weights_quantization_info, framework::LogLevel::ERRORS);
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100112}
113
Georgios Pinitas358ca202017-12-07 16:47:52 +0000114// *INDENT-OFF*
115// clang-format off
116DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(zip(zip(zip(
117 framework::dataset::make("InputInfo", { TensorInfo(TensorShape(9U, 5U, 7U, 3U), 1, DataType::F32), // Mismatching data types
Georgios Pinitas358ca202017-12-07 16:47:52 +0000118 TensorInfo(TensorShape(8U, 4U, 6U, 4U), 1, DataType::F32),
119 TensorInfo(TensorShape(8U, 4U, 6U, 4U), 1, DataType::F32),
120 TensorInfo(TensorShape(9U, 5U, 7U, 3U), 1, DataType::F32), // Invalid weights dimensions
121 TensorInfo(TensorShape(9U, 5U, 7U, 3U), 1, DataType::F32), // Wrongly reshaped weights
Georgios Pinitas358ca202017-12-07 16:47:52 +0000122 }),
123 framework::dataset::make("WeightsInfo",{ TensorInfo(TensorShape(315U, 271U), 1, DataType::F16),
Georgios Pinitas358ca202017-12-07 16:47:52 +0000124 TensorInfo(TensorShape(192U, 192U), 1, DataType::F32),
125 TensorInfo(TensorShape(192U, 192U), 1, DataType::F32),
Michalis Spyroud466c2d2018-01-30 10:54:39 +0000126 TensorInfo(TensorShape(217U, 231U), 1, DataType::F32),
Georgios Pinitas358ca202017-12-07 16:47:52 +0000127 TensorInfo(TensorShape(217U, 315U), 1, DataType::F32),
Georgios Pinitas358ca202017-12-07 16:47:52 +0000128 })),
129 framework::dataset::make("BiasInfo",{ TensorInfo(TensorShape(271U), 1, DataType::F32),
Georgios Pinitas358ca202017-12-07 16:47:52 +0000130 TensorInfo(TensorShape(192U), 1, DataType::F32),
131 TensorInfo(TensorShape(192U), 1, DataType::F32),
132 TensorInfo(TensorShape(271U), 1, DataType::F32),
133 TensorInfo(TensorShape(271U), 1, DataType::F32),
Georgios Pinitas358ca202017-12-07 16:47:52 +0000134 })),
135 framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(271U, 3U), 1, DataType::F32),
Georgios Pinitas358ca202017-12-07 16:47:52 +0000136 TensorInfo(TensorShape(192U, 4U), 1, DataType::F32),
137 TensorInfo(TensorShape(192U, 4U), 1, DataType::F32),
138 TensorInfo(TensorShape(271U, 3U), 1, DataType::F32),
139 TensorInfo(TensorShape(271U, 3U), 1, DataType::F32),
Georgios Pinitas358ca202017-12-07 16:47:52 +0000140 })),
Vidhya Sudhan Loganathan0fc25452018-06-18 14:40:56 +0100141 framework::dataset::make("TransposeWeights",{ true, true, false, true, true })),
142 framework::dataset::make("ReshapedWeights",{ false, false, false, false, false})),
143 framework::dataset::make("Expected", { false, true, true, false, false })),
Georgios Pinitas358ca202017-12-07 16:47:52 +0000144 input_info, weights_info, bias_info, output_info, transpose_weights, reshaped_weights, expected)
145{
146 Status status = CLFullyConnectedLayer::validate(&input_info.clone()->set_is_resizable(false),
147 &weights_info.clone()->set_is_resizable(false),
148 &bias_info.clone()->set_is_resizable(false),
149 &output_info.clone()->set_is_resizable(false),
150 transpose_weights,
151 reshaped_weights);
152 ARM_COMPUTE_EXPECT(bool(status) == expected, framework::LogLevel::ERRORS);
153}
154// clang-format on
155// *INDENT-ON*
156
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100157template <typename T>
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100158using CLFullyConnectedLayerFixture = FullyConnectedLayerValidationFixture<CLTensor, CLAccessor, CLFullyConnectedLayer, T, false>;
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100159
160TEST_SUITE(Float)
161TEST_SUITE(FP16)
Georgios Pinitas583137c2017-08-31 18:12:42 +0100162FIXTURE_DATA_TEST_CASE(RunSmall, CLFullyConnectedLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallFullyConnectedLayerDataset(),
163 FullyConnectedParameters),
164 framework::dataset::make("DataType", DataType::F16)))
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100165{
166 // Validate output
steniu013e05e4e2017-08-25 17:18:01 +0100167 validate(CLAccessor(_target), _reference, tolerance_f16, tolerance_num);
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100168}
Georgios Pinitas583137c2017-08-31 18:12:42 +0100169FIXTURE_DATA_TEST_CASE(RunLarge, CLFullyConnectedLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeFullyConnectedLayerDataset(),
170 FullyConnectedParameters),
171 framework::dataset::make("DataType", DataType::F16)))
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100172{
173 // Validate output
steniu013e05e4e2017-08-25 17:18:01 +0100174 validate(CLAccessor(_target), _reference, tolerance_f16, tolerance_num);
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100175}
176TEST_SUITE_END()
177
178TEST_SUITE(FP32)
179FIXTURE_DATA_TEST_CASE(RunSmall, CLFullyConnectedLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallFullyConnectedLayerDataset(), FullyConnectedParameters),
180 framework::dataset::make("DataType", DataType::F32)))
181{
182 // Validate output
183 validate(CLAccessor(_target), _reference, tolerance_f32);
184}
185FIXTURE_DATA_TEST_CASE(RunLarge, CLFullyConnectedLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeFullyConnectedLayerDataset(), FullyConnectedParameters),
186 framework::dataset::make("DataType", DataType::F32)))
187{
188 // Validate output
189 validate(CLAccessor(_target), _reference, tolerance_f32);
190}
191TEST_SUITE_END()
192TEST_SUITE_END()
193
194template <typename T>
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100195using CLFullyConnectedLayerFixedPointFixture = FullyConnectedLayerValidationFixedPointFixture<CLTensor, CLAccessor, CLFullyConnectedLayer, T, false>;
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100196
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000197template <typename T>
198using CLFullyConnectedLayerQuantizedFixture = FullyConnectedLayerValidationQuantizedFixture<CLTensor, CLAccessor, CLFullyConnectedLayer, T, false>;
199
200TEST_SUITE(Quantized)
201TEST_SUITE(QASYMM8)
202FIXTURE_DATA_TEST_CASE(RunSmall, CLFullyConnectedLayerQuantizedFixture<uint8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(
203 combine(datasets::SmallFullyConnectedLayerDataset(),
204 FullyConnectedParameters),
205 framework::dataset::make("DataType", DataType::QASYMM8)),
206 framework::dataset::make("QuantizationInfo", { QuantizationInfo(1.f / 255.f, 10) })))
207{
208 // Validate output
209 validate(CLAccessor(_target), _reference, tolerance_qasymm8);
210}
211FIXTURE_DATA_TEST_CASE(RunLarge, CLFullyConnectedLayerQuantizedFixture<uint8_t>, framework::DatasetMode::NIGHTLY, combine(combine(
212 combine(datasets::LargeFullyConnectedLayerDataset(),
213 FullyConnectedParameters),
214 framework::dataset::make("DataType", DataType::QASYMM8)),
215 framework::dataset::make("QuantizationInfo", { QuantizationInfo(1.f / 256.f, 10) })))
216{
217 // Validate output
218 validate(CLAccessor(_target), _reference, tolerance_qasymm8);
219}
220TEST_SUITE_END()
221TEST_SUITE_END()
222
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100223TEST_SUITE_END()
224TEST_SUITE_END()
225} // namespace validation
226} // namespace test
227} // namespace arm_compute