From ce7f51f0a8f815ff0e0dd94e209a8cf1802ac914 Mon Sep 17 00:00:00 2001 From: Teresa Charlin Date: Tue, 5 Mar 2024 15:33:10 +0000 Subject: IVGCVSW-8231 ScatterNd added to TFLite parser Signed-off-by: Teresa Charlin Change-Id: I5b87304319f5e83e8eba9cb2d934fc4a6aebe85b --- docs/05_01_parsers.dox | 3 +- src/armnnTfLiteParser/TfLiteParser.cpp | 47 ++++++++++- src/armnnTfLiteParser/TfLiteParser.hpp | 3 +- src/armnnTfLiteParser/test/ScatterNd.cpp | 130 +++++++++++++++++++++++++++++++ 4 files changed, 180 insertions(+), 3 deletions(-) create mode 100644 src/armnnTfLiteParser/test/ScatterNd.cpp diff --git a/docs/05_01_parsers.dox b/docs/05_01_parsers.dox index 7dcf8e2553..03b8ea22a2 100644 --- a/docs/05_01_parsers.dox +++ b/docs/05_01_parsers.dox @@ -1,4 +1,4 @@ -/// Copyright (c) 2022-2023 Arm Ltd and Contributors. All rights reserved. +/// Copyright (c) 2022-2024 Arm Ltd and Contributors. All rights reserved. /// /// SPDX-License-Identifier: MIT /// @@ -177,6 +177,7 @@ The Arm NN SDK TensorFlow Lite parser currently supports the following operators - RESIZE_NEAREST_NEIGHBOR - REVERSE_V2 - RSQRT +- SCATTER_ND - SHAPE - SIN - SLICE 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 Optionalsubgraphs[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 }}}); +} + +} -- cgit v1.2.1