blob: 7ecf7266acb3e63540c9b00e6ea8514fb6f83f16 [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 }
30 case LayerType::Constant:
31 {
32 return ConvertConstantToTosaOperator(layer, outputs);
33 }
34 case LayerType::Convolution2d:
35 {
36 auto conv2dDesc = PolymorphicDowncast<const Convolution2dDescriptor*>(&descriptor);
37 return ConvertConv2dToTosaOperator(layer, inputs, outputs, conv2dDesc);
Matthew Sloyan164bf4f2022-10-28 18:02:17 +010038 }
Cathal Corbettbd18eab2022-11-15 12:56:16 +000039 case LayerType::Pooling2d:
40 {
41 auto poolDesc = PolymorphicDowncast<const Pooling2dDescriptor*>(&descriptor);
42
43 bool avgPoolIgnoreValue =
44 (poolDesc->m_PoolType == PoolingAlgorithm::Average) &&
45 (poolDesc->m_PaddingMethod == PaddingMethod::IgnoreValue);
46
47 if (poolDesc->m_PoolType == PoolingAlgorithm::L2)
48 {
49 return CreateEmptyTosaSerializationBasicBlock();
50 }
51 else if (avgPoolIgnoreValue)
52 {
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000053 return ConvertAvgPool2DIgnoreValueToTosaOperator(layer, inputs, outputs, poolDesc);
Cathal Corbettbd18eab2022-11-15 12:56:16 +000054 }
55 else
56 {
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000057 return ConvertPooling2DToTosaOperator(layer, inputs, outputs, poolDesc);
Cathal Corbettbd18eab2022-11-15 12:56:16 +000058 }
59 }
Cathal Corbettb30e6552022-12-07 11:50:50 +000060 case LayerType::Reshape:
61 {
62 auto reshapeDesc = PolymorphicDowncast<const ReshapeDescriptor*>(&descriptor);
63 return ConvertReshapeToTosaOperator(layer, inputs, outputs, reshapeDesc);
64 }
Cathal Corbett3b9acd52022-12-09 12:17:27 +000065 case LayerType::Slice:
66 {
67 auto sliceDesc = PolymorphicDowncast<const SliceDescriptor*>(&descriptor);
68 return ConvertSliceToTosaOperator(layer, inputs, outputs, sliceDesc);
69 }
Matthew Sloyanfc9d5e72022-12-08 13:38:23 +000070 case LayerType::TransposeConvolution2d:
71 {
72 auto transposeConv2dDesc = PolymorphicDowncast<const TransposeConvolution2dDescriptor*>(&descriptor);
73 return ConvertTransposeConv2dToTosaOperator(layer, inputs, outputs, transposeConv2dDesc);
74 }
Matthew Sloyan164bf4f2022-10-28 18:02:17 +010075 default:
76 {
Cathal Corbettbd18eab2022-11-15 12:56:16 +000077 return CreateEmptyTosaSerializationBasicBlock();
Matthew Sloyan164bf4f2022-10-28 18:02:17 +010078 }
79 }
80}
81
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000082TosaSerializationBasicBlock* GetTosaMappingFromLayer(Layer* layer)
Matthew Sloyan164bf4f2022-10-28 18:02:17 +010083{
84 std::vector<const TensorInfo*> inputs;
85 for (auto inputSlot : layer->GetInputSlots())
86 {
87 inputs.push_back(&inputSlot.GetConnection()->GetTensorInfo());
88 }
89
90 std::vector<const TensorInfo*> outputs;
91 for (auto& outputSlot : layer->GetOutputSlots())
92 {
93 outputs.push_back(&outputSlot.GetTensorInfo());
94 }
95
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000096 TosaSerializationBasicBlock* basicBlock = GetTosaMapping(layer,
97 layer->GetType(),
Matthew Sloyan164bf4f2022-10-28 18:02:17 +010098 inputs,
99 outputs,
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +0000100 layer->GetParameters());
Matthew Sloyan164bf4f2022-10-28 18:02:17 +0100101 return basicBlock;
102}