From 59f8f65f047546e4a73966a988d087eeb6602cea Mon Sep 17 00:00:00 2001 From: Ryan OShea Date: Thu, 11 May 2023 20:37:53 +0100 Subject: IVGCVSW-7735 Opaque Delegate Cleanup * Move TFL_TheStableDelegate to opaque/armnn_delegate_external.cpp * Change TFL_TheStableDelegate to extern variable * Remove duplicated opaque test sources * Add support for missing Fill operator * Enable support for Mirror Pad * Fix failing Split tests Signed-off-by: Ryan OShea Signed-off-by: Matthew Sloyan Change-Id: I7f8d8b4269bb4fbe27b6f47709cbd828554d37d8 --- delegate/CMakeLists.txt | 86 +--------------- delegate/opaque/CMakeLists.txt | 1 + delegate/opaque/include/armnn_delegate.hpp | 3 +- delegate/opaque/src/Fill.hpp | 130 ++++++++++++++++++++++++ delegate/opaque/src/Split.hpp | 49 ++++++--- delegate/opaque/src/armnn_delegate.cpp | 34 +++---- delegate/opaque/src/armnn_external_delegate.cpp | 39 +++++++ 7 files changed, 226 insertions(+), 116 deletions(-) diff --git a/delegate/CMakeLists.txt b/delegate/CMakeLists.txt index 272c9a5a73..bb552d33bb 100644 --- a/delegate/CMakeLists.txt +++ b/delegate/CMakeLists.txt @@ -143,8 +143,6 @@ if(BUILD_UNIT_TESTS) test/Convolution2dTest.cpp test/Convolution3dTest.cpp test/ConvolutionTestHelper.hpp - test/DelegateOptionsTest.cpp - test/DelegateOptionsTestHelper.hpp test/DepthwiseConvolution2dTest.cpp test/ElementwiseBinaryTest.cpp test/ElementwiseBinaryTestHelper.hpp @@ -240,7 +238,9 @@ if(BUILD_UNIT_TESTS) set(classicDelegate_unittest_sources) list(APPEND classicDelegate_unittest_sources classic/src/test/ArmnnClassicDelegateTest.cpp - classic/src/test/DelegateTestInterpreter.cpp) + classic/src/test/DelegateTestInterpreter.cpp + test/DelegateOptionsTest.cpp + test/DelegateOptionsTestHelper.hpp) add_executable(DelegateUnitTests ${commonDelegate_unittest_sources} ${classicDelegate_unittest_sources}) @@ -258,88 +258,12 @@ if(BUILD_UNIT_TESTS) if (BUILD_OPAQUE_DELEGATE) set(opaqueDelegate_unittest_sources) list(APPEND opaqueDelegate_unittest_sources - common/src/test/DelegateTestInterpreter.hpp - common/src/test/DelegateTestInterpreterUtils.hpp opaque/src/test/ArmnnOpaqueDelegateTest.cpp - opaque/src/test/DelegateTestInterpreter.cpp - test/ActivationTest.cpp - test/ActivationTestHelper.hpp - test/ArgMinMaxTest.cpp - test/ArgMinMaxTestHelper.hpp - test/BatchMatMulTest.cpp - test/BatchMatMulTestHelper.hpp - test/BatchSpaceTest.cpp - test/BatchSpaceTestHelper.hpp - test/CastTest.cpp - test/CastTestHelper.hpp - test/ComparisonTest.cpp - test/ComparisonTestHelper.hpp - test/ControlTest.cpp - test/ControlTestHelper.hpp - test/Convolution2dTest.cpp - test/Convolution3dTest.cpp - test/ConvolutionTestHelper.hpp - test/DepthwiseConvolution2dTest.cpp - test/ElementwiseBinaryTest.cpp - test/ElementwiseBinaryTestHelper.hpp - test/ElementwiseUnaryTestHelper.hpp - test/ElementwiseUnaryTest.cpp - test/ExpandDimsTest.cpp - test/FullyConnectedTest.cpp - test/FullyConnectedTestHelper.hpp - test/GatherTest.cpp - test/GatherTestHelper.hpp - test/GatherNdTest.cpp - test/GatherNdTestHelper.hpp - test/LstmTest.cpp - test/LstmTestHelper.hpp - test/LogicalTest.cpp - test/LogicalTestHelper.hpp - test/NormalizationTest.cpp - test/NormalizationTestHelper.hpp - test/PackTest.cpp - test/PackTestHelper.hpp - test/PadTest.cpp - test/PadTestHelper.hpp - test/Pooling2dTest.cpp - test/Pooling2dTestHelper.hpp - test/Pooling3dTest.cpp - test/Pooling3dTestHelper.hpp - test/PreluTest.cpp - test/PreluTestHelper.hpp - test/ReduceTest.cpp - test/ReduceTestHelper.hpp - test/ReshapeTest.cpp - test/ResizeTest.cpp - test/ResizeTestHelper.hpp - test/RoundTest.cpp - test/RoundTestHelper.hpp - test/QuantizationTest.cpp - test/QuantizationTestHelper.hpp - test/ShapeTest.cpp - test/ShapeTestHelper.hpp - test/SliceTest.cpp - test/SliceTestHelper.hpp - test/SoftmaxTest.cpp - test/SoftmaxTestHelper.hpp - test/SpaceDepthTest.cpp - test/SpaceDepthTestHelper.hpp - test/SqueezeTest.cpp - test/StridedSliceTest.cpp - test/StridedSliceTestHelper.hpp - test/TestUtils.hpp - test/TestUtils.cpp - test/TransposeConvolution2dTest.cpp - test/TransposeTest.cpp - test/TransposeTestHelper.hpp - test/UnidirectionalSequenceLstmTest.cpp - test/UnidirectionalSequenceLstmTestHelper.hpp - test/UnpackTest.cpp - test/UnpackTestHelper.hpp) + opaque/src/test/DelegateTestInterpreter.cpp) # Until all operators are supported, we have to add tests one by one above to opaqueDelegate_unittest_sources. # After we add can add commonDelegate_unittest_sources to the add_executable below. - add_executable(OpaqueDelegateUnitTests ${opaqueDelegate_unittest_sources}) + add_executable(OpaqueDelegateUnitTests ${opaqueDelegate_unittest_sources} ${commonDelegate_unittest_sources}) target_include_directories(OpaqueDelegateUnitTests SYSTEM PRIVATE "${TF_LITE_SCHEMA_INCLUDE_PATH}") target_include_directories(OpaqueDelegateUnitTests SYSTEM PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/common/src/test") diff --git a/delegate/opaque/CMakeLists.txt b/delegate/opaque/CMakeLists.txt index a33d3d8e90..1e00709f01 100644 --- a/delegate/opaque/CMakeLists.txt +++ b/delegate/opaque/CMakeLists.txt @@ -10,6 +10,7 @@ list(APPEND armnnOpaqueDelegateObject_sources src/Activation.hpp src/ArgMinMax.hpp src/armnn_delegate.cpp + src/armnn_external_delegate.cpp src/BatchMatMul.hpp src/BatchSpace.hpp src/Comparison.hpp diff --git a/delegate/opaque/include/armnn_delegate.hpp b/delegate/opaque/include/armnn_delegate.hpp index bb6451f649..ff860c4b1c 100644 --- a/delegate/opaque/include/armnn_delegate.hpp +++ b/delegate/opaque/include/armnn_delegate.hpp @@ -6,6 +6,7 @@ #pragma once #include +#include #include #include @@ -86,8 +87,6 @@ static int TfLiteArmnnOpaqueDelegateErrno(TfLiteOpaqueDelegate* delegate) { retu /// In order for the delegate to be loaded by TfLite const TfLiteOpaqueDelegatePlugin* GetArmnnDelegatePluginApi(); -extern const TfLiteStableDelegate TFL_TheStableDelegate; - using tflite::delegates::DelegatePluginInterface; using TfLiteOpaqueDelegatePtr = tflite::delegates::TfLiteDelegatePtr; diff --git a/delegate/opaque/src/Fill.hpp b/delegate/opaque/src/Fill.hpp index e16969768e..a8cdf3a56f 100644 --- a/delegate/opaque/src/Fill.hpp +++ b/delegate/opaque/src/Fill.hpp @@ -2,3 +2,133 @@ // Copyright © 2023 Arm Ltd and Contributors. All rights reserved. // SPDX-License-Identifier: MIT // + +#pragma once + +#include + +namespace armnnOpaqueDelegate +{ + + TfLiteStatus VisitFillOperator(DelegateData& delegateData, + TfLiteOpaqueContext* tfLiteContext, + TfLiteOpaqueNode* tfLiteNode, + int nodeIndex, + int32_t tfLiteFillOperatorCode) + { + TF_LITE_ENSURE_STATUS(ValidateNumOutputs(tfLiteContext, tfLiteNode, 1, nodeIndex)); + + switch(tfLiteFillOperatorCode) + { + case kTfLiteBuiltinFill: + TF_LITE_ENSURE_STATUS(ValidateNumInputs(tfLiteContext, tfLiteNode, 2, nodeIndex)); + break; + default: + return kTfLiteError; + } + + // Inputs + int numInputs = 0; + const int* inputTensors; + if (TfLiteOpaqueNodeInputs(tfLiteNode, &inputTensors, &numInputs) != kTfLiteOk) + { + TF_LITE_OPAQUE_MAYBE_KERNEL_LOG( + tfLiteContext, + "TfLiteArmnnOpaqueDelegate: Unable to gather input tensor indices from node #%d: ", + nodeIndex); + return kTfLiteError; + } + + const TfLiteOpaqueTensor* tfLiteInputTensor = TfLiteOpaqueContextGetOpaqueTensor(tfLiteContext, + inputTensors[0]); + if (!IsValid(tfLiteContext, tfLiteInputTensor, tfLiteFillOperatorCode, nodeIndex)) + { + return kTfLiteError; + } + + const TfLiteOpaqueTensor* tfLiteFillTensor = TfLiteOpaqueContextGetOpaqueTensor(tfLiteContext, + inputTensors[1]); + if (!IsValid(tfLiteContext, tfLiteFillTensor, tfLiteFillOperatorCode, nodeIndex)) + { + return kTfLiteError; + } + + int numOutputs = 0; + const int* outputTensors; + if (TfLiteOpaqueNodeOutputs(tfLiteNode, &outputTensors, &numOutputs) != kTfLiteOk) + { + TF_LITE_OPAQUE_MAYBE_KERNEL_LOG( + tfLiteContext, + "TfLiteArmnnOpaqueDelegate: Unable to gather output tensor indices from node #%d: ", + nodeIndex); + return kTfLiteError; + } + + const TfLiteOpaqueTensor* tfLiteOutputTensor = TfLiteOpaqueContextGetOpaqueTensor(tfLiteContext, + outputTensors[0]); + if (!IsValid(tfLiteContext, tfLiteOutputTensor, tfLiteFillOperatorCode, nodeIndex)) + { + return kTfLiteError; + } + + armnn::TensorInfo inputTensorInfo = GetTensorInfoForTfLiteOpaqueTensor(tfLiteInputTensor); + const armnn::TensorInfo& outputTensorInfo = GetTensorInfoForTfLiteOpaqueTensor(tfLiteOutputTensor, true); + + armnn::FillDescriptor descriptor; + switch (TfLiteOpaqueTensorType(tfLiteFillTensor)) + { + case kTfLiteFloat32: + descriptor.m_Value = *static_cast(TfLiteOpaqueTensorData(tfLiteFillTensor)); + break; + case kTfLiteInt32: + descriptor.m_Value = *static_cast(TfLiteOpaqueTensorData(tfLiteFillTensor)); + break; + default: + TF_LITE_OPAQUE_MAYBE_KERNEL_LOG( + tfLiteContext, + "TfLiteArmnnOpaqueDelegate: FILL value data type is not supported in operator #%d node #%d: ", + tfLiteFillOperatorCode, nodeIndex); + return kTfLiteError; + } + + bool isSupported = false; + armnn::BackendId setBackend; + auto validateFunc = [&](const armnn::TensorInfo& outInfo, bool& isSupported) + { + FORWARD_LAYER_OPAQUE_SUPPORT_FUNC("FILL", + tfLiteContext, + IsFillSupported, + delegateData.m_Backends, + isSupported, + setBackend, + inputTensorInfo, + outInfo, + descriptor); + }; + + if (!delegateData.m_Network) + { + validateFunc(outputTensorInfo, isSupported); + return isSupported ? kTfLiteOk : kTfLiteError; + } + + armnn::IConnectableLayer* layer = delegateData.m_Network->AddFillLayer(descriptor); + layer->SetBackendId(setBackend); + ARMNN_ASSERT(layer != nullptr); + + armnn::IOutputSlot& outputSlot = layer->GetOutputSlot(0); + outputSlot.SetTensorInfo(outputTensorInfo); + + auto inputsTensorsProcess = ProcessInputs(layer, + delegateData, + tfLiteContext, + tfLiteNode); + if (inputsTensorsProcess == kTfLiteError) + { + return inputsTensorsProcess; + } + + return Connect(layer, tfLiteContext, tfLiteNode, delegateData); + } + +} // namespace armnnDelegate diff --git a/delegate/opaque/src/Split.hpp b/delegate/opaque/src/Split.hpp index 3840ed9620..d3d00e4d63 100644 --- a/delegate/opaque/src/Split.hpp +++ b/delegate/opaque/src/Split.hpp @@ -81,7 +81,8 @@ TfLiteStatus VisitSplitOperator(DelegateData& delegateData, // -1 == 3, -2 == 2, -3 == 1, -4 == 0 TF_LITE_OPAQUE_MAYBE_KERNEL_LOG( tfLiteContext, - "TfLiteArmnnDelegate: Operation has invalid axis: #%d. Axis must be in range [-n, n) in node #%d:", + "TfLiteOpaqueArmnnDelegate: Operation has invalid axis: #%d. " + "Axis must be in range [-n, n) in node #%d:", axis, nodeIndex); } const unsigned int splitDim = ComputeWrappedIndex(axis, inputTensorInfo.GetNumDimensions()); @@ -104,8 +105,9 @@ TfLiteStatus VisitSplitOperator(DelegateData& delegateData, { TF_LITE_OPAQUE_MAYBE_KERNEL_LOG( tfLiteContext, - "TfLiteArmnnDelegate: The number of dimensions: #%d for input tensors of the split op cannot be " - "greater than #%d in node #%d: ", inputDimSize, MaxNumOfTensorDimensions, nodeIndex); + "TfLiteOpaqueArmnnDelegate: The number of dimensions: #%d for input tensors of the split op cannot be " + "greater than #%d in node #%d: ", + inputDimSize, MaxNumOfTensorDimensions, nodeIndex); return kTfLiteError; } @@ -121,7 +123,7 @@ TfLiteStatus VisitSplitOperator(DelegateData& delegateData, { TF_LITE_OPAQUE_MAYBE_KERNEL_LOG( tfLiteContext, - "TfLiteArmnnDelegate: Number of splits #%d must evenly divide the dimension #%d in node #%d: ", + "TfLiteOpaqueArmnnDelegate: Number of splits #%d must evenly divide the dimension #%d in node #%d: ", numSplits, splitterDimSizes[splitDim], nodeIndex); return kTfLiteError; } @@ -165,10 +167,25 @@ TfLiteStatus VisitSplitOperator(DelegateData& delegateData, } // Connect the input slots - if(Connect(layer, tfLiteContext, tfLiteNode, delegateData) != kTfLiteOk) + delegateData.m_OutputSlotForNode[inputTensors[1]]->Connect(layer->GetInputSlot(0)); + + if(numSplits != static_cast(layer->GetNumOutputSlots())) { + TF_LITE_OPAQUE_MAYBE_KERNEL_LOG( + tfLiteContext, + "TfLiteOpaqueArmnnDelegate: Expected number of splits #%d does not " + "match the number of output slots #%d in node #%d: ", + numSplits, layer->GetNumOutputSlots(), nodeIndex); return kTfLiteError; } + + // Prepare output slots + for (unsigned int outputIndex = 0; outputIndex < layer->GetNumOutputSlots(); ++outputIndex) + { + armnn::IOutputSlot& outputSlot = layer->GetOutputSlot(outputIndex); + delegateData.m_OutputSlotForNode[ + static_cast(outputTensors[outputIndex])] = &outputSlot; + } return kTfLiteOk; } @@ -224,7 +241,8 @@ TfLiteStatus VisitSplitVOperator(DelegateData& delegateData, { TF_LITE_OPAQUE_MAYBE_KERNEL_LOG( tfLiteContext, - "TfLiteArmnnDelegate: Operation has invalid axis: #%d. Axis must be in range [-n, n) in node #%d:", + "TfLiteOpaqueArmnnDelegate: Operation has invalid axis: #%d. " + "Axis must be in range [-n, n) in node #%d:", axis, nodeIndex); } const unsigned int splitDim = ComputeWrappedIndex(axisTensorData[0], inputTensorInfo.GetNumDimensions()); @@ -243,7 +261,8 @@ TfLiteStatus VisitSplitVOperator(DelegateData& delegateData, if (numSplits <= 0) { TF_LITE_OPAQUE_MAYBE_KERNEL_LOG( - tfLiteContext, "TfLiteArmnnDelegate: Invalid number of splits %d in node #%d", + tfLiteContext, + "TfLiteOpaqueArmnnDelegate: Invalid number of splits %d in node #%d", numSplits, nodeIndex); return kTfLiteError; } @@ -278,8 +297,9 @@ TfLiteStatus VisitSplitVOperator(DelegateData& delegateData, { TF_LITE_OPAQUE_MAYBE_KERNEL_LOG( tfLiteContext, - "TfLiteArmnnDelegate: The number of dimensions: #%d for input tensors of the split op cannot be " - "greater than #%d in node #%d: ", inputDimSize, MaxNumOfTensorDimensions, nodeIndex); + "TfLiteOpaqueArmnnDelegate: The number of dimensions: #%d for input tensors of the split op cannot be " + "greater than #%d in node #%d: ", + inputDimSize, MaxNumOfTensorDimensions, nodeIndex); return kTfLiteError; } @@ -312,8 +332,10 @@ TfLiteStatus VisitSplitVOperator(DelegateData& delegateData, if (splitSum != armnn::numeric_cast(inputTensorInfo.GetShape()[splitDim])) { TF_LITE_OPAQUE_MAYBE_KERNEL_LOG( - tfLiteContext, "TfLiteArmnnDelegate: SplitV split_sizes does not sum to the dimension of value " - "along split_dim in node #%d", nodeIndex); + tfLiteContext, + "TfLiteOpaqueArmnnDelegate: SplitV split_sizes does not sum to the dimension " + "of value along split_dim in node #%d", + nodeIndex); return kTfLiteError; } } @@ -324,8 +346,9 @@ TfLiteStatus VisitSplitVOperator(DelegateData& delegateData, else { TF_LITE_OPAQUE_MAYBE_KERNEL_LOG( - tfLiteContext, "TfLiteArmnnDelegate: SplitV cannot infer split size for " - "more than one split in node #%d", + tfLiteContext, + "TfLiteOpaqueArmnnDelegate: SplitV cannot infer split size for " + "more than one split in node #%d", nodeIndex); return kTfLiteError; } diff --git a/delegate/opaque/src/armnn_delegate.cpp b/delegate/opaque/src/armnn_delegate.cpp index 45aff85346..f32a6f43b8 100644 --- a/delegate/opaque/src/armnn_delegate.cpp +++ b/delegate/opaque/src/armnn_delegate.cpp @@ -6,8 +6,6 @@ #include #include -#include - #include "Activation.hpp" #include "ArgMinMax.hpp" #include "BatchMatMul.hpp" @@ -50,7 +48,6 @@ #include #include #include -#include #include #include @@ -59,16 +56,8 @@ namespace armnnOpaqueDelegate { -const TfLiteStableDelegate TFL_TheStableDelegate = -{ - /*delegate_abi_version=*/ TFL_STABLE_DELEGATE_ABI_VERSION, - /*delegate_name=*/ "armnn_delegate", - /*delegate_version=*/ OPAQUE_DELEGATE_VERSION, - /*delegate_plugin=*/ GetArmnnDelegatePluginApi() -}; - static auto* g_delegate_plugin_ArmnnDelegatePlugin_ = - new tflite::delegates::DelegatePluginRegistry::Register(TFL_TheStableDelegate.delegate_name, + new tflite::delegates::DelegatePluginRegistry::Register("armnn_delegate", ArmnnDelegatePlugin::New); ArmnnOpaqueDelegate::ArmnnOpaqueDelegate(armnnDelegate::DelegateOptions options) @@ -126,7 +115,7 @@ TfLiteStatus DoPrepare(TfLiteOpaqueContext* tfLiteContext, TfLiteOpaqueDelegate* // ArmNN Opaque Delegate Registration TfLiteRegistrationExternal* kernelRegistration = TfLiteRegistrationExternalCreate(kTfLiteBuiltinDelegate, - TFL_TheStableDelegate.delegate_name, + "armnn_delegate", /*version=*/OPAQUE_DELEGATE_MAJOR_VERSION); if(kernelRegistration == nullptr) { @@ -227,13 +216,6 @@ void TfLiteArmnnOpaqueDelegateDelete(TfLiteOpaqueDelegate* tfLiteDelegate) } } -const TfLiteOpaqueDelegatePlugin* GetArmnnDelegatePluginApi() -{ - static constexpr TfLiteOpaqueDelegatePlugin armnnPlugin{ - TfLiteArmnnOpaqueDelegateCreate, TfLiteArmnnOpaqueDelegateDelete, TfLiteArmnnOpaqueDelegateErrno}; - return &armnnPlugin; -} - const std::string ArmnnOpaqueDelegate::GetVersion() { return OPAQUE_DELEGATE_VERSION; } @@ -776,6 +758,12 @@ TfLiteStatus ArmnnSubgraph::VisitNode(DelegateData& delegateData, tfLiteNode, nodeIndex, kTfLiteBuiltinExpandDims); + case kTfLiteBuiltinFill: + return VisitFillOperator(delegateData, + tfLiteContext, + tfLiteNode, + nodeIndex, + kTfLiteBuiltinFill); case kTfLiteBuiltinFloor: return VisitFloorOperator(delegateData, tfLiteContext, @@ -928,6 +916,12 @@ TfLiteStatus ArmnnSubgraph::VisitNode(DelegateData& delegateData, tfLiteNode, nodeIndex, kTfLiteBuiltinMinimum); + case kTfLiteBuiltinMirrorPad: + return VisitPadOperator(delegateData, + tfLiteContext, + tfLiteNode, + nodeIndex, + kTfLiteBuiltinMirrorPad); case kTfLiteBuiltinMul: return VisitElementwiseBinaryOperator(delegateData, tfLiteContext, diff --git a/delegate/opaque/src/armnn_external_delegate.cpp b/delegate/opaque/src/armnn_external_delegate.cpp index e16969768e..6cc29f3fe1 100644 --- a/delegate/opaque/src/armnn_external_delegate.cpp +++ b/delegate/opaque/src/armnn_external_delegate.cpp @@ -2,3 +2,42 @@ // Copyright © 2023 Arm Ltd and Contributors. All rights reserved. // SPDX-License-Identifier: MIT // + +#include + +namespace { + + TfLiteOpaqueDelegate* ArmNNDelegateCreateFunc(const void* tflite_settings) + { + auto delegate = armnnOpaqueDelegate::TfLiteArmnnOpaqueDelegateCreate(tflite_settings); + return delegate; + } + + void ArmNNDelegateDestroyFunc(TfLiteOpaqueDelegate* armnnDelegate) + { + armnnOpaqueDelegate::TfLiteArmnnOpaqueDelegateDelete( + armnnDelegate); + } + + int ArmNNDelegateErrnoFunc(TfLiteOpaqueDelegate* sample_stable_delegate) + { + return 0; + } + + const TfLiteOpaqueDelegatePlugin armnn_delegate_plugin = { + ArmNNDelegateCreateFunc, ArmNNDelegateDestroyFunc, + ArmNNDelegateErrnoFunc}; + + const TfLiteStableDelegate armnn_delegate = { + /*delegate_abi_version=*/ TFL_STABLE_DELEGATE_ABI_VERSION, + /*delegate_name=*/ "armnn_delegate", + /*delegate_version=*/ OPAQUE_DELEGATE_VERSION, + /*delegate_plugin=*/ &armnn_delegate_plugin + }; + +} // namespace + +/** + * The ArmNN delegate to be loaded dynamically + */ +extern "C" const TfLiteStableDelegate TFL_TheStableDelegate = armnn_delegate; \ No newline at end of file -- cgit v1.2.1