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 Optimizer()12Optimizer::Optimizer() 13 { 14 } 15 Pass(Graph & graph,const Optimizations & optimizations)16void 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 64