diff options
author | Sadik Armagan <sadik.armagan@arm.com> | 2021-01-22 14:25:31 +0000 |
---|---|---|
committer | Sadik Armagan <sadik.armagan@arm.com> | 2021-01-22 14:25:31 +0000 |
commit | 2686849118217ba692439407adb53ec3849d48ac (patch) | |
tree | d9f81368b4b61321b6065415837624a93b91a72a /src/armnnTfLiteParser/TfLiteParser.cpp | |
parent | 2cd184763ff7f8767e751f2fe0c461714350aae6 (diff) | |
download | armnn-2686849118217ba692439407adb53ec3849d48ac.tar.gz |
MLCE-335 'DEPTH_TO_SPACE and GATHER operator support in TfLiteParser'
* Added GATHER operator support to TfLiteParser
* Added DEPTH_TO_SPACE operator support to TfLiteParser
Signed-off-by: Sadik Armagan <sadik.armagan@arm.com>
Change-Id: Id5d3b54e2d850eb9f19417029efbeb73a3029e69
Diffstat (limited to 'src/armnnTfLiteParser/TfLiteParser.cpp')
-rw-r--r-- | src/armnnTfLiteParser/TfLiteParser.cpp | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/src/armnnTfLiteParser/TfLiteParser.cpp b/src/armnnTfLiteParser/TfLiteParser.cpp index a18471a71e..1a1e854395 100644 --- a/src/armnnTfLiteParser/TfLiteParser.cpp +++ b/src/armnnTfLiteParser/TfLiteParser.cpp @@ -547,11 +547,13 @@ TfLiteParser::TfLiteParser(const Optional<ITfLiteParser::TfLiteParserOptions>& o m_ParserFunctions[tflite::BuiltinOperator_CONCATENATION] = &TfLiteParser::ParseConcatenation; m_ParserFunctions[tflite::BuiltinOperator_CONV_2D] = &TfLiteParser::ParseConv2D; m_ParserFunctions[tflite::BuiltinOperator_CUSTOM] = &TfLiteParser::ParseCustomOperator; + m_ParserFunctions[tflite::BuiltinOperator_DEPTH_TO_SPACE] = &TfLiteParser::ParseDepthToSpace; m_ParserFunctions[tflite::BuiltinOperator_DEPTHWISE_CONV_2D] = &TfLiteParser::ParseDepthwiseConv2D; m_ParserFunctions[tflite::BuiltinOperator_DEQUANTIZE] = &TfLiteParser::ParseDequantize; m_ParserFunctions[tflite::BuiltinOperator_ELU] = &TfLiteParser::ParseElu; m_ParserFunctions[tflite::BuiltinOperator_EXP] = &TfLiteParser::ParseExp; m_ParserFunctions[tflite::BuiltinOperator_FULLY_CONNECTED] = &TfLiteParser::ParseFullyConnected; + m_ParserFunctions[tflite::BuiltinOperator_GATHER] = &TfLiteParser::ParseGather; m_ParserFunctions[tflite::BuiltinOperator_HARD_SWISH] = &TfLiteParser::ParseHardSwish; m_ParserFunctions[tflite::BuiltinOperator_LEAKY_RELU] = &TfLiteParser::ParseLeakyRelu; m_ParserFunctions[tflite::BuiltinOperator_LOGISTIC] = &TfLiteParser::ParseLogistic; @@ -2903,6 +2905,95 @@ void TfLiteParser::ParseArgMax(size_t subgraphIndex, size_t operatorIndex) RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes); } +void TfLiteParser::ParseGather(size_t subgraphIndex, size_t operatorIndex) +{ + CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); + + TfLiteParser::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); + CHECK_VALID_SIZE(inputs.size(), 2); + TfLiteParser::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); + CHECK_VALID_SIZE(outputs.size(), 1); + + armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); + armnn::TensorInfo indicesTensorInfo = ToTensorInfo(inputs[1]); + TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); + + armnn::GatherDescriptor gatherDescriptor; + + const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; + const auto * options = operatorPtr->builtin_options.AsGatherOptions(); + auto axis = options->axis; + + auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions()); + auto indicesDimensions = indicesTensorInfo.GetNumDimensions(); + auto outputDimensions = outputTensorInfo.GetNumDimensions(); + if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0))) + { + throw ParseException( + fmt::format("Operation has invalid axis: {} It is out of bounds [ -{}, {} ) {}", + axis, + inputDimensions, inputDimensions, + CHECK_LOCATION().AsString())); + } + if (outputDimensions != static_cast<unsigned int>(inputDimensions) + indicesDimensions - 1) + { + throw ParseException( + fmt::format("Operation has invalid output dimensions: {} Output must be an ({} + {} - 1) -D tensor {}", + outputDimensions, + inputDimensions, indicesDimensions, + CHECK_LOCATION().AsString())); + } + + gatherDescriptor.m_Axis = axis; + + auto layerName = fmt::format("Gather:{}:{}", subgraphIndex, operatorIndex); + IConnectableLayer* layer = m_Network->AddGatherLayer(gatherDescriptor, layerName.c_str()); + ARMNN_ASSERT(layer != nullptr); + layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); + + auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); + RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]}); + + auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); + RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); +} + +void TfLiteParser::ParseDepthToSpace(size_t subgraphIndex, size_t operatorIndex) +{ + CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); + + TfLiteParser::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); + CHECK_VALID_SIZE(inputs.size(), 1); + TfLiteParser::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); + CHECK_VALID_SIZE(outputs.size(), 1); + + armnn::DepthToSpaceDescriptor descriptor; + + const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; + const auto * options = operatorPtr->builtin_options.AsDepthToSpaceOptions(); + auto blockSize = options->block_size; + if (blockSize < 2) + { + throw ParseException( + fmt::format("Operation has invalid block size: {} Block size should be >= 2 {}", + blockSize, + CHECK_LOCATION().AsString())); + } + descriptor.m_BlockSize = armnn::numeric_cast<uint32_t>(blockSize); + + auto layerName = fmt::format("DepthToSpace:{}:{}", subgraphIndex, operatorIndex); + IConnectableLayer* layer = m_Network->AddDepthToSpaceLayer(descriptor, layerName.c_str()); + ARMNN_ASSERT(layer != nullptr); + TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); + layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); + + auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); + RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); + + auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); + RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); +} + armnn::IConnectableLayer* TfLiteParser::AddFusedActivationLayer(armnn::IConnectableLayer* prevLayer, unsigned int outputSlot, tflite::ActivationFunctionType activationType) |