blob: 3adcf61dc946725820e3fedd21a4fa6a4fd5e3f7 [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
51/** CNN data types */
52const auto CNNDataTypes = framework::dataset::make("DataType",
53{
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +000054#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Moritz Pflanzer69d33412017-08-09 11:45:15 +010055 DataType::F16,
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +000056#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Moritz Pflanzer69d33412017-08-09 11:45:15 +010057 DataType::F32,
Moritz Pflanzer69d33412017-08-09 11:45:15 +010058});
59
60const auto FullyConnectedParameters = combine(framework::dataset::make("TransposeWeights", { false, true }), framework::dataset::make("ReshapeWeights", { false, true }));
61} // namespace
62
63TEST_SUITE(NEON)
64TEST_SUITE(FullyConnectedLayer)
65
66DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(combine(framework::dataset::concat(datasets::SmallFullyConnectedLayerDataset(), datasets::LargeFullyConnectedLayerDataset()),
67 FullyConnectedParameters),
68 CNNDataTypes),
69 src_shape, weights_shape, bias_shape, dst_shape, transpose_weights, reshape_weights, data_type)
70{
71 // Set fixed point position data type allowed
72 int fixed_point_position = is_data_type_fixed_point(data_type) ? 3 : 0;
73
74 TensorShape ws(weights_shape);
75
76 // Transpose weights if not done in the function
77 if(!reshape_weights || !transpose_weights)
78 {
79 const size_t shape_x = ws.x();
80 ws.set(0, ws.y());
81 ws.set(1, shape_x);
82
83 // Weights have to be passed reshaped
84 // Transpose 1xW for batched version
85 if(!reshape_weights && dst_shape.y() > 1)
86 {
87 const float transpose_width = 16.0f / data_size_from_type(data_type);
88 const size_t shape_x = ws.x();
89 ws.set(0, ws.y() * static_cast<unsigned int>(transpose_width));
90 ws.set(1, static_cast<unsigned int>(std::ceil(shape_x / transpose_width)));
91 }
92 }
93
94 // Create tensors
95 Tensor src = create_tensor<Tensor>(src_shape, data_type, 1, fixed_point_position);
96 Tensor weights = create_tensor<Tensor>(ws, data_type, 1, fixed_point_position);
97 Tensor bias = create_tensor<Tensor>(bias_shape, data_type, 1, fixed_point_position);
98 Tensor dst = create_tensor<Tensor>(dst_shape, data_type, 1, fixed_point_position);
99
100 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
101 ARM_COMPUTE_EXPECT(weights.info()->is_resizable(), framework::LogLevel::ERRORS);
102 ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS);
103 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
104
105 // Create and configure function.
106 NEFullyConnectedLayer 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);
112}
113
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +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
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +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
122 TensorInfo(TensorShape(8U, 4U, 6U, 4U), 1, DataType::F32),
123 }),
124 framework::dataset::make("WeightsInfo",{ TensorInfo(TensorShape(315U, 271U), 1, DataType::F16),
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000125 TensorInfo(TensorShape(192U, 192U), 1, DataType::F32),
126 TensorInfo(TensorShape(192U, 192U), 1, DataType::F32),
127 TensorInfo(TensorShape(217U, 315U), 1, DataType::F32),
128 TensorInfo(TensorShape(217U, 315U), 1, DataType::F32),
129 TensorInfo(TensorShape(192U, 192U), 1, DataType::F32),
130 })),
131 framework::dataset::make("BiasInfo",{ TensorInfo(TensorShape(271U), 1, DataType::F32),
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000132 TensorInfo(TensorShape(192U), 1, DataType::F32),
133 TensorInfo(TensorShape(192U), 1, DataType::F32),
134 TensorInfo(TensorShape(271U), 1, DataType::F32),
135 TensorInfo(TensorShape(271U), 1, DataType::F32),
136 TensorInfo(TensorShape(192U), 1, DataType::F32),
137 })),
138 framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(271U, 3U), 1, DataType::F32),
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000139 TensorInfo(TensorShape(192U, 4U), 1, DataType::F32),
140 TensorInfo(TensorShape(192U, 4U), 1, DataType::F32),
141 TensorInfo(TensorShape(271U, 3U), 1, DataType::F32),
142 TensorInfo(TensorShape(271U, 3U), 1, DataType::F32),
143 TensorInfo(TensorShape(192U, 4U), 1, DataType::F32),
144 })),
Vidhya Sudhan Loganathan0fc25452018-06-18 14:40:56 +0100145 framework::dataset::make("TransposeWeights",{ true, true, false, true, true, true })),
146 framework::dataset::make("ReshapedWeights",{ false, false, false, false, false , false})),
147 framework::dataset::make("Expected", { false, true, true, false, false, true })),
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000148 input_info, weights_info, bias_info, output_info, transpose_weights, reshaped_weights, expected)
149{
150 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);
151 ARM_COMPUTE_EXPECT(bool(status) == expected, framework::LogLevel::ERRORS);
152}
153// clang-format on
154// *INDENT-ON*
155
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100156template <typename T>
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100157using NEFullyConnectedLayerFixture = FullyConnectedLayerValidationFixture<Tensor, Accessor, NEFullyConnectedLayer, T, true>;
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100158
159TEST_SUITE(Float)
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000160#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100161TEST_SUITE(FP16)
Georgios Pinitas583137c2017-08-31 18:12:42 +0100162FIXTURE_DATA_TEST_CASE(RunSmall, NEFullyConnectedLayerFixture<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
167 validate(Accessor(_target), _reference, tolerance_f16);
168}
Georgios Pinitas583137c2017-08-31 18:12:42 +0100169FIXTURE_DATA_TEST_CASE(RunLarge, NEFullyConnectedLayerFixture<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
174 validate(Accessor(_target), _reference, tolerance_f16);
175}
176TEST_SUITE_END()
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000177#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100178
179TEST_SUITE(FP32)
180FIXTURE_DATA_TEST_CASE(RunSmall, NEFullyConnectedLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallFullyConnectedLayerDataset(), FullyConnectedParameters),
181 framework::dataset::make("DataType", DataType::F32)))
182{
183 // Validate output
184 validate(Accessor(_target), _reference, tolerance_f32);
185}
186FIXTURE_DATA_TEST_CASE(RunLarge, NEFullyConnectedLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeFullyConnectedLayerDataset(), FullyConnectedParameters),
187 framework::dataset::make("DataType", DataType::F32)))
188{
189 // Validate output
190 validate(Accessor(_target), _reference, tolerance_f32);
191}
192TEST_SUITE_END()
193TEST_SUITE_END()
194
195template <typename T>
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100196using NEFullyConnectedLayerFixedPointFixture = FullyConnectedLayerValidationFixedPointFixture<Tensor, Accessor, NEFullyConnectedLayer, T, true>;
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100197
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100198TEST_SUITE_END()
199TEST_SUITE_END()
200} // namespace validation
201} // namespace test
202} // namespace arm_compute