diff options
Diffstat (limited to 'src/backends/tosaCommon/operatorMappings')
3 files changed, 126 insertions, 0 deletions
diff --git a/src/backends/tosaCommon/operatorMappings/AdditionOperator.hpp b/src/backends/tosaCommon/operatorMappings/AdditionOperator.hpp new file mode 100644 index 0000000000..98c01e2cb8 --- /dev/null +++ b/src/backends/tosaCommon/operatorMappings/AdditionOperator.hpp @@ -0,0 +1,52 @@ +// +// 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* ConvertAdditionToTosaOperator(const std::vector<const TensorInfo*>& inputs, + const std::vector<const TensorInfo*>& outputs) +{ + // A helper function with static global variables ensures uniqueness + // for dynamically generating input, output and block names + std::string input0Name = std::string("Op_ADD_input0_") + GetUniqueTosaMappingID(); + std::string input1Name = std::string("Op_ADD_input1_") + GetUniqueTosaMappingID(); + std::string outputName = std::string("Op_ADD_output0_") + GetUniqueTosaMappingID(); + std::string blockName = std::string("Op_ADD_block_") + GetUniqueTosaMappingID(); + + TosaSerializationOperator* op = new TosaSerializationOperator(Op_ADD, + Attribute_NONE, + nullptr, + {input0Name, input1Name}, + {outputName}); + + std::vector<int32_t> inputShape0 = GetTosaTensorShape(inputs[0]->GetShape()); + DType inputDType0 = ArmNNToDType(inputs[0]->GetDataType()); + + std::vector<int32_t> inputShape1 = GetTosaTensorShape(inputs[1]->GetShape()); + DType inputDType1 = ArmNNToDType(inputs[1]->GetDataType()); + + std::vector<int32_t> outputShape0 = GetTosaTensorShape(outputs[0]->GetShape()); + DType outputDType0 = ArmNNToDType(outputs[0]->GetDataType()); + + TosaSerializationTensor* inputTensor0 = new TosaSerializationTensor(input0Name, inputShape0, inputDType0, {}); + TosaSerializationTensor* inputTensor1 = new TosaSerializationTensor(input1Name, inputShape1, inputDType1, {}); + 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, inputTensor1, outputTensor0}, // tensors + {input0Name, input1Name}, // inputs + {outputName}); // outputs +} diff --git a/src/backends/tosaCommon/operatorMappings/CMakeLists.txt b/src/backends/tosaCommon/operatorMappings/CMakeLists.txt new file mode 100644 index 0000000000..3965a6ab04 --- /dev/null +++ b/src/backends/tosaCommon/operatorMappings/CMakeLists.txt @@ -0,0 +1,14 @@ +# +# Copyright © 2022 Arm Ltd and Contributors. All rights reserved. +# SPDX-License-Identifier: MIT +# + +list(APPEND armnnTosaBackendOperators_sources + AdditionOperator.hpp + TosaOperatorUtils.hpp + ) + +add_library(armnnTosaBackendOperators OBJECT ${armnnTosaBackendOperators_sources}) +target_include_directories(armnnTosaBackendOperators PRIVATE ${PROJECT_SOURCE_DIR}/src/armnn) +target_include_directories(armnnTosaBackendOperators PRIVATE ${PROJECT_SOURCE_DIR}/src/armnnUtils) +target_include_directories(armnnTosaBackendOperators PRIVATE ${PROJECT_SOURCE_DIR}/src/backends) diff --git a/src/backends/tosaCommon/operatorMappings/TosaOperatorUtils.hpp b/src/backends/tosaCommon/operatorMappings/TosaOperatorUtils.hpp new file mode 100644 index 0000000000..b887721648 --- /dev/null +++ b/src/backends/tosaCommon/operatorMappings/TosaOperatorUtils.hpp @@ -0,0 +1,60 @@ +// +// Copyright © 2022 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#pragma once + +#include <armnn/Tensor.hpp> +#include <armnn/Types.hpp> + +#include <tosa_generated.h> + +using namespace armnn; +using namespace tosa; + +// Function to return Tosa datatype from input ArmNN datatype. +DType ArmNNToDType(const DataType& type) +{ + switch (type) + { + case DataType::Float16: + case DataType::Float32: + case DataType::BFloat16: + return DType_FLOAT; + case DataType::QAsymmU8: + return DType_UINT8; + case DataType::QSymmS8: + case DataType::QAsymmS8: + return DType_INT8; + case DataType::QSymmS16: + return DType_INT16; + case DataType::Signed32: + return DType_INT32; + case DataType::Signed64: + // No signed 64, only DType_INT48. + return DType_UNKNOWN; + case DataType::Boolean: + return DType_BOOL; + default: + return DType_UNKNOWN; + } +} + +// Function to return Tosa tensor shape from input ArmNN tensor shape. +std::vector<int32_t> GetTosaTensorShape(const TensorShape& shape) +{ + std::vector<int32_t> returnShape; + for (u_int32_t i = 0; i < shape.GetNumDimensions(); i++) + { + returnShape.push_back(static_cast<int32_t>(shape[i])); + } + return returnShape; +} + +// Function to return unique int as a string to ensure uniqueness between all input, output and block names. +static int uniqueTosaMappingID = 0; +std::string GetUniqueTosaMappingID() +{ + return std::to_string(++uniqueTosaMappingID); +} |