diff options
author | Cathal Corbett <cathal.corbett@arm.com> | 2022-11-15 12:56:16 +0000 |
---|---|---|
committer | Cathal Corbett <cathal.corbett@arm.com> | 2022-11-16 11:12:24 +0000 |
commit | bd18eab07a8f30492de1e462b1815189014cb8d5 (patch) | |
tree | 7c26e2ecb94ffa1ea67204e02011a8166931eb09 /src/backends/tosaCommon/operatorMappings | |
parent | 194086f01bdd1af52ddd976eb5b4152255565127 (diff) | |
download | armnn-bd18eab07a8f30492de1e462b1815189014cb8d5.tar.gz |
IVGCVSW-7345 Add Pooling2d support to TOSA Reference Backend
Signed-off-by: Cathal Corbett <cathal.corbett@arm.com>
Change-Id: I73a47e513fe2d064ef233b121a68ef2edf0396dc
Diffstat (limited to 'src/backends/tosaCommon/operatorMappings')
7 files changed, 366 insertions, 0 deletions
diff --git a/src/backends/tosaCommon/operatorMappings/AvgPool2DIgnoreValueOperator.cpp b/src/backends/tosaCommon/operatorMappings/AvgPool2DIgnoreValueOperator.cpp new file mode 100644 index 0000000000..b3d2687c30 --- /dev/null +++ b/src/backends/tosaCommon/operatorMappings/AvgPool2DIgnoreValueOperator.cpp @@ -0,0 +1,107 @@ +// +// Copyright © 2022 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#include "Pooling2DOperator.hpp" + +TosaSerializationBasicBlock* ConvertAvgPool2DIgnoreValueToTosaOperator(const std::vector<const TensorInfo*>& inputs, + const std::vector<const TensorInfo*>& outputs, + bool isMain, + const Pooling2dDescriptor* poolDescriptor) +{ + + // A helper function with static global variables ensures uniqueness + // for dynamically generating input, output and block names + std::string padInputName = std::string("Op_PAD_input0_") + GetUniqueTosaMappingID(); + std::string padOutputName = std::string("Op_PAD_intermediate0_") + GetUniqueTosaMappingID(); + std::string poolOutputName = std::string("Op_AVG_POOL2D_output0_") + GetUniqueTosaMappingID(); + std::string blockName = std::string("Op_AVG_POOL2D_block_") + GetUniqueTosaMappingID(); + + // If it's the first block, overwrite block name with main. + if (isMain) + { + blockName = std::string("main"); + } + + std::vector<int> paddings; + if (poolDescriptor->m_DataLayout == DataLayout::NHWC) + { + paddings = {0, + 0, + static_cast<int>(poolDescriptor->m_PadTop), + static_cast<int>(poolDescriptor->m_PadBottom), + static_cast<int>(poolDescriptor->m_PadLeft), + static_cast<int>(poolDescriptor->m_PadRight), + 0, + 0 + }; + } + else + { + paddings = {0, + 0, + 0, + 0, + static_cast<int>(poolDescriptor->m_PadTop), + static_cast<int>(poolDescriptor->m_PadBottom), + static_cast<int>(poolDescriptor->m_PadLeft), + static_cast<int>(poolDescriptor->m_PadRight) + }; + } + + TosaPadAttribute padAttribute(paddings, 0, 0.0f); + TosaSerializationOperator* opPad = new TosaSerializationOperator(Op_PAD, + Attribute_PadAttribute, + &padAttribute, + {padInputName}, + {padOutputName}); + + std::vector<int> pad = {0, 0, 0, 0}; + std::vector<int> kernel = {static_cast<int>(poolDescriptor->m_PoolHeight), + static_cast<int>(poolDescriptor->m_PoolWidth)}; + std::vector<int> stride = {static_cast<int>(poolDescriptor->m_StrideY), + static_cast<int>(poolDescriptor->m_StrideX)}; + TosaPoolAttribute poolAttribute(pad, kernel, stride, 0, 0, ArmNNToDType(inputs[0]->GetDataType())); + + TosaSerializationOperator* opPool = new TosaSerializationOperator(Op_AVG_POOL2D, + Attribute_PoolAttribute, + &poolAttribute, + {padOutputName}, + {poolOutputName}); + + std::vector<int32_t> inputShape = GetTosaTensorShape(inputs[0]->GetShape()); + DType inputDType = ArmNNToDType(inputs[0]->GetDataType()); + + std::vector<int32_t> outputShape = GetTosaTensorShape(outputs[0]->GetShape()); + DType outputDType = ArmNNToDType(outputs[0]->GetDataType()); + + std::vector<int32_t> intermediateShape; + if (poolDescriptor->m_DataLayout == DataLayout::NHWC) + { + intermediateShape = {inputShape[0], + inputShape[1] + paddings[2] + paddings[3], + inputShape[2] + paddings[4] + paddings[5], + inputShape[3]}; + } + else + { + intermediateShape = {inputShape[0], + inputShape[1], + inputShape[2] + paddings[4] + paddings[5], + inputShape[3] + paddings[6] + paddings[7]}; + } + + TosaSerializationTensor* inputTensor = new TosaSerializationTensor(padInputName, inputShape, inputDType, {}); + TosaSerializationTensor* intermediateTensor = new TosaSerializationTensor( + padOutputName, intermediateShape, inputDType, {}); + TosaSerializationTensor* outputTensor = new TosaSerializationTensor(poolOutputName, outputShape, outputDType, {}); + + // operatorInputNames/operatorOutputNames ends up being the same as + // blockInputNames/blockOutputNames for one-to-one ArmNN to Tosa mappings + return new TosaSerializationBasicBlock(blockName, // name + {opPad, opPool}, // operators + {inputTensor, intermediateTensor, outputTensor}, // tensors + {padInputName}, // inputs + {poolOutputName}); // outputs +}
\ No newline at end of file diff --git a/src/backends/tosaCommon/operatorMappings/AvgPool2DIgnoreValueOperator.hpp b/src/backends/tosaCommon/operatorMappings/AvgPool2DIgnoreValueOperator.hpp new file mode 100644 index 0000000000..63ae190cc9 --- /dev/null +++ b/src/backends/tosaCommon/operatorMappings/AvgPool2DIgnoreValueOperator.hpp @@ -0,0 +1,19 @@ +// +// Copyright © 2022 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#pragma once + +#include <Layer.hpp> + +#include <tosa_serialization_handler.h> +#include "TosaOperatorUtils.hpp" + +using namespace armnn; +using namespace tosa; + +TosaSerializationBasicBlock* ConvertAvgPool2DIgnoreValueToTosaOperator(const std::vector<const TensorInfo*>& inputs, + const std::vector<const TensorInfo*>& outputs, + bool isMain, + const Pooling2dDescriptor* poolDescriptor); diff --git a/src/backends/tosaCommon/operatorMappings/CMakeLists.txt b/src/backends/tosaCommon/operatorMappings/CMakeLists.txt index 9fc33e9205..7fdc9226af 100644 --- a/src/backends/tosaCommon/operatorMappings/CMakeLists.txt +++ b/src/backends/tosaCommon/operatorMappings/CMakeLists.txt @@ -6,6 +6,10 @@ list(APPEND armnnTosaBackendOperators_sources AdditionOperator.hpp AdditionOperator.cpp + AvgPool2DIgnoreValueOperator.hpp + AvgPool2DIgnoreValueOperator.cpp + Pooling2DOperator.hpp + Pooling2DOperator.cpp TosaOperatorUtils.hpp ) diff --git a/src/backends/tosaCommon/operatorMappings/Pooling2DOperator.cpp b/src/backends/tosaCommon/operatorMappings/Pooling2DOperator.cpp new file mode 100644 index 0000000000..cd707edb3a --- /dev/null +++ b/src/backends/tosaCommon/operatorMappings/Pooling2DOperator.cpp @@ -0,0 +1,60 @@ +// +// Copyright © 2022 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#include "Pooling2DOperator.hpp" + +TosaSerializationBasicBlock* ConvertPooling2DToTosaOperator(const std::vector<const TensorInfo*>& inputs, + const std::vector<const TensorInfo*>& outputs, + bool isMain, + const Pooling2dDescriptor* poolDescriptor) +{ + std::string poolType = (poolDescriptor->m_PoolType == PoolingAlgorithm::Max) ? "Op_MAX" : "Op_AVG"; + Op opcode = (poolDescriptor->m_PoolType == PoolingAlgorithm::Max) ? Op_MAX_POOL2D : Op_AVG_POOL2D; + + // A helper function with static global variables ensures uniqueness + // for dynamically generating input, output and block names + std::string input0Name = poolType + std::string("_POOL2D_input0_") + GetUniqueTosaMappingID(); + std::string outputName = poolType + std::string("_POOL2D_output0_") + GetUniqueTosaMappingID(); + std::string blockName = poolType + std::string("_POOL2D_block_") + GetUniqueTosaMappingID(); + + // If it's the first block, overwrite block name with main. + if (isMain) + { + blockName = std::string("main"); + } + + std::vector<int> pad = {static_cast<int>(poolDescriptor->m_PadTop), + static_cast<int>(poolDescriptor->m_PadBottom), + static_cast<int>(poolDescriptor->m_PadLeft), + static_cast<int>(poolDescriptor->m_PadRight)}; + std::vector<int> kernel = {static_cast<int>(poolDescriptor->m_PoolHeight), + static_cast<int>(poolDescriptor->m_PoolWidth)}; + std::vector<int> stride = {static_cast<int>(poolDescriptor->m_StrideY), + static_cast<int>(poolDescriptor->m_StrideX)}; + TosaPoolAttribute attribute(pad, kernel, stride, 0, 0, ArmNNToDType(inputs[0]->GetDataType())); + + TosaSerializationOperator* op = new TosaSerializationOperator(opcode, + Attribute_PoolAttribute, + &attribute, + {input0Name}, + {outputName}); + + std::vector<int32_t> inputShape0 = GetTosaTensorShape(inputs[0]->GetShape()); + DType inputDType0 = ArmNNToDType(inputs[0]->GetDataType()); + + std::vector<int32_t> outputShape0 = GetTosaTensorShape(outputs[0]->GetShape()); + DType outputDType0 = ArmNNToDType(outputs[0]->GetDataType()); + + TosaSerializationTensor* inputTensor0 = new TosaSerializationTensor(input0Name, inputShape0, inputDType0, {}); + TosaSerializationTensor* outputTensor0 = new TosaSerializationTensor(outputName, outputShape0, outputDType0, {}); + + // operatorInputNames/operatorOutputNames ends up being the same as + // blockInputNames/blockOutputNames for one-to-one ArmNN to Tosa mappings + return new TosaSerializationBasicBlock(blockName, // name + {op}, // operators + {inputTensor0, outputTensor0}, // tensors + {input0Name}, // inputs + {outputName}); // outputs +}
\ No newline at end of file diff --git a/src/backends/tosaCommon/operatorMappings/Pooling2DOperator.hpp b/src/backends/tosaCommon/operatorMappings/Pooling2DOperator.hpp new file mode 100644 index 0000000000..22d2a3ae29 --- /dev/null +++ b/src/backends/tosaCommon/operatorMappings/Pooling2DOperator.hpp @@ -0,0 +1,19 @@ +// +// Copyright © 2022 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#pragma once + +#include <Layer.hpp> + +#include <tosa_serialization_handler.h> +#include "TosaOperatorUtils.hpp" + +using namespace armnn; +using namespace tosa; + +TosaSerializationBasicBlock* ConvertPooling2DToTosaOperator(const std::vector<const TensorInfo*>& inputs, + const std::vector<const TensorInfo*>& outputs, + bool isMain, + const Pooling2dDescriptor* poolDescriptor); diff --git a/src/backends/tosaCommon/operatorMappings/TosaCommonOperators.hpp b/src/backends/tosaCommon/operatorMappings/TosaCommonOperators.hpp new file mode 100644 index 0000000000..00b5f0fa68 --- /dev/null +++ b/src/backends/tosaCommon/operatorMappings/TosaCommonOperators.hpp @@ -0,0 +1,10 @@ +// +// Copyright © 2022 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#pragma once + +#include "AdditionOperator.hpp" +#include "AvgPool2DIgnoreValueOperator.hpp" +#include "Pooling2DOperator.hpp"
\ No newline at end of file diff --git a/src/backends/tosaCommon/operatorMappings/TosaOperatorUtils.hpp b/src/backends/tosaCommon/operatorMappings/TosaOperatorUtils.hpp index f580a53ebc..f51b2109b4 100644 --- a/src/backends/tosaCommon/operatorMappings/TosaOperatorUtils.hpp +++ b/src/backends/tosaCommon/operatorMappings/TosaOperatorUtils.hpp @@ -59,3 +59,150 @@ inline std::string GetUniqueTosaMappingID() { return std::to_string(++uniqueTosaMappingID); } + +// Function to return Tosa Op as string. +inline std::string TosaOpToString(Op tosaOp) +{ + switch (tosaOp) + { + case Op_ADD: + return "Op_ADD"; + case Op_AVG_POOL2D: + return "Op_AVG_POOL2D"; + case Op_MAX_POOL2D: + return "Op_MAX_POOL2D"; + case Op_PAD: + return "Op_PAD"; + case Op_UNKNOWN: + return "Op_UNKNOWN"; + case Op_ARGMAX: + return "Op_ARGMAX"; + case Op_CONV2D: + return "Op_CONV2D"; + case Op_CONV3D: + return "Op_CONV3D"; + case Op_DEPTHWISE_CONV2D: + return "Op_DEPTHWISE_CONV2D"; + case Op_FULLY_CONNECTED: + return "Op_FULLY_CONNECTED"; + case Op_MATMUL: + return "Op_MATMUL"; + case Op_TRANSPOSE_CONV2D: + return "Op_TRANSPOSE_CONV2D"; + case Op_CLAMP: + return "Op_CLAMP"; + case Op_RESERVED: + return "Op_RESERVED"; + case Op_SIGMOID: + return "Op_SIGMOID"; + case Op_TANH: + return "Op_TANH"; + case Op_ARITHMETIC_RIGHT_SHIFT: + return "Op_ARITHMETIC_RIGHT_SHIFT"; + case Op_BITWISE_AND: + return "Op_BITWISE_AND"; + case Op_BITWISE_OR: + return "Op_BITWISE_OR"; + case Op_BITWISE_XOR: + return "Op_BITWISE_XOR"; + case Op_INTDIV: + return "Op_INTDIV"; + case Op_LOGICAL_AND: + return "Op_LOGICAL_AND"; + case Op_LOGICAL_LEFT_SHIFT: + return "Op_LOGICAL_LEFT_SHIFT"; + case Op_LOGICAL_RIGHT_SHIFT: + return "Op_LOGICAL_RIGHT_SHIFT"; + case Op_LOGICAL_OR: + return "Op_LOGICAL_OR"; + case Op_LOGICAL_XOR: + return "Op_LOGICAL_XOR"; + case Op_MAXIMUM: + return "Op_MAXIMUM"; + case Op_MINIMUM: + return "Op_MINIMUM"; + case Op_MUL: + return "Op_MUL"; + case Op_POW: + return "Op_POW"; + case Op_SUB: + return "Op_SUB"; + case Op_TABLE: + return "Op_TABLE"; + case Op_ABS: + return "Op_ABS"; + case Op_BITWISE_NOT: + return "Op_BITWISE_NOT"; + case Op_CEIL: + return "Op_CEIL"; + case Op_CLZ: + return "Op_CLZ"; + case Op_EXP: + return "Op_EXP"; + case Op_FLOOR: + return "Op_FLOOR"; + case Op_LOG: + return "Op_LOG"; + case Op_LOGICAL_NOT: + return "Op_LOGICAL_NOT"; + case Op_NEGATE: + return "Op_NEGATE"; + case Op_RECIPROCAL: + return "Op_RECIPROCAL"; + case Op_RSQRT: + return "Op_RSQRT"; + case Op_SELECT: + return "Op_SELECT"; + case Op_EQUAL: + return "Op_EQUAL"; + case Op_GREATER: + return "Op_GREATER"; + case Op_GREATER_EQUAL: + return "Op_GREATER_EQUAL"; + case Op_REDUCE_ANY: + return "Op_REDUCE_ANY"; + case Op_REDUCE_ALL: + return "Op_REDUCE_ALL"; + case Op_REDUCE_MAX: + return "Op_REDUCE_MAX"; + case Op_REDUCE_MIN: + return "Op_REDUCE_MIN"; + case Op_REDUCE_PRODUCT: + return "Op_REDUCE_PRODUCT"; + case Op_REDUCE_SUM: + return "Op_REDUCE_SUM"; + case Op_CONCAT: + return "Op_CONCAT"; + case Op_RESHAPE: + return "Op_RESHAPE"; + case Op_REVERSE: + return "Op_REVERSE"; + case Op_SLICE: + return "Op_SLICE"; + case Op_TILE: + return "Op_TILE"; + case Op_TRANSPOSE: + return "Op_TRANSPOSE"; + case Op_GATHER: + return "Op_GATHER"; + case Op_SCATTER: + return "Op_SCATTER"; + case Op_RESIZE: + return "Op_RESIZE"; + case Op_CAST: + return "Op_CAST"; + case Op_RESCALE: + return "Op_RESCALE"; + case Op_CONST: + return "Op_CONST"; + case Op_IDENTITY: + return "Op_IDENTITY"; + case Op_CUSTOM: + return "Op_CUSTOM"; + case Op_COND_IF: + return "Op_COND_IF"; + case Op_WHILE_LOOP: + return "Op_WHILE_LOOP"; + } + return ""; +} |