ArmNN
 24.08
MovePermuteUp.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017-2018,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/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;
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::optimizations::MovePermuteUpImpl
Definition: MovePermuteUp.hpp:16
armnn::OutputSlot
Definition: Layer.hpp:100
armnn::TensorInfo
Definition: Tensor.hpp:152
armnn::LayerType::Floor
@ Floor
armnn::OutputHandler::SetTensorInfo
void SetTensorInfo(const TensorInfo &tensorInfo)
Sets the TensorInfo used by this output handler.
Definition: OutputHandler.cpp:15
armnn::optimizations::MovePermuteUpImpl::~MovePermuteUpImpl
~MovePermuteUpImpl()=default
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::Permuted
armnn::TensorShape Permuted(const armnn::TensorShape &srcShape, const armnn::PermutationVector &mappings)
Definition: Permute.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::LayerType::ElementwiseBinary
@ ElementwiseBinary
armnn::Layer
Definition: Layer.hpp:230
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::optimizations::MovePermuteUpImpl::Run
void Run(Graph &graph, InputSlot &connection) const
Run for every connection between a base Layer (any) and a child PermuteLayer.
Definition: MovePermuteUp.hpp:23
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
Permute.hpp
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
armnn::Layer::GetType
LayerType GetType() const override
Returns the armnn::LayerType of this layer.
Definition: Layer.hpp:286
armnn::optimizations::MovePermuteUpImpl::MovePermuteUpImpl
MovePermuteUpImpl()=default
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::PermuteLayer
This layer represents a permutation operation.
Definition: PermuteLayer.hpp:15
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:481