aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTeresa Charlin <teresa.charlinreyes@arm.com>2024-03-05 15:33:10 +0000
committerKevin May <kevin.may@arm.com>2024-03-12 16:12:41 +0000
commitce7f51f0a8f815ff0e0dd94e209a8cf1802ac914 (patch)
tree7b1879cd830944d6bc2b929c8d6aac75e9a49b66
parentbd7bea776b716b3893ae6630b90ed9e5fd47d19d (diff)
downloadarmnn-ce7f51f0a8f815ff0e0dd94e209a8cf1802ac914.tar.gz
IVGCVSW-8231 ScatterNd added to TFLite parser
Signed-off-by: Teresa Charlin <teresa.charlinreyes@arm.com> Change-Id: I5b87304319f5e83e8eba9cb2d934fc4a6aebe85b
-rw-r--r--docs/05_01_parsers.dox3
-rw-r--r--src/armnnTfLiteParser/TfLiteParser.cpp47
-rw-r--r--src/armnnTfLiteParser/TfLiteParser.hpp3
-rw-r--r--src/armnnTfLiteParser/test/ScatterNd.cpp130
4 files changed, 180 insertions, 3 deletions
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 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 }}});
+}
+
+}