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