blob: ed0edcd3bec33cbb840b6ee958ff33d0b9c22a44 [file] [log] [blame]
Moritz Pflanzer69d33412017-08-09 11:45:15 +01001/*
Anthony Barbier1c0d0ff2018-01-31 13:05:09 +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/NEON/functions/NEFullyConnectedLayer.h"
26#include "arm_compute/runtime/Tensor.h"
27#include "arm_compute/runtime/TensorAllocator.h"
Moritz Pflanzer69d33412017-08-09 11:45:15 +010028#include "tests/NEON/Accessor.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 */
Moritz Pflanzerff1c3602017-09-22 12:41:25 +010046constexpr RelativeTolerance<float> tolerance_f32(0.01f);
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +000047#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Moritz Pflanzerff1c3602017-09-22 12:41:25 +010048constexpr RelativeTolerance<float> tolerance_f16(0.01f);
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +000049#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC*/
Moritz Pflanzer69d33412017-08-09 11:45:15 +010050/** Tolerance for fixed point operations */
51constexpr AbsoluteTolerance<float> tolerance_fixed_point(1.f);
52
53/** CNN data types */
54const auto CNNDataTypes = framework::dataset::make("DataType",
55{
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +000056#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Moritz Pflanzer69d33412017-08-09 11:45:15 +010057 DataType::F16,
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +000058#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Moritz Pflanzer69d33412017-08-09 11:45:15 +010059 DataType::F32,
60 DataType::QS8,
61 DataType::QS16,
62});
63
64const auto FullyConnectedParameters = combine(framework::dataset::make("TransposeWeights", { false, true }), framework::dataset::make("ReshapeWeights", { false, true }));
65} // namespace
66
67TEST_SUITE(NEON)
68TEST_SUITE(FullyConnectedLayer)
69
70DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(combine(framework::dataset::concat(datasets::SmallFullyConnectedLayerDataset(), datasets::LargeFullyConnectedLayerDataset()),
71 FullyConnectedParameters),
72 CNNDataTypes),
73 src_shape, weights_shape, bias_shape, dst_shape, transpose_weights, reshape_weights, data_type)
74{
75 // Set fixed point position data type allowed
76 int fixed_point_position = is_data_type_fixed_point(data_type) ? 3 : 0;
77
78 TensorShape ws(weights_shape);
79
80 // Transpose weights if not done in the function
81 if(!reshape_weights || !transpose_weights)
82 {
83 const size_t shape_x = ws.x();
84 ws.set(0, ws.y());
85 ws.set(1, shape_x);
86
87 // Weights have to be passed reshaped
88 // Transpose 1xW for batched version
89 if(!reshape_weights && dst_shape.y() > 1)
90 {
91 const float transpose_width = 16.0f / data_size_from_type(data_type);
92 const size_t shape_x = ws.x();
93 ws.set(0, ws.y() * static_cast<unsigned int>(transpose_width));
94 ws.set(1, static_cast<unsigned int>(std::ceil(shape_x / transpose_width)));
95 }
96 }
97
98 // Create tensors
99 Tensor src = create_tensor<Tensor>(src_shape, data_type, 1, fixed_point_position);
100 Tensor weights = create_tensor<Tensor>(ws, data_type, 1, fixed_point_position);
101 Tensor bias = create_tensor<Tensor>(bias_shape, data_type, 1, fixed_point_position);
102 Tensor dst = create_tensor<Tensor>(dst_shape, data_type, 1, fixed_point_position);
103
104 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
105 ARM_COMPUTE_EXPECT(weights.info()->is_resizable(), framework::LogLevel::ERRORS);
106 ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS);
107 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
108
109 // Create and configure function.
110 NEFullyConnectedLayer fc;
111 fc.configure(&src, &weights, &bias, &dst, transpose_weights, !reshape_weights);
112
113 // Validate valid region
114 const ValidRegion dst_valid_region = shape_to_valid_region(dst_shape);
115 validate(dst.info()->valid_region(), dst_valid_region);
116}
117
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000118// *INDENT-OFF*
119// clang-format off
120DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(zip(zip(zip(
121 framework::dataset::make("InputInfo", { TensorInfo(TensorShape(9U, 5U, 7U, 3U), 1, DataType::F32), // Mismatching data types
122 TensorInfo(TensorShape(9U, 5U, 7U, 3U), 1, DataType::QS8, 2), // Mismatching fixed point position
123 TensorInfo(TensorShape(8U, 4U, 6U, 4U), 1, DataType::F32),
124 TensorInfo(TensorShape(8U, 4U, 6U, 4U), 1, DataType::F32),
125 TensorInfo(TensorShape(9U, 5U, 7U, 3U), 1, DataType::F32), // Invalid weights dimensions
126 TensorInfo(TensorShape(9U, 5U, 7U, 3U), 1, DataType::F32), // Wrongly reshaped weights
127 TensorInfo(TensorShape(8U, 4U, 6U, 4U), 1, DataType::F32),
128 }),
129 framework::dataset::make("WeightsInfo",{ TensorInfo(TensorShape(315U, 271U), 1, DataType::F16),
130 TensorInfo(TensorShape(315U, 271U), 1, DataType::QS8, 3),
131 TensorInfo(TensorShape(192U, 192U), 1, DataType::F32),
132 TensorInfo(TensorShape(192U, 192U), 1, DataType::F32),
133 TensorInfo(TensorShape(217U, 315U), 1, DataType::F32),
134 TensorInfo(TensorShape(217U, 315U), 1, DataType::F32),
135 TensorInfo(TensorShape(192U, 192U), 1, DataType::F32),
136 })),
137 framework::dataset::make("BiasInfo",{ TensorInfo(TensorShape(271U), 1, DataType::F32),
138 TensorInfo(TensorShape(271U), 1, DataType::QS8, 2),
139 TensorInfo(TensorShape(192U), 1, DataType::F32),
140 TensorInfo(TensorShape(192U), 1, DataType::F32),
141 TensorInfo(TensorShape(271U), 1, DataType::F32),
142 TensorInfo(TensorShape(271U), 1, DataType::F32),
143 TensorInfo(TensorShape(192U), 1, DataType::F32),
144 })),
145 framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(271U, 3U), 1, DataType::F32),
146 TensorInfo(TensorShape(271U, 3U), 1, DataType::QS8, 3),
147 TensorInfo(TensorShape(192U, 4U), 1, DataType::F32),
148 TensorInfo(TensorShape(192U, 4U), 1, DataType::F32),
149 TensorInfo(TensorShape(271U, 3U), 1, DataType::F32),
150 TensorInfo(TensorShape(271U, 3U), 1, DataType::F32),
151 TensorInfo(TensorShape(192U, 4U), 1, DataType::F32),
152 })),
153 framework::dataset::make("TransposeWeights",{ true, true, true, false, true, true, true })),
154 framework::dataset::make("ReshapedWeights",{ false, false, false, false, false, false , false})),
155 framework::dataset::make("Expected", { false, false, true, true, false, false, true })),
156 input_info, weights_info, bias_info, output_info, transpose_weights, reshaped_weights, expected)
157{
158 Status status = NEFullyConnectedLayer::validate(&input_info.clone()->set_is_resizable(false), &weights_info.clone()->set_is_resizable(false), &bias_info.clone()->set_is_resizable(false), &output_info.clone()->set_is_resizable(false), transpose_weights, reshaped_weights);
159 ARM_COMPUTE_EXPECT(bool(status) == expected, framework::LogLevel::ERRORS);
160}
161// clang-format on
162// *INDENT-ON*
163
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100164template <typename T>
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100165using NEFullyConnectedLayerFixture = FullyConnectedLayerValidationFixture<Tensor, Accessor, NEFullyConnectedLayer, T, true>;
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100166
167TEST_SUITE(Float)
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000168#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100169TEST_SUITE(FP16)
Georgios Pinitas583137c2017-08-31 18:12:42 +0100170FIXTURE_DATA_TEST_CASE(RunSmall, NEFullyConnectedLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallFullyConnectedLayerDataset(),
171 FullyConnectedParameters),
172 framework::dataset::make("DataType", DataType::F16)))
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100173{
174 // Validate output
175 validate(Accessor(_target), _reference, tolerance_f16);
176}
Georgios Pinitas583137c2017-08-31 18:12:42 +0100177FIXTURE_DATA_TEST_CASE(RunLarge, NEFullyConnectedLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeFullyConnectedLayerDataset(),
178 FullyConnectedParameters),
179 framework::dataset::make("DataType", DataType::F16)))
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100180{
181 // Validate output
182 validate(Accessor(_target), _reference, tolerance_f16);
183}
184TEST_SUITE_END()
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000185#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100186
187TEST_SUITE(FP32)
188FIXTURE_DATA_TEST_CASE(RunSmall, NEFullyConnectedLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallFullyConnectedLayerDataset(), FullyConnectedParameters),
189 framework::dataset::make("DataType", DataType::F32)))
190{
191 // Validate output
192 validate(Accessor(_target), _reference, tolerance_f32);
193}
194FIXTURE_DATA_TEST_CASE(RunLarge, NEFullyConnectedLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeFullyConnectedLayerDataset(), FullyConnectedParameters),
195 framework::dataset::make("DataType", DataType::F32)))
196{
197 // Validate output
198 validate(Accessor(_target), _reference, tolerance_f32);
199}
200TEST_SUITE_END()
201TEST_SUITE_END()
202
203template <typename T>
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100204using NEFullyConnectedLayerFixedPointFixture = FullyConnectedLayerValidationFixedPointFixture<Tensor, Accessor, NEFullyConnectedLayer, T, true>;
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100205
Georgios Pinitas45bcc3a2017-11-29 11:06:49 +0000206TEST_SUITE(FixedPoint)
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100207TEST_SUITE(QS8)
208// Testing for fixed point position [1,6) as reciprocal limits the maximum fixed point position to 5
Anthony Barbier1c0d0ff2018-01-31 13:05:09 +0000209FIXTURE_DATA_TEST_CASE(RunTiny, NEFullyConnectedLayerFixedPointFixture<int8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::TinyFullyConnectedLayerDataset(),
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100210 FullyConnectedParameters),
211 framework::dataset::make("DataType",
212 DataType::QS8)),
213 framework::dataset::make("FractionalBits", 1, 6)))
214{
215 // Validate output
216 validate(Accessor(_target), _reference, tolerance_fixed_point);
217}
Anthony Barbier1c0d0ff2018-01-31 13:05:09 +0000218FIXTURE_DATA_TEST_CASE(RunSmall, NEFullyConnectedLayerFixedPointFixture<int8_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::SmallFullyConnectedLayerDataset(),
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100219 FullyConnectedParameters),
220 framework::dataset::make("DataType",
221 DataType::QS8)),
222 framework::dataset::make("FractionalBits", 1, 6)))
223{
224 // Validate output
225 validate(Accessor(_target), _reference, tolerance_fixed_point);
226}
227TEST_SUITE_END()
228
229TEST_SUITE(QS16)
230// Testing for fixed point position [1,14) as reciprocal limits the maximum fixed point position to 14
Anthony Barbier1c0d0ff2018-01-31 13:05:09 +0000231FIXTURE_DATA_TEST_CASE(RunTiny, NEFullyConnectedLayerFixedPointFixture<int16_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::TinyFullyConnectedLayerDataset(),
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100232 FullyConnectedParameters),
233 framework::dataset::make("DataType",
234 DataType::QS16)),
235 framework::dataset::make("FractionalBits", 1, 14)))
236{
237 // Validate output
238 validate(Accessor(_target), _reference, tolerance_fixed_point);
239}
Anthony Barbier1c0d0ff2018-01-31 13:05:09 +0000240FIXTURE_DATA_TEST_CASE(RunSmall, NEFullyConnectedLayerFixedPointFixture<int16_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::SmallFullyConnectedLayerDataset(),
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100241 FullyConnectedParameters),
242 framework::dataset::make("DataType",
243 DataType::QS16)),
244 framework::dataset::make("FractionalBits", 1, 14)))
245{
246 // Validate output
247 validate(Accessor(_target), _reference, tolerance_fixed_point);
248}
249TEST_SUITE_END()
250TEST_SUITE_END()
251
252TEST_SUITE_END()
253TEST_SUITE_END()
254} // namespace validation
255} // namespace test
256} // namespace arm_compute