ArmNN
 20.02
SquashEqualSiblings.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #pragma once
6 
7 #include "Optimization.hpp"
8 
10 
11 namespace armnn
12 {
13 namespace optimizations
14 {
15 
16 template <typename Comparable>
18 {
19 public:
20  /// Run for every connection between a base Layer (any) and a child ComparableLayer.
21  /// For all siblings of the child layer that compare equal to it, bypasses and removes
22  /// them. I.e., moves the connections in the outputs of the siblings to the outputs of
23  /// the child layer, so the siblings are left unconnected (and later removed).
24  void Run(Graph& graph, InputSlot& connection) const
25  {
26  IgnoreUnused(graph);
27  auto& child = connection.GetOwningLayer();
28 
29  if (!child.IsOutputUnconnected())
30  {
31  OutputSlot& baseOutput = *connection.GetConnectedOutputSlot();
32 
33  if (baseOutput.GetNumConnections() > 1)
34  {
35  auto& comparableChild = *boost::polymorphic_downcast<Comparable*>(&child);
36 
37  Layer* lowestPriorityChild = &child;
38  for (auto&& it : baseOutput.GetConnections())
39  {
40  Layer* sibling = &it->GetOwningLayer();
41  if ((sibling != lowestPriorityChild) && comparableChild.IsEqual(*sibling))
42  {
43  if (sibling->GetPriority() < lowestPriorityChild->GetPriority())
44  {
45  std::swap(sibling, lowestPriorityChild);
46  }
47  // Bypasses sibling. It will be removed as it's left unconnected.
48  auto siblingOut = sibling->BeginOutputSlots();
49  for (auto lowestPriorityChildOut = lowestPriorityChild->BeginOutputSlots();
50  lowestPriorityChildOut != lowestPriorityChild->EndOutputSlots(); ++lowestPriorityChildOut)
51  {
52  siblingOut->MoveAllConnections(*lowestPriorityChildOut);
53  ++siblingOut;
54  }
55  }
56  }
57  }
58  }
59  }
60 
61 protected:
62  SquashEqualSiblingsImpl() = default;
63  ~SquashEqualSiblingsImpl() = default;
64 };
65 
70 
71 } // namespace optimizations
72 } // namespace armnn
const std::vector< InputSlot * > & GetConnections() const
Definition: Layer.hpp:125
void swap(OriginsDescriptor &first, OriginsDescriptor &second)
Copyright (c) 2020 ARM Limited.
void IgnoreUnused(Ts &&...)
unsigned int GetNumConnections() const override
Definition: Layer.hpp:138
LayerPriority GetPriority() const
Definition: Layer.cpp:288
const OutputSlot * GetConnectedOutputSlot() const
Definition: Layer.hpp:55
Layer & GetOwningLayer() const
Definition: Layer.hpp:52
This layer represents a transpose operation.
std::vector< OutputSlot >::iterator BeginOutputSlots()
Definition: Layer.hpp:239
void Run(Graph &graph, InputSlot &connection) const
Run for every connection between a base Layer (any) and a child ComparableLayer.