Compute Library
 20.05
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"
31 #include "tests/datasets/FullyConnectedLayerDataset.h"
33 #include "tests/framework/Macros.h"
36 #include "tests/validation/fixtures/FullyConnectedLayerFixture.h"
37 
38 namespace arm_compute
39 {
40 namespace test
41 {
42 namespace validation
43 {
44 namespace
45 {
46 /** Tolerance for float operations */
47 constexpr RelativeTolerance<float> rel_tolerance_f32(0.05f);
48 constexpr AbsoluteTolerance<float> abs_tolerance_f32(0.0001f);
49 RelativeTolerance<half_float::half> tolerance_f16(half(0.2));
50 constexpr float tolerance_num = 0.07f; /**< Tolerance number */
51 
52 /** CNN data types */
53 const auto CNNDataTypes = framework::dataset::make("DataType",
54 {
57 });
58 
59 const auto FullyConnectedParameters = combine(framework::dataset::make("TransposeWeights", { false, true }), framework::dataset::make("ReshapeWeights", { false, true }));
60 const auto ActivationFunctionsDataset = framework::dataset::make("ActivationInfo", ActivationLayerInfo());
61 } // namespace
62 
63 TEST_SUITE(GC)
64 TEST_SUITE(FullyConnectedLayer)
65 
66 DATA_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 {
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 
81  // Create tensors
82  GCTensor src = create_tensor<GCTensor>(src_shape, data_type, 1);
83  GCTensor weights = create_tensor<GCTensor>(ws, data_type, 1);
84  GCTensor bias = create_tensor<GCTensor>(bias_shape, data_type, 1);
85  GCTensor dst = create_tensor<GCTensor>(dst_shape, data_type, 1);
86 
87  ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
91 
92  // Create Fully Connected layer info
94  fc_info.transpose_weights = transpose_weights;
95  fc_info.are_weights_reshaped = !reshape_weights;
96 
97  // Create and configure function.
99  fc.configure(&src, &weights, &bias, &dst, fc_info);
100 
101  // Validate valid region
104 }
105 
106 template <typename T>
107 using GCFullyConnectedLayerFixture = FullyConnectedLayerValidationFixture<GCTensor, GCAccessor, GCFullyConnectedLayer, T>;
108 
109 TEST_SUITE(Float)
110 TEST_SUITE(FP16)
111 FIXTURE_DATA_TEST_CASE(RunSmall, GCFullyConnectedLayerFixture<half_float::half>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallFullyConnectedLayerDataset(),
112  FullyConnectedParameters),
113  framework::dataset::make("DataType", DataType::F16)),
114  ActivationFunctionsDataset))
115 {
116  // Validate output
117  validate(GCAccessor(_target), _reference, tolerance_f16, tolerance_num);
118 }
120  FullyConnectedParameters),
122  ActivationFunctionsDataset))
123 {
124  // Validate output
125  validate(GCAccessor(_target), _reference, tolerance_f16, tolerance_num);
126 }
128 
129 TEST_SUITE(FP32)
130 FIXTURE_DATA_TEST_CASE(RunSmall, GCFullyConnectedLayerFixture<float>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(datasets::SmallFullyConnectedLayerDataset(), FullyConnectedParameters),
131  framework::dataset::make("DataType", DataType::F32)),
132  ActivationFunctionsDataset))
133 {
134  // Validate output
135  validate(GCAccessor(_target), _reference, rel_tolerance_f32);
136 }
137 FIXTURE_DATA_TEST_CASE(RunLarge, GCFullyConnectedLayerFixture<float>, framework::DatasetMode::NIGHTLY, combine(combine(combine(datasets::LargeFullyConnectedLayerDataset(), FullyConnectedParameters),
139  ActivationFunctionsDataset))
140 {
141  // Validate output
142  validate(GCAccessor(_target), _reference, rel_tolerance_f32, 0, abs_tolerance_f32);
143 }
146 
149 } // namespace validation
150 } // namespace test
151 } // namespace arm_compute
void configure(const IGCTensor *input, const IGCTensor *weights, const IGCTensor *biases, IGCTensor *output, FullyConnectedLayerInfo fc_info=FullyConnectedLayerInfo())
Set the input and output tensors.
Shape of a tensor.
Definition: TensorShape.h:39
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
Accessor implementation for GCTensor objects.
Definition: GCAccessor.h:35
ValidRegion valid_region() const override
Valid region of the tensor.
Definition: TensorInfo.h:303
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:1580
std::enable_if< is_container< T >::value, ContainerDataset< T > >::type make(std::string name, T &&values)
Helper function to create a ContainerDataset.
FullyConnectedLayerValidationFixture< GCTensor, GCAccessor, GCFullyConnectedLayer, T > GCFullyConnectedLayerFixture
Interface for OpenGL ES tensor.
Definition: GCTensor.h:38
Copyright (c) 2017-2020 ARM Limited.
1 channel, 1 F16 per channel
ITensorInfo * info() const override
Interface to be implemented by the child class to return the tensor's metadata.
Definition: Tensor.cpp:33
T x() const
Alias to access the size of the first dimension.
Definition: Dimensions.h:81
virtual bool is_resizable() const =0
Flag indicating whether the size of the tensor can be changed.
DatasetMode
Possible dataset modes.
Definition: DatasetModes.h:40
TEST_SUITE_END() FIXTURE_DATA_TEST_CASE(RunSmall
[CLActivationLayer Test snippet]
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
bool transpose_weights
Transpose weights if true.
Definition: Types.h:1583
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
JoinDataset< T, U > concat(T &&dataset1, U &&dataset2)
Helper function to create a JoinDataset.
Definition: JoinDataset.h:160
T y() const
Alias to access the size of the second dimension.
Definition: Dimensions.h:86
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:187
DataType
Available data types.
Definition: Types.h:77
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:225
Basic function to compute a Fully Connected layer on OpenGL ES.
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)