blob: 1452e4aefdf94699fd128286e2f8d2feb5b305e1 [file] [log] [blame]
Matthew Sloyan164bf4f2022-10-28 18:02:17 +01001//
2// Copyright © 2022 Arm Ltd and Contributors. All rights reserved.
3// 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, {}, {});
Cathal Corbettbd18eab2022-11-15 12:56:16 +000015 return new TosaSerializationBasicBlock("", {op}, {}, {}, {});
16}
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:
27 {
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000028 return ConvertAdditionToTosaOperator(layer, inputs, outputs);
29 }
Kevin May5b58e312022-12-15 10:15:21 +000030 case LayerType::Concat:
31 {
32 auto concatDesc = PolymorphicDowncast<const OriginsDescriptor*>(&descriptor);
33 return ConvertConcatToTosaOperator(layer, inputs, outputs, concatDesc);
34 }
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000035 case LayerType::Constant:
36 {
37 return ConvertConstantToTosaOperator(layer, outputs);
38 }
39 case LayerType::Convolution2d:
40 {
41 auto conv2dDesc = PolymorphicDowncast<const Convolution2dDescriptor*>(&descriptor);
42 return ConvertConv2dToTosaOperator(layer, inputs, outputs, conv2dDesc);
Matthew Sloyan164bf4f2022-10-28 18:02:17 +010043 }
Cathal Corbettbd18eab2022-11-15 12:56:16 +000044 case LayerType::Pooling2d:
45 {
46 auto poolDesc = PolymorphicDowncast<const Pooling2dDescriptor*>(&descriptor);
47
48 bool avgPoolIgnoreValue =
49 (poolDesc->m_PoolType == PoolingAlgorithm::Average) &&
50 (poolDesc->m_PaddingMethod == PaddingMethod::IgnoreValue);
51
52 if (poolDesc->m_PoolType == PoolingAlgorithm::L2)
53 {
54 return CreateEmptyTosaSerializationBasicBlock();
55 }
56 else if (avgPoolIgnoreValue)
57 {
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000058 return ConvertAvgPool2DIgnoreValueToTosaOperator(layer, inputs, outputs, poolDesc);
Cathal Corbettbd18eab2022-11-15 12:56:16 +000059 }
60 else
61 {
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000062 return ConvertPooling2DToTosaOperator(layer, inputs, outputs, poolDesc);
Cathal Corbettbd18eab2022-11-15 12:56:16 +000063 }
64 }
Cathal Corbettb30e6552022-12-07 11:50:50 +000065 case LayerType::Reshape:
66 {
67 auto reshapeDesc = PolymorphicDowncast<const ReshapeDescriptor*>(&descriptor);
68 return ConvertReshapeToTosaOperator(layer, inputs, outputs, reshapeDesc);
69 }
Cathal Corbett3b9acd52022-12-09 12:17:27 +000070 case LayerType::Slice:
71 {
72 auto sliceDesc = PolymorphicDowncast<const SliceDescriptor*>(&descriptor);
73 return ConvertSliceToTosaOperator(layer, inputs, outputs, sliceDesc);
74 }
Matthew Sloyanfc9d5e72022-12-08 13:38:23 +000075 case LayerType::TransposeConvolution2d:
76 {
77 auto transposeConv2dDesc = PolymorphicDowncast<const TransposeConvolution2dDescriptor*>(&descriptor);
78 return ConvertTransposeConv2dToTosaOperator(layer, inputs, outputs, transposeConv2dDesc);
79 }
Matthew Sloyan164bf4f2022-10-28 18:02:17 +010080 default:
81 {
Cathal Corbettbd18eab2022-11-15 12:56:16 +000082 return CreateEmptyTosaSerializationBasicBlock();
Matthew Sloyan164bf4f2022-10-28 18:02:17 +010083 }
84 }
85}
86
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000087TosaSerializationBasicBlock* GetTosaMappingFromLayer(Layer* layer)
Matthew Sloyan164bf4f2022-10-28 18:02:17 +010088{
89 std::vector<const TensorInfo*> inputs;
90 for (auto inputSlot : layer->GetInputSlots())
91 {
92 inputs.push_back(&inputSlot.GetConnection()->GetTensorInfo());
93 }
94
95 std::vector<const TensorInfo*> outputs;
96 for (auto& outputSlot : layer->GetOutputSlots())
97 {
98 outputs.push_back(&outputSlot.GetTensorInfo());
99 }
100
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +0000101 TosaSerializationBasicBlock* basicBlock = GetTosaMapping(layer,
102 layer->GetType(),
Matthew Sloyan164bf4f2022-10-28 18:02:17 +0100103 inputs,
104 outputs,
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +0000105 layer->GetParameters());
Matthew Sloyan164bf4f2022-10-28 18:02:17 +0100106 return basicBlock;
107}