blob: 15629ffab0feec54686d1771d9b7926cd4e66793 [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 Sloyan164bf4f2022-10-28 18:02:17 +010070 default:
71 {
Cathal Corbettbd18eab2022-11-15 12:56:16 +000072 return CreateEmptyTosaSerializationBasicBlock();
Matthew Sloyan164bf4f2022-10-28 18:02:17 +010073 }
74 }
75}
76
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000077TosaSerializationBasicBlock* GetTosaMappingFromLayer(Layer* layer)
Matthew Sloyan164bf4f2022-10-28 18:02:17 +010078{
79 std::vector<const TensorInfo*> inputs;
80 for (auto inputSlot : layer->GetInputSlots())
81 {
82 inputs.push_back(&inputSlot.GetConnection()->GetTensorInfo());
83 }
84
85 std::vector<const TensorInfo*> outputs;
86 for (auto& outputSlot : layer->GetOutputSlots())
87 {
88 outputs.push_back(&outputSlot.GetTensorInfo());
89 }
90
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000091 TosaSerializationBasicBlock* basicBlock = GetTosaMapping(layer,
92 layer->GetType(),
Matthew Sloyan164bf4f2022-10-28 18:02:17 +010093 inputs,
94 outputs,
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000095 layer->GetParameters());
Matthew Sloyan164bf4f2022-10-28 18:02:17 +010096 return basicBlock;
97}