blob: 35b9d2938bb4799f92c072fdea6e712a8435fd45 [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);
52
53/** CNN data types */
54const auto CNNDataTypes = framework::dataset::make("DataType",
55{
56 DataType::F16,
57 DataType::F32,
58 DataType::QS8,
59 DataType::QS16,
60});
61
62const auto FullyConnectedParameters = combine(framework::dataset::make("TransposeWeights", { false, true }), framework::dataset::make("ReshapeWeights", { false, true }));
63} // namespace
64
65TEST_SUITE(CL)
66TEST_SUITE(FullyConnectedLayer)
67
68DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(combine(framework::dataset::concat(datasets::SmallFullyConnectedLayerDataset(), datasets::LargeFullyConnectedLayerDataset()),
69 FullyConnectedParameters),
70 CNNDataTypes),
71 src_shape, weights_shape, bias_shape, dst_shape, transpose_weights, reshape_weights, data_type)
72{
73 // Set fixed point position data type allowed
74 int fixed_point_position = is_data_type_fixed_point(data_type) ? 3 : 0;
75
76 TensorShape ws(weights_shape);
77
78 // Transpose weights if not done in the function
79 if(!reshape_weights || !transpose_weights)
80 {
81 const size_t shape_x = ws.x();
82 ws.set(0, ws.y());
83 ws.set(1, shape_x);
Moritz Pflanzer69d33412017-08-09 11:45:15 +010084 }
85
86 // Create tensors
87 CLTensor src = create_tensor<CLTensor>(src_shape, data_type, 1, fixed_point_position);
88 CLTensor weights = create_tensor<CLTensor>(ws, data_type, 1, fixed_point_position);
89 CLTensor bias = create_tensor<CLTensor>(bias_shape, data_type, 1, fixed_point_position);
90 CLTensor dst = create_tensor<CLTensor>(dst_shape, data_type, 1, fixed_point_position);
91
92 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
93 ARM_COMPUTE_EXPECT(weights.info()->is_resizable(), framework::LogLevel::ERRORS);
94 ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS);
95 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
96
97 // Create and configure function.
98 CLFullyConnectedLayer fc;
99 fc.configure(&src, &weights, &bias, &dst, transpose_weights, !reshape_weights);
100
101 // Validate valid region
102 const ValidRegion dst_valid_region = shape_to_valid_region(dst_shape);
103 validate(dst.info()->valid_region(), dst_valid_region);
104}
105
106template <typename T>
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100107using CLFullyConnectedLayerFixture = FullyConnectedLayerValidationFixture<CLTensor, CLAccessor, CLFullyConnectedLayer, T, false>;
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100108
109TEST_SUITE(Float)
110TEST_SUITE(FP16)
Georgios Pinitas583137c2017-08-31 18:12:42 +0100111FIXTURE_DATA_TEST_CASE(RunSmall, CLFullyConnectedLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallFullyConnectedLayerDataset(),
112 FullyConnectedParameters),
113 framework::dataset::make("DataType", DataType::F16)))
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100114{
115 // Validate output
steniu013e05e4e2017-08-25 17:18:01 +0100116 validate(CLAccessor(_target), _reference, tolerance_f16, tolerance_num);
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100117}
Georgios Pinitas583137c2017-08-31 18:12:42 +0100118FIXTURE_DATA_TEST_CASE(RunLarge, CLFullyConnectedLayerFixture<half>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeFullyConnectedLayerDataset(),
119 FullyConnectedParameters),
120 framework::dataset::make("DataType", DataType::F16)))
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100121{
122 // Validate output
steniu013e05e4e2017-08-25 17:18:01 +0100123 validate(CLAccessor(_target), _reference, tolerance_f16, tolerance_num);
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100124}
125TEST_SUITE_END()
126
127TEST_SUITE(FP32)
128FIXTURE_DATA_TEST_CASE(RunSmall, CLFullyConnectedLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallFullyConnectedLayerDataset(), FullyConnectedParameters),
129 framework::dataset::make("DataType", DataType::F32)))
130{
131 // Validate output
132 validate(CLAccessor(_target), _reference, tolerance_f32);
133}
134FIXTURE_DATA_TEST_CASE(RunLarge, CLFullyConnectedLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeFullyConnectedLayerDataset(), FullyConnectedParameters),
135 framework::dataset::make("DataType", DataType::F32)))
136{
137 // Validate output
138 validate(CLAccessor(_target), _reference, tolerance_f32);
139}
140TEST_SUITE_END()
141TEST_SUITE_END()
142
143template <typename T>
Gian Marco Iodiceedfa9f42017-08-15 11:45:22 +0100144using CLFullyConnectedLayerFixedPointFixture = FullyConnectedLayerValidationFixedPointFixture<CLTensor, CLAccessor, CLFullyConnectedLayer, T, false>;
Moritz Pflanzer69d33412017-08-09 11:45:15 +0100145
146TEST_SUITE(Quantized)
147TEST_SUITE(QS8)
148// Testing for fixed point position [1,6) as reciprocal limits the maximum fixed point position to 5
149FIXTURE_DATA_TEST_CASE(RunSmall, CLFullyConnectedLayerFixedPointFixture<int8_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallFullyConnectedLayerDataset(),
150 FullyConnectedParameters),
151 framework::dataset::make("DataType",
152 DataType::QS8)),
153 framework::dataset::make("FractionalBits", 1, 6)))
154{
155 // Validate output
156 validate(CLAccessor(_target), _reference, tolerance_fixed_point);
157}
158FIXTURE_DATA_TEST_CASE(RunLarge, CLFullyConnectedLayerFixedPointFixture<int8_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeFullyConnectedLayerDataset(),
159 FullyConnectedParameters),
160 framework::dataset::make("DataType",
161 DataType::QS8)),
162 framework::dataset::make("FractionalBits", 1, 6)))
163{
164 // Validate output
165 validate(CLAccessor(_target), _reference, tolerance_fixed_point);
166}
167TEST_SUITE_END()
168
169TEST_SUITE(QS16)
170// Testing for fixed point position [1,14) as reciprocal limits the maximum fixed point position to 14
171FIXTURE_DATA_TEST_CASE(RunSmall, CLFullyConnectedLayerFixedPointFixture<int16_t>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallFullyConnectedLayerDataset(),
172 FullyConnectedParameters),
173 framework::dataset::make("DataType",
174 DataType::QS16)),
175 framework::dataset::make("FractionalBits", 1, 14)))
176{
177 // Validate output
178 validate(CLAccessor(_target), _reference, tolerance_fixed_point);
179}
180FIXTURE_DATA_TEST_CASE(RunLarge, CLFullyConnectedLayerFixedPointFixture<int16_t>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeFullyConnectedLayerDataset(),
181 FullyConnectedParameters),
182 framework::dataset::make("DataType",
183 DataType::QS16)),
184 framework::dataset::make("FractionalBits", 1, 14)))
185{
186 // Validate output
187 validate(CLAccessor(_target), _reference, tolerance_fixed_point);
188}
189TEST_SUITE_END()
190TEST_SUITE_END()
191
192TEST_SUITE_END()
193TEST_SUITE_END()
194} // namespace validation
195} // namespace test
196} // namespace arm_compute