blob: a37eaf29b3fc7487136e7c00f5ba47f2e0ce9909 [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
11void SetBasicBlockConstantTensorData(Layer* layer, TosaSerializationBasicBlock* /*basicBlock*/)
12{
13 switch (layer->GetType())
14 {
15 case LayerType::Convolution2d:
16 {
17 // ToDo: using Convolution2d as an example as it has constant tensors for weights and bias.
18 // ToDo: manually set TosaOperator data of basicBlock where constant tensors exist.
19 }
20 default:
21 // If no switch statement for layer, no constant tensors exist in that layer, return
22 return;
23 }
24}
25
Cathal Corbettbd18eab2022-11-15 12:56:16 +000026TosaSerializationBasicBlock* CreateEmptyTosaSerializationBasicBlock()
27{
28 // empty basic block when no tosa mapping implemented/exists
29 TosaSerializationOperator* op =
30 new TosaSerializationOperator(Op_UNKNOWN, Attribute_NONE, nullptr, {}, {});
31 return new TosaSerializationBasicBlock("", {op}, {}, {}, {});
32}
33
Matthew Sloyan164bf4f2022-10-28 18:02:17 +010034TosaSerializationBasicBlock* GetTosaMapping(const LayerType type,
35 const std::vector<const TensorInfo*>& inputs,
36 const std::vector<const TensorInfo*>& outputs,
Cathal Corbettbd18eab2022-11-15 12:56:16 +000037 const BaseDescriptor& descriptor,
Matthew Sloyan5c54c382022-11-09 16:28:51 +000038 bool isMain = false)
Matthew Sloyan164bf4f2022-10-28 18:02:17 +010039{
40 switch (type)
41 {
42 case LayerType::Addition:
43 {
Matthew Sloyan5c54c382022-11-09 16:28:51 +000044 return ConvertAdditionToTosaOperator(inputs, outputs, isMain);
Matthew Sloyan164bf4f2022-10-28 18:02:17 +010045 }
Cathal Corbettbd18eab2022-11-15 12:56:16 +000046 case LayerType::Pooling2d:
47 {
48 auto poolDesc = PolymorphicDowncast<const Pooling2dDescriptor*>(&descriptor);
49
50 bool avgPoolIgnoreValue =
51 (poolDesc->m_PoolType == PoolingAlgorithm::Average) &&
52 (poolDesc->m_PaddingMethod == PaddingMethod::IgnoreValue);
53
54 if (poolDesc->m_PoolType == PoolingAlgorithm::L2)
55 {
56 return CreateEmptyTosaSerializationBasicBlock();
57 }
58 else if (avgPoolIgnoreValue)
59 {
60 return ConvertAvgPool2DIgnoreValueToTosaOperator(inputs, outputs, isMain, poolDesc);
61 }
62 else
63 {
64 return ConvertPooling2DToTosaOperator(inputs, outputs, isMain, poolDesc);
65 }
66 }
Matthew Sloyan164bf4f2022-10-28 18:02:17 +010067 default:
68 {
Cathal Corbettbd18eab2022-11-15 12:56:16 +000069 return CreateEmptyTosaSerializationBasicBlock();
Matthew Sloyan164bf4f2022-10-28 18:02:17 +010070 }
71 }
72}
73
Matthew Sloyan5c54c382022-11-09 16:28:51 +000074TosaSerializationBasicBlock* GetTosaMappingFromLayer(Layer* layer, bool isMain = false)
Matthew Sloyan164bf4f2022-10-28 18:02:17 +010075{
76 std::vector<const TensorInfo*> inputs;
77 for (auto inputSlot : layer->GetInputSlots())
78 {
79 inputs.push_back(&inputSlot.GetConnection()->GetTensorInfo());
80 }
81
82 std::vector<const TensorInfo*> outputs;
83 for (auto& outputSlot : layer->GetOutputSlots())
84 {
85 outputs.push_back(&outputSlot.GetTensorInfo());
86 }
87
88 TosaSerializationBasicBlock* basicBlock = GetTosaMapping(layer->GetType(),
89 inputs,
90 outputs,
Matthew Sloyan5c54c382022-11-09 16:28:51 +000091 layer->GetParameters(),
92 isMain);
Matthew Sloyan164bf4f2022-10-28 18:02:17 +010093 SetBasicBlockConstantTensorData(layer, basicBlock);
94 return basicBlock;
95}