ArmNN
 22.08
OptimizationViewsTests.cpp File Reference

Go to the source code of this file.

Functions

void CheckLayers (Graph &graph)
 
 TEST_SUITE ("OptimizationViewsTestSuite")
 

Function Documentation

◆ CheckLayers()

void CheckLayers ( Graph graph)

Definition at line 22 of file OptimizationViewsTests.cpp.

References armnn::Input, armnn::Output, and armnn::PreCompiled.

Referenced by IDeserializer::GetNetworkOutputBindingInfo(), and TEST_SUITE().

23 {
24  unsigned int m_inputLayerCount = 0, m_outputLayerCount = 0, m_addLayerCount = 0;
25  for(auto layer : graph)
26  {
27  switch(layer->GetType())
28  {
29  case LayerType::Input:
30  ++m_inputLayerCount;
31  CHECK((layer->GetName() == std::string("inLayer0") ||
32  layer->GetName() == std::string("inLayer1")));
33  break;
34  // The Addition layer should become a PreCompiled Layer after Optimisation
35  case LayerType::PreCompiled:
36  ++m_addLayerCount;
37  CHECK(std::string(layer->GetName()) == "pre-compiled");
38  break;
39  case LayerType::Output:
40  ++m_outputLayerCount;
41  CHECK(std::string(layer->GetName()) == "outLayer");
42  break;
43  default:
44  //Fail for anything else
45  CHECK(false);
46  }
47  }
48  CHECK(m_inputLayerCount == 2);
49  CHECK(m_outputLayerCount == 1);
50  CHECK(m_addLayerCount == 1);
51 }

◆ TEST_SUITE()

TEST_SUITE ( "OptimizationViewsTestSuite"  )

Definition at line 53 of file OptimizationViewsTests.cpp.

References OptimizationViews::AddFailedSubgraph(), Graph::AddLayer(), OptimizationViews::AddSubstitution(), OptimizationViews::AddUntouchedSubgraph(), CheckLayers(), IOutputSlot::Connect(), OutputSlot::Connect(), armnn::CpuRef, IRuntime::Create(), INetwork::Create(), CreateInputsFrom(), CreateOutputsFrom(), CreateSubgraphViewFrom(), armnn::Float32, armnn::GetGraphForTesting(), MockBackend::GetIdStatic(), OptimizationViews::GetINetwork(), IConnectableLayer::GetInputSlot(), Layer::GetInputSlot(), IConnectableLayer::GetOutputSlot(), Layer::GetOutputSlot(), armnn::Optimize(), IOutputSlot::SetTensorInfo(), Graph::SubstituteSubgraph(), and OptimizationViews::Validate().

54 {
55 TEST_CASE("OptimizedViewsSubgraphLayerCount")
56 {
57  OptimizationViews view;
58  // Construct a graph with 3 layers
59  Graph baseGraph;
60 
61  Layer* const inputLayer = baseGraph.AddLayer<InputLayer>(0, "input");
62 
63  Convolution2dDescriptor convDescriptor;
64  PreCompiledDescriptor substitutionLayerDescriptor(2, 1);
65  Layer* const convLayer1 = baseGraph.AddLayer<Convolution2dLayer>(convDescriptor, "conv1");
66  Layer* const convLayer2 = baseGraph.AddLayer<Convolution2dLayer>(convDescriptor, "conv2");
67  Layer* const weightsLayer1 = baseGraph.AddLayer<ConstantLayer>("weights1");
68  Layer* const weightsLayer2 = baseGraph.AddLayer<ConstantLayer>("weights2");
69  Layer* const substitutableCompiledLayer =
70  baseGraph.AddLayer<PreCompiledLayer>(substitutionLayerDescriptor, "pre-compiled");
71 
72  Layer* const outputLayer = baseGraph.AddLayer<OutputLayer>(0, "output");
73 
74  inputLayer->GetOutputSlot(0).Connect(convLayer1->GetInputSlot(0));
75  weightsLayer1->GetOutputSlot(0).Connect(convLayer1->GetInputSlot(1));
76  convLayer1->GetOutputSlot(0).Connect(convLayer2->GetInputSlot(0));
77  weightsLayer2->GetOutputSlot(0).Connect(convLayer2->GetInputSlot(1));
78  convLayer2->GetOutputSlot(0).Connect(outputLayer->GetInputSlot(0));
79 
80  // Subgraph for a failed layer
83  CreateOutputsFrom({convLayer1}),
84  {convLayer1});
85  // Subgraph for an untouched layer
86  SubgraphViewSelector::SubgraphViewPtr untouchedSubgraph =
88  CreateOutputsFrom({convLayer2}),
89  {convLayer2});
90  // Subgraph for a substitutable layer
91  SubgraphViewSelector::SubgraphViewPtr substitutableSubgraph =
93  CreateOutputsFrom({convLayer2}),
94  {substitutableCompiledLayer});
95  // Create a Graph containing a layer to substitute in
96  Graph substitutableGraph;
97  Layer* const substitutionpreCompiledLayer =
98  substitutableGraph.AddLayer<PreCompiledLayer>(substitutionLayerDescriptor, "pre-compiled");
99 
100  // Subgraph for a substitution layer
101  SubgraphViewSelector::SubgraphViewPtr substitutionSubgraph =
102  CreateSubgraphViewFrom(CreateInputsFrom(substitutionpreCompiledLayer),
103  CreateOutputsFrom({substitutionpreCompiledLayer}),
104  {substitutionpreCompiledLayer});
105 
106  // Sub in the graph
107  baseGraph.SubstituteSubgraph(*substitutableSubgraph, *substitutionSubgraph);
108 
109  view.AddFailedSubgraph(SubgraphView(*failedSubgraph));
110  view.AddUntouchedSubgraph(SubgraphView(*untouchedSubgraph));
111 
114  CreateOutputsFrom({convLayer2}),
115  {substitutionpreCompiledLayer});
116  view.AddSubstitution({*baseSubgraph, *substitutionSubgraph});
117 
118  // Construct original subgraph to compare against
119  SubgraphViewSelector::SubgraphViewPtr originalSubgraph =
121  CreateOutputsFrom({convLayer2}),
122  {convLayer1, convLayer2, substitutionpreCompiledLayer});
123 
124  CHECK(view.Validate(*originalSubgraph));
125 }
126 
127 
128 TEST_CASE("OptimizedViewsSubgraphLayerCountUsingGetINetwork")
129 {
130  OptimizationViews view;
131 
132  IConnectableLayer* const inputLayer = view.GetINetwork()->AddInputLayer(0, "input");
133 
134  DepthwiseConvolution2dDescriptor convDescriptor;
135  PreCompiledDescriptor substitutionLayerDescriptor(2, 1);
136  CompiledBlobPtr blobPtr;
137  BackendId backend = Compute::CpuRef;
138 
139  Layer* convLayer1 = PolymorphicDowncast<Layer*>(
140  view.GetINetwork()->AddDepthwiseConvolution2dLayer(convDescriptor,
141  "conv1"));
142 
143  Layer* convLayer2 = PolymorphicDowncast<Layer*>(
144  view.GetINetwork()->AddDepthwiseConvolution2dLayer(convDescriptor,
145  "conv2"));
146 
147  IConnectableLayer* const outputLayer = view.GetINetwork()->AddOutputLayer(0, "output");
148 
149  inputLayer->GetOutputSlot(0).Connect(convLayer1->GetInputSlot(0));
150  convLayer1->GetOutputSlot(0).Connect(convLayer2->GetInputSlot(0));
151  convLayer2->GetOutputSlot(0).Connect(outputLayer->GetInputSlot(0));
152 
153  // Subgraph for a failed layer
155  CreateOutputsFrom({convLayer1}),
156  {convLayer1});
157  // Subgraph for an untouched layer
159  CreateOutputsFrom({convLayer2}),
160  {convLayer2});
161 
162  // Create a Network containing a layer to substitute in
163  NetworkImpl net;
164  Layer* substitutionpreCompiledLayer = PolymorphicDowncast<Layer*>(
165  net.AddPrecompiledLayer(substitutionLayerDescriptor, std::move(blobPtr), backend));
166 
167  // Subgraph for a substitution layer
168  SubgraphViewSelector::SubgraphViewPtr substitutionSubgraph =
169  CreateSubgraphViewFrom(CreateInputsFrom(substitutionpreCompiledLayer),
170  CreateOutputsFrom({substitutionpreCompiledLayer}),
171  {substitutionpreCompiledLayer});
172 
173  view.AddFailedSubgraph(SubgraphView(*failedSubgraph));
174  view.AddUntouchedSubgraph(SubgraphView(*untouchedSubgraph));
175 
177  CreateOutputsFrom({convLayer2}),
178  {substitutionpreCompiledLayer});
179  view.AddSubstitution({*baseSubgraph, *substitutionSubgraph});
180 
181  // Construct original subgraph to compare against
182  SubgraphViewSelector::SubgraphViewPtr originalSubgraph =
184  CreateOutputsFrom({convLayer2}),
185  {convLayer1, convLayer2, substitutionpreCompiledLayer});
186 
187  CHECK(view.Validate(*originalSubgraph));
188 }
189 
190 TEST_CASE("OptimizedViewsSubgraphLayerCountFailValidate")
191 {
192  OptimizationViews view;
193  // Construct a graph with 3 layers
194  Graph baseGraph;
195 
196  Layer* const inputLayer = baseGraph.AddLayer<InputLayer>(0, "input");
197 
198  Convolution2dDescriptor convDescriptor;
199  PreCompiledDescriptor substitutionLayerDescriptor(2, 1);
200  Layer* const convLayer1 = baseGraph.AddLayer<Convolution2dLayer>(convDescriptor, "conv1");
201  Layer* const convLayer2 = baseGraph.AddLayer<Convolution2dLayer>(convDescriptor, "conv2");
202  Layer* const weightsLayer1 = baseGraph.AddLayer<ConstantLayer>("weights1");
203  Layer* const weightsLayer2 = baseGraph.AddLayer<ConstantLayer>("weights2");
204  Layer* const substitutableCompiledLayer =
205  baseGraph.AddLayer<PreCompiledLayer>(substitutionLayerDescriptor, "pre-compiled");
206 
207  Layer* const outputLayer = baseGraph.AddLayer<OutputLayer>(0, "output");
208 
209 
210  inputLayer->GetOutputSlot(0).Connect(convLayer1->GetInputSlot(0));
211  weightsLayer1->GetOutputSlot(0).Connect(convLayer1->GetInputSlot(1));
212  convLayer1->GetOutputSlot(0).Connect(convLayer2->GetInputSlot(0));
213  weightsLayer2->GetOutputSlot(0).Connect(convLayer2->GetInputSlot(1));
214  convLayer2->GetOutputSlot(0).Connect(outputLayer->GetInputSlot(0));
215 
216  // Subgraph for an untouched layer
217  SubgraphViewSelector::SubgraphViewPtr untouchedSubgraph =
219  CreateOutputsFrom({convLayer2}),
220  {convLayer2});
221  // Subgraph for a substitutable layer
222  SubgraphViewSelector::SubgraphViewPtr substitutableSubgraph =
224  CreateOutputsFrom({convLayer2}),
225  {substitutableCompiledLayer});
226  // Create a Graph containing a layer to substitute in
227  Graph substitutableGraph;
228  Layer* const substitutionpreCompiledLayer =
229  substitutableGraph.AddLayer<PreCompiledLayer>(substitutionLayerDescriptor, "pre-compiled");
230 
231  // Subgraph for a substitution layer
232  SubgraphViewSelector::SubgraphViewPtr substitutionSubgraph =
233  CreateSubgraphViewFrom(CreateInputsFrom(substitutionpreCompiledLayer),
234  CreateOutputsFrom({substitutionpreCompiledLayer}),
235  {substitutionpreCompiledLayer});
236 
237  // Sub in the graph
238  baseGraph.SubstituteSubgraph(*substitutableSubgraph, *substitutionSubgraph);
239 
240  view.AddUntouchedSubgraph(SubgraphView(*untouchedSubgraph));
241 
244  CreateOutputsFrom({convLayer2}),
245  {substitutionpreCompiledLayer});
246  view.AddSubstitution({*baseSubgraph, *substitutionSubgraph});
247 
248  // Construct original subgraph to compare against
249  SubgraphViewSelector::SubgraphViewPtr originalSubgraph =
251  CreateOutputsFrom({convLayer2}),
252  {convLayer1, convLayer2, substitutionpreCompiledLayer});
253 
254  // Validate should fail as convLayer1 is not counted
255  CHECK(!view.Validate(*originalSubgraph));
256 }
257 
258 TEST_CASE("OptimizeViewsValidateDeviceMockBackend")
259 {
260  // build up the structure of the network
262 
263  armnn::IConnectableLayer* input = net->AddInputLayer(0, "inLayer0");
264  armnn::IConnectableLayer* input1 = net->AddInputLayer(1, "inLayer1");
265 
266  armnn::IConnectableLayer* addition = net->AddAdditionLayer("addLayer");
267 
268  armnn::IConnectableLayer* output = net->AddOutputLayer(0, "outLayer");
269 
270  input->GetOutputSlot(0).Connect(addition->GetInputSlot(0));
271  input1->GetOutputSlot(0).Connect(addition->GetInputSlot(1));
272  addition->GetOutputSlot(0).Connect(output->GetInputSlot(0));
273 
277 
278  armnn::MockBackendInitialiser initialiser;
281 
282  std::vector<armnn::BackendId> backends = { MockBackend().GetIdStatic() };
283  armnn::IOptimizedNetworkPtr optNet = armnn::Optimize(*net, backends, runtime->GetDeviceSpec());
284  CHECK(optNet);
285 
286  // Check the optimised graph
287  armnn::Graph& graph = GetGraphForTesting(optNet.get());
288  CheckLayers(graph);
289 }
290 
291 }
A layer that the constant data can be bound to.
static IRuntimePtr Create(const CreationOptions &options)
Definition: Runtime.cpp:49
Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
Definition: INetwork.hpp:68
LayerT * AddLayer(Args &&... args)
Adds a new layer, of type LayerType, to the graph constructed with the arguments passed.
Definition: Graph.hpp:456
A Convolution2dDescriptor for the Convolution2dLayer.
int Connect(InputSlot &destination)
Definition: Layer.cpp:112
std::unique_ptr< IRuntime, void(*)(IRuntime *runtime)> IRuntimePtr
Definition: IRuntime.hpp:33
void CheckLayers(Graph &graph)
void AddSubstitution(SubstitutionPair &&substitution)
Private implementation of INetwork.
Definition: Network.hpp:31
virtual void SetTensorInfo(const TensorInfo &tensorInfo)=0
std::unique_ptr< void, CompiledBlobDeleter > CompiledBlobPtr
Definition: INetwork.hpp:242
The SubgraphView class represents a subgraph of a Graph.
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
Definition: Layer.hpp:324
A layer user-provided data can be bound to (e.g. inputs, outputs).
Definition: OutputLayer.hpp:13
IOptimizedNetworkPtr Optimize(const INetwork &network, const std::vector< BackendId > &backendPreferences, const IDeviceSpec &deviceSpec, const OptimizerOptions &options=OptimizerOptions(), Optional< std::vector< std::string > &> messages=EmptyOptional())
Create an optimized version of the network.
Definition: Network.cpp:1864
static const BackendId & GetIdStatic()
Definition: MockBackend.cpp:17
void AddFailedSubgraph(SubgraphView &&subgraph)
std::unique_ptr< IOptimizedNetwork, void(*)(IOptimizedNetwork *network)> IOptimizedNetworkPtr
Definition: INetwork.hpp:239
bool Validate(const SubgraphView &originalSubgraph) const
void AddUntouchedSubgraph(SubgraphView &&subgraph)
void SubstituteSubgraph(SubgraphView &subgraph, IConnectableLayer *substituteLayer)
Substitutes the given sub-graph with either a new layer or a new sub-graph.
Definition: Graph.cpp:435
Graph & GetGraphForTesting(IOptimizedNetwork *optNet)
Definition: TestUtils.cpp:49
std::unique_ptr< SubgraphView > SubgraphViewPtr
SubgraphView::SubgraphViewPtr CreateSubgraphViewFrom(SubgraphView::InputSlots &&inputs, SubgraphView::OutputSlots &&outputs, SubgraphView::Layers &&layers)
SubgraphView::OutputSlots CreateOutputsFrom(const std::vector< Layer *> &layers)
A layer user-provided data can be bound to (e.g. inputs, outputs).
Definition: InputLayer.hpp:13
virtual const IInputSlot & GetInputSlot(unsigned int index) const =0
Get a const input slot handle by slot index.
const OutputSlot & GetOutputSlot(unsigned int index=0) const override
Get the const output slot handle by slot index.
Definition: Layer.hpp:326
virtual const IOutputSlot & GetOutputSlot(unsigned int index) const =0
Get the const output slot handle by slot index.
This layer represents a convolution 2d operation.
std::unique_ptr< INetwork, void(*)(INetwork *network)> INetworkPtr
Definition: INetwork.hpp:238
virtual int Connect(IInputSlot &destination)=0
A PreCompiledDescriptor for the PreCompiledLayer.
static INetworkPtr Create(NetworkOptions networkOptions={})
Definition: Network.cpp:475
SubgraphView::InputSlots CreateInputsFrom(Layer *layer, std::vector< unsigned int > ignoreSlots)
A DepthwiseConvolution2dDescriptor for the DepthwiseConvolution2dLayer.