Matthew Sloyan | 164bf4f | 2022-10-28 18:02:17 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2022 Arm Ltd and Contributors. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "TosaMappings.hpp" |
| 7 | |
| 8 | using namespace armnn; |
| 9 | using namespace tosa; |
| 10 | |
| 11 | void 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 Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 26 | TosaSerializationBasicBlock* 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 Sloyan | 164bf4f | 2022-10-28 18:02:17 +0100 | [diff] [blame] | 34 | TosaSerializationBasicBlock* GetTosaMapping(const LayerType type, |
| 35 | const std::vector<const TensorInfo*>& inputs, |
| 36 | const std::vector<const TensorInfo*>& outputs, |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 37 | const BaseDescriptor& descriptor, |
Matthew Sloyan | 5c54c38 | 2022-11-09 16:28:51 +0000 | [diff] [blame] | 38 | bool isMain = false) |
Matthew Sloyan | 164bf4f | 2022-10-28 18:02:17 +0100 | [diff] [blame] | 39 | { |
| 40 | switch (type) |
| 41 | { |
| 42 | case LayerType::Addition: |
| 43 | { |
Matthew Sloyan | 5c54c38 | 2022-11-09 16:28:51 +0000 | [diff] [blame] | 44 | return ConvertAdditionToTosaOperator(inputs, outputs, isMain); |
Matthew Sloyan | 164bf4f | 2022-10-28 18:02:17 +0100 | [diff] [blame] | 45 | } |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 46 | 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 Sloyan | 164bf4f | 2022-10-28 18:02:17 +0100 | [diff] [blame] | 67 | default: |
| 68 | { |
Cathal Corbett | bd18eab | 2022-11-15 12:56:16 +0000 | [diff] [blame] | 69 | return CreateEmptyTosaSerializationBasicBlock(); |
Matthew Sloyan | 164bf4f | 2022-10-28 18:02:17 +0100 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
Matthew Sloyan | 5c54c38 | 2022-11-09 16:28:51 +0000 | [diff] [blame] | 74 | TosaSerializationBasicBlock* GetTosaMappingFromLayer(Layer* layer, bool isMain = false) |
Matthew Sloyan | 164bf4f | 2022-10-28 18:02:17 +0100 | [diff] [blame] | 75 | { |
| 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 Sloyan | 5c54c38 | 2022-11-09 16:28:51 +0000 | [diff] [blame] | 91 | layer->GetParameters(), |
| 92 | isMain); |
Matthew Sloyan | 164bf4f | 2022-10-28 18:02:17 +0100 | [diff] [blame] | 93 | SetBasicBlockConstantTensorData(layer, basicBlock); |
| 94 | return basicBlock; |
| 95 | } |