ArmNN
 24.08
TosaMappings.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2022-2024 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "TosaMappings.hpp"
7 
8 using namespace armnn;
9 using namespace tosa;
10 
11 TosaSerializationBasicBlock* CreateEmptyTosaSerializationBasicBlock()
12 {
13  // Empty basic block when no TOSA mapping implemented/exists
14  auto* op = new TosaSerializationOperator(Op_UNKNOWN, Attribute_NONE, nullptr, {}, {});
15  return new TosaSerializationBasicBlock("", "", {op}, {}, {}, {});
16 }
17 
18 TosaSerializationBasicBlock* GetTosaMapping(const Layer* layer,
19  const LayerType type,
20  const std::vector<const TensorInfo*>& inputs,
21  const std::vector<const TensorInfo*>& outputs,
22  const BaseDescriptor& descriptor)
23 {
24  switch (type)
25  {
27  {
28  auto activationDesc = PolymorphicDowncast<const ActivationDescriptor*>(&descriptor);
29  switch (activationDesc->m_Function)
30  {
32  {
33  return ConvertLeakyReluToTosaOperator(layer, inputs, outputs, activationDesc);
34  }
37  {
38  return ConvertReluToTosaOperator(layer, inputs, outputs, activationDesc);
39  }
41  {
42  return ConvertGeluToTosaOperator(layer, inputs, outputs, activationDesc);
43  }
44  default:
45  {
47  }
48  }
49  }
53  {
54  return ConvertElementwiseBinaryToTosaOperator(layer, type, inputs, outputs);
55  }
57  {
58  auto binaryDesc = PolymorphicDowncast<const ElementwiseBinaryDescriptor*>(&descriptor);
59  return ConvertElementwiseBinaryToTosaOperator(layer, type, inputs, outputs, binaryDesc);
60  }
62  {
63  auto unaryDesc = PolymorphicDowncast<const ElementwiseUnaryDescriptor*>(&descriptor);
64  switch(unaryDesc->m_Operation)
65  {
67  {
68  return ConvertRsqrtOperator(layer, inputs, outputs, unaryDesc);
69  }
71  {
72  return ConvertExpOperator(layer, inputs, outputs, unaryDesc);
73  }
75  {
76  return ConvertLogOperator(layer, inputs, outputs, unaryDesc);
77  }
78  default:
79  {
81  }
82  }
83  }
85  {
86  auto batchMatMulDesc = PolymorphicDowncast<const BatchMatMulDescriptor*>(&descriptor);
87  return ConvertBatchMatMulToTosaOperator(layer, inputs, outputs, batchMatMulDesc);
88  }
89  case LayerType::Concat:
90  {
91  auto concatDesc = PolymorphicDowncast<const OriginsDescriptor*>(&descriptor);
92  return ConvertConcatToTosaOperator(layer, inputs, outputs, concatDesc);
93  }
95  {
96  bool isDepthwiseConv2dWeights = false;
97  if(layer)
98  {
99  // The difference in layout of weights in Tensorflow/ArmNN and the layout
100  // described in TOSA means we must permute the weights from [1, H, W, C * M] to [H, W, C, M].
101  unsigned int slotIdx = layer->GetOutputSlot().GetConnection(0)->GetSlotIndex();
103  if(type == LayerType::DepthwiseConvolution2d && slotIdx == 1)
104  {
105  isDepthwiseConv2dWeights = true;
106  }
107  }
108  return ConvertConstantToTosaOperator(layer, outputs, isDepthwiseConv2dWeights);
109  }
111  {
112  auto conv2dDesc = PolymorphicDowncast<const Convolution2dDescriptor*>(&descriptor);
113  return ConvertConv2dToTosaOperator(layer, inputs, outputs, conv2dDesc);
114  }
116  {
117  auto conv2dDesc = PolymorphicDowncast<const DepthwiseConvolution2dDescriptor*>(&descriptor);
118  return ConvertDepthwiseConv2dToTosaOperator(layer, inputs, outputs, conv2dDesc);
119  }
121  {
122  auto fullyConnectedDesc = PolymorphicDowncast<const FullyConnectedDescriptor*>(&descriptor);
123  return ConvertFullyConnectedToTosaOperator(layer, inputs, outputs, fullyConnectedDesc);
124  }
125  case LayerType::Pad:
126  {
127  auto padDesc = PolymorphicDowncast<const PadDescriptor*>(&descriptor);
128  return ConvertPadToTosaOperator(layer, inputs, outputs, padDesc);
129  }
131  {
132  auto poolDesc = PolymorphicDowncast<const Pooling2dDescriptor*>(&descriptor);
133 
134  bool avgPoolIgnoreValue =
135  (poolDesc->m_PoolType == PoolingAlgorithm::Average) &&
136  (poolDesc->m_PaddingMethod == PaddingMethod::IgnoreValue);
137 
138  if (poolDesc->m_PoolType == PoolingAlgorithm::L2)
139  {
141  }
142  else if (avgPoolIgnoreValue)
143  {
144  return ConvertAvgPool2DIgnoreValueToTosaOperator(layer, inputs, outputs, poolDesc);
145  }
146  else
147  {
148  return ConvertPooling2DToTosaOperator(layer, inputs, outputs, poolDesc);
149  }
150  }
151  case LayerType::Mean:
152  {
153  auto meanDesc = PolymorphicDowncast<const MeanDescriptor*>(&descriptor);
154 
155  ReduceDescriptor reduceDesc;
156  reduceDesc.m_KeepDims = meanDesc->m_KeepDims;
157  reduceDesc.m_vAxis = meanDesc->m_Axis;
159 
160  return ConvertReduceToTosaOperator(layer, inputs, outputs, &reduceDesc);
161  }
162  case LayerType::Quantize:
163  {
164  return ConvertQuantizeToTosaOperator(layer, inputs, outputs);
165  }
166  case LayerType::Reduce:
167  {
168  auto reduceDesc = PolymorphicDowncast<const ReduceDescriptor*>(&descriptor);
169  return ConvertReduceToTosaOperator(layer, inputs, outputs, reduceDesc);
170  }
171  case LayerType::Reshape:
172  {
173  auto reshapeDesc = PolymorphicDowncast<const ReshapeDescriptor*>(&descriptor);
174  return ConvertReshapeToTosaOperator(layer, inputs, outputs, reshapeDesc);
175  }
176  case LayerType::Resize:
177  {
178  auto resizeDesc = PolymorphicDowncast<const ResizeDescriptor*>(&descriptor);
179  return ConvertResizeToTosaOperator(layer, inputs, outputs, resizeDesc);
180  }
181  case LayerType::Slice:
182  {
183  auto sliceDesc = PolymorphicDowncast<const SliceDescriptor*>(&descriptor);
184  return ConvertSliceToTosaOperator(layer, inputs, outputs, sliceDesc);
185  }
186  case LayerType::Softmax:
187  {
188  auto softmaxDesc = PolymorphicDowncast<const SoftmaxDescriptor*>(&descriptor);
189  return ConvertSoftmaxToTosaOperator(layer, inputs, outputs, softmaxDesc);
190  }
191  case LayerType::Splitter:
192  {
193  auto splitDesc = PolymorphicDowncast<const SplitterDescriptor*>(&descriptor);
194  return ConvertSplitToTosaOperator(layer, inputs, outputs, splitDesc);
195  }
197  {
198  auto transposeConv2dDesc = PolymorphicDowncast<const TransposeConvolution2dDescriptor*>(&descriptor);
199  return ConvertTransposeConv2dToTosaOperator(layer, inputs, outputs, transposeConv2dDesc);
200  }
202  {
203  auto transposeDesc = PolymorphicDowncast<const TransposeDescriptor*>(&descriptor);
204  return ConvertTransposeToTosaOperator(layer, inputs, outputs, transposeDesc);
205  }
206  default:
207  {
209  }
210  }
211 }
212 
213 TosaSerializationBasicBlock* GetTosaMappingFromLayer(const Layer* layer)
214 {
215  std::vector<const TensorInfo*> inputs;
216  for (auto inputSlot : layer->GetInputSlots())
217  {
218  inputs.push_back(&inputSlot.GetTensorInfo());
219  }
220 
221  std::vector<const TensorInfo*> outputs;
222  for (auto& outputSlot : layer->GetOutputSlots())
223  {
224  outputs.push_back(&outputSlot.GetTensorInfo());
225  }
226 
227  TosaSerializationBasicBlock* basicBlock = GetTosaMapping(layer,
228  layer->GetType(),
229  inputs,
230  outputs,
231  layer->GetParameters());
232  return basicBlock;
233 }
ConvertGeluToTosaOperator
TosaSerializationBasicBlock * ConvertGeluToTosaOperator(const Layer *layer, const std::vector< const TensorInfo * > &inputs, const std::vector< const TensorInfo * > &outputs, const ActivationDescriptor *desc)
Definition: GeluOperator.cpp:17
CreateEmptyTosaSerializationBasicBlock
TosaSerializationBasicBlock * CreateEmptyTosaSerializationBasicBlock()
Definition: TosaMappings.cpp:11
ConvertReshapeToTosaOperator
TosaSerializationBasicBlock * ConvertReshapeToTosaOperator(const Layer *layer, const std::vector< const TensorInfo * > &inputs, const std::vector< const TensorInfo * > &outputs, const ReshapeDescriptor *reshapeDescriptor)
Definition: ReshapeOperator.cpp:8
armnn::LayerType::Splitter
@ Splitter
armnn::ActivationFunction::LeakyReLu
@ LeakyReLu
ConvertRsqrtOperator
TosaSerializationBasicBlock * ConvertRsqrtOperator(const Layer *layer, const std::vector< const TensorInfo * > &inputs, const std::vector< const TensorInfo * > &outputs, const ElementwiseUnaryDescriptor *unaryDescriptor)
Definition: RsqrtOperator.cpp:8
armnn::InputSlot::GetOwningLayer
Layer & GetOwningLayer() const
Definition: Layer.hpp:53
ConvertElementwiseBinaryToTosaOperator
TosaSerializationBasicBlock * ConvertElementwiseBinaryToTosaOperator(const Layer *layer, const LayerType type, const std::vector< const TensorInfo * > &inputs, const std::vector< const TensorInfo * > &outputs, const ElementwiseBinaryDescriptor *descriptor)
Definition: ElementwiseBinaryOperator.cpp:36
ConvertAvgPool2DIgnoreValueToTosaOperator
TosaSerializationBasicBlock * ConvertAvgPool2DIgnoreValueToTosaOperator(const Layer *layer, const std::vector< const TensorInfo * > &inputs, const std::vector< const TensorInfo * > &outputs, const Pooling2dDescriptor *poolDescriptor)
Definition: AvgPool2DIgnoreValueOperator.cpp:8
ConvertConv2dToTosaOperator
TosaSerializationBasicBlock * ConvertConv2dToTosaOperator(const Layer *layer, const std::vector< const TensorInfo * > &inputs, const std::vector< const TensorInfo * > &outputs, const Convolution2dDescriptor *conv2dDescriptor)
Definition: Conv2dOperator.cpp:10
armnn::LayerType::Transpose
@ Transpose
ConvertPooling2DToTosaOperator
TosaSerializationBasicBlock * ConvertPooling2DToTosaOperator(const Layer *layer, const std::vector< const TensorInfo * > &inputs, const std::vector< const TensorInfo * > &outputs, const Pooling2dDescriptor *poolDescriptor)
Definition: Pooling2DOperator.cpp:8
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
armnn::PoolingAlgorithm::L2
@ L2
ConvertConcatToTosaOperator
TosaSerializationBasicBlock * ConvertConcatToTosaOperator(const Layer *layer, const std::vector< const TensorInfo * > &inputs, const std::vector< const TensorInfo * > &outputs, const OriginsDescriptor *concatDescriptor)
Definition: ConcatOperator.cpp:8
armnn::ActivationFunction::BoundedReLu
@ BoundedReLu
min(a, max(b, input)) ReLu1 & ReLu6.
armnn::Layer::GetInputSlots
const std::vector< InputSlot > & GetInputSlots() const
Definition: Layer.hpp:258
armnn::LayerType::Reduce
@ Reduce
armnn::LayerType::ElementwiseUnary
@ ElementwiseUnary
armnn::ReduceOperation::Mean
@ Mean
armnn::ActivationFunction::Gelu
@ Gelu
armnn::LayerType::ElementwiseBinary
@ ElementwiseBinary
armnn::ReduceDescriptor::m_ReduceOperation
ReduceOperation m_ReduceOperation
Specifies the reduction operation to execute.
Definition: Descriptors.hpp:1558
ConvertSplitToTosaOperator
TosaSerializationBasicBlock * ConvertSplitToTosaOperator(const Layer *layer, const std::vector< const TensorInfo * > &inputs, const std::vector< const TensorInfo * > &outputs, const SplitterDescriptor *splitDescriptor)
Definition: SplitOperator.cpp:14
armnn::Layer
Definition: Layer.hpp:230
ConvertPadToTosaOperator
TosaSerializationBasicBlock * ConvertPadToTosaOperator(const Layer *layer, const std::vector< const TensorInfo * > &inputs, const std::vector< const TensorInfo * > &outputs, const PadDescriptor *padDescriptor)
Definition: PadOperator.cpp:9
armnn::InputSlot::GetSlotIndex
unsigned int GetSlotIndex() const override
Definition: Layer.hpp:54
armnn::LayerType::Slice
@ Slice
GetTosaMappingFromLayer
TosaSerializationBasicBlock * GetTosaMappingFromLayer(const Layer *layer)
Definition: TosaMappings.cpp:213
armnn::UnaryOperation::Rsqrt
@ Rsqrt
armnn::LayerType::Subtraction
@ Subtraction
armnn::LayerType::Concat
@ Concat
armnn::UnaryOperation::Exp
@ Exp
ConvertSliceToTosaOperator
TosaSerializationBasicBlock * ConvertSliceToTosaOperator(const Layer *layer, const std::vector< const TensorInfo * > &inputs, const std::vector< const TensorInfo * > &outputs, const SliceDescriptor *sliceDescriptor)
Definition: SliceOperator.cpp:8
armnn::LayerType::TransposeConvolution2d
@ TransposeConvolution2d
armnn::LayerType::Softmax
@ Softmax
ConvertReluToTosaOperator
TosaSerializationBasicBlock * ConvertReluToTosaOperator(const Layer *layer, const std::vector< const TensorInfo * > &inputs, const std::vector< const TensorInfo * > &outputs, const ActivationDescriptor *desc)
Definition: ReluOperator.cpp:17
armnn::PaddingMethod::IgnoreValue
@ IgnoreValue
The padding fields count, but are ignored.
armnn::LayerType::Quantize
@ Quantize
armnn::LayerType::Multiplication
@ Multiplication
armnn::LayerType::Addition
@ Addition
ConvertLeakyReluToTosaOperator
TosaSerializationBasicBlock * ConvertLeakyReluToTosaOperator(const Layer *layer, const std::vector< const TensorInfo * > &inputs, const std::vector< const TensorInfo * > &outputs, const ActivationDescriptor *activationDescriptor)
Definition: LeakyReluOperator.cpp:17
armnn::Layer::GetOutputSlots
const std::vector< OutputSlot > & GetOutputSlots() const
Definition: Layer.hpp:259
armnn::BaseDescriptor
Base class for all descriptors.
Definition: Descriptors.hpp:22
GetTosaMapping
TosaSerializationBasicBlock * GetTosaMapping(const Layer *layer, const LayerType type, const std::vector< const TensorInfo * > &inputs, const std::vector< const TensorInfo * > &outputs, const BaseDescriptor &descriptor)
Definition: TosaMappings.cpp:18
armnn::LayerType::Pooling2d
@ Pooling2d
armnn::PoolingAlgorithm::Average
@ Average
ConvertTransposeConv2dToTosaOperator
TosaSerializationBasicBlock * ConvertTransposeConv2dToTosaOperator(const Layer *layer, const std::vector< const TensorInfo * > &inputs, const std::vector< const TensorInfo * > &outputs, const TransposeConvolution2dDescriptor *descriptor)
Definition: TransposeConv2dOperator.cpp:10
armnn::ReduceDescriptor::m_KeepDims
bool m_KeepDims
if true then output shape has no change.
Definition: Descriptors.hpp:1554
armnn::Layer::GetParameters
virtual const BaseDescriptor & GetParameters() const override
If the layer has a descriptor return it.
Definition: Layer.hpp:378
armnn::LayerType::FullyConnected
@ FullyConnected
ConvertQuantizeToTosaOperator
TosaSerializationBasicBlock * ConvertQuantizeToTosaOperator(const Layer *layer, const std::vector< const TensorInfo * > &inputs, const std::vector< const TensorInfo * > &outputs)
Definition: QuantizeOperator.cpp:15
armnn::UnaryOperation::Log
@ Log
armnn::LayerType::BatchMatMul
@ BatchMatMul
armnn::LayerType::DepthwiseConvolution2d
@ DepthwiseConvolution2d
ConvertReduceToTosaOperator
TosaSerializationBasicBlock * ConvertReduceToTosaOperator(const Layer *layer, const std::vector< const TensorInfo * > &inputs, const std::vector< const TensorInfo * > &outputs, const ReduceDescriptor *reduceDescriptor)
Definition: ReduceOperator.cpp:17
armnn::Layer::GetType
LayerType GetType() const override
Returns the armnn::LayerType of this layer.
Definition: Layer.hpp:286
armnn::LayerType::Reshape
@ Reshape
ConvertConstantToTosaOperator
TosaSerializationBasicBlock * ConvertConstantToTosaOperator(const Layer *layer, const std::vector< const TensorInfo * > &outputs, bool isDepthwiseConv2dWeights=false)
Definition: ConstantOperator.cpp:10
armnn::ReduceDescriptor::m_vAxis
std::vector< uint32_t > m_vAxis
The indices of the dimensions to reduce.
Definition: Descriptors.hpp:1556
ConvertDepthwiseConv2dToTosaOperator
TosaSerializationBasicBlock * ConvertDepthwiseConv2dToTosaOperator(const Layer *layer, const std::vector< const TensorInfo * > &inputs, const std::vector< const TensorInfo * > &outputs, const DepthwiseConvolution2dDescriptor *conv2dDescriptor)
Definition: DepthwiseConv2dOperator.cpp:10
ConvertFullyConnectedToTosaOperator
TosaSerializationBasicBlock * ConvertFullyConnectedToTosaOperator(const Layer *layer, const std::vector< const TensorInfo * > &inputs, const std::vector< const TensorInfo * > &outputs, const FullyConnectedDescriptor *fcDescriptor)
Definition: FullyConnectedOperator.cpp:16
armnn::ActivationFunction::ReLu
@ ReLu
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
armnn::LayerType::Pad
@ Pad
armnn::LayerType::Mean
@ Mean
ConvertResizeToTosaOperator
TosaSerializationBasicBlock * ConvertResizeToTosaOperator(const Layer *layer, const std::vector< const TensorInfo * > &inputs, const std::vector< const TensorInfo * > &outputs, const ResizeDescriptor *resizeDescriptor)
Definition: ResizeOperator.cpp:15
ConvertSoftmaxToTosaOperator
TosaSerializationBasicBlock * ConvertSoftmaxToTosaOperator(const Layer *layer, const std::vector< const TensorInfo * > &inputs, const std::vector< const TensorInfo * > &outputs, const SoftmaxDescriptor *softmaxDescriptor)
Definition: SoftmaxOperator.cpp:17
ConvertTransposeToTosaOperator
TosaSerializationBasicBlock * ConvertTransposeToTosaOperator(const Layer *layer, const std::vector< const TensorInfo * > &inputs, const std::vector< const TensorInfo * > &outputs, const TransposeDescriptor *transposeDescriptor)
Definition: TransposeOperator.cpp:8
ConvertLogOperator
TosaSerializationBasicBlock * ConvertLogOperator(const Layer *layer, const std::vector< const TensorInfo * > &inputs, const std::vector< const TensorInfo * > &outputs, const ElementwiseUnaryDescriptor *unaryDescriptor)
Definition: LogOperator.cpp:13
armnn::LayerType::Resize
@ Resize
armnn::LayerType::Convolution2d
@ Convolution2d
armnn::LayerType::Activation
@ Activation
armnn::ReduceDescriptor
A ReduceDescriptor for the REDUCE operators.
Definition: Descriptors.hpp:1538
TosaMappings.hpp
armnn::LayerType
LayerType
When adding a new layer, adapt also the LastLayer enum value in the enum class LayerType below.
Definition: Types.hpp:491
armnn::OutputSlot::GetConnection
const InputSlot * GetConnection(unsigned int index) const override
Definition: Layer.cpp:83
ConvertExpOperator
TosaSerializationBasicBlock * ConvertExpOperator(const Layer *layer, const std::vector< const TensorInfo * > &inputs, const std::vector< const TensorInfo * > &outputs, const ElementwiseUnaryDescriptor *unaryDescriptor)
Definition: ExpOperator.cpp:13
ConvertBatchMatMulToTosaOperator
TosaSerializationBasicBlock * ConvertBatchMatMulToTosaOperator(const Layer *layer, const std::vector< const TensorInfo * > &inputs, const std::vector< const TensorInfo * > &outputs, const BatchMatMulDescriptor *descriptor)
Definition: BatchMatMulOperator.cpp:14
armnn::LayerType::Constant
@ Constant