Compute Library
 21.08
FullyConnectedLayer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2021 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"
28 #include "tests/CL/CLAccessor.h"
30 #include "tests/datasets/FullyConnectedLayerDataset.h"
32 #include "tests/framework/Macros.h"
35 #include "tests/validation/fixtures/FullyConnectedLayerFixture.h"
36 
37 namespace arm_compute
38 {
39 namespace test
40 {
41 namespace validation
42 {
43 namespace
44 {
45 /** Tolerance for float operations */
46 constexpr RelativeTolerance<float> rel_tolerance_f32(0.05f); /**< Relative tolerance value for comparing reference's output against implementation's output for DataType:F32 */
47 constexpr AbsoluteTolerance<float> abs_tolerance_f32(0.0001f); /**< Absolute tolerance value for comparing reference's output against implementation's output for DataType::F32 */
48 RelativeTolerance<half_float::half> tolerance_f16(half(0.2)); /**< Relative tolerance value for comparing reference's output against implementation's output for DataType::F16 */
49 constexpr float tolerance_num = 0.07f; /**< Tolerance number */
50 
51 /** Tolerance for quantized asymmetric operations */
52 constexpr AbsoluteTolerance<uint8_t> tolerance_qasymm8(1);
53 
54 const auto FullyConnectedParameters = combine(framework::dataset::make("TransposeWeights", { false, true }), framework::dataset::make("ReshapeWeights", { false, true }));
55 
56 const auto QuantizationData = framework::dataset::make("QuantizationInfo",
57 {
58  QuantizationInfo(1.f / 255.f, 10),
59  QuantizationInfo(1.1f, 10),
60 });
61 
62 const auto ActivationFunctionsDataset = framework::dataset::make("ActivationInfo",
63 {
64  ActivationLayerInfo(),
67  ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 0.75f, 0.25f),
69 });
70 
71 const auto ActivationFunctionsQuantizedDataset = concat(concat(concat(
72  framework::dataset::make("ActivationInfo", ActivationLayerInfo()),
73  framework::dataset::make("ActivationInfo", ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU))),
74  framework::dataset::make("ActivationInfo", ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 0.5f))),
75  framework::dataset::make("ActivationInfo", ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 0.75f, 0.25f)));
76 } // namespace
77 
78 TEST_SUITE(CL)
79 TEST_SUITE(FullyConnectedLayer)
80 
81 // *INDENT-OFF*
82 // clang-format off
83 DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(zip(zip(zip(
84  framework::dataset::make("InputInfo", { TensorInfo(TensorShape(9U, 5U, 7U, 3U), 1, DataType::F32), // Mismatching data types
85  TensorInfo(TensorShape(8U, 4U, 6U, 4U), 1, DataType::F32),
86  TensorInfo(TensorShape(8U, 4U, 6U, 4U), 1, DataType::F32),
87  TensorInfo(TensorShape(9U, 5U, 7U, 3U), 1, DataType::F32), // Invalid weights dimensions
88  TensorInfo(TensorShape(9U, 5U, 7U, 3U), 1, DataType::F32), // Wrongly reshaped weights
89  }),
90  framework::dataset::make("WeightsInfo",{ TensorInfo(TensorShape(315U, 271U), 1, DataType::F16),
91  TensorInfo(TensorShape(192U, 192U), 1, DataType::F32),
92  TensorInfo(TensorShape(192U, 192U), 1, DataType::F32),
93  TensorInfo(TensorShape(217U, 231U), 1, DataType::F32),
94  TensorInfo(TensorShape(217U, 315U), 1, DataType::F32),
95  })),
101  })),
107  })),
108  framework::dataset::make("TransposeWeights",{ true, true, false, true, true })),
109  framework::dataset::make("ReshapedWeights",{ false, false, false, false, false})),
110  framework::dataset::make("Expected", { false, true, true, false, false })),
111  input_info, weights_info, bias_info, output_info, transpose_weights, reshaped_weights, expected)
112 {
113  // Create Fully Connected layer info
114  FullyConnectedLayerInfo fc_info;
115  fc_info.transpose_weights = transpose_weights;
116  fc_info.are_weights_reshaped = reshaped_weights;
117 
118  Status status = CLFullyConnectedLayer::validate(&input_info.clone()->set_is_resizable(false),
119  &weights_info.clone()->set_is_resizable(false),
120  &bias_info.clone()->set_is_resizable(false),
121  &output_info.clone()->set_is_resizable(false),
122  fc_info);
124 }
125 // clang-format on
126 // *INDENT-ON*
127 
128 template <typename T>
129 using CLFullyConnectedLayerFixture = FullyConnectedLayerValidationFixture<CLTensor, CLAccessor, CLFullyConnectedLayer, T>;
130 template <typename T>
131 using CLFullyConnectedLayerMixedDataLayoutFixture = FullyConnectedLayerValidationFixture<CLTensor, CLAccessor, CLFullyConnectedLayer, T, true>;
132 template <typename T>
133 using CLFullyConnectedLayerDynamicWeightsFixture = FullyConnectedWithDynamicWeightsFixture<CLTensor, CLAccessor, CLFullyConnectedLayer, T>;
134 
135 TEST_SUITE(Float)
136 TEST_SUITE(FP16)
137 FIXTURE_DATA_TEST_CASE(RunSmall, CLFullyConnectedLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallFullyConnectedLayerDataset(),
138  FullyConnectedParameters),
139  framework::dataset::make("DataType", DataType::F16)),
140  ActivationFunctionsDataset))
141 {
142  // Validate output
143  validate(CLAccessor(_target), _reference, tolerance_f16, tolerance_num);
144 }
146  FullyConnectedParameters),
148  ActivationFunctionsDataset))
149 {
150  // Validate output
151  validate(CLAccessor(_target), _reference, tolerance_f16, tolerance_num);
152 }
154 
155 TEST_SUITE(FP32)
156 FIXTURE_DATA_TEST_CASE(RunSmall, CLFullyConnectedLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallFullyConnectedLayerDataset(), FullyConnectedParameters),
157  framework::dataset::make("DataType", DataType::F32)),
158  ActivationFunctionsDataset))
159 {
160  // Validate output
161  validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0, abs_tolerance_f32);
162 }
164  framework::dataset::make("Input", TensorShape(9U, 5U, 7U)),
165  framework::dataset::make("Weights", TensorShape(315U, 271U))),
166  framework::dataset::make("Biases", TensorShape(271U))),
167  framework::dataset::make("Output", TensorShape(271U))),
168  FullyConnectedParameters),
171 {
172  // Validate output
173  validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0, abs_tolerance_f32);
174 }
178 {
179 }
180 FIXTURE_DATA_TEST_CASE(RunLarge, CLFullyConnectedLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeFullyConnectedLayerDataset(), FullyConnectedParameters),
182  ActivationFunctionsDataset))
183 {
184  // Validate output
185  validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0, abs_tolerance_f32);
186 }
189 
190 template <typename T>
191 using CLFullyConnectedLayerQuantizedFixture = FullyConnectedLayerValidationQuantizedFixture<CLTensor, CLAccessor, CLFullyConnectedLayer, T>;
192 template <typename T>
193 using CLFullyConnectedLayerQuantizedMixedDataLayoutFixture = FullyConnectedLayerValidationQuantizedFixture<CLTensor, CLAccessor, CLFullyConnectedLayer, T, true>;
194 
195 TEST_SUITE(Quantized)
196 TEST_SUITE(QASYMM8)
198  combine(combine(combine(combine(datasets::SmallFullyConnectedLayerDataset(), FullyConnectedParameters), framework::dataset::make("DataType", DataType::QASYMM8)), QuantizationData),
199  ActivationFunctionsQuantizedDataset))
200 {
201  // Validate output
202  validate(CLAccessor(_target), _reference, tolerance_qasymm8);
203 }
206  framework::dataset::make("Input", TensorShape(9U, 5U, 7U)),
207  framework::dataset::make("Weights", TensorShape(315U, 271U))),
208  framework::dataset::make("Biases", TensorShape(271U))),
209  framework::dataset::make("Output", TensorShape(271U))),
210  FullyConnectedParameters),
214 {
215  // Validate output
216  validate(CLAccessor(_target), _reference, tolerance_qasymm8);
217 }
219  combine(combine(combine(combine(datasets::LargeFullyConnectedLayerDataset(), FullyConnectedParameters), framework::dataset::make("DataType", DataType::QASYMM8)), QuantizationData),
220  ActivationFunctionsQuantizedDataset))
221 {
222  // Validate output
223  validate(CLAccessor(_target), _reference, tolerance_qasymm8);
224 }
225 TEST_SUITE_END() /* QASYMM8 */
228  combine(combine(combine(combine(datasets::SmallFullyConnectedLayerDataset(), FullyConnectedParameters), framework::dataset::make("DataType", DataType::QASYMM8_SIGNED)), QuantizationData),
229  ActivationFunctionsQuantizedDataset))
230 {
231  // Validate output
232  validate(CLAccessor(_target), _reference, tolerance_qasymm8);
233 }
236  framework::dataset::make("Input", TensorShape(9U, 5U, 7U)),
237  framework::dataset::make("Weights", TensorShape(315U, 271U))),
238  framework::dataset::make("Biases", TensorShape(271U))),
239  framework::dataset::make("Output", TensorShape(271U))),
240  FullyConnectedParameters),
244 {
245  // Validate output
246  validate(CLAccessor(_target), _reference, tolerance_qasymm8);
247 }
248 TEST_SUITE_END() // QASYMM8_SIGNED
249 TEST_SUITE_END() // Quantized
250 TEST_SUITE_END() // FullyConnectedLayer
251 TEST_SUITE_END() // CL
252 } // namespace validation
253 } // namespace test
254 } // namespace arm_compute
Shape of a tensor.
Definition: TensorShape.h:39
FullyConnectedLayerValidationFixture< CLTensor, CLAccessor, CLFullyConnectedLayer, T > CLFullyConnectedLayerFixture
FullyConnectedLayerValidationQuantizedFixture< CLTensor, CLAccessor, CLFullyConnectedLayer, T > CLFullyConnectedLayerQuantizedFixture
half_float::half half
16-bit floating point type
Definition: Types.h:46
1 channel, 1 F32 per channel
ARM_COMPUTE_EXPECT(has_error==expected, framework::LogLevel::ERRORS)
Fully connected layer info.
Definition: Types.h:1538
std::enable_if< is_container< T >::value, ContainerDataset< T > >::type make(std::string name, T &&values)
Helper function to create a ContainerDataset.
Status class.
Definition: Error.h:52
Activation Layer Information class.
Definition: Types.h:1475
Copyright (c) 2017-2021 Arm Limited.
1 channel, 1 F16 per channel
DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(framework::dataset::make("InputInfo", { TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QASYMM8), TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QASYMM8), TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16), }), framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F16), TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QASYMM8), TensorInfo(TensorShape(27U, 13U, 2U), 1, DataType::QASYMM8), TensorInfo(TensorShape(30U, 11U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16, QuantizationInfo(1.f/32768.f, 0)), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16, QuantizationInfo(1.f/32768.f, 0)), TensorInfo(TensorShape(32U, 13U, 2U), 1, DataType::QSYMM16, QuantizationInfo(1.f/32768.f, 0)), })), framework::dataset::make("ActivationInfo", { ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::SQRT), })), framework::dataset::make("Expected", { false, true, true, true, false, false, true, true, false })), input_info, output_info, act_info, expected)
DatasetMode
Possible dataset modes.
Definition: DatasetModes.h:40
TEST_SUITE_END() FIXTURE_DATA_TEST_CASE(RunSmall
[CLActivationLayer Test snippet]
static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, FullyConnectedLayerInfo fc_info=FullyConnectedLayerInfo())
Static function to check if given info will lead to a valid configuration of CLFullyConnectedLayer.
quantized, asymmetric fixed-point 8-bit number unsigned
bool are_weights_reshaped
Reshape the weights tensor if false.
Definition: Types.h:1545
Accessor implementation for CLTensor objects.
Definition: CLAccessor.h:36
FullyConnectedLayerValidationFixture< CLTensor, CLAccessor, CLFullyConnectedLayer, T, true > CLFullyConnectedLayerMixedDataLayoutFixture
TEST_SUITE(U8_to_S8) FIXTURE_DATA_TEST_CASE(RunSmall
validate(CLAccessor(output_state), expected_output)
FIXTURE_DATA_TEST_CASE(RunSmall, CLAbsLayerFixture< half >, framework::DatasetMode::PRECOMMIT, combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::F16)))
Definition: AbsLayer.cpp:50
FullyConnectedWithDynamicWeightsFixture< CLTensor, CLAccessor, CLFullyConnectedLayer, T > CLFullyConnectedLayerDynamicWeightsFixture
FullyConnectedLayerValidationQuantizedFixture< CLTensor, CLAccessor, CLFullyConnectedLayer, T, true > CLFullyConnectedLayerQuantizedMixedDataLayoutFixture
bool transpose_weights
Transpose weights if true.
Definition: Types.h:1544
Store the tensor&#39;s metadata.
Definition: TensorInfo.h:43
JoinDataset< T, U > concat(T &&dataset1, U &&dataset2)
Helper function to create a JoinDataset.
Definition: JoinDataset.h:160
quantized, asymmetric fixed-point 8-bit number signed
RelativeTolerance< half_float::half > tolerance_f16(half(0.2))
F16 Tolerance value for comparing reference&#39;s output against implementation&#39;s output for floating poi...
zip(zip(framework::dataset::make("Weights", { TensorInfo(TensorShape(32U, 13U, 2U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U, 2U), 1, DataType::F32), TensorInfo(TensorShape(32U, 13U, 2U, 1U), 1, DataType::F32), }), framework::dataset::make("MVBGInfo",{ TensorInfo(TensorShape(2U), 1, DataType::F32), TensorInfo(TensorShape(2U), 1, DataType::F16), TensorInfo(TensorShape(5U), 1, DataType::F32), })), framework::dataset::make("Expected", { true, false, false}))
DataType
Available data types.
Definition: Types.h:77
constexpr float abs_tolerance_f32(0.0001f)
F32 Absolute tolerance value for comparing reference&#39;s output against implementation&#39;s output for flo...
combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::F32)))
Definition: AbsLayer.cpp:65