blob: dff266d793a415c4e7d5e2fa66c3f3dbdb34a8e6 [file] [log] [blame]
Matthew Sloyan164bf4f2022-10-28 18:02:17 +01001//
David Monahand7fca092023-01-12 14:53:34 +00002// Copyright © 2022-2023 Arm Ltd and Contributors. All rights reserved.
Matthew Sloyan164bf4f2022-10-28 18:02:17 +01003// SPDX-License-Identifier: MIT
4//
5
6#include "TosaMappings.hpp"
7
8using namespace armnn;
9using namespace tosa;
10
Cathal Corbettbd18eab2022-11-15 12:56:16 +000011TosaSerializationBasicBlock* CreateEmptyTosaSerializationBasicBlock()
12{
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000013 // Empty basic block when no TOSA mapping implemented/exists
14 auto* op = new TosaSerializationOperator(Op_UNKNOWN, Attribute_NONE, nullptr, {}, {});
Narumol Prangnawaratad323af2023-09-29 17:00:38 +010015 return new TosaSerializationBasicBlock("", "", {op}, {}, {}, {});
Cathal Corbettbd18eab2022-11-15 12:56:16 +000016}
17
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000018TosaSerializationBasicBlock* GetTosaMapping(const Layer* layer,
19 const LayerType type,
Matthew Sloyan164bf4f2022-10-28 18:02:17 +010020 const std::vector<const TensorInfo*>& inputs,
21 const std::vector<const TensorInfo*>& outputs,
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000022 const BaseDescriptor& descriptor)
Matthew Sloyan164bf4f2022-10-28 18:02:17 +010023{
24 switch (type)
25 {
26 case LayerType::Addition:
Nikhil Raj9a339462022-12-05 11:24:35 +000027 case LayerType::Multiplication:
28 case LayerType::Subtraction:
Matthew Sloyan164bf4f2022-10-28 18:02:17 +010029 {
Nikhil Raj9a339462022-12-05 11:24:35 +000030 return ConvertElementwiseBinaryToTosaOperator(layer, type, inputs, outputs);
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000031 }
Tianle Cheng7790dc62023-12-12 13:52:22 +000032 case LayerType::ElementwiseBinary:
33 {
34 auto binaryDesc = PolymorphicDowncast<const ElementwiseBinaryDescriptor*>(&descriptor);
35 return ConvertElementwiseBinaryToTosaOperator(layer, type, inputs, outputs, binaryDesc);
36 }
David Monahand7fca092023-01-12 14:53:34 +000037 case LayerType::ElementwiseUnary:
38 {
39 auto unaryDesc = PolymorphicDowncast<const ElementwiseUnaryDescriptor*>(&descriptor);
40 return ConvertElementwiseUnaryOperator(layer, inputs, outputs, unaryDesc);
41 }
Kevin May5b58e312022-12-15 10:15:21 +000042 case LayerType::Concat:
43 {
44 auto concatDesc = PolymorphicDowncast<const OriginsDescriptor*>(&descriptor);
45 return ConvertConcatToTosaOperator(layer, inputs, outputs, concatDesc);
46 }
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000047 case LayerType::Constant:
48 {
49 return ConvertConstantToTosaOperator(layer, outputs);
50 }
51 case LayerType::Convolution2d:
52 {
53 auto conv2dDesc = PolymorphicDowncast<const Convolution2dDescriptor*>(&descriptor);
54 return ConvertConv2dToTosaOperator(layer, inputs, outputs, conv2dDesc);
Matthew Sloyan164bf4f2022-10-28 18:02:17 +010055 }
Cathal Corbettbd18eab2022-11-15 12:56:16 +000056 case LayerType::Pooling2d:
57 {
58 auto poolDesc = PolymorphicDowncast<const Pooling2dDescriptor*>(&descriptor);
59
60 bool avgPoolIgnoreValue =
61 (poolDesc->m_PoolType == PoolingAlgorithm::Average) &&
62 (poolDesc->m_PaddingMethod == PaddingMethod::IgnoreValue);
63
64 if (poolDesc->m_PoolType == PoolingAlgorithm::L2)
65 {
66 return CreateEmptyTosaSerializationBasicBlock();
67 }
68 else if (avgPoolIgnoreValue)
69 {
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000070 return ConvertAvgPool2DIgnoreValueToTosaOperator(layer, inputs, outputs, poolDesc);
Cathal Corbettbd18eab2022-11-15 12:56:16 +000071 }
72 else
73 {
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000074 return ConvertPooling2DToTosaOperator(layer, inputs, outputs, poolDesc);
Cathal Corbettbd18eab2022-11-15 12:56:16 +000075 }
76 }
Cathal Corbettb30e6552022-12-07 11:50:50 +000077 case LayerType::Reshape:
78 {
79 auto reshapeDesc = PolymorphicDowncast<const ReshapeDescriptor*>(&descriptor);
80 return ConvertReshapeToTosaOperator(layer, inputs, outputs, reshapeDesc);
81 }
Teresa Charlince655882023-11-21 15:44:13 +000082 case LayerType::Resize:
83 {
84 auto resizeDesc = PolymorphicDowncast<const ResizeDescriptor*>(&descriptor);
85 return ConvertResizeToTosaOperator(layer, inputs, outputs, resizeDesc);
86 }
Cathal Corbett3b9acd52022-12-09 12:17:27 +000087 case LayerType::Slice:
88 {
89 auto sliceDesc = PolymorphicDowncast<const SliceDescriptor*>(&descriptor);
90 return ConvertSliceToTosaOperator(layer, inputs, outputs, sliceDesc);
91 }
Kevin May1bea6be2023-12-12 11:18:46 +000092 case LayerType::Splitter:
93 {
94 auto splitDesc = PolymorphicDowncast<const SplitterDescriptor*>(&descriptor);
95 return ConvertSplitToTosaOperator(layer, inputs, outputs, splitDesc);
96 }
Matthew Sloyanfc9d5e72022-12-08 13:38:23 +000097 case LayerType::TransposeConvolution2d:
98 {
99 auto transposeConv2dDesc = PolymorphicDowncast<const TransposeConvolution2dDescriptor*>(&descriptor);
100 return ConvertTransposeConv2dToTosaOperator(layer, inputs, outputs, transposeConv2dDesc);
101 }
Cathal Corbett0bb096d2022-12-22 13:09:38 +0000102 case LayerType::Transpose:
103 {
104 auto transposeDesc = PolymorphicDowncast<const TransposeDescriptor*>(&descriptor);
105 return ConvertTransposeToTosaOperator(layer, inputs, outputs, transposeDesc);
106 }
Matthew Sloyan164bf4f2022-10-28 18:02:17 +0100107 default:
108 {
Cathal Corbettbd18eab2022-11-15 12:56:16 +0000109 return CreateEmptyTosaSerializationBasicBlock();
Matthew Sloyan164bf4f2022-10-28 18:02:17 +0100110 }
111 }
112}
113
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +0000114TosaSerializationBasicBlock* GetTosaMappingFromLayer(Layer* layer)
Matthew Sloyan164bf4f2022-10-28 18:02:17 +0100115{
116 std::vector<const TensorInfo*> inputs;
117 for (auto inputSlot : layer->GetInputSlots())
118 {
Mike Kellya9ac6ba2023-06-30 15:18:26 +0100119 inputs.push_back(&inputSlot.GetTensorInfo());
Matthew Sloyan164bf4f2022-10-28 18:02:17 +0100120 }
121
122 std::vector<const TensorInfo*> outputs;
123 for (auto& outputSlot : layer->GetOutputSlots())
124 {
125 outputs.push_back(&outputSlot.GetTensorInfo());
126 }
127
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +0000128 TosaSerializationBasicBlock* basicBlock = GetTosaMapping(layer,
129 layer->GetType(),
Matthew Sloyan164bf4f2022-10-28 18:02:17 +0100130 inputs,
131 outputs,
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +0000132 layer->GetParameters());
Matthew Sloyan164bf4f2022-10-28 18:02:17 +0100133 return basicBlock;
134}