ArmNN
 24.02
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  if (activationDesc->m_Function == ActivationFunction::LeakyReLu)
30  {
31  return ConvertActivationToTosaOperator(layer, inputs, outputs, activationDesc);
32  }
33  else
34  {
36  }
37  }
41  {
42  return ConvertElementwiseBinaryToTosaOperator(layer, type, inputs, outputs);
43  }
45  {
46  auto binaryDesc = PolymorphicDowncast<const ElementwiseBinaryDescriptor*>(&descriptor);
47  return ConvertElementwiseBinaryToTosaOperator(layer, type, inputs, outputs, binaryDesc);
48  }
50  {
51  auto unaryDesc = PolymorphicDowncast<const ElementwiseUnaryDescriptor*>(&descriptor);
52  return ConvertElementwiseUnaryOperator(layer, inputs, outputs, unaryDesc);
53  }
54  case LayerType::Concat:
55  {
56  auto concatDesc = PolymorphicDowncast<const OriginsDescriptor*>(&descriptor);
57  return ConvertConcatToTosaOperator(layer, inputs, outputs, concatDesc);
58  }
60  {
61  return ConvertConstantToTosaOperator(layer, outputs);
62  }
64  {
65  auto conv2dDesc = PolymorphicDowncast<const Convolution2dDescriptor*>(&descriptor);
66  return ConvertConv2dToTosaOperator(layer, inputs, outputs, conv2dDesc);
67  }
69  {
70  auto poolDesc = PolymorphicDowncast<const Pooling2dDescriptor*>(&descriptor);
71 
72  bool avgPoolIgnoreValue =
73  (poolDesc->m_PoolType == PoolingAlgorithm::Average) &&
74  (poolDesc->m_PaddingMethod == PaddingMethod::IgnoreValue);
75 
76  if (poolDesc->m_PoolType == PoolingAlgorithm::L2)
77  {
79  }
80  else if (avgPoolIgnoreValue)
81  {
82  return ConvertAvgPool2DIgnoreValueToTosaOperator(layer, inputs, outputs, poolDesc);
83  }
84  else
85  {
86  return ConvertPooling2DToTosaOperator(layer, inputs, outputs, poolDesc);
87  }
88  }
90  {
91  return ConvertQuantizeToTosaOperator(layer, inputs, outputs);
92  }
93  case LayerType::Reshape:
94  {
95  auto reshapeDesc = PolymorphicDowncast<const ReshapeDescriptor*>(&descriptor);
96  return ConvertReshapeToTosaOperator(layer, inputs, outputs, reshapeDesc);
97  }
98  case LayerType::Resize:
99  {
100  auto resizeDesc = PolymorphicDowncast<const ResizeDescriptor*>(&descriptor);
101  return ConvertResizeToTosaOperator(layer, inputs, outputs, resizeDesc);
102  }
103  case LayerType::Slice:
104  {
105  auto sliceDesc = PolymorphicDowncast<const SliceDescriptor*>(&descriptor);
106  return ConvertSliceToTosaOperator(layer, inputs, outputs, sliceDesc);
107  }
108  case LayerType::Splitter:
109  {
110  auto splitDesc = PolymorphicDowncast<const SplitterDescriptor*>(&descriptor);
111  return ConvertSplitToTosaOperator(layer, inputs, outputs, splitDesc);
112  }
114  {
115  auto transposeConv2dDesc = PolymorphicDowncast<const TransposeConvolution2dDescriptor*>(&descriptor);
116  return ConvertTransposeConv2dToTosaOperator(layer, inputs, outputs, transposeConv2dDesc);
117  }
119  {
120  auto transposeDesc = PolymorphicDowncast<const TransposeDescriptor*>(&descriptor);
121  return ConvertTransposeToTosaOperator(layer, inputs, outputs, transposeDesc);
122  }
123  default:
124  {
126  }
127  }
128 }
129 
130 TosaSerializationBasicBlock* GetTosaMappingFromLayer(Layer* layer)
131 {
132  std::vector<const TensorInfo*> inputs;
133  for (auto inputSlot : layer->GetInputSlots())
134  {
135  inputs.push_back(&inputSlot.GetTensorInfo());
136  }
137 
138  std::vector<const TensorInfo*> outputs;
139  for (auto& outputSlot : layer->GetOutputSlots())
140  {
141  outputs.push_back(&outputSlot.GetTensorInfo());
142  }
143 
144  TosaSerializationBasicBlock* basicBlock = GetTosaMapping(layer,
145  layer->GetType(),
146  inputs,
147  outputs,
148  layer->GetParameters());
149  return basicBlock;
150 }
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
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:8
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:8
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::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::Layer::GetInputSlots
const std::vector< InputSlot > & GetInputSlots() const
Definition: Layer.hpp:258
armnn::LayerType::ElementwiseUnary
@ ElementwiseUnary
ConvertElementwiseUnaryOperator
TosaSerializationBasicBlock * ConvertElementwiseUnaryOperator(const Layer *layer, const std::vector< const TensorInfo * > &inputs, const std::vector< const TensorInfo * > &outputs, const ElementwiseUnaryDescriptor *unaryDescriptor)
Definition: ElementwiseUnaryOperator.cpp:8
ConvertConstantToTosaOperator
TosaSerializationBasicBlock * ConvertConstantToTosaOperator(const Layer *layer, const std::vector< const TensorInfo * > &outputs)
Definition: ConstantOperator.cpp:10
armnn::LayerType::ElementwiseBinary
@ ElementwiseBinary
ConvertSplitToTosaOperator
TosaSerializationBasicBlock * ConvertSplitToTosaOperator(const Layer *layer, const std::vector< const TensorInfo * > &inputs, const std::vector< const TensorInfo * > &outputs, const SplitterDescriptor *splitDescriptor)
Definition: SplitOperator.cpp:13
armnn::Layer
Definition: Layer.hpp:230
armnn::LayerType::Slice
@ Slice
armnn::LayerType::Subtraction
@ Subtraction
armnn::LayerType::Concat
@ Concat
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::PaddingMethod::IgnoreValue
@ IgnoreValue
The padding fields count, but are ignored.
armnn::LayerType::Quantize
@ Quantize
armnn::LayerType::Multiplication
@ Multiplication
armnn::LayerType::Addition
@ Addition
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::Layer::GetParameters
virtual const BaseDescriptor & GetParameters() const override
If the layer has a descriptor return it.
Definition: Layer.hpp:378
ConvertQuantizeToTosaOperator
TosaSerializationBasicBlock * ConvertQuantizeToTosaOperator(const Layer *layer, const std::vector< const TensorInfo * > &inputs, const std::vector< const TensorInfo * > &outputs)
Definition: QuantizeOperator.cpp:13
armnn::Layer::GetType
LayerType GetType() const override
Returns the armnn::LayerType of this layer.
Definition: Layer.hpp:286
armnn::LayerType::Reshape
@ Reshape
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
ConvertActivationToTosaOperator
TosaSerializationBasicBlock * ConvertActivationToTosaOperator(const Layer *layer, const std::vector< const TensorInfo * > &inputs, const std::vector< const TensorInfo * > &outputs, const ActivationDescriptor *activationDescriptor)
Definition: ActivationOperator.cpp:17
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
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
armnn::LayerType::Resize
@ Resize
armnn::LayerType::Convolution2d
@ Convolution2d
armnn::LayerType::Activation
@ Activation
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
GetTosaMappingFromLayer
TosaSerializationBasicBlock * GetTosaMappingFromLayer(Layer *layer)
Definition: TosaMappings.cpp:130
armnn::LayerType::Constant
@ Constant