ArmNN  NotReleased
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 
9 #include <boost/core/ignore_unused.hpp>
10 
11 namespace armnn
12 {
13 namespace optimizations
14 {
15 
16 template <typename Comparable>
18 {
19 public:
24  void Run(Graph& graph, InputSlot& connection) const
25  {
26  boost::ignore_unused(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 
68 
69 } // namespace optimizations
70 } // namespace armnn
const OutputSlot * GetConnectedOutputSlot() const
Definition: Layer.hpp:55
LayerPriority GetPriority() const
Definition: Layer.cpp:287
unsigned int GetNumConnections() const override
Definition: Layer.hpp:138
void Run(Graph &graph, InputSlot &connection) const
void swap(OriginsDescriptor &first, OriginsDescriptor &second)
const std::vector< InputSlot * > & GetConnections() const
Definition: Layer.hpp:125
std::vector< OutputSlot >::iterator BeginOutputSlots()
Definition: Layer.hpp:239
Layer & GetOwningLayer() const
Definition: Layer.hpp:52