blob: 0d8c8774b870eae47ac2e01a188f9f692867e69d [file] [log] [blame]
Moritz Pflanzer69d33412017-08-09 11:45:15 +01001/*
2 * Copyright (c) 2017 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"
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
Moritz Pflanzer69d33412017-08-09 11:45:15 +010050/** Tolerance for fixed point operations */
51constexpr AbsoluteTolerance<float> tolerance_fixed_point(1.f);
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +000052/** Tolerance for quantized asymmetric operations */
53constexpr AbsoluteTolerance<uint8_t> tolerance_qasymm8(1);
Moritz Pflanzer69d33412017-08-09 11:45:15 +010054
55/** CNN data types */
56const auto CNNDataTypes = framework::dataset::make("DataType",
57{
58 DataType::F16,
59 DataType::F32,
60 DataType::QS8,
61 DataType::QS16,
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +000062 DataType::QASYMM8,
Moritz Pflanzer69d33412017-08-09 11:45:15 +010063});
64
65const auto FullyConnectedParameters = combine(framework::dataset::make("TransposeWeights", { false, true }), framework::dataset::make("ReshapeWeights", { false, true }));
66} // namespace
67
68TEST_SUITE(CL)
69TEST_SUITE(FullyConnectedLayer)
70
71DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(combine(framework::dataset::concat(datasets::SmallFullyConnectedLayerDataset(), datasets::LargeFullyConnectedLayerDataset()),
72 FullyConnectedParameters),
73 CNNDataTypes),
74 src_shape, weights_shape, bias_shape, dst_shape, transpose_weights, reshape_weights, data_type)
75{
76 // Set fixed point position data type allowed
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +000077 const int fixed_point_position = is_data_type_fixed_point(data_type) ? 3 : 0;
78 const DataType bias_data_type = is_data_type_quantized_asymmetric(data_type) ? DataType::S32 : data_type;
79 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 +010080
81 TensorShape ws(weights_shape);
82
83 // Transpose weights if not done in the function
84 if(!reshape_weights || !transpose_weights)
85 {
86 const size_t shape_x = ws.x();
87 ws.set(0, ws.y());
88 ws.set(1, shape_x);
Moritz Pflanzer69d33412017-08-09 11:45:15 +010089 }
90
91 // Create tensors
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +000092 CLTensor src = create_tensor<CLTensor>(src_shape, data_type, 1, fixed_point_position, quantization_info);
93 CLTensor weights = create_tensor<CLTensor>(ws, data_type, 1, fixed_point_position, quantization_info);
94 CLTensor bias = create_tensor<CLTensor>(bias_shape, bias_data_type, 1, fixed_point_position, quantization_info);
95 CLTensor dst = create_tensor<CLTensor>(dst_shape, data_type, 1, fixed_point_position, quantization_info);
Moritz Pflanzer69d33412017-08-09 11:45:15 +010096
97 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
98 ARM_COMPUTE_EXPECT(weights.info()->is_resizable(), framework::LogLevel::ERRORS);
99 ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS);
100 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
101
Chunosov5124be52017-11-22 20:42:13 +0700102 const QuantizationInfo src_quantization_info = src.info()->quantization_info();
103 const QuantizationInfo weights_quantization_info = weights.info()->quantization_info();
104
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100105 // Create and configure function.
106 CLFullyConnectedLayer fc;
107 fc.configure(&src, &weights, &bias, &dst, transpose_weights, !reshape_weights);
108
109 // Validate valid region
110 const ValidRegion dst_valid_region = shape_to_valid_region(dst_shape);
111 validate(dst.info()->valid_region(), dst_valid_region);
Chunosov5124be52017-11-22 20:42:13 +0700112
113 // Validate QuantizationInfo
114 ARM_COMPUTE_EXPECT(src.info()->quantization_info() == src_quantization_info, framework::LogLevel::ERRORS);
115 ARM_COMPUTE_EXPECT(weights.info()->quantization_info() == weights_quantization_info, framework::LogLevel::ERRORS);
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100116}
117
118template <typename T>
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100119using CLFullyConnectedLayerFixture = FullyConnectedLayerValidationFixture<CLTensor, CLAccessor, CLFullyConnectedLayer, T, false>;
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100120
121TEST_SUITE(Float)
122TEST_SUITE(FP16)
Georgios Pinitas583137c2017-08-31 18:12:42 +0100123FIXTURE_DATA_TEST_CASE(RunSmall, CLFullyConnectedLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallFullyConnectedLayerDataset(),
124 FullyConnectedParameters),
125 framework::dataset::make("DataType", DataType::F16)))
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100126{
127 // Validate output
steniu013e05e4e2017-08-25 17:18:01 +0100128 validate(CLAccessor(_target), _reference, tolerance_f16, tolerance_num);
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100129}
Georgios Pinitas583137c2017-08-31 18:12:42 +0100130FIXTURE_DATA_TEST_CASE(RunLarge, CLFullyConnectedLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeFullyConnectedLayerDataset(),
131 FullyConnectedParameters),
132 framework::dataset::make("DataType", DataType::F16)))
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100133{
134 // Validate output
steniu013e05e4e2017-08-25 17:18:01 +0100135 validate(CLAccessor(_target), _reference, tolerance_f16, tolerance_num);
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100136}
137TEST_SUITE_END()
138
139TEST_SUITE(FP32)
140FIXTURE_DATA_TEST_CASE(RunSmall, CLFullyConnectedLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallFullyConnectedLayerDataset(), FullyConnectedParameters),
141 framework::dataset::make("DataType", DataType::F32)))
142{
143 // Validate output
144 validate(CLAccessor(_target), _reference, tolerance_f32);
145}
146FIXTURE_DATA_TEST_CASE(RunLarge, CLFullyConnectedLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeFullyConnectedLayerDataset(), FullyConnectedParameters),
147 framework::dataset::make("DataType", DataType::F32)))
148{
149 // Validate output
150 validate(CLAccessor(_target), _reference, tolerance_f32);
151}
152TEST_SUITE_END()
153TEST_SUITE_END()
154
155template <typename T>
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100156using CLFullyConnectedLayerFixedPointFixture = FullyConnectedLayerValidationFixedPointFixture<CLTensor, CLAccessor, CLFullyConnectedLayer, T, false>;
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100157
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000158TEST_SUITE(FixedPoint)
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100159TEST_SUITE(QS8)
160// Testing for fixed point position [1,6) as reciprocal limits the maximum fixed point position to 5
161FIXTURE_DATA_TEST_CASE(RunSmall, CLFullyConnectedLayerFixedPointFixture<int8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallFullyConnectedLayerDataset(),
162 FullyConnectedParameters),
163 framework::dataset::make("DataType",
164 DataType::QS8)),
165 framework::dataset::make("FractionalBits", 1, 6)))
166{
167 // Validate output
168 validate(CLAccessor(_target), _reference, tolerance_fixed_point);
169}
170FIXTURE_DATA_TEST_CASE(RunLarge, CLFullyConnectedLayerFixedPointFixture<int8_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeFullyConnectedLayerDataset(),
171 FullyConnectedParameters),
172 framework::dataset::make("DataType",
173 DataType::QS8)),
174 framework::dataset::make("FractionalBits", 1, 6)))
175{
176 // Validate output
177 validate(CLAccessor(_target), _reference, tolerance_fixed_point);
178}
179TEST_SUITE_END()
180
181TEST_SUITE(QS16)
182// Testing for fixed point position [1,14) as reciprocal limits the maximum fixed point position to 14
183FIXTURE_DATA_TEST_CASE(RunSmall, CLFullyConnectedLayerFixedPointFixture<int16_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallFullyConnectedLayerDataset(),
184 FullyConnectedParameters),
185 framework::dataset::make("DataType",
186 DataType::QS16)),
187 framework::dataset::make("FractionalBits", 1, 14)))
188{
189 // Validate output
190 validate(CLAccessor(_target), _reference, tolerance_fixed_point);
191}
192FIXTURE_DATA_TEST_CASE(RunLarge, CLFullyConnectedLayerFixedPointFixture<int16_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeFullyConnectedLayerDataset(),
193 FullyConnectedParameters),
194 framework::dataset::make("DataType",
195 DataType::QS16)),
196 framework::dataset::make("FractionalBits", 1, 14)))
197{
198 // Validate output
199 validate(CLAccessor(_target), _reference, tolerance_fixed_point);
200}
201TEST_SUITE_END()
202TEST_SUITE_END()
203
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000204template <typename T>
205using CLFullyConnectedLayerQuantizedFixture = FullyConnectedLayerValidationQuantizedFixture<CLTensor, CLAccessor, CLFullyConnectedLayer, T, false>;
206
207TEST_SUITE(Quantized)
208TEST_SUITE(QASYMM8)
209FIXTURE_DATA_TEST_CASE(RunSmall, CLFullyConnectedLayerQuantizedFixture<uint8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(
210 combine(datasets::SmallFullyConnectedLayerDataset(),
211 FullyConnectedParameters),
212 framework::dataset::make("DataType", DataType::QASYMM8)),
213 framework::dataset::make("QuantizationInfo", { QuantizationInfo(1.f / 255.f, 10) })))
214{
215 // Validate output
216 validate(CLAccessor(_target), _reference, tolerance_qasymm8);
217}
218FIXTURE_DATA_TEST_CASE(RunLarge, CLFullyConnectedLayerQuantizedFixture<uint8_t>, framework::DatasetMode::NIGHTLY, combine(combine(
219 combine(datasets::LargeFullyConnectedLayerDataset(),
220 FullyConnectedParameters),
221 framework::dataset::make("DataType", DataType::QASYMM8)),
222 framework::dataset::make("QuantizationInfo", { QuantizationInfo(1.f / 256.f, 10) })))
223{
224 // Validate output
225 validate(CLAccessor(_target), _reference, tolerance_qasymm8);
226}
227TEST_SUITE_END()
228TEST_SUITE_END()
229
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100230TEST_SUITE_END()
231TEST_SUITE_END()
232} // namespace validation
233} // namespace test
234} // namespace arm_compute