diff options
Diffstat (limited to 'src/armnnTfLiteParser')
-rw-r--r-- | src/armnnTfLiteParser/TfLiteParser.cpp | 47 | ||||
-rw-r--r-- | src/armnnTfLiteParser/TfLiteParser.hpp | 3 | ||||
-rw-r--r-- | src/armnnTfLiteParser/test/ScatterNd.cpp | 130 |
3 files changed, 178 insertions, 2 deletions
diff --git a/src/armnnTfLiteParser/TfLiteParser.cpp b/src/armnnTfLiteParser/TfLiteParser.cpp index 049d6049a7..3fd81ff973 100644 --- a/src/armnnTfLiteParser/TfLiteParser.cpp +++ b/src/armnnTfLiteParser/TfLiteParser.cpp @@ -1,5 +1,5 @@ // -// Copyright © 2017-2023 Arm Ltd and Contributors. All rights reserved. +// Copyright © 2017-2024 Arm Ltd and Contributors. All rights reserved. // SPDX-License-Identifier: MIT // @@ -810,6 +810,7 @@ TfLiteParserImpl::TfLiteParserImpl(const Optional<ITfLiteParser::TfLiteParserOpt m_ParserFunctions[tflite::BuiltinOperator_RESIZE_NEAREST_NEIGHBOR] = &TfLiteParserImpl::ParseResizeNearestNeighbor; m_ParserFunctions[tflite::BuiltinOperator_REVERSE_V2] = &TfLiteParserImpl::ParseReverseV2; m_ParserFunctions[tflite::BuiltinOperator_RSQRT] = &TfLiteParserImpl::ParseRsqrt; + m_ParserFunctions[tflite::BuiltinOperator_SCATTER_ND] = &TfLiteParserImpl::ParseScatterNd; m_ParserFunctions[tflite::BuiltinOperator_SQRT] = &TfLiteParserImpl::ParseSqrt; m_ParserFunctions[tflite::BuiltinOperator_SHAPE] = &TfLiteParserImpl::ParseShape; m_ParserFunctions[tflite::BuiltinOperator_SIN] = &TfLiteParserImpl::ParseSin; @@ -2279,6 +2280,50 @@ void TfLiteParserImpl::ParseLogSoftmax(size_t subgraphIndex, size_t operatorInde RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); } +void TfLiteParserImpl::ParseScatterNd(size_t subgraphIndex, size_t operatorIndex) +{ + CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); + + TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); + CHECK_VALID_SIZE(inputs.size(), 3); + TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); + CHECK_VALID_SIZE(outputs.size(), 1); + + armnn::TensorInfo indicesTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0); + armnn::TensorInfo updatesTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1); + armnn::TensorInfo shapeTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2); + armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); + + // TFLite currently only have these options: update and no input given, just shape. + armnn::ScatterNdDescriptor descriptor(armnn::ScatterNdFunction::Update, false); + + const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; + const auto* options = operatorPtr->builtin_options.AsScatterNdOptions(); + IgnoreUnused(options); + + auto layerName = fmt::format("ScatterND:{}:{}", subgraphIndex, operatorIndex); + + IConnectableLayer* layer = m_Network->AddScatterNdLayer(descriptor, layerName.c_str()); + + if (!layer) + { + throw NullPointerException(fmt::format("Layer {} pointer is null {}", + operatorIndex, CHECK_LOCATION().AsString())); + } + + outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1, 2}); + layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); + + auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); + RegisterInputSlots(subgraphIndex, + operatorIndex, + layer, + {inputTensorIndexes[2], inputTensorIndexes[0], inputTensorIndexes[1]}); + + auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); + RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); +} + void TfLiteParserImpl::ParseSpaceToBatchND(size_t subgraphIndex, size_t operatorIndex) { CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); diff --git a/src/armnnTfLiteParser/TfLiteParser.hpp b/src/armnnTfLiteParser/TfLiteParser.hpp index 4ee0650626..f566131ce1 100644 --- a/src/armnnTfLiteParser/TfLiteParser.hpp +++ b/src/armnnTfLiteParser/TfLiteParser.hpp @@ -1,5 +1,5 @@ // -// Copyright © 2017-2023 Arm Ltd and Contributors. All rights reserved. +// Copyright © 2017-2024 Arm Ltd and Contributors. All rights reserved. // SPDX-License-Identifier: MIT // #pragma once @@ -180,6 +180,7 @@ private: void ParseResizeNearestNeighbor(size_t subgraphIndex, size_t operatorIndex); void ParseReverseV2(size_t subgraphIndex, size_t operatorIndex); void ParseRsqrt(size_t subgraphIndex, size_t operatorIndex); + void ParseScatterNd(size_t subgraphIndex, size_t operatorIndex); void ParseShape(size_t subgraphIndex, size_t operatorIndex); void ParseSin(size_t subgraphIndex, size_t operatorIndex); void ParseSlice(size_t subgraphIndex, size_t operatorIndex); diff --git a/src/armnnTfLiteParser/test/ScatterNd.cpp b/src/armnnTfLiteParser/test/ScatterNd.cpp new file mode 100644 index 0000000000..fa283c1a4a --- /dev/null +++ b/src/armnnTfLiteParser/test/ScatterNd.cpp @@ -0,0 +1,130 @@ +// +// Copyright © 2024 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#include "ParserFlatbuffersFixture.hpp" + + +TEST_SUITE("TensorflowLiteParser_ScatterNd") +{ +struct ScatterNdFixture : public ParserFlatbuffersFixture +{ + explicit ScatterNdFixture(const std::string& shapeShape, + const std::string& outputShape, + const std::string& dataType = "FLOAT32", + const std::string& scale = "1.0", + const std::string& offset = "0") + { + const std::string& indicesShape = "[ 3, 1 ]"; + const std::string& updatesShape = "[ 3 ]"; + + m_JsonString = R"( + { + "version": 3, + "operator_codes": [ { "builtin_code": "SCATTER_ND" } ], + "subgraphs": [ { + "tensors": [ + { + "shape": )" + indicesShape + R"(, + "type": "INT32", + "buffer": 0, + "name": "indices", + "quantization": { + "details_type": "NONE", + "quantized_dimension": 0 + }, + }, + { + "shape": )" + updatesShape + R"( , + "type": )" + dataType + R"(, + "buffer": 1, + "name": "updates", + "quantization": { + "min": [ 0.0 ], + "max": [ 255.0 ], + "scale": [ )" + scale + R"( ], + "zero_point": [ )" + offset + R"( ], + } + }, + { + "shape": )" + shapeShape + R"( , + "type": "INT32", + "buffer": 2, + "name": "shape", + "quantization": { + "details_type": "NONE", + "quantized_dimension": 0 + }, + "is_variable": false, + "has_rank": true + }, + { + "shape": )" + outputShape + R"(, + "type": )" + dataType + R"(, + "buffer": 3, + "name": "outputTensor", + "quantization": { + "min": [ 0.0 ], + "max": [ 255.0 ], + "scale": [ )" + scale + R"( ], + "zero_point": [ )" + offset + R"( ], + } + } + ], + "inputs": [ 0, 1, 2 ], + "outputs": [ 3 ], + "operators": [ + { + "opcode_index": 0, + "inputs": [ 0, 1, 2 ], + "outputs": [ 3 ], + "builtin_options_type": "ScatterNdOptions", + "builtin_options": {}, + "custom_options_format": "FLEXBUFFERS" + } + ], + } ], + "buffers" : [ + { "data": [ 0, 1, 2 ], + "offset": 0, + "size": 0 }, + { "data": [ 1, 2, 3 ], + "offset": 0, + "size": 0 }, + { }, + { }, + ] + } + )"; + Setup(); + } +}; + +struct SimpleScatterNdFixture : public ScatterNdFixture +{ + SimpleScatterNdFixture() : ScatterNdFixture("[ 1 ]", "[ 5 ]") {} +}; + +TEST_CASE_FIXTURE(SimpleScatterNdFixture, "ParseScatterNd") +{ + RunTest<2, armnn::DataType::Signed32, armnn::DataType::Float32> + (0, + {{ "shape", { 5 }}}, + {{ "outputTensor", {1, 2, 3, 0, 0 }}}); +} + +struct ScatterNdUint8Fixture : public ScatterNdFixture +{ + ScatterNdUint8Fixture() : ScatterNdFixture("[ 2 ]", "[ 3, 3 ]", "UINT8") {} +}; + +TEST_CASE_FIXTURE(ScatterNdUint8Fixture, "ParseScatterNdUint8") +{ + RunTest<2, armnn::DataType::Signed32, armnn::DataType::QAsymmU8> + (0, + {{ "shape", { 3, 3 }}}, + {{ "outputTensor", { 1, 0, 0, 0, 2, 0, 0, 0, 3 }}}); +} + +} |