Compute Library
 20.02.1
FullyConnectedLayer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2020 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 /** CNN data types */
55 const auto CNNDataTypes = framework::dataset::make("DataType",
56 {
61 });
62 
63 const auto FullyConnectedParameters = combine(framework::dataset::make("TransposeWeights", { false, true }), framework::dataset::make("ReshapeWeights", { false, true }));
64 
65 const auto QuantizationData = framework::dataset::make("QuantizationInfo",
66 {
67  QuantizationInfo(1.f / 255.f, 10),
68  QuantizationInfo(1.1f, 10),
69 });
70 } // namespace
71 
72 TEST_SUITE(CL)
73 TEST_SUITE(FullyConnectedLayer)
74 
75 DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(combine(datasets::SmallFullyConnectedLayerDataset(),
76  FullyConnectedParameters),
77  CNNDataTypes),
78  src_shape, weights_shape, bias_shape, dst_shape, transpose_weights, reshape_weights, data_type)
79 {
81  const QuantizationInfo quantization_info = is_data_type_quantized_asymmetric(data_type) ? QuantizationInfo(2.f / 255.f, 127) : QuantizationInfo();
82 
84 
85  // Transpose weights if not done in the function
86  if(!reshape_weights || !transpose_weights)
87  {
88  const size_t shape_x = ws.x();
89  ws.set(0, ws.y());
90  ws.set(1, shape_x);
91  }
92 
93  // Create tensors
94  CLTensor src = create_tensor<CLTensor>(src_shape, data_type, 1, quantization_info);
95  CLTensor weights = create_tensor<CLTensor>(ws, data_type, 1, quantization_info);
96  CLTensor bias = create_tensor<CLTensor>(bias_shape, bias_data_type, 1, quantization_info);
97  CLTensor dst = create_tensor<CLTensor>(dst_shape, data_type, 1, quantization_info);
98 
99  ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
103 
104  const QuantizationInfo src_quantization_info = src.info()->quantization_info();
106 
107  // Create Fully Connected layer info
108  FullyConnectedLayerInfo fc_info;
109  fc_info.transpose_weights = transpose_weights;
110  fc_info.are_weights_reshaped = !reshape_weights;
111 
112  // Create and configure function.
114  fc.configure(&src, &weights, &bias, &dst, fc_info);
115 
116  // Validate valid region
119 
120  // Validate QuantizationInfo
123 }
124 
125 // *INDENT-OFF*
126 // clang-format off
128  framework::dataset::make("InputInfo", { TensorInfo(TensorShape(9U, 5U, 7U, 3U), 1, DataType::F32), // Mismatching data types
129  TensorInfo(TensorShape(8U, 4U, 6U, 4U), 1, DataType::F32),
130  TensorInfo(TensorShape(8U, 4U, 6U, 4U), 1, DataType::F32),
131  TensorInfo(TensorShape(9U, 5U, 7U, 3U), 1, DataType::F32), // Invalid weights dimensions
132  TensorInfo(TensorShape(9U, 5U, 7U, 3U), 1, DataType::F32), // Wrongly reshaped weights
133  }),
134  framework::dataset::make("WeightsInfo",{ TensorInfo(TensorShape(315U, 271U), 1, DataType::F16),
135  TensorInfo(TensorShape(192U, 192U), 1, DataType::F32),
136  TensorInfo(TensorShape(192U, 192U), 1, DataType::F32),
137  TensorInfo(TensorShape(217U, 231U), 1, DataType::F32),
138  TensorInfo(TensorShape(217U, 315U), 1, DataType::F32),
139  })),
145  })),
146  framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(271U, 3U), 1, DataType::F32),
147  TensorInfo(TensorShape(192U, 4U), 1, DataType::F32),
148  TensorInfo(TensorShape(192U, 4U), 1, DataType::F32),
149  TensorInfo(TensorShape(271U, 3U), 1, DataType::F32),
150  TensorInfo(TensorShape(271U, 3U), 1, DataType::F32),
151  })),
152  framework::dataset::make("TransposeWeights",{ true, true, false, true, true })),
153  framework::dataset::make("ReshapedWeights",{ false, false, false, false, false})),
154  framework::dataset::make("Expected", { false, true, true, false, false })),
155  input_info, weights_info, bias_info, output_info, transpose_weights, reshaped_weights, expected)
156 {
157  // Create Fully Connected layer info
158  FullyConnectedLayerInfo fc_info;
159  fc_info.transpose_weights = transpose_weights;
160  fc_info.are_weights_reshaped = reshaped_weights;
161 
162  Status status = CLFullyConnectedLayer::validate(&input_info.clone()->set_is_resizable(false),
163  &weights_info.clone()->set_is_resizable(false),
164  &bias_info.clone()->set_is_resizable(false),
165  &output_info.clone()->set_is_resizable(false),
166  fc_info);
168 }
169 // clang-format on
170 // *INDENT-ON*
171 
172 template <typename T>
173 using CLFullyConnectedLayerFixture = FullyConnectedLayerValidationFixture<CLTensor, CLAccessor, CLFullyConnectedLayer, T>;
174 
175 TEST_SUITE(Float)
176 TEST_SUITE(FP16)
177 FIXTURE_DATA_TEST_CASE(RunSmall, CLFullyConnectedLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallFullyConnectedLayerDataset(),
178  FullyConnectedParameters),
179  framework::dataset::make("DataType", DataType::F16)))
180 {
181  // Validate output
182  validate(CLAccessor(_target), _reference, tolerance_f16, tolerance_num);
183 }
185  FullyConnectedParameters),
187 {
188  // Validate output
189  validate(CLAccessor(_target), _reference, tolerance_f16, tolerance_num);
190 }
192 
193 TEST_SUITE(FP32)
194 FIXTURE_DATA_TEST_CASE(RunSmall, CLFullyConnectedLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallFullyConnectedLayerDataset(), FullyConnectedParameters),
195  framework::dataset::make("DataType", DataType::F32)))
196 {
197  // Validate output
198  validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0, abs_tolerance_f32);
199 }
200 FIXTURE_DATA_TEST_CASE(RunLarge, CLFullyConnectedLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargeFullyConnectedLayerDataset(), FullyConnectedParameters),
202 {
203  // Validate output
204  validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0, abs_tolerance_f32);
205 }
208 
209 template <typename T>
210 using CLFullyConnectedLayerQuantizedFixture = FullyConnectedLayerValidationQuantizedFixture<CLTensor, CLAccessor, CLFullyConnectedLayer, T>;
211 
212 TEST_SUITE(Quantized)
213 TEST_SUITE(QASYMM8)
215  combine(combine(combine(datasets::SmallFullyConnectedLayerDataset(), FullyConnectedParameters), framework::dataset::make("DataType", DataType::QASYMM8)), QuantizationData))
216 {
217  // Validate output
218  validate(CLAccessor(_target), _reference, tolerance_qasymm8);
219 }
221  combine(combine(combine(datasets::LargeFullyConnectedLayerDataset(), FullyConnectedParameters), framework::dataset::make("DataType", DataType::QASYMM8)), QuantizationData))
222 {
223  // Validate output
224  validate(CLAccessor(_target), _reference, tolerance_qasymm8);
225 }
226 TEST_SUITE_END() /* QASYMM8 */
227 TEST_SUITE(QASYMM8_SIGNED)
229  combine(combine(combine(datasets::SmallFullyConnectedLayerDataset(), FullyConnectedParameters), framework::dataset::make("DataType", DataType::QASYMM8_SIGNED)), QuantizationData))
230 {
231  // Validate output
232  validate(CLAccessor(_target), _reference, tolerance_qasymm8);
233 }
234 TEST_SUITE_END() /* QASYMM8_SIGNED */
235 TEST_SUITE_END() /* Quantized */
236 
239 } // namespace validation
240 } // namespace test
241 } // namespace arm_compute
Shape of a tensor.
Definition: TensorShape.h:39
Basic function to compute a Fully Connected layer on OpenCL.
TensorInfo * info() const override
Interface to be implemented by the child class to return the tensor's metadata.
Definition: CLTensor.cpp:41
bool is_resizable() const override
Flag indicating whether the size of the tensor can be changed.
Definition: TensorInfo.h:285
FullyConnectedLayerValidationFixture< CLTensor, CLAccessor, CLFullyConnectedLayer, T > CLFullyConnectedLayerFixture
ValidRegion valid_region() const override
Valid region of the tensor.
Definition: TensorInfo.h:303
FullyConnectedLayerValidationQuantizedFixture< CLTensor, CLAccessor, CLFullyConnectedLayer, T > CLFullyConnectedLayerQuantizedFixture
QuantizationInfo quantization_info() const override
Get the quantization settings (scale and offset) of the tensor.
Definition: TensorInfo.h:311
half_float::half half
16-bit floating point type
Definition: Types.h:45
1 channel, 1 F32 per channel
ARM_COMPUTE_EXPECT(has_error==expected, framework::LogLevel::ERRORS)
Fully connected layer info.
Definition: Types.h:803
std::enable_if< is_container< T >::value, ContainerDataset< T > >::type make(std::string name, T &&values)
Helper function to create a ContainerDataset.
const QuantizationInfo src_quantization_info
Copyright (c) 2017-2020 ARM Limited.
1 channel, 1 F16 per channel
1 channel, 1 S32 per channel
T x() const
Alias to access the size of the first dimension.
Definition: Dimensions.h:81
void configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, FullyConnectedLayerInfo fc_info=FullyConnectedLayerInfo())
Set the input and output tensors.
Quantization information.
DatasetMode
Possible dataset modes.
Definition: DatasetModes.h:40
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:807
Accessor implementation for CLTensor objects.
Definition: CLAccessor.h:35
bool is_data_type_quantized_asymmetric(DataType dt)
Check if a given data type is of asymmetric quantized type.
Definition: Utils.h:1139
FIXTURE_DATA_TEST_CASE(RunSmall, CLAbsLayerFixture< half >, framework::DatasetMode::PRECOMMIT, combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::F16)))
Definition: AbsLayer.cpp:50
validate(dst.info() ->valid_region(), valid_region)
const ValidRegion dst_valid_region
TEST_SUITE_END() FIXTURE_DATA_TEST_CASE(RunSmall
Input data sets.
const QuantizationInfo weights_quantization_info
bool transpose_weights
Transpose weights if true.
Definition: Types.h:806
TensorShape & set(size_t dimension, size_t value, bool apply_dim_correction=true)
Accessor to set the value of one of the dimensions.
Definition: TensorShape.h:78
Store the tensor's metadata.
Definition: TensorInfo.h:45
T y() const
Alias to access the size of the second dimension.
Definition: Dimensions.h:86
quantized, asymmetric fixed-point 8-bit number signed
RelativeTolerance< half_float::half > tolerance_f16(half(0.2))
F16 Tolerance value for comparing reference's output against implementation's output for floating poi...
Container for valid region of a window.
Definition: Types.h:184
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:75
constexpr float abs_tolerance_f32(0.0001f)
F32 Absolute tolerance value for comparing reference's output against implementation's output for flo...
combine(datasets::SmallShapes(), framework::dataset::make("DataType", DataType::F32)))
Definition: AbsLayer.cpp:65
TEST_SUITE(U8_to_S8) DATA_TEST_CASE(Configuration
ValidRegion shape_to_valid_region(const TensorShape &a_shape, bool border_undefined=false, BorderSize border_size=BorderSize(0))
Create a valid region based on tensor shape, border mode and border size.
Definition: Utils.h:224
cast configure & src
Definition: Cast.cpp:169
DATA_TEST_CASE(Configuration, framework::DatasetMode::ALL, combine(datasets::SmallShapes(), AbsoluteDifferenceU8Dataset), shape, data_type0, data_type1, output_data_type)
Basic implementation of the OpenCL tensor interface.
Definition: CLTensor.h:41