ArmNN
 23.05
SubgraphUtils.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2022 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #pragma once
7 
9 
10 namespace armnn
11 {
12 
13 namespace
14 {
15 
16 //
17 // this helper only works if all layers where the inputs connect to are not selected
18 //
19 
20 SubgraphView::IInputSlots CreateIInputsFrom(const std::vector<armnn::IConnectableLayer*>& layers)
21 {
23  for (auto&& layer : layers)
24  {
25  for (unsigned int i = 0 ; i < layer->GetNumInputSlots(); ++i)
26  {
27  result.push_back(&(layer->GetInputSlot(i)));
28  }
29  }
30  return result;
31 }
32 
33 //
34 // this helper only works if all layers where the outputs connect to are not selected
35 //
36 
37 SubgraphView::IOutputSlots CreateIOutputsFrom(const std::vector<armnn::IConnectableLayer*>& layers)
38 {
40  for (auto &&layer: layers)
41  {
42  for (unsigned int i = 0; i < layer->GetNumOutputSlots(); ++i)
43  {
44  result.push_back(&(layer->GetOutputSlot(i)));
45  }
46  }
47  return result;
48 }
49 
50 }
51 
52 inline void ReportUntouchedLayers(OptimizationViews& optimizationViews, std::map<LayerGuid, Layer*> untouched)
53 {
54  std::vector<Layer*> untouchedVector;
55  for (const auto& pair : untouched)
56  {
57  Layer* layer = pair.second;
58  SubgraphView subgraphView({layer},
59  CreateIInputsFrom({layer}),
60  CreateIOutputsFrom({layer}));
61  optimizationViews.AddUntouchedSubgraph(std::move(subgraphView));
62  }
63 }
64 
65 template<typename LayerType>
67  LayerType* baseLayer,
68  LayerType* replacementLayer,
69  PadLayer* padLayer)
70 {
71  SubgraphView substitutionSubgraph({padLayer, baseLayer},
72  CreateIInputsFrom({padLayer}),
73  CreateIOutputsFrom({baseLayer}));
74  SubgraphView replacementSubgraph(replacementLayer);
75 
76  optimizationViews.AddSubstitution({substitutionSubgraph, replacementSubgraph});
77 
78  return replacementLayer;
79 }
80 
81 template<typename LayerType>
83  Pooling2dLayer* baseLayer,
84  Pooling2dDescriptor& poolDescriptor,
85  PadLayer* padLayer)
86 {
87  IConnectableLayer* replacement =
88  optimizationViews.GetINetwork()->AddPooling2dLayer(poolDescriptor, "folded-pad-into-pool2d");
89  LayerType* replacementLayer = PolymorphicDowncast<LayerType*>(replacement);
90 
91  FoldPadLayer(optimizationViews,
92  baseLayer,
93  replacementLayer,
94  padLayer);
95 
96  return replacementLayer;
97 }
98 
99 } // namespace armnn
armnn::OptimizationViews::GetINetwork
INetwork * GetINetwork()
Definition: OptimizationViews.hpp:63
armnn::IConnectableLayer
Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
Definition: INetwork.hpp:68
armnn::Layer
Definition: Layer.hpp:217
armnn::OptimizationViews::AddSubstitution
void AddSubstitution(SubstitutionPair &&substitution)
Definition: OptimizationViews.hpp:38
armnn::INetwork::AddPooling2dLayer
IConnectableLayer * AddPooling2dLayer(const Pooling2dDescriptor &pooling2dDescriptor, const char *name=nullptr)
Adds a 2D pooling layer to the network.
Definition: Network.cpp:349
armnn::ReportUntouchedLayers
void ReportUntouchedLayers(OptimizationViews &optimizationViews, std::map< LayerGuid, Layer * > untouched)
Definition: SubgraphUtils.hpp:52
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
armnn::SubgraphView::IInputSlots
std::vector< IInputSlot * > IInputSlots
Definition: SubgraphView.hpp:58
armnn::OptimizationViews
Definition: OptimizationViews.hpp:17
armnn::LayerType
LayerType
When adding a new layer, adapt also the LastLayer enum value in the enum class LayerType below.
Definition: Types.hpp:479
armnn::OptimizationViews::AddUntouchedSubgraph
void AddUntouchedSubgraph(SubgraphView &&subgraph)
Definition: OptimizationViews.hpp:48
armnn::FoldPadIntoAveragePool2d
LayerType * FoldPadIntoAveragePool2d(OptimizationViews &optimizationViews, Pooling2dLayer *baseLayer, Pooling2dDescriptor &poolDescriptor, PadLayer *padLayer)
Definition: SubgraphUtils.hpp:82
armnn::SubgraphView
The SubgraphView class represents a subgraph of a Graph.
Definition: SubgraphView.hpp:31
FoldPadIntoLayer2d.hpp
armnn::FoldPadLayer
LayerType * FoldPadLayer(OptimizationViews &optimizationViews, LayerType *baseLayer, LayerType *replacementLayer, PadLayer *padLayer)
Definition: SubgraphUtils.hpp:66
armnn::SubgraphView::IOutputSlots
std::vector< IOutputSlot * > IOutputSlots
Definition: SubgraphView.hpp:60
armnn::PadLayer
This layer represents a pad operation.
Definition: PadLayer.hpp:14
armnn::Pooling2dLayer
This layer represents a pooling 2d operation.
Definition: Pooling2dLayer.hpp:13
armnn::Pooling2dDescriptor
A Pooling2dDescriptor for the Pooling2dLayer.
Definition: Descriptors.hpp:359