IVGCVSW-1946: Remove armnn/src from the include paths
[platform/upstream/armnn.git] / src / backends / backendsCommon / test / LayerReleaseConstantDataTest.cpp
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5
6 #include <Graph.hpp>
7
8 #include <backendsCommon/CpuTensorHandle.hpp>
9 #include <backendsCommon/WorkloadData.hpp>
10
11 #include <cl/ClWorkloadFactory.hpp>
12
13 #include <boost/cast.hpp>
14 #include <boost/test/unit_test.hpp>
15
16 #include <utility>
17
18 using namespace armnn;
19 using namespace std;
20
21 // connects two layers
22 void Connect(Layer* from, Layer* to, const TensorInfo& tensorInfo, unsigned int fromIndex = 0, unsigned int toIndex = 0)
23 {
24     from->GetOutputSlot(fromIndex).Connect(to->GetInputSlot(toIndex));
25     from->GetOutputHandler(fromIndex).SetTensorInfo(tensorInfo);
26 }
27
28 /////////////////////////////////////////////////////////////////////////////////////////////
29 // The following test are created specifically to test ReleaseConstantData() method in the Layer
30 // They build very simple graphs including the layer will be checked.
31 // Checks weights and biases before the method called and after.
32 /////////////////////////////////////////////////////////////////////////////////////////////
33
34 BOOST_AUTO_TEST_SUITE(LayerReleaseConstantDataTest)
35
36 BOOST_AUTO_TEST_CASE(ReleaseBatchNormalizationLayerConstantDataTest)
37 {
38     Graph             graph;
39     ClWorkloadFactory factory;
40
41     // create the layer we're testing
42     BatchNormalizationDescriptor layerDesc;
43     layerDesc.m_Eps = 0.05f;
44     BatchNormalizationLayer* const layer = graph.AddLayer<BatchNormalizationLayer>(layerDesc, "layer");
45
46     armnn::TensorInfo weightInfo({3}, armnn::DataType::Float32);
47     layer->m_Mean     = std::make_unique<ScopedCpuTensorHandle>(weightInfo);
48     layer->m_Variance = std::make_unique<ScopedCpuTensorHandle>(weightInfo);
49     layer->m_Beta     = std::make_unique<ScopedCpuTensorHandle>(weightInfo);
50     layer->m_Gamma    = std::make_unique<ScopedCpuTensorHandle>(weightInfo);
51     layer->m_Mean->Allocate();
52     layer->m_Variance->Allocate();
53     layer->m_Beta->Allocate();
54     layer->m_Gamma->Allocate();
55
56     // create extra layers
57     Layer* const input = graph.AddLayer<InputLayer>(0, "input");
58     Layer* const output = graph.AddLayer<OutputLayer>(0, "output");
59
60     // connect up
61     armnn::TensorInfo tensorInfo({2, 3, 1, 1}, armnn::DataType::Float32);
62     Connect(input, layer, tensorInfo);
63     Connect(layer, output, tensorInfo);
64
65     // check the constants that they are not NULL
66     BOOST_CHECK(layer->m_Mean != nullptr);
67     BOOST_CHECK(layer->m_Variance != nullptr);
68     BOOST_CHECK(layer->m_Beta != nullptr);
69     BOOST_CHECK(layer->m_Gamma != nullptr);
70
71     // free up the constants..
72     layer->ReleaseConstantData();
73
74     // check the constants that they are NULL now
75     BOOST_CHECK(layer->m_Mean == nullptr);
76     BOOST_CHECK(layer->m_Variance == nullptr);
77     BOOST_CHECK(layer->m_Beta == nullptr);
78     BOOST_CHECK(layer->m_Gamma == nullptr);
79
80  }
81
82
83  BOOST_AUTO_TEST_CASE(ReleaseConvolution2dLayerConstantDataTest)
84  {
85      Graph             graph;
86      ClWorkloadFactory factory;
87
88      // create the layer we're testing
89      Convolution2dDescriptor layerDesc;
90      layerDesc.m_PadLeft = 3;
91      layerDesc.m_PadRight = 3;
92      layerDesc.m_PadTop = 1;
93      layerDesc.m_PadBottom = 1;
94      layerDesc.m_StrideX = 2;
95      layerDesc.m_StrideY = 4;
96      layerDesc.m_BiasEnabled = true;
97
98      Convolution2dLayer* const layer = graph.AddLayer<Convolution2dLayer>(layerDesc, "layer");
99
100      layer->m_Weight = std::make_unique<ScopedCpuTensorHandle>(TensorInfo({2, 3, 5, 3},
101                                                                           armnn::DataType::Float32));
102      layer->m_Bias   = std::make_unique<ScopedCpuTensorHandle>
103              (TensorInfo({2}, GetBiasDataType(armnn::DataType::Float32)));
104
105      layer->m_Weight->Allocate();
106      layer->m_Bias->Allocate();
107
108      // create extra layers
109      Layer* const input = graph.AddLayer<InputLayer>(0, "input");
110      Layer* const output = graph.AddLayer<OutputLayer>(0, "output");
111
112      // connect up
113      Connect(input, layer, TensorInfo({2, 3, 8, 16}, armnn::DataType::Float32));
114      Connect(layer, output, TensorInfo({2, 2, 2, 10}, armnn::DataType::Float32));
115
116      // check the constants that they are not NULL
117      BOOST_CHECK(layer->m_Weight != nullptr);
118      BOOST_CHECK(layer->m_Bias != nullptr);
119
120      // free up the constants..
121      layer->ReleaseConstantData();
122
123      // check the constants that they are NULL now
124      BOOST_CHECK(layer->m_Weight == nullptr);
125      BOOST_CHECK(layer->m_Bias == nullptr);
126 }
127
128 BOOST_AUTO_TEST_CASE(ReleaseDepthwiseConvolution2dLayerConstantDataTest)
129 {
130     Graph             graph;
131     ClWorkloadFactory factory;
132
133     // create the layer we're testing
134     DepthwiseConvolution2dDescriptor layerDesc;
135     layerDesc.m_PadLeft         = 3;
136     layerDesc.m_PadRight        = 3;
137     layerDesc.m_PadTop          = 1;
138     layerDesc.m_PadBottom       = 1;
139     layerDesc.m_StrideX         = 2;
140     layerDesc.m_StrideY         = 4;
141     layerDesc.m_BiasEnabled     = true;
142
143     DepthwiseConvolution2dLayer* const layer = graph.AddLayer<DepthwiseConvolution2dLayer>(layerDesc, "layer");
144
145     layer->m_Weight = std::make_unique<ScopedCpuTensorHandle>(TensorInfo({3, 3, 5, 3}, DataType::Float32));
146     layer->m_Bias   = std::make_unique<ScopedCpuTensorHandle>(TensorInfo({9}, DataType::Float32));
147     layer->m_Weight->Allocate();
148     layer->m_Bias->Allocate();
149
150     // create extra layers
151     Layer* const input = graph.AddLayer<InputLayer>(0, "input");
152     Layer* const output = graph.AddLayer<OutputLayer>(0, "output");
153
154     // connect up
155     Connect(input, layer, TensorInfo({2, 3, 8, 16}, armnn::DataType::Float32));
156     Connect(layer, output, TensorInfo({2, 9, 2, 10}, armnn::DataType::Float32));
157
158     // check the constants that they are not NULL
159     BOOST_CHECK(layer->m_Weight != nullptr);
160     BOOST_CHECK(layer->m_Bias != nullptr);
161
162     // free up the constants..
163     layer->ReleaseConstantData();
164
165     // check the constants that they are NULL now
166     BOOST_CHECK(layer->m_Weight == nullptr);
167     BOOST_CHECK(layer->m_Bias == nullptr);
168 }
169
170 BOOST_AUTO_TEST_CASE(ReleaseFullyConnectedLayerConstantDataTest)
171 {
172     Graph             graph;
173     ClWorkloadFactory factory;
174
175     // create the layer we're testing
176     FullyConnectedDescriptor layerDesc;
177     layerDesc.m_BiasEnabled = true;
178     layerDesc.m_TransposeWeightMatrix = true;
179
180     FullyConnectedLayer* const layer = graph.AddLayer<FullyConnectedLayer>(layerDesc, "layer");
181
182     float inputsQScale = 1.0f;
183     float outputQScale = 2.0f;
184
185     layer->m_Weight = std::make_unique<ScopedCpuTensorHandle>(TensorInfo({7, 20},
186                                                           DataType::QuantisedAsymm8, inputsQScale, 0));
187     layer->m_Bias   = std::make_unique<ScopedCpuTensorHandle>(TensorInfo({7},
188                                                           GetBiasDataType(DataType::QuantisedAsymm8), inputsQScale));
189     layer->m_Weight->Allocate();
190     layer->m_Bias->Allocate();
191
192     // create extra layers
193     Layer* const input = graph.AddLayer<InputLayer>(0, "input");
194     Layer* const output = graph.AddLayer<OutputLayer>(0, "output");
195
196     // connect up
197     Connect(input, layer, TensorInfo({3, 1, 4, 5}, DataType::QuantisedAsymm8, inputsQScale));
198     Connect(layer, output, TensorInfo({3, 7}, DataType::QuantisedAsymm8, outputQScale));
199
200     // check the constants that they are not NULL
201     BOOST_CHECK(layer->m_Weight != nullptr);
202     BOOST_CHECK(layer->m_Bias != nullptr);
203
204     // free up the constants..
205     layer->ReleaseConstantData();
206
207     // check the constants that they are NULL now
208     BOOST_CHECK(layer->m_Weight == nullptr);
209     BOOST_CHECK(layer->m_Bias == nullptr);
210 }
211
212 BOOST_AUTO_TEST_SUITE_END()
213