ArmNN
 24.02
MoveTransposeUp.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2020,2023 Arm Ltd and Contributors. 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;
72  {
73  auto descriptor = PolymorphicDowncast<const ElementwiseBinaryDescriptor*>(&base.GetParameters());
74  return (descriptor->m_Operation == BinaryOperation::Add ||
75  descriptor->m_Operation == BinaryOperation::Mul);
76  }
77  default:
78  return false;
79  }
80  }
81 };
82 
84 
85 } // namespace optimizations
86 } // namespace armnn
armnn::BinaryOperation::Mul
@ Mul
armnn::BinaryOperation::Add
@ Add
armnn::InputSlot::GetOwningLayer
Layer & GetOwningLayer() const
Definition: Layer.hpp:53
armnn::OutputSlot
Definition: Layer.hpp:100
armnn::TensorInfo
Definition: Tensor.hpp:152
armnn::LayerType::Floor
@ Floor
armnn::optimizations::MoveTransposeUpImpl::~MoveTransposeUpImpl
~MoveTransposeUpImpl()=default
armnn::OutputHandler::SetTensorInfo
void SetTensorInfo(const TensorInfo &tensorInfo)
Sets the TensorInfo used by this output handler.
Definition: OutputHandler.cpp:15
armnn::Layer::GetOutputSlot
const OutputSlot & GetOutputSlot(unsigned int index=0) const override
Get the const output slot handle by slot index.
Definition: Layer.hpp:339
armnnUtils::TransposeTensorShape
armnn::TensorShape TransposeTensorShape(const armnn::TensorShape &srcShape, const armnn::PermutationVector &mappings)
Definition: Transpose.cpp:125
Optimization.hpp
armnn::Layer::GetInputSlot
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
Definition: Layer.hpp:337
armnn::optimizations::MoveTransposeUpImpl::Run
void Run(Graph &graph, InputSlot &connection) const
Run for every connection between a base Layer (any) and a child TransposeLayer.
Definition: MoveTransposeUp.hpp:23
armnn::LayerType::ElementwiseBinary
@ ElementwiseBinary
armnn::optimizations::MoveTransposeUpImpl::MoveTransposeUpImpl
MoveTransposeUpImpl()=default
armnn::Layer
Definition: Layer.hpp:230
armnn::TransposeLayer
This layer represents a transpose operation.
Definition: TransposeLayer.hpp:15
armnn::OutputSlot::GetOwningLayer
Layer & GetOwningLayer() const
Definition: Layer.hpp:132
armnn::OptimizeForConnection
Definition: Optimization.hpp:118
armnn::OutputSlot::GetNumConnections
unsigned int GetNumConnections() const override
Definition: Layer.hpp:158
PolymorphicDowncast.hpp
armnn::Layer::EndInputSlots
std::vector< InputSlot >::iterator EndInputSlots()
Definition: Layer.hpp:263
armnn::Layer::GetOutputHandler
const OutputHandler & GetOutputHandler(unsigned int i=0) const
Definition: Layer.hpp:245
armnn::LayerType::Multiplication
@ Multiplication
armnn::LayerType::Addition
@ Addition
armnn::PermutationVector
Definition: Types.hpp:314
armnn::Layer::BeginInputSlots
std::vector< InputSlot >::iterator BeginInputSlots()
Definition: Layer.hpp:262
armnn::InputSlot
Definition: Layer.hpp:42
armnn::Layer::GetParameters
virtual const BaseDescriptor & GetParameters() const override
If the layer has a descriptor return it.
Definition: Layer.hpp:378
Transpose.hpp
armnn::Layer::GetType
LayerType GetType() const override
Returns the armnn::LayerType of this layer.
Definition: Layer.hpp:286
armnn::InputSlot::GetConnectedOutputSlot
const OutputSlot * GetConnectedOutputSlot() const
Definition: Layer.hpp:56
armnn::LayerType::MemCopy
@ MemCopy
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
armnn::optimizations::MoveTransposeUpImpl
Definition: MoveTransposeUp.hpp:16
armnn::LayerType::FakeQuantization
@ FakeQuantization
armnn::LayerType::Activation
@ Activation
armnn::Graph
Definition: Graph.hpp:30
armnn::Graph::InsertNewLayer
LayerT * InsertNewLayer(InputSlot &insertBefore, Args &&... args)
Inserts a new layer between the output slot currently connected to insertBefore and insertBefore itse...
Definition: Graph.hpp:471