ArmNN
 21.02
MoveTransposeUp.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2020 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #pragma once
6 
7 #include "Optimization.hpp"
8 
10 #include <armnnUtils/Transpose.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 TransposeLayer. 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 transpose 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 (CanMoveTransposeToInputs(base))
32  {
33  auto transpose = PolymorphicDowncast<TransposeLayer*>(&connection.GetOwningLayer());
34  const PermutationVector& perm = transpose->GetPermutation();
35 
36  // Inserts an equivalent transpose before every input of the base layer.
37  for (auto baseInput = base.BeginInputSlots(); baseInput != base.EndInputSlots(); ++baseInput)
38  {
39  // Inserts a new transpose layer.
40  const std::string name = std::string("moved_up-") + transpose->GetName();
41  TransposeLayer& permLayer = *graph.InsertNewLayer<TransposeLayer>(*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::TransposeTensorShape(parentOutput.GetTensorInfo(), perm);
46  permLayer.GetOutputHandler().SetTensorInfo(permOutInfo);
47  }
48 
49  // Bypasses transpose. It will be removed as it's left unconnected.
50  transpose->GetOutputSlot().MoveAllConnections(base.GetOutputSlot());
51  }
52  }
53  }
54 
55 protected:
56  MoveTransposeUpImpl() = default;
57  ~MoveTransposeUpImpl() = default;
58 
59 private:
60  static bool CanMoveTransposeToInputs(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:242
Layer & GetOwningLayer() const
Definition: Layer.hpp:115
Copyright (c) 2021 ARM Limited and Contributors.
std::vector< InputSlot >::iterator BeginInputSlots()
Definition: Layer.hpp:241
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:316
LayerType GetType() const override
Returns the armnn::LayerType of this layer.
Definition: Layer.hpp:265
const OutputSlot * GetConnectedOutputSlot() const
Definition: Layer.hpp:55
Layer & GetOwningLayer() const
Definition: Layer.hpp:52
This layer represents a transpose operation.
void SetTensorInfo(const TensorInfo &tensorInfo)
Sets the TensorInfo used by this output handler.
void Run(Graph &graph, InputSlot &connection) const
Run for every connection between a base Layer (any) and a child TransposeLayer.
const OutputHandler & GetOutputHandler(unsigned int i=0) const
Definition: Layer.hpp:225
const OutputSlot & GetOutputSlot(unsigned int index=0) const override
Get the const output slot handle by slot index.
Definition: Layer.hpp:318
armnn::TensorShape TransposeTensorShape(const armnn::TensorShape &srcShape, const armnn::PermutationVector &mappings)
Definition: Transpose.cpp:98
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