ArmNN
 20.11
MovePermuteUp.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 #include <armnnUtils/Permute.hpp>
11 
12 namespace armnn
13 {
14 namespace optimizations
15 {
17 {
18 public:
19  /// Run for every connection between a base Layer (any) and a child PermuteLayer. If the type
20  /// of the base layer allows it, it moves the permutation to the inputs of the base layer.
21  /// I.e., adds equivalent permutations before the inputs of the base layer and moves the
22  /// connections in the output of the child permute layer to the output of the base layer.
23  void Run(Graph& graph, InputSlot& connection) const
24  {
25  OutputSlot& baseOutput = *connection.GetConnectedOutputSlot();
26 
27  if (baseOutput.GetNumConnections() == 1U)
28  {
29  Layer& base = baseOutput.GetOwningLayer();
30 
31  if (CanMovePermuteToInputs(base))
32  {
33  auto permute = PolymorphicDowncast<PermuteLayer*>(&connection.GetOwningLayer());
34  const PermutationVector& perm = permute->GetPermutation();
35 
36  // Inserts an equivalent permute before every input of the base layer.
37  for (auto baseInput = base.BeginInputSlots(); baseInput != base.EndInputSlots(); ++baseInput)
38  {
39  // Inserts a new permute layer.
40  const std::string name = std::string("moved_up-") + permute->GetName();
41  PermuteLayer& permLayer = *graph.InsertNewLayer<PermuteLayer>(*baseInput, perm, name.c_str());
42 
43  // Sets output tensor info for the new layer.
44  OutputSlot& parentOutput = *permLayer.GetInputSlot(0).GetConnectedOutputSlot();
45  const TensorInfo permOutInfo = armnnUtils::Permuted(parentOutput.GetTensorInfo(), perm);
46  permLayer.GetOutputHandler().SetTensorInfo(permOutInfo);
47  }
48 
49  // Bypasses permute. It will be removed as it's left unconnected.
50  permute->GetOutputSlot().MoveAllConnections(base.GetOutputSlot());
51  }
52  }
53  }
54 
55 protected:
56  MovePermuteUpImpl() = default;
57  ~MovePermuteUpImpl() = default;
58 
59 private:
60  static bool CanMovePermuteToInputs(const Layer& base)
61  {
62  switch (base.GetType())
63  {
67  case LayerType::Floor:
68  case LayerType::MemCopy:
70  return true;
71  default:
72  return false;
73  }
74  }
75 };
76 
78 
79 } // namespace optimizations
80 } // namespace armnn
std::vector< InputSlot >::iterator EndInputSlots()
Definition: Layer.hpp:239
Layer & GetOwningLayer() const
Definition: Layer.hpp:115
Copyright (c) 2020 ARM Limited.
This layer represents a permutation operation.
std::vector< InputSlot >::iterator BeginInputSlots()
Definition: Layer.hpp:238
unsigned int GetNumConnections() const override
Definition: Layer.hpp:138
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
Definition: Layer.hpp:313
const OutputSlot * GetConnectedOutputSlot() const
Definition: Layer.hpp:55
Layer & GetOwningLayer() const
Definition: Layer.hpp:52
void SetTensorInfo(const TensorInfo &tensorInfo)
Sets the TensorInfo used by this output handler.
const OutputHandler & GetOutputHandler(unsigned int i=0) const
Definition: Layer.hpp:222
LayerType GetType() const
Definition: Layer.hpp:262
void Run(Graph &graph, InputSlot &connection) const
Run for every connection between a base Layer (any) and a child PermuteLayer.
const OutputSlot & GetOutputSlot(unsigned int index=0) const override
Get the const output slot handle by slot index.
Definition: Layer.hpp:315
LayerT * InsertNewLayer(InputSlot &insertBefore, Args &&... args)
Inserts a new layer between the output slot currently connected to insertBefore and insertBefore itse...
Definition: Graph.hpp:416
armnn::TensorShape Permuted(const armnn::TensorShape &srcShape, const armnn::PermutationVector &mappings)
Definition: Permute.cpp:98