ArmNN
 25.11
Loading...
Searching...
No Matches
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"
8
9namespace armnn
10{
11
12Optimizer::Optimizer()
13{
14}
15
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
#define ARMNN_SCOPED_PROFILING_EVENT(backendId, name)
Iterator begin()
Returns iterator pointing to the beginning of the list. Lowercase for range-based for loops.
Definition Graph.hpp:176
void EraseLayer(Iterator pos)
Deletes the layer at the specified position.
Definition Graph.hpp:517
Iterator end()
Returns iterator pointing to the end of the list. Lowercase for range-based for loops.
Definition Graph.hpp:178
Iterator GetPosInGraph(Layer &layer)
Gets the position of a layer in the graph.
Definition Graph.hpp:455
Graph & TopologicalSort()
Sorts layers in topological order and return this.
Definition Graph.hpp:191
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.