ArmNN
 22.08
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
Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
Definition: INetwork.hpp:68
void ReportUntouchedLayers(OptimizationViews &optimizationViews, std::map< LayerGuid, Layer *> untouched)
void AddSubstitution(SubstitutionPair &&substitution)
Copyright (c) 2021 ARM Limited and Contributors.
This layer represents a pad operation.
Definition: PadLayer.hpp:14
The SubgraphView class represents a subgraph of a Graph.
std::vector< IOutputSlot * > IOutputSlots
std::vector< IInputSlot * > IInputSlots
void AddUntouchedSubgraph(SubgraphView &&subgraph)
This layer represents a pooling 2d operation.
LayerType * FoldPadIntoAveragePool2d(OptimizationViews &optimizationViews, Pooling2dLayer *baseLayer, Pooling2dDescriptor &poolDescriptor, PadLayer *padLayer)
LayerType * FoldPadLayer(OptimizationViews &optimizationViews, LayerType *baseLayer, LayerType *replacementLayer, PadLayer *padLayer)
A Pooling2dDescriptor for the Pooling2dLayer.
LayerType
When adding a new layer, adapt also the LastLayer enum value in the enum class LayerType below...
Definition: Types.hpp:468