From 98427a19b7e820283909d3e4ae00bc9447e461fc Mon Sep 17 00:00:00 2001 From: Teresa Charlin Date: Wed, 25 Nov 2020 18:22:57 +0000 Subject: [PATCH] IVGCVSW-5384 TfLiteDelegate: Implement the Gather operator Signed-off-by: Teresa Charlin Change-Id: Iaf2112363d2b191327711d8e083fee2a751c35c5 --- delegate/CMakeLists.txt | 2 + delegate/TensorFlowLiteDelegateSupport.md | 2 + delegate/src/Gather.hpp | 96 +++++++++++++--- delegate/src/test/GatherTest.cpp | 117 +++++++++++++++++++ delegate/src/test/GatherTestHelper.hpp | 181 ++++++++++++++++++++++++++++++ 5 files changed, 382 insertions(+), 16 deletions(-) create mode 100644 delegate/src/test/GatherTest.cpp create mode 100644 delegate/src/test/GatherTestHelper.hpp diff --git a/delegate/CMakeLists.txt b/delegate/CMakeLists.txt index 38f7bd1..c04472e 100644 --- a/delegate/CMakeLists.txt +++ b/delegate/CMakeLists.txt @@ -120,6 +120,8 @@ if(BUILD_UNIT_TESTS) src/test/ElementwiseUnaryTestHelper.hpp src/test/FullyConnectedTest.cpp src/test/FullyConnectedTestHelper.hpp + src/test/GatherTest.cpp + src/test/GatherTestHelper.hpp src/test/Pooling2dTest.cpp src/test/Pooling2dTestHelper.hpp src/test/QuantizationTest.cpp diff --git a/delegate/TensorFlowLiteDelegateSupport.md b/delegate/TensorFlowLiteDelegateSupport.md index b1b39f6..00938b9 100644 --- a/delegate/TensorFlowLiteDelegateSupport.md +++ b/delegate/TensorFlowLiteDelegateSupport.md @@ -28,6 +28,8 @@ The Arm NN SDK TensorFlow Lite delegate currently supports the following operato * FULLY_CONNECTED, Supported Fused Activation: RELU , RELU6 , TANH, NONE +* GATHER + * GREATER * GREATER_OR_EQUAL diff --git a/delegate/src/Gather.hpp b/delegate/src/Gather.hpp index 98d8dc9..9ed0fe1 100644 --- a/delegate/src/Gather.hpp +++ b/delegate/src/Gather.hpp @@ -5,29 +5,93 @@ #pragma once -#include - -#include -#include -#include -#include +#include "DelegateUtils.hpp" +#include +#include +#include +#include namespace armnnDelegate { - TfLiteStatus VisitGatherOperator(DelegateData& delegateData, TfLiteContext* tfLiteContext, TfLiteNode* tfLiteNode, int nodeIndex, - int32_t gatherOperatorCode) + int32_t operatorCode) { - armnn::IgnoreUnused(delegateData, - tfLiteContext, - tfLiteNode, - nodeIndex, - gatherOperatorCode); + TF_LITE_ENSURE_STATUS(ValidateNumInputs(tfLiteContext, tfLiteNode, 2, nodeIndex)); + TF_LITE_ENSURE_STATUS(ValidateNumOutputs(tfLiteContext, tfLiteNode, 1, nodeIndex)); - return kTfLiteError; -} + const TfLiteTensor* tfLiteTensors = tfLiteContext->tensors; + + const TfLiteTensor& tfLiteInputTensor = tfLiteTensors[tfLiteNode->inputs->data[0]]; + if (!IsValid(tfLiteContext, tfLiteInputTensor, operatorCode, nodeIndex)) + { + return kTfLiteError; + } + + const TfLiteTensor& tfLiteIndicesTensor = tfLiteTensors[tfLiteNode->inputs->data[1]]; + if (!IsValid(tfLiteContext, tfLiteIndicesTensor, operatorCode, nodeIndex)) + { + return kTfLiteError; + } + + const TfLiteTensor& tfLiteOutputTensor = tfLiteTensors[tfLiteNode->outputs->data[0]]; + if (!IsValid(tfLiteContext, tfLiteOutputTensor, operatorCode, nodeIndex)) + { + return kTfLiteError; + } + + auto* gatherParameters = reinterpret_cast(tfLiteNode->builtin_data); + auto axis = gatherParameters->axis; -} // namespace armnnDelegate + const armnn::TensorInfo& inputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteInputTensor); + const armnn::TensorInfo& indicesTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteIndicesTensor); + const armnn::TensorInfo& outputTensorInfo = GetTensorInfoForTfLiteTensor(tfLiteOutputTensor); + armnn::GatherDescriptor gatherDescriptor; + gatherDescriptor.m_Axis = axis; + + auto inputDimensions = static_cast(inputTensorInfo.GetNumDimensions()); + auto indicesDimensions = indicesTensorInfo.GetNumDimensions(); + auto outputDimensions = outputTensorInfo.GetNumDimensions(); + if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0))) + { + TF_LITE_MAYBE_KERNEL_LOG( tfLiteContext, + "TfLiteArmnnDelegate: Operation has invalid axis: %d. It is out of bounds [-%d, %d))", + axis, inputDimensions, inputDimensions); + return kTfLiteError; + } + if (outputDimensions != static_cast(inputDimensions) + indicesDimensions - 1) + { + TF_LITE_MAYBE_KERNEL_LOG( tfLiteContext, + "Operation has invalid output dimensions: %d. Output must be an (%d + %d - 1)-D tensor", + outputDimensions, inputDimensions, indicesDimensions); + return kTfLiteError; + } + + if (!delegateData.m_Network) + { + // Check if supported + bool isSupported = false; + FORWARD_LAYER_SUPPORT_FUNC(__func__, + tfLiteContext, + IsGatherSupported, + delegateData.m_Backends, + isSupported, + inputTensorInfo, + indicesTensorInfo, + outputTensorInfo, + gatherDescriptor); + return isSupported ? kTfLiteOk : kTfLiteError; + } + + armnn::IConnectableLayer* layer = delegateData.m_Network->AddGatherLayer(gatherDescriptor); + ARMNN_ASSERT(layer != nullptr); + + layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); + + Connect(layer, tfLiteNode, delegateData); + + return kTfLiteOk; +} +} // namespace armnnDelegate \ No newline at end of file diff --git a/delegate/src/test/GatherTest.cpp b/delegate/src/test/GatherTest.cpp new file mode 100644 index 0000000..6dd0151 --- /dev/null +++ b/delegate/src/test/GatherTest.cpp @@ -0,0 +1,117 @@ +// +// Copyright © 2020 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#include "GatherTestHelper.hpp" + +#include + +#include +#include + +#include + +namespace armnnDelegate +{ + +// GATHER Operator +void GatherUint8Test(std::vector& backends) +{ + + std::vector paramsShape{8}; + std::vector indicesShape{3}; + std::vector expectedOutputShape{3}; + + int32_t axis = 0; + std::vector paramsValues{1, 2, 3, 4, 5, 6, 7, 8}; + std::vector indicesValues{7, 6, 5}; + std::vector expectedOutputValues{8, 7, 6}; + + GatherTest(::tflite::TensorType_UINT8, + backends, + paramsShape, + indicesShape, + expectedOutputShape, + axis, + paramsValues, + indicesValues, + expectedOutputValues); +} + +void GatherFp32Test(std::vector& backends) +{ + std::vector paramsShape{8}; + std::vector indicesShape{3}; + std::vector expectedOutputShape{3}; + + int32_t axis = 0; + std::vector paramsValues{1.1f, 2.2f, 3.3f, 4.4f, 5.5f, 6.6f, 7.7f, 8.8f}; + std::vector indicesValues{7, 6, 5}; + std::vector expectedOutputValues{8.8f, 7.7f, 6.6f}; + + GatherTest(::tflite::TensorType_FLOAT32, + backends, + paramsShape, + indicesShape, + expectedOutputShape, + axis, + paramsValues, + indicesValues, + expectedOutputValues); +} + +// GATHER Test Suite +TEST_SUITE("GATHER_CpuRefTests") +{ + +TEST_CASE ("GATHER_Uint8_CpuRef_Test") +{ + std::vector backends = {armnn::Compute::CpuRef}; + GatherUint8Test(backends); +} + +TEST_CASE ("GATHER_Fp32_CpuRef_Test") +{ + std::vector backends = {armnn::Compute::CpuRef}; + GatherFp32Test(backends); +} + +} + +TEST_SUITE("GATHER_CpuAccTests") +{ + +TEST_CASE ("GATHER_Uint8_CpuAcc_Test") +{ + std::vector backends = {armnn::Compute::CpuAcc}; + GatherUint8Test(backends); +} + +TEST_CASE ("GATHER_Fp32_CpuAcc_Test") +{ + std::vector backends = {armnn::Compute::CpuAcc}; + GatherFp32Test(backends); +} + +} + +TEST_SUITE("GATHER_GpuAccTests") +{ + +TEST_CASE ("GATHER_Uint8_GpuAcc_Test") +{ + std::vector backends = {armnn::Compute::GpuAcc}; + GatherUint8Test(backends); +} + +TEST_CASE ("GATHER_Fp32_GpuAcc_Test") +{ + std::vector backends = {armnn::Compute::GpuAcc}; + GatherFp32Test(backends); +} + +} +// End of GATHER Test Suite + +} // namespace armnnDelegate \ No newline at end of file diff --git a/delegate/src/test/GatherTestHelper.hpp b/delegate/src/test/GatherTestHelper.hpp new file mode 100644 index 0000000..d8bfe37 --- /dev/null +++ b/delegate/src/test/GatherTestHelper.hpp @@ -0,0 +1,181 @@ +// +// Copyright © 2020 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#pragma once + +#include "TestUtils.hpp" + +#include + +#include +#include +#include +#include +#include +#include + +#include + +namespace +{ + +std::vector CreateGatherTfLiteModel(tflite::TensorType tensorType, + std::vector& paramsShape, + std::vector& indicesShape, + const std::vector& expectedOutputShape, + int32_t axis, + float quantScale = 1.0f, + int quantOffset = 0) +{ + using namespace tflite; + flatbuffers::FlatBufferBuilder flatBufferBuilder; + + std::vector> buffers; + buffers.push_back(CreateBuffer(flatBufferBuilder, flatBufferBuilder.CreateVector({}))); + + auto quantizationParameters = + CreateQuantizationParameters(flatBufferBuilder, + 0, + 0, + flatBufferBuilder.CreateVector({quantScale}), + flatBufferBuilder.CreateVector({quantOffset})); + + std::array, 3> tensors; + tensors[0] = CreateTensor(flatBufferBuilder, + flatBufferBuilder.CreateVector(paramsShape.data(), + paramsShape.size()), + tensorType, + 0, + flatBufferBuilder.CreateString("params"), + quantizationParameters); + tensors[1] = CreateTensor(flatBufferBuilder, + flatBufferBuilder.CreateVector(indicesShape.data(), + indicesShape.size()), + ::tflite::TensorType_INT32, + 0, + flatBufferBuilder.CreateString("indices"), + quantizationParameters); + tensors[2] = CreateTensor(flatBufferBuilder, + flatBufferBuilder.CreateVector(expectedOutputShape.data(), + expectedOutputShape.size()), + tensorType, + 0, + flatBufferBuilder.CreateString("output"), + quantizationParameters); + + + // create operator + tflite::BuiltinOptions operatorBuiltinOptionsType = tflite::BuiltinOptions_GatherOptions; + flatbuffers::Offset operatorBuiltinOptions = CreateGatherOptions(flatBufferBuilder).Union(); + + const std::vector operatorInputs{{0, 1}}; + const std::vector operatorOutputs{{2}}; + flatbuffers::Offset controlOperator = + CreateOperator(flatBufferBuilder, + 0, + flatBufferBuilder.CreateVector(operatorInputs.data(), + operatorInputs.size()), + flatBufferBuilder.CreateVector(operatorOutputs.data(), + operatorOutputs.size()), + operatorBuiltinOptionsType, + operatorBuiltinOptions); + + const std::vector subgraphInputs{{0, 1}}; + const std::vector subgraphOutputs{{2}}; + flatbuffers::Offset subgraph = + CreateSubGraph(flatBufferBuilder, + flatBufferBuilder.CreateVector(tensors.data(), tensors.size()), + flatBufferBuilder.CreateVector(subgraphInputs.data(), + subgraphInputs.size()), + flatBufferBuilder.CreateVector(subgraphOutputs.data(), + subgraphOutputs.size()), + flatBufferBuilder.CreateVector(&controlOperator, 1)); + + flatbuffers::Offset modelDescription = + flatBufferBuilder.CreateString("ArmnnDelegate: GATHER Operator Model"); + flatbuffers::Offset operatorCode = CreateOperatorCode(flatBufferBuilder, + BuiltinOperator_GATHER); + + flatbuffers::Offset flatbufferModel = + CreateModel(flatBufferBuilder, + TFLITE_SCHEMA_VERSION, + flatBufferBuilder.CreateVector(&operatorCode, 1), + flatBufferBuilder.CreateVector(&subgraph, 1), + modelDescription, + flatBufferBuilder.CreateVector(buffers.data(), buffers.size())); + + flatBufferBuilder.Finish(flatbufferModel); + + return std::vector(flatBufferBuilder.GetBufferPointer(), + flatBufferBuilder.GetBufferPointer() + flatBufferBuilder.GetSize()); +} + +template +void GatherTest(tflite::TensorType tensorType, + std::vector& backends, + std::vector& paramsShape, + std::vector& indicesShape, + std::vector& expectedOutputShape, + int32_t axis, + std::vector& paramsValues, + std::vector& indicesValues, + std::vector& expectedOutputValues, + float quantScale = 1.0f, + int quantOffset = 0) +{ + using namespace tflite; + std::vector modelBuffer = CreateGatherTfLiteModel(tensorType, + paramsShape, + indicesShape, + expectedOutputShape, + axis, + quantScale, + quantOffset); + const Model* tfLiteModel = GetModel(modelBuffer.data()); + + // Create TfLite Interpreters + std::unique_ptr armnnDelegate; + CHECK(InterpreterBuilder(tfLiteModel, ::tflite::ops::builtin::BuiltinOpResolver()) + (&armnnDelegate) == kTfLiteOk); + CHECK(armnnDelegate != nullptr); + CHECK(armnnDelegate->AllocateTensors() == kTfLiteOk); + + std::unique_ptr tfLiteDelegate; + CHECK(InterpreterBuilder(tfLiteModel, ::tflite::ops::builtin::BuiltinOpResolver()) + (&tfLiteDelegate) == kTfLiteOk); + CHECK(tfLiteDelegate != nullptr); + CHECK(tfLiteDelegate->AllocateTensors() == kTfLiteOk); + + // Create the ArmNN Delegate + armnnDelegate::DelegateOptions delegateOptions(backends); + std::unique_ptr + theArmnnDelegate(armnnDelegate::TfLiteArmnnDelegateCreate(delegateOptions), + armnnDelegate::TfLiteArmnnDelegateDelete); + CHECK(theArmnnDelegate != nullptr); + + // Modify armnnDelegateInterpreter to use armnnDelegate + CHECK(armnnDelegate->ModifyGraphWithDelegate(theArmnnDelegate.get()) == kTfLiteOk); + + // Set input data + armnnDelegate::FillInput(tfLiteDelegate, 0, paramsValues); + armnnDelegate::FillInput(armnnDelegate, 0, paramsValues); + armnnDelegate::FillInput(tfLiteDelegate, 1, indicesValues); + armnnDelegate::FillInput(armnnDelegate, 1, indicesValues); + + // Run EnqueWorkload + CHECK(tfLiteDelegate->Invoke() == kTfLiteOk); + CHECK(armnnDelegate->Invoke() == kTfLiteOk); + + // Compare output data + armnnDelegate::CompareOutputData(tfLiteDelegate, + armnnDelegate, + expectedOutputShape, + expectedOutputValues, + 0); + + tfLiteDelegate.reset(nullptr); + armnnDelegate.reset(nullptr); +} +} // anonymous namespace \ No newline at end of file -- 2.7.4