ArmNN
 21.02
Optimizer.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #include "Optimizer.hpp"
6 #include "Observable.hpp"
7 #include "optimizations/All.hpp"
8 
9 namespace armnn
10 {
11 
12 Optimizer::Optimizer()
13 {
14 }
15 
16 void Optimizer::Pass(Graph& graph, const Optimizations& optimizations)
17 {
18  // Create observables to observe changes to the graph
19  AddedLayerObservable addedLayerObservable(graph);
20  ErasedLayerNamesObservable erasedLayerNamesObservable(graph);
21 
22  bool graphNeedsSorting = false;
23  auto it = graph.TopologicalSort().end();
24 
25  // Calls TopologicalSort() for every iteration to re-order the list in case layers were added/removed.
26  while (it != graph.TopologicalSort().begin())
27  {
28  --it;
29  for (auto&& optimization : optimizations)
30  {
31  ARMNN_ASSERT(*it);
32  optimization->Run(graph, **it);
33 
34  if ((*it)->IsOutputUnconnected())
35  {
36  auto next = std::next(graph.GetPosInGraph(**it));
37  graph.EraseLayer(it);
38  it = next;
39  graphNeedsSorting = true;
40  }
41 
42  // Add the names of erased layers as related layers to the new added layers
43  for (auto& erasedLayerName : erasedLayerNamesObservable)
44  {
45  for (auto& addedLayer : addedLayerObservable)
46  {
47  addedLayer->AddRelatedLayerName(erasedLayerName);
48  }
49  }
50 
51  erasedLayerNamesObservable.Clear();
52  addedLayerObservable.Clear();
53 
54  if (graphNeedsSorting)
55  {
56  graphNeedsSorting = false;
57  break;
58  }
59  }
60  }
61 }
62 
63 } // namespace armnn
Iterator begin()
Returns iterator pointing to the beginning of the list. Lowercase for range-based for loops...
Definition: Graph.hpp:162
void EraseLayer(Iterator pos)
Deletes the layer at the specified position.
Definition: Graph.hpp:449
static void Pass(Graph &graph, const Optimizations &optimizations)
Definition: Optimizer.cpp:16
std::vector< OptimizationPtr > Optimizations
Definition: Optimizer.hpp:18
Copyright (c) 2021 ARM Limited and Contributors.
Iterator GetPosInGraph(Layer &layer)
Gets the position of a layer in the graph.
Definition: Graph.hpp:394
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
Iterator end()
Returns iterator pointing to the end of the list. Lowercase for range-based for loops.
Definition: Graph.hpp:164
Graph & TopologicalSort()
Sorts layers in topological order and return this.
Definition: Graph.hpp:177