ArmNN
 20.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 
11 namespace armnn
12 {
13 namespace optimizations
14 {
16 {
17 public:
18  /// Run for every connection between a base Layer (any) and a child TransposeLayer. If the type
19  /// of the base layer allows it, it moves the permutation to the inputs of the base layer.
20  /// I.e., adds equivalent permutations before the inputs of the base layer and moves the
21  /// connections in the output of the child transpose layer to the output of the base layer.
22  void Run(Graph& graph, InputSlot& connection) const
23  {
24  OutputSlot& baseOutput = *connection.GetConnectedOutputSlot();
25 
26  if (baseOutput.GetNumConnections() == 1U)
27  {
28  Layer& base = baseOutput.GetOwningLayer();
29 
30  if (CanMoveTransposeToInputs(base))
31  {
32  auto transpose = boost::polymorphic_downcast<TransposeLayer*>(&connection.GetOwningLayer());
33  const PermutationVector& perm = transpose->GetPermutation();
34 
35  // Inserts an equivalent transpose before every input of the base layer.
36  for (auto baseInput = base.BeginInputSlots(); baseInput != base.EndInputSlots(); ++baseInput)
37  {
38  // Inserts a new transpose layer.
39  const std::string name = std::string("moved_up-") + transpose->GetName();
40  TransposeLayer& permLayer = *graph.InsertNewLayer<TransposeLayer>(*baseInput, perm, name.c_str());
41 
42  // Sets output tensor info for the new layer.
43  OutputSlot& parentOutput = *permLayer.GetInputSlot(0).GetConnectedOutputSlot();
44  const TensorInfo permOutInfo = armnnUtils::TransposeTensorShape(parentOutput.GetTensorInfo(), perm);
45  permLayer.GetOutputHandler().SetTensorInfo(permOutInfo);
46  }
47 
48  // Sets transposed output tensor info
49  const TensorInfo& childOutInfo = transpose->GetOutputHandler().GetTensorInfo();
50  base.GetOutputHandler().SetTensorInfo(childOutInfo);
51 
52  // Bypasses transpose. It will be removed as it's left unconnected.
53  transpose->GetOutputSlot().MoveAllConnections(base.GetOutputSlot());
54  }
55  }
56  }
57 
58 protected:
59  MoveTransposeUpImpl() = default;
60  ~MoveTransposeUpImpl() = default;
61 
62 private:
63  static bool CanMoveTransposeToInputs(const Layer& base)
64  {
65  switch (base.GetType())
66  {
70  case LayerType::Floor:
71  case LayerType::MemCopy:
73  return true;
74  default:
75  return false;
76  }
77  }
78 };
79 
81 
82 } // namespace optimizations
83 } // namespace armnn
std::vector< InputSlot >::iterator EndInputSlots()
Definition: Layer.hpp:236
Layer & GetOwningLayer() const
Definition: Layer.hpp:115
Copyright (c) 2020 ARM Limited.
std::vector< InputSlot >::iterator BeginInputSlots()
Definition: Layer.hpp:235
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:310
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:221
LayerType GetType() const
Definition: Layer.hpp:259
const OutputSlot & GetOutputSlot(unsigned int index=0) const override
Get the const output slot handle by slot index.
Definition: Layer.hpp:312
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:409