Release 18.02
[platform/upstream/armnn.git] / src / armnn / test / Network_test.cpp
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // See LICENSE file in the project root for full license information.
4 //
5 #include <boost/test/unit_test.hpp>
6
7 #include "armnn/ArmNN.hpp"
8 #include "Network.hpp"
9 #include "Graph.hpp"
10 #include "backends/RefWorkloadFactory.hpp"
11
12 #include "GraphUtils.hpp"
13
14 namespace
15 {
16
17 bool AreAllLayerInputSlotsConnected(const armnn::IConnectableLayer& layer)
18 {
19     bool allConnected = true;
20     for (unsigned int i = 0; i < layer.GetNumInputSlots(); ++i)
21     {
22         const bool inputConnected = layer.GetInputSlot(i).GetConnection() != nullptr;
23         allConnected &= inputConnected;
24     }
25     return allConnected;
26 }
27
28 }
29
30 BOOST_AUTO_TEST_SUITE(Network)
31
32 BOOST_AUTO_TEST_CASE(NetworkBasic)
33 {
34     armnn::Network net;
35     BOOST_TEST(net.PrintGraph() == armnn::Status::Success);
36 }
37
38 BOOST_AUTO_TEST_CASE(LayerNamesAreOptionalForINetwork)
39 {
40     armnn::Network net;
41     armnn::INetwork& inet = net;
42     inet.AddInputLayer(0);
43     inet.AddAdditionLayer();
44     inet.AddActivationLayer(armnn::ActivationDescriptor());
45     inet.AddOutputLayer(0);
46 }
47
48 BOOST_AUTO_TEST_CASE(LayerNamesAreOptionalForNetwork)
49 {
50     armnn::Network net;
51     net.AddInputLayer(0);
52     net.AddAdditionLayer();
53     net.AddActivationLayer(armnn::ActivationDescriptor());
54     net.AddOutputLayer(0);
55 }
56
57 BOOST_AUTO_TEST_CASE(NetworkModification)
58 {
59     armnn::Network net;
60
61     armnn::IConnectableLayer* const inputLayer = net.AddInputLayer(0, "input layer");
62     BOOST_TEST(inputLayer);
63
64     unsigned int dims[] = { 10,1,1,1 };
65     std::vector<float> convWeightsData(10);
66     armnn::ConstTensor weights(armnn::TensorInfo(4, dims, armnn::DataType::Float32), convWeightsData);
67
68     armnn::Convolution2dDescriptor convDesc2d;
69     armnn::IConnectableLayer* const convLayer = net.AddConvolution2dLayer(convDesc2d, weights, "conv layer");
70     BOOST_TEST(convLayer);
71
72     inputLayer->GetOutputSlot(0).Connect(convLayer->GetInputSlot(0));
73
74     armnn::FullyConnectedDescriptor fullyConnectedDesc;
75     armnn::IConnectableLayer* const fullyConnectedLayer = net.AddFullyConnectedLayer(fullyConnectedDesc,
76                                                                                      weights,
77                                                                                      "fully connected");
78     BOOST_TEST(fullyConnectedLayer);
79
80     convLayer->GetOutputSlot(0).Connect(fullyConnectedLayer->GetInputSlot(0));
81
82     armnn::Pooling2dDescriptor pooling2dDesc;
83     armnn::IConnectableLayer* const poolingLayer = net.AddPooling2dLayer(pooling2dDesc, "pooling2d");
84     BOOST_TEST(poolingLayer);
85
86     fullyConnectedLayer->GetOutputSlot(0).Connect(poolingLayer->GetInputSlot(0));
87
88     armnn::ActivationDescriptor activationDesc;
89     armnn::IConnectableLayer* const activationLayer = net.AddActivationLayer(activationDesc, "activation");
90     BOOST_TEST(activationLayer);
91
92     poolingLayer->GetOutputSlot(0).Connect(activationLayer->GetInputSlot(0));
93
94     armnn::NormalizationDescriptor normalizationDesc;
95     armnn::IConnectableLayer* const normalizationLayer = net.AddNormalizationLayer(normalizationDesc, "normalization");
96     BOOST_TEST(normalizationLayer);
97
98     activationLayer->GetOutputSlot(0).Connect(normalizationLayer->GetInputSlot(0));
99
100     armnn::SoftmaxDescriptor softmaxDesc;
101     armnn::IConnectableLayer* const softmaxLayer = net.AddSoftmaxLayer(softmaxDesc, "softmax");
102     BOOST_TEST(softmaxLayer);
103
104     normalizationLayer->GetOutputSlot(0).Connect(softmaxLayer->GetInputSlot(0));
105
106     armnn::BatchNormalizationDescriptor batchNormDesc;
107
108     armnn::TensorInfo tensorInfo({ 1 }, armnn::DataType::Float32);
109     std::vector<float> data(tensorInfo.GetNumBytes() / sizeof(float));
110     armnn::ConstTensor invalidTensor(tensorInfo, data);
111
112     armnn::IConnectableLayer* const batchNormalizationLayer = net.AddBatchNormalizationLayer(batchNormDesc,
113         invalidTensor,
114         invalidTensor,
115         invalidTensor,
116         invalidTensor,
117         "batch norm");
118     BOOST_TEST(batchNormalizationLayer);
119
120     softmaxLayer->GetOutputSlot(0).Connect(batchNormalizationLayer->GetInputSlot(0));
121
122     armnn::IConnectableLayer* const additionLayer = net.AddAdditionLayer("addition");
123     BOOST_TEST(additionLayer);
124
125     batchNormalizationLayer->GetOutputSlot(0).Connect(additionLayer->GetInputSlot(0));
126     batchNormalizationLayer->GetOutputSlot(0).Connect(additionLayer->GetInputSlot(1));
127
128     armnn::IConnectableLayer* const multiplicationLayer = net.AddMultiplicationLayer("multiplication");
129     BOOST_TEST(multiplicationLayer);
130
131     additionLayer->GetOutputSlot(0).Connect(multiplicationLayer->GetInputSlot(0));
132     additionLayer->GetOutputSlot(0).Connect(multiplicationLayer->GetInputSlot(1));
133
134     armnn::IConnectableLayer* const outputLayer = net.AddOutputLayer(0, "output layer");
135     BOOST_TEST(outputLayer);
136
137     multiplicationLayer->GetOutputSlot(0).Connect(outputLayer->GetInputSlot(0));
138
139     //Test that all layers are present in the graph
140     BOOST_TEST(net.GetGraph().GetNumLayers() == 11);
141
142     //Test that the vertices exist and have correct names
143     BOOST_TEST(GraphHasNamedLayer(net.GetGraph(), "input layer"));
144     BOOST_TEST(GraphHasNamedLayer(net.GetGraph(), "conv layer"));
145     BOOST_TEST(GraphHasNamedLayer(net.GetGraph(), "fully connected"));
146     BOOST_TEST(GraphHasNamedLayer(net.GetGraph(), "pooling2d"));
147     BOOST_TEST(GraphHasNamedLayer(net.GetGraph(), "activation"));
148     BOOST_TEST(GraphHasNamedLayer(net.GetGraph(), "normalization"));
149     BOOST_TEST(GraphHasNamedLayer(net.GetGraph(), "softmax"));
150     BOOST_TEST(GraphHasNamedLayer(net.GetGraph(), "batch norm"));
151     BOOST_TEST(GraphHasNamedLayer(net.GetGraph(), "addition"));
152     BOOST_TEST(GraphHasNamedLayer(net.GetGraph(), "multiplication"));
153     BOOST_TEST(GraphHasNamedLayer(net.GetGraph(), "output layer"));
154
155     auto checkOneOutputToOneInputConnection = []
156         (const armnn::IConnectableLayer* const srcLayer,
157          const armnn::IConnectableLayer* const tgtLayer,
158          int expectedSrcNumInputs = 1,
159          int expectedDstNumOutputs = 1)
160         {
161             BOOST_TEST(srcLayer->GetNumInputSlots() == expectedSrcNumInputs);
162             BOOST_TEST(srcLayer->GetNumOutputSlots() == 1);
163             BOOST_TEST(tgtLayer->GetNumInputSlots() == 1);
164             BOOST_TEST(tgtLayer->GetNumOutputSlots() == expectedDstNumOutputs);
165
166             BOOST_TEST(srcLayer->GetOutputSlot(0).GetNumConnections() == 1);
167             BOOST_TEST(srcLayer->GetOutputSlot(0).GetConnection(0) == &tgtLayer->GetInputSlot(0));
168             BOOST_TEST(&srcLayer->GetOutputSlot(0) == tgtLayer->GetInputSlot(0).GetConnection());
169         };
170     auto checkOneOutputToTwoInputsConnections = []
171         (const armnn::IConnectableLayer* const srcLayer,
172          const armnn::IConnectableLayer* const tgtLayer,
173          int expectedSrcNumInputs,
174          int expectedDstNumOutputs = 1)
175         {
176             BOOST_TEST(srcLayer->GetNumInputSlots() == expectedSrcNumInputs);
177             BOOST_TEST(srcLayer->GetNumOutputSlots() == 1);
178             BOOST_TEST(tgtLayer->GetNumInputSlots() == 2);
179             BOOST_TEST(tgtLayer->GetNumOutputSlots() == expectedDstNumOutputs);
180
181             BOOST_TEST(srcLayer->GetOutputSlot(0).GetNumConnections() == 2);
182             for (unsigned int i = 0; i < srcLayer->GetOutputSlot(0).GetNumConnections(); ++i)
183             {
184                 BOOST_TEST(srcLayer->GetOutputSlot(0).GetConnection(i) == &tgtLayer->GetInputSlot(i));
185                 BOOST_TEST(&srcLayer->GetOutputSlot(0) == tgtLayer->GetInputSlot(i).GetConnection());
186             }
187         };
188
189     BOOST_TEST(AreAllLayerInputSlotsConnected(*convLayer));
190     BOOST_TEST(AreAllLayerInputSlotsConnected(*fullyConnectedLayer));
191     BOOST_TEST(AreAllLayerInputSlotsConnected(*poolingLayer));
192     BOOST_TEST(AreAllLayerInputSlotsConnected(*activationLayer));
193     BOOST_TEST(AreAllLayerInputSlotsConnected(*normalizationLayer));
194     BOOST_TEST(AreAllLayerInputSlotsConnected(*softmaxLayer));
195     BOOST_TEST(AreAllLayerInputSlotsConnected(*batchNormalizationLayer));
196     BOOST_TEST(AreAllLayerInputSlotsConnected(*additionLayer));
197     BOOST_TEST(AreAllLayerInputSlotsConnected(*multiplicationLayer));
198     BOOST_TEST(AreAllLayerInputSlotsConnected(*outputLayer));
199
200     // Check connectivity
201     checkOneOutputToOneInputConnection(inputLayer, convLayer, 0);
202     checkOneOutputToOneInputConnection(convLayer, fullyConnectedLayer);
203     checkOneOutputToOneInputConnection(fullyConnectedLayer, poolingLayer);
204     checkOneOutputToOneInputConnection(poolingLayer, activationLayer);
205     checkOneOutputToOneInputConnection(activationLayer, normalizationLayer);
206     checkOneOutputToOneInputConnection(normalizationLayer, softmaxLayer);
207     checkOneOutputToOneInputConnection(softmaxLayer, batchNormalizationLayer);
208     checkOneOutputToTwoInputsConnections(batchNormalizationLayer, additionLayer, 1);
209     checkOneOutputToTwoInputsConnections(additionLayer, multiplicationLayer, 2);
210     checkOneOutputToOneInputConnection(multiplicationLayer, outputLayer, 2, 0);
211 }
212
213 BOOST_AUTO_TEST_CASE(NetworkModification_SplitterMerger)
214 {
215     armnn::Network net;
216
217     // Add an input layer and an input tensor descriptor.
218     armnn::IConnectableLayer* inputLayer = net.AddInputLayer(0, "input layer");
219     BOOST_TEST(inputLayer);
220
221     // Add a splitter layer
222     armnn::ViewsDescriptor splitterDesc(2,4);
223
224     armnn::IConnectableLayer* splitterLayer = net.AddSplitterLayer(splitterDesc, "splitter layer");
225     BOOST_TEST(splitterLayer);
226
227     inputLayer->GetOutputSlot(0).Connect(splitterLayer->GetInputSlot(0));
228
229     // Add a softmax layer 1
230     armnn::SoftmaxDescriptor softmaxDescriptor;
231     armnn::IConnectableLayer* softmaxLayer1 = net.AddSoftmaxLayer(softmaxDescriptor, "softmax_1");
232     BOOST_TEST(softmaxLayer1);
233
234     splitterLayer->GetOutputSlot(0).Connect(softmaxLayer1->GetInputSlot(0));
235
236     // Add a softmax layer 2
237     armnn::IConnectableLayer* softmaxLayer2 = net.AddSoftmaxLayer(softmaxDescriptor, "softmax_2");
238     BOOST_TEST(softmaxLayer2);
239
240     splitterLayer->GetOutputSlot(1).Connect(softmaxLayer2->GetInputSlot(0));
241
242     // Add a merger layer
243     armnn::OriginsDescriptor mergerDesc(2, 4);
244
245     armnn::IConnectableLayer* mergerLayer = net.AddMergerLayer(mergerDesc, "merger layer");
246     BOOST_TEST(mergerLayer);
247
248     softmaxLayer1->GetOutputSlot(0).Connect(mergerLayer->GetInputSlot(0));
249     softmaxLayer2->GetOutputSlot(0).Connect(mergerLayer->GetInputSlot(1));
250
251     // Add an output layer
252     armnn::IConnectableLayer* outputLayer = net.AddOutputLayer(0, "output layer");
253     BOOST_TEST(outputLayer);
254
255     mergerLayer->GetOutputSlot(0).Connect(outputLayer->GetInputSlot(0));
256
257     BOOST_TEST(splitterLayer->GetNumOutputSlots() == 2);
258     BOOST_TEST(splitterLayer->GetOutputSlot(0).GetConnection(0) == &softmaxLayer1->GetInputSlot(0));
259     BOOST_TEST(&splitterLayer->GetOutputSlot(0) == softmaxLayer1->GetInputSlot(0).GetConnection());
260     BOOST_TEST(splitterLayer->GetOutputSlot(1).GetConnection(0) == &softmaxLayer2->GetInputSlot(0));
261     BOOST_TEST(&splitterLayer->GetOutputSlot(1) == softmaxLayer2->GetInputSlot(0).GetConnection());
262
263     BOOST_TEST(mergerLayer->GetNumInputSlots() == 2);
264     BOOST_TEST(softmaxLayer1->GetOutputSlot(0).GetConnection(0) == &mergerLayer->GetInputSlot(0));
265     BOOST_TEST(&softmaxLayer1->GetOutputSlot(0) == mergerLayer->GetInputSlot(0).GetConnection());
266     BOOST_TEST(softmaxLayer2->GetOutputSlot(0).GetConnection(0) == &mergerLayer->GetInputSlot(1));
267     BOOST_TEST(&softmaxLayer2->GetOutputSlot(0) == mergerLayer->GetInputSlot(1).GetConnection());
268 }
269
270 BOOST_AUTO_TEST_CASE(NetworkModification_SplitterAddition)
271 {
272     armnn::Network net;
273
274     // Add an input layer and an input tensor descriptor.
275     armnn::IConnectableLayer* layer = net.AddInputLayer(0, "input layer");
276     BOOST_TEST(layer);
277
278     // Add a splitter layer
279     armnn::ViewsDescriptor splitterDesc(2,4);
280
281     armnn::IConnectableLayer* const splitterLayer = net.AddSplitterLayer(splitterDesc, "splitter layer");
282     BOOST_TEST(splitterLayer);
283
284     layer->GetOutputSlot(0).Connect(splitterLayer->GetInputSlot(0));
285
286     // Add a softmax layer 1
287     armnn::SoftmaxDescriptor softmaxDescriptor;
288     armnn::IConnectableLayer* const softmax1Layer = net.AddSoftmaxLayer(softmaxDescriptor, "softmax_1");
289     BOOST_TEST(softmax1Layer);
290
291     splitterLayer->GetOutputSlot(0).Connect(softmax1Layer->GetInputSlot(0));
292
293     // Add a softmax layer 2
294     armnn::IConnectableLayer* const softmax2Layer = net.AddSoftmaxLayer(softmaxDescriptor, "softmax_2");
295     BOOST_TEST(softmax2Layer);
296
297     splitterLayer->GetOutputSlot(1).Connect(softmax2Layer->GetInputSlot(0));
298
299     // Add addition layer
300     layer = net.AddAdditionLayer("add layer");
301     BOOST_TEST(layer);
302
303     softmax1Layer->GetOutputSlot(0).Connect(layer->GetInputSlot(0));
304     softmax2Layer->GetOutputSlot(0).Connect(layer->GetInputSlot(1));
305
306     // Add an output layer
307     armnn::IConnectableLayer* prevLayer = layer;
308     layer = net.AddOutputLayer(0, "output layer");
309
310     prevLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(0));
311
312     BOOST_TEST(layer);
313 }
314
315 BOOST_AUTO_TEST_CASE(NetworkModification_SplitterMultiplication)
316 {
317     armnn::Network net;
318
319     // Add an input layer and an input tensor descriptor.
320     armnn::IConnectableLayer* layer = net.AddInputLayer(0, "input layer");
321     BOOST_TEST(layer);
322
323     // Add a splitter layer
324     armnn::ViewsDescriptor splitterDesc(2,4);
325     armnn::IConnectableLayer* const splitterLayer = net.AddSplitterLayer(splitterDesc, "splitter layer");
326     BOOST_TEST(splitterLayer);
327
328     layer->GetOutputSlot(0).Connect(splitterLayer->GetInputSlot(0));
329
330     // Add a softmax layer 1
331     armnn::SoftmaxDescriptor softmaxDescriptor;
332     armnn::IConnectableLayer* const softmax1Layer = net.AddSoftmaxLayer(softmaxDescriptor, "softmax_1");
333     BOOST_TEST(softmax1Layer);
334
335     splitterLayer->GetOutputSlot(0).Connect(softmax1Layer->GetInputSlot(0));
336
337     // Add a softmax layer 2
338     armnn::IConnectableLayer* const softmax2Layer = net.AddSoftmaxLayer(softmaxDescriptor, "softmax_2");
339     BOOST_TEST(softmax2Layer);
340
341     splitterLayer->GetOutputSlot(1).Connect(softmax2Layer->GetInputSlot(0));
342
343     // Add multiplication layer
344     layer = net.AddMultiplicationLayer("multiplication layer");
345     BOOST_TEST(layer);
346
347     softmax1Layer->GetOutputSlot(0).Connect(layer->GetInputSlot(0));
348     softmax2Layer->GetOutputSlot(0).Connect(layer->GetInputSlot(1));
349
350     // Add an output layer
351     armnn::IConnectableLayer* prevLayer = layer;
352     layer = net.AddOutputLayer(0, "output layer");
353     BOOST_TEST(layer);
354
355     prevLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(0));
356 }
357
358 BOOST_AUTO_TEST_CASE(ValidateWorkloads)
359 {
360     const armnn::TensorInfo desc({3, 5}, armnn::DataType::Float32);
361
362     armnn::Network  net;
363
364     armnn::NormalizationDescriptor nmDesc;
365     armnn::ActivationDescriptor acDesc;
366
367     //    in
368     //     |
369     //    nm
370     //   /  |
371     //  ac  |
372     //   \  |
373     //    ml
374     //     |
375     //    sm
376     //     |
377     //    ot
378     armnn::IConnectableLayer* layer = net.AddInputLayer(0, "in");
379     layer->GetOutputSlot(0).SetTensorInfo(desc);
380
381     armnn::IConnectableLayer* const normLayer = net.AddNormalizationLayer(nmDesc, "nm");
382
383     layer->GetOutputSlot(0).Connect(normLayer->GetInputSlot(0));
384     normLayer->GetOutputSlot(0).SetTensorInfo(desc);
385
386     layer = net.AddActivationLayer(acDesc, "ac");
387
388     normLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(0));
389     layer->GetOutputSlot(0).SetTensorInfo(desc);
390
391     armnn::IConnectableLayer* prevLayer = layer;
392     layer = net.AddMultiplicationLayer("ml");
393
394     prevLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(0));
395     normLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(1));
396     layer->GetOutputSlot(0).SetTensorInfo(desc);
397
398     prevLayer = layer;
399     armnn::SoftmaxDescriptor softmaxDescriptor;
400     layer = net.AddSoftmaxLayer(softmaxDescriptor, "sm");
401
402     prevLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(0));
403     layer->GetOutputSlot(0).SetTensorInfo(desc);
404
405     prevLayer = layer;
406     layer = net.AddOutputLayer(0, "ot");
407
408     prevLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(0));
409
410     armnn::DeviceSpec spec;
411     spec.DefaultComputeDevice = armnn::Compute::CpuRef;
412
413     armnn::IOptimizedNetworkPtr optNet = Optimize(net, spec);
414     static_cast<armnn::OptimizedNetwork*>(optNet.get())->GetGraph().AllocateDynamicBuffers();
415
416     // validate workloads
417     armnn::RefWorkloadFactory fact;
418     for (auto&& layer : static_cast<armnn::OptimizedNetwork*>(optNet.get())->GetGraph())
419     {
420         BOOST_CHECK_NO_THROW(
421             layer->CreateWorkload(static_cast<armnn::OptimizedNetwork*>(optNet.get())->GetGraph(), fact));
422     }
423 }
424
425 BOOST_AUTO_TEST_SUITE_END()