ArmNN
 24.08
Optimizer.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017,2024 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 {
19  // Create observables to observe changes to the graph
20  AddedLayerObservable addedLayerObservable(graph);
21  ErasedLayerNamesObservable erasedLayerNamesObservable(graph);
22 
23  bool graphNeedsSorting = false;
24  auto it = graph.TopologicalSort().end();
25 
26  // Calls TopologicalSort() for every iteration to re-order the list in case layers were added/removed.
27  while (it != graph.TopologicalSort().begin())
28  {
29  --it;
30  for (auto&& optimization : optimizations)
31  {
32  if (!*it)
33  {
34  throw armnn::NullPointerException("Layer must not be null.");
35  }
36 
37  optimization->Run(graph, **it);
38 
39  if ((*it)->IsOutputUnconnected())
40  {
41  auto next = std::next(graph.GetPosInGraph(**it));
42  graph.EraseLayer(it);
43  it = next;
44  graphNeedsSorting = true;
45  }
46 
47  // Add the names of erased layers as related layers to the new added layers
48  for (auto& erasedLayerName : erasedLayerNamesObservable)
49  {
50  for (auto& addedLayer : addedLayerObservable)
51  {
52  addedLayer->AddRelatedLayerName(erasedLayerName);
53  }
54  }
55 
56  erasedLayerNamesObservable.Clear();
57  addedLayerObservable.Clear();
58 
59  if (graphNeedsSorting)
60  {
61  graphNeedsSorting = false;
62  break;
63  }
64  }
65  }
66 }
67 
68 } // namespace armnn
armnn::Compute::Undefined
@ Undefined
armnn::Graph::GetPosInGraph
Iterator GetPosInGraph(Layer &layer)
Gets the position of a layer in the graph.
Definition: Graph.hpp:455
armnn::Graph::EraseLayer
void EraseLayer(Iterator pos)
Deletes the layer at the specified position.
Definition: Graph.hpp:517
All.hpp
armnn::AddedLayerObservable
Definition: Observable.hpp:46
Optimizer.hpp
armnn::Optimizer::Optimizations
std::vector< OptimizationPtr > Optimizations
Definition: Optimizer.hpp:18
Observable.hpp
armnn::Graph::begin
Iterator begin()
Returns iterator pointing to the beginning of the list. Lowercase for range-based for loops.
Definition: Graph.hpp:176
ARMNN_SCOPED_PROFILING_EVENT
#define ARMNN_SCOPED_PROFILING_EVENT(backendId, name)
Definition: Profiling.hpp:220
armnn::ErasedLayerNamesObservable
Definition: Observable.hpp:56
armnn::GraphObservable::Clear
void Clear()
Definition: Observable.hpp:26
armnn::Graph::end
Iterator end()
Returns iterator pointing to the end of the list. Lowercase for range-based for loops.
Definition: Graph.hpp:178
armnn::Graph::TopologicalSort
Graph & TopologicalSort()
Sorts layers in topological order and return this.
Definition: Graph.hpp:191
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
armnn::Optimizer::Pass
static void Pass(Graph &graph, const Optimizations &optimizations)
Definition: Optimizer.cpp:16
armnn::NullPointerException
Definition: Exceptions.hpp:146
armnn::Graph
Definition: Graph.hpp:30