blob: 2ff432b2d32cbaa56db26b730914a8bd93bef5d9 [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/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
118template <typename T>
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100119using NEFullyConnectedLayerFixture = FullyConnectedLayerValidationFixture<Tensor, Accessor, NEFullyConnectedLayer, T, true>;
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100120
121TEST_SUITE(Float)
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000122#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100123TEST_SUITE(FP16)
Georgios Pinitas583137c2017-08-31 18:12:42 +0100124FIXTURE_DATA_TEST_CASE(RunSmall, NEFullyConnectedLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallFullyConnectedLayerDataset(),
125 FullyConnectedParameters),
126 framework::dataset::make("DataType", DataType::F16)))
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100127{
128 // Validate output
129 validate(Accessor(_target), _reference, tolerance_f16);
130}
Georgios Pinitas583137c2017-08-31 18:12:42 +0100131FIXTURE_DATA_TEST_CASE(RunLarge, NEFullyConnectedLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeFullyConnectedLayerDataset(),
132 FullyConnectedParameters),
133 framework::dataset::make("DataType", DataType::F16)))
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100134{
135 // Validate output
136 validate(Accessor(_target), _reference, tolerance_f16);
137}
138TEST_SUITE_END()
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000139#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100140
141TEST_SUITE(FP32)
142FIXTURE_DATA_TEST_CASE(RunSmall, NEFullyConnectedLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallFullyConnectedLayerDataset(), FullyConnectedParameters),
143 framework::dataset::make("DataType", DataType::F32)))
144{
145 // Validate output
146 validate(Accessor(_target), _reference, tolerance_f32);
147}
148FIXTURE_DATA_TEST_CASE(RunLarge, NEFullyConnectedLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeFullyConnectedLayerDataset(), FullyConnectedParameters),
149 framework::dataset::make("DataType", DataType::F32)))
150{
151 // Validate output
152 validate(Accessor(_target), _reference, tolerance_f32);
153}
154TEST_SUITE_END()
155TEST_SUITE_END()
156
157template <typename T>
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100158using NEFullyConnectedLayerFixedPointFixture = FullyConnectedLayerValidationFixedPointFixture<Tensor, Accessor, NEFullyConnectedLayer, T, true>;
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100159
160TEST_SUITE(Quantized)
161TEST_SUITE(QS8)
162// Testing for fixed point position [1,6) as reciprocal limits the maximum fixed point position to 5
163FIXTURE_DATA_TEST_CASE(RunSmall, NEFullyConnectedLayerFixedPointFixture<int8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallFullyConnectedLayerDataset(),
164 FullyConnectedParameters),
165 framework::dataset::make("DataType",
166 DataType::QS8)),
167 framework::dataset::make("FractionalBits", 1, 6)))
168{
169 // Validate output
170 validate(Accessor(_target), _reference, tolerance_fixed_point);
171}
172FIXTURE_DATA_TEST_CASE(RunLarge, NEFullyConnectedLayerFixedPointFixture<int8_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeFullyConnectedLayerDataset(),
173 FullyConnectedParameters),
174 framework::dataset::make("DataType",
175 DataType::QS8)),
176 framework::dataset::make("FractionalBits", 1, 6)))
177{
178 // Validate output
179 validate(Accessor(_target), _reference, tolerance_fixed_point);
180}
181TEST_SUITE_END()
182
183TEST_SUITE(QS16)
184// Testing for fixed point position [1,14) as reciprocal limits the maximum fixed point position to 14
185FIXTURE_DATA_TEST_CASE(RunSmall, NEFullyConnectedLayerFixedPointFixture<int16_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallFullyConnectedLayerDataset(),
186 FullyConnectedParameters),
187 framework::dataset::make("DataType",
188 DataType::QS16)),
189 framework::dataset::make("FractionalBits", 1, 14)))
190{
191 // Validate output
192 validate(Accessor(_target), _reference, tolerance_fixed_point);
193}
194FIXTURE_DATA_TEST_CASE(RunLarge, NEFullyConnectedLayerFixedPointFixture<int16_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeFullyConnectedLayerDataset(),
195 FullyConnectedParameters),
196 framework::dataset::make("DataType",
197 DataType::QS16)),
198 framework::dataset::make("FractionalBits", 1, 14)))
199{
200 // Validate output
201 validate(Accessor(_target), _reference, tolerance_fixed_point);
202}
203TEST_SUITE_END()
204TEST_SUITE_END()
205
206TEST_SUITE_END()
207TEST_SUITE_END()
208} // namespace validation
209} // namespace test
210} // namespace arm_compute