diff options
author | Kevin May <kevin.may@arm.com> | 2024-03-11 09:31:10 +0000 |
---|---|---|
committer | TeresaARM <teresa.charlinreyes@arm.com> | 2024-03-12 18:23:16 +0000 |
commit | 93bbf00d968101fb9a9174ad011b655ca7100546 (patch) | |
tree | 324a08f34ce1e2de285942c6bfbc25aba1c77dcb /delegate/classic | |
parent | e3cc71663e39a2d46bad3ef7e6c146ff9a9e5b0e (diff) | |
download | armnn-93bbf00d968101fb9a9174ad011b655ca7100546.tar.gz |
IVGCVSW-8232 ScatterNd added to delegate and opaque delegate
Signed-off-by: Kevin May <kevin.may@arm.com>
Signed-off-by: Teresa Charlin <teresa.charlinreyes@arm.com>
Change-Id: I5839f54c71f74eaa6819333393bb3054db9db5be
Diffstat (limited to 'delegate/classic')
-rw-r--r-- | delegate/classic/CMakeLists.txt | 3 | ||||
-rw-r--r-- | delegate/classic/src/ScatterNd.hpp | 161 | ||||
-rw-r--r-- | delegate/classic/src/armnn_delegate.cpp | 7 |
3 files changed, 170 insertions, 1 deletions
diff --git a/delegate/classic/CMakeLists.txt b/delegate/classic/CMakeLists.txt index fbd19ede30..72ecc28d97 100644 --- a/delegate/classic/CMakeLists.txt +++ b/delegate/classic/CMakeLists.txt @@ -1,5 +1,5 @@ # -# Copyright © 2023 Arm Ltd and Contributors. All rights reserved. +# Copyright © 2023-2024 Arm Ltd and Contributors. All rights reserved. # SPDX-License-Identifier: MIT # @@ -37,6 +37,7 @@ list(APPEND armnnClassicDelegateObject_sources src/Resize.hpp src/ReverseV2.hpp src/Round.hpp + src/ScatterNd.hpp src/Shape.hpp src/SharedFunctions.hpp src/SharedFunctions.cpp diff --git a/delegate/classic/src/ScatterNd.hpp b/delegate/classic/src/ScatterNd.hpp new file mode 100644 index 0000000000..c73e231c46 --- /dev/null +++ b/delegate/classic/src/ScatterNd.hpp @@ -0,0 +1,161 @@ +// +// Copyright © 2024 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#pragma once + +#include <tensorflow/lite/builtin_ops.h> +#include <tensorflow/lite/c/builtin_op_data.h> +#include <tensorflow/lite/c/common.h> +#include <tensorflow/lite/minimal_logging.h> +#include <tensorflow/lite/kernels/internal/tensor_ctypes.h> +#include <tensorflow/lite/schema/schema_generated.h> + +namespace armnnDelegate +{ +TfLiteStatus ValidateScatterNdOperator(DelegateData& delegateData, + TfLiteContext* tfLiteContext, + const armnn::TensorInfo& indicesInfo, + const armnn::TensorInfo& updatesInfo, + const armnn::TensorInfo& shapeInfo, + const armnn::TensorInfo& outputInfo, + const armnn::ScatterNdDescriptor& descriptor) +{ + bool isSupported = false; + FORWARD_LAYER_SUPPORT_FUNC("SCATTER_ND", + tfLiteContext, + IsScatterNdSupported, + delegateData.m_Backends, + isSupported, + armnn::BackendId(), + shapeInfo, + indicesInfo, + updatesInfo, + outputInfo, + descriptor); + return isSupported ? kTfLiteOk : kTfLiteError; +} + +TfLiteStatus VisitScatterNdOperator(DelegateData& delegateData, + TfLiteContext* tfLiteContext, + TfLiteNode* tfLiteNode, + int nodeIndex, + int32_t scatterNdOperatorCode) +{ + TF_LITE_ENSURE_STATUS(ValidateNumInputs(tfLiteContext, tfLiteNode, 3, nodeIndex)); + TF_LITE_ENSURE_STATUS(ValidateNumOutputs(tfLiteContext, tfLiteNode, 1, nodeIndex)); + + const TfLiteTensor* tfLiteTensors = tfLiteContext->tensors; + + // The indices tensor are the positions the data is updated/scattered into + const TfLiteTensor& tfLiteIndicesTensor = tfLiteTensors[tfLiteNode->inputs->data[0]]; + if (IsDynamicTensor(tfLiteIndicesTensor)) + { + TF_LITE_MAYBE_KERNEL_LOG( + tfLiteContext, + "TfLiteArmnnDelegate: Dynamic input tensors are not supported in operator #%d node #%d: ", + scatterNdOperatorCode, nodeIndex); + return kTfLiteError; + } + + // The updates tensor provides the data which will be updated/scattered into the relevant indices + const TfLiteTensor& tfLiteUpdatesTensor = tfLiteTensors[tfLiteNode->inputs->data[1]]; + if (IsDynamicTensor(tfLiteUpdatesTensor)) + { + TF_LITE_MAYBE_KERNEL_LOG( + tfLiteContext, + "TfLiteArmnnDelegate: Dynamic input tensors are not supported in operator #%d node #%d: ", + scatterNdOperatorCode, nodeIndex); + return kTfLiteError; + } + + // For tflite scatternd there is no input tensor + // The shape tensor is a 1D tensor which represents the shape of an input tensor to be filled with zeros + const TfLiteTensor& tfLiteShapeTensor = tfLiteTensors[tfLiteNode->inputs->data[2]]; + if (IsDynamicTensor(tfLiteUpdatesTensor)) + { + TF_LITE_MAYBE_KERNEL_LOG( + tfLiteContext, + "TfLiteArmnnDelegate: Dynamic input tensors are not supported in operator #%d node #%d: ", + scatterNdOperatorCode, nodeIndex); + return kTfLiteError; + } + + // The output tensor + const TfLiteTensor& tfLiteOutputTensor = tfLiteTensors[tfLiteNode->outputs->data[0]]; + if (IsDynamicTensor(tfLiteOutputTensor)) + { + TF_LITE_MAYBE_KERNEL_LOG( + tfLiteContext, + "TfLiteArmnnDelegate: Dynamic output tensors are not supported in operator #%d node #%d: ", + scatterNdOperatorCode, nodeIndex); + return kTfLiteError; + } + + const armnn::TensorInfo& indicesTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteIndicesTensor); + const armnn::TensorInfo& updatesTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteUpdatesTensor); + const armnn::TensorInfo& shapeTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteShapeTensor); + const armnn::TensorInfo& outputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteOutputTensor); + + armnn::ScatterNdDescriptor scatterNdDescriptor; + scatterNdDescriptor.m_Function = armnn::ScatterNdFunction::Update; + scatterNdDescriptor.m_InputEnabled = false; + scatterNdDescriptor.m_Axis = 0; + scatterNdDescriptor.m_AxisEnabled = false; + + // Check output dimensions + if (shapeTensorInfo.GetShape().GetNumElements() != outputTensorInfo.GetNumDimensions()) + { + TF_LITE_MAYBE_KERNEL_LOG( + tfLiteContext, + "TfLiteArmnnDelegate: Shape tensor number of elements and output tensor dimension differ", + "Operator: #%d node #%d: ", + scatterNdOperatorCode, nodeIndex); + return kTfLiteError; + } + + // No network pointer indicates that only support for this operator should be checked + if (!delegateData.m_Network) + { + return ValidateScatterNdOperator(delegateData, + tfLiteContext, + indicesTensorInfo, + updatesTensorInfo, + shapeTensorInfo, + outputTensorInfo, + scatterNdDescriptor); + } + + auto layerName = GetLayerName(armnn::LayerType::ScatterNd, nodeIndex); + armnn::IConnectableLayer* layer = delegateData.m_Network->AddScatterNdLayer(scatterNdDescriptor, layerName.c_str()); + + if (layer == nullptr) + { + return kTfLiteError; + } + + layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); + + if (ProcessInputs(layer, delegateData, tfLiteContext, tfLiteNode, nodeIndex) != kTfLiteOk) + { + return kTfLiteError; + } + + if (static_cast<unsigned int>(tfLiteNode->outputs->size) != layer->GetNumOutputSlots()) + { + return kTfLiteError; + } + + delegateData.m_OutputSlotForNode[tfLiteNode->inputs->data[2]]->Connect(layer->GetInputSlot(0)); + delegateData.m_OutputSlotForNode[tfLiteNode->inputs->data[0]]->Connect(layer->GetInputSlot(1)); + delegateData.m_OutputSlotForNode[tfLiteNode->inputs->data[1]]->Connect(layer->GetInputSlot(2)); + + // Prepare output slots + armnn::IOutputSlot& outputSlot = layer->GetOutputSlot(0); + delegateData.m_OutputSlotForNode[static_cast<unsigned long>(tfLiteNode->outputs->data[0])] = &outputSlot; + + return kTfLiteOk; +} + +} // namespace armnnDelegate
\ No newline at end of file diff --git a/delegate/classic/src/armnn_delegate.cpp b/delegate/classic/src/armnn_delegate.cpp index 05bf9b2ee7..52621ee6c1 100644 --- a/delegate/classic/src/armnn_delegate.cpp +++ b/delegate/classic/src/armnn_delegate.cpp @@ -34,6 +34,7 @@ #include "Resize.hpp" #include "ReverseV2.hpp" #include "Round.hpp" +#include "ScatterNd.hpp" #include "Shape.hpp" #include "Slice.hpp" #include "StridedSlice.hpp" @@ -1070,6 +1071,12 @@ TfLiteStatus ArmnnSubgraph::VisitNode(DelegateData& delegateData, tfLiteNode, nodeIndex, kTfLiteBuiltinTransposeConv); + case kTfLiteBuiltinScatterNd: + return VisitScatterNdOperator(delegateData, + tfLiteContext, + tfLiteNode, + nodeIndex, + kTfLiteBuiltinScatterNd); case kTfLiteBuiltinSoftmax: return VisitSoftmaxOperator(delegateData, tfLiteContext, |