blob: 174778b8ef27e68bb4f55adc14b19677995dac1e [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{
Moritz Pflanzer69d33412017-08-09 11:45:15 +010071 TensorShape ws(weights_shape);
72
73 // Transpose weights if not done in the function
74 if(!reshape_weights || !transpose_weights)
75 {
76 const size_t shape_x = ws.x();
77 ws.set(0, ws.y());
78 ws.set(1, shape_x);
79
80 // Weights have to be passed reshaped
81 // Transpose 1xW for batched version
82 if(!reshape_weights && dst_shape.y() > 1)
83 {
84 const float transpose_width = 16.0f / data_size_from_type(data_type);
85 const size_t shape_x = ws.x();
86 ws.set(0, ws.y() * static_cast<unsigned int>(transpose_width));
87 ws.set(1, static_cast<unsigned int>(std::ceil(shape_x / transpose_width)));
88 }
89 }
90
91 // Create tensors
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010092 Tensor src = create_tensor<Tensor>(src_shape, data_type, 1);
93 Tensor weights = create_tensor<Tensor>(ws, data_type, 1);
94 Tensor bias = create_tensor<Tensor>(bias_shape, data_type, 1);
95 Tensor dst = create_tensor<Tensor>(dst_shape, data_type, 1);
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
102 // Create and configure function.
103 NEFullyConnectedLayer fc;
104 fc.configure(&src, &weights, &bias, &dst, transpose_weights, !reshape_weights);
105
106 // Validate valid region
107 const ValidRegion dst_valid_region = shape_to_valid_region(dst_shape);
108 validate(dst.info()->valid_region(), dst_valid_region);
109}
110
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000111// *INDENT-OFF*
112// clang-format off
113DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(zip(zip(zip(
114 framework::dataset::make("InputInfo", { TensorInfo(TensorShape(9U, 5U, 7U, 3U), 1, DataType::F32), // Mismatching data types
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000115 TensorInfo(TensorShape(8U, 4U, 6U, 4U), 1, DataType::F32),
116 TensorInfo(TensorShape(8U, 4U, 6U, 4U), 1, DataType::F32),
117 TensorInfo(TensorShape(9U, 5U, 7U, 3U), 1, DataType::F32), // Invalid weights dimensions
118 TensorInfo(TensorShape(9U, 5U, 7U, 3U), 1, DataType::F32), // Wrongly reshaped weights
119 TensorInfo(TensorShape(8U, 4U, 6U, 4U), 1, DataType::F32),
120 }),
121 framework::dataset::make("WeightsInfo",{ TensorInfo(TensorShape(315U, 271U), 1, DataType::F16),
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000122 TensorInfo(TensorShape(192U, 192U), 1, DataType::F32),
123 TensorInfo(TensorShape(192U, 192U), 1, DataType::F32),
124 TensorInfo(TensorShape(217U, 315U), 1, DataType::F32),
125 TensorInfo(TensorShape(217U, 315U), 1, DataType::F32),
126 TensorInfo(TensorShape(192U, 192U), 1, DataType::F32),
127 })),
128 framework::dataset::make("BiasInfo",{ TensorInfo(TensorShape(271U), 1, DataType::F32),
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000129 TensorInfo(TensorShape(192U), 1, DataType::F32),
130 TensorInfo(TensorShape(192U), 1, DataType::F32),
131 TensorInfo(TensorShape(271U), 1, DataType::F32),
132 TensorInfo(TensorShape(271U), 1, DataType::F32),
133 TensorInfo(TensorShape(192U), 1, DataType::F32),
134 })),
135 framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(271U, 3U), 1, DataType::F32),
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +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),
140 TensorInfo(TensorShape(192U, 4U), 1, DataType::F32),
141 })),
Vidhya Sudhan Loganathan0fc25452018-06-18 14:40:56 +0100142 framework::dataset::make("TransposeWeights",{ true, true, false, true, true, true })),
143 framework::dataset::make("ReshapedWeights",{ false, false, false, false, false , false})),
144 framework::dataset::make("Expected", { false, true, true, false, false, true })),
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000145 input_info, weights_info, bias_info, output_info, transpose_weights, reshaped_weights, expected)
146{
147 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);
148 ARM_COMPUTE_EXPECT(bool(status) == expected, framework::LogLevel::ERRORS);
149}
150// clang-format on
151// *INDENT-ON*
152
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100153template <typename T>
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100154using NEFullyConnectedLayerFixture = FullyConnectedLayerValidationFixture<Tensor, Accessor, NEFullyConnectedLayer, T, true>;
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100155
156TEST_SUITE(Float)
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000157#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100158TEST_SUITE(FP16)
Georgios Pinitas583137c2017-08-31 18:12:42 +0100159FIXTURE_DATA_TEST_CASE(RunSmall, NEFullyConnectedLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallFullyConnectedLayerDataset(),
160 FullyConnectedParameters),
161 framework::dataset::make("DataType", DataType::F16)))
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100162{
163 // Validate output
164 validate(Accessor(_target), _reference, tolerance_f16);
165}
Georgios Pinitas583137c2017-08-31 18:12:42 +0100166FIXTURE_DATA_TEST_CASE(RunLarge, NEFullyConnectedLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeFullyConnectedLayerDataset(),
167 FullyConnectedParameters),
168 framework::dataset::make("DataType", DataType::F16)))
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100169{
170 // Validate output
171 validate(Accessor(_target), _reference, tolerance_f16);
172}
173TEST_SUITE_END()
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000174#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100175
176TEST_SUITE(FP32)
177FIXTURE_DATA_TEST_CASE(RunSmall, NEFullyConnectedLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallFullyConnectedLayerDataset(), FullyConnectedParameters),
178 framework::dataset::make("DataType", DataType::F32)))
179{
180 // Validate output
181 validate(Accessor(_target), _reference, tolerance_f32);
182}
183FIXTURE_DATA_TEST_CASE(RunLarge, NEFullyConnectedLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeFullyConnectedLayerDataset(), FullyConnectedParameters),
184 framework::dataset::make("DataType", DataType::F32)))
185{
186 // Validate output
187 validate(Accessor(_target), _reference, tolerance_f32);
188}
189TEST_SUITE_END()
190TEST_SUITE_END()
191
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100192TEST_SUITE_END()
193TEST_SUITE_END()
194} // namespace validation
195} // namespace test
196} // namespace arm_compute