From ebe392df1635790bf21714549adb97f2f75559e1 Mon Sep 17 00:00:00 2001 From: Matthew Sloyan Date: Thu, 30 Mar 2023 10:12:08 +0100 Subject: IVGCVSW-7562 Implement DelegateTestInterpreter for classic delegate * Updated all tests to use new DelegateTestInterpreter. * Fixed some unit tests where the shape was incorrect. * Add file identifier to FlatBuffersBuilder, as it is required for validation when creating the model using new API. Signed-off-by: Matthew Sloyan Change-Id: I1c4f5464367b35d4528571fa94d14bfaef18fb4d --- delegate/test/TransposeTestHelper.hpp | 129 +++++++++++----------------------- 1 file changed, 40 insertions(+), 89 deletions(-) (limited to 'delegate/test/TransposeTestHelper.hpp') diff --git a/delegate/test/TransposeTestHelper.hpp b/delegate/test/TransposeTestHelper.hpp index 99bb60b91a..57f4e291bf 100644 --- a/delegate/test/TransposeTestHelper.hpp +++ b/delegate/test/TransposeTestHelper.hpp @@ -5,15 +5,17 @@ #pragma once +#include "TestUtils.hpp" + #include +#include #include -#include #include -#include -#include #include +#include + #include namespace @@ -76,102 +78,51 @@ std::vector CreateTransposeTfLiteModel(tflite::TensorType tensorType, flatBufferBuilder.CreateVector(&subgraph, 1), modelDescription, flatBufferBuilder.CreateVector(buffers, 4)); - flatBufferBuilder.Finish(flatbufferModel); + flatBufferBuilder.Finish(flatbufferModel, armnnDelegate::FILE_IDENTIFIER); return std::vector(flatBufferBuilder.GetBufferPointer(), flatBufferBuilder.GetBufferPointer() + flatBufferBuilder.GetSize()); } -void TransposeFP32Test(std::vector& backends) +template +void TransposeTest(std::vector& backends, + std::vector& inputShape, + std::vector& inputPermVecShape, + std::vector& outputShape, + std::vector& inputValues, + std::vector& inputPermVec, + std::vector& expectedOutputValues) { - using namespace tflite; - - // set test input data - std::vector input0Shape {4, 2, 3}; - std::vector inputPermVecShape {3}; - std::vector outputShape {2, 3, 4}; + using namespace delegateTestInterpreter; - std::vector input0Values = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, - 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}; - std::vector inputPermVec = {2, 0, 1}; - std::vector expectedOutputValues = {0, 3, 6, 9, 12, 15, 18, 21, 1, 4, 7, 10, - 13, 16, 19, 22, 2, 5, 8, 11, 14, 17, 20, 23}; - - // create model + // Create model std::vector modelBuffer = CreateTransposeTfLiteModel(::tflite::TensorType_FLOAT32, - input0Shape, + inputShape, inputPermVecShape, outputShape, inputPermVec); - const Model* tfLiteModel = GetModel(modelBuffer.data()); - // Create TfLite Interpreters - std::unique_ptr armnnDelegateInterpreter; - CHECK(InterpreterBuilder(tfLiteModel, ::tflite::ops::builtin::BuiltinOpResolver()) - (&armnnDelegateInterpreter) == kTfLiteOk); - CHECK(armnnDelegateInterpreter != nullptr); - CHECK(armnnDelegateInterpreter->AllocateTensors() == kTfLiteOk); - - std::unique_ptr tfLiteInterpreter; - CHECK(InterpreterBuilder(tfLiteModel, ::tflite::ops::builtin::BuiltinOpResolver()) - (&tfLiteInterpreter) == kTfLiteOk); - CHECK(tfLiteInterpreter != nullptr); - CHECK(tfLiteInterpreter->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(armnnDelegateInterpreter->ModifyGraphWithDelegate(theArmnnDelegate.get()) == kTfLiteOk); - - // Set input data for tflite - auto tfLiteInterpreterInput0Id = tfLiteInterpreter->inputs()[0]; - auto tfLiteInterpreterInput0Data = tfLiteInterpreter->typed_tensor(tfLiteInterpreterInput0Id); - for (unsigned int i = 0; i < input0Values.size(); ++i) - { - tfLiteInterpreterInput0Data[i] = input0Values[i]; - } - - auto tfLiteInterpreterInput1Id = tfLiteInterpreter->inputs()[1]; - auto tfLiteInterpreterInput1Data = tfLiteInterpreter->typed_tensor(tfLiteInterpreterInput1Id); - for (unsigned int i = 0; i < inputPermVec.size(); ++i) - { - tfLiteInterpreterInput1Data[i] = inputPermVec[i]; - } - - //Set input data for armnn delegate - auto armnnDelegateInput0Id = armnnDelegateInterpreter->inputs()[0]; - auto armnnDelegateInput0Data = armnnDelegateInterpreter->typed_tensor(armnnDelegateInput0Id); - for (unsigned int i = 0; i < input0Values.size(); ++i) - { - armnnDelegateInput0Data[i] = input0Values[i]; - } - - auto armnnDelegateInput1Id = armnnDelegateInterpreter->inputs()[1]; - auto armnnDelegateInput1Data = armnnDelegateInterpreter->typed_tensor(armnnDelegateInput1Id); - for (unsigned int i = 0; i < inputPermVec.size(); ++i) - { - armnnDelegateInput1Data[i] = inputPermVec[i]; - } - - // Run EnqueWorkload - CHECK(tfLiteInterpreter->Invoke() == kTfLiteOk); - CHECK(armnnDelegateInterpreter->Invoke() == kTfLiteOk); - - // Compare output data - auto tfLiteInterpreterOutputId = tfLiteInterpreter->outputs()[0]; - auto tfLiteInterpreterOutputData = tfLiteInterpreter->typed_tensor(tfLiteInterpreterOutputId); - auto armnnDelegateOutputId = armnnDelegateInterpreter->outputs()[0]; - auto armnnDelegateOutputData = armnnDelegateInterpreter->typed_tensor(armnnDelegateOutputId); - for (size_t i = 0; i < expectedOutputValues.size(); ++i) - { - CHECK(expectedOutputValues[i] == armnnDelegateOutputData[i]); - CHECK(tfLiteInterpreterOutputData[i] == expectedOutputValues[i]); - CHECK(tfLiteInterpreterOutputData[i] == armnnDelegateOutputData[i]); - } - - armnnDelegateInterpreter.reset(nullptr); + // Setup interpreter with just TFLite Runtime. + auto tfLiteInterpreter = DelegateTestInterpreter(modelBuffer); + CHECK(tfLiteInterpreter.AllocateTensors() == kTfLiteOk); + CHECK(tfLiteInterpreter.FillInputTensor(inputValues, 0) == kTfLiteOk); + CHECK(tfLiteInterpreter.FillInputTensor(inputPermVec, 1) == kTfLiteOk); + CHECK(tfLiteInterpreter.Invoke() == kTfLiteOk); + std::vector tfLiteOutputValues = tfLiteInterpreter.GetOutputResult(0); + std::vector tfLiteOutputShape = tfLiteInterpreter.GetOutputShape(0); + + // Setup interpreter with Arm NN Delegate applied. + auto armnnInterpreter = DelegateTestInterpreter(modelBuffer, backends); + CHECK(armnnInterpreter.AllocateTensors() == kTfLiteOk); + CHECK(armnnInterpreter.FillInputTensor(inputValues, 0) == kTfLiteOk); + CHECK(armnnInterpreter.FillInputTensor(inputPermVec, 1) == kTfLiteOk); + CHECK(armnnInterpreter.Invoke() == kTfLiteOk); + std::vector armnnOutputValues = armnnInterpreter.GetOutputResult(0); + std::vector armnnOutputShape = armnnInterpreter.GetOutputShape(0); + + armnnDelegate::CompareOutputData(tfLiteOutputValues, armnnOutputValues, expectedOutputValues); + armnnDelegate::CompareOutputShape(tfLiteOutputShape, armnnOutputShape, outputShape); + + tfLiteInterpreter.Cleanup(); + armnnInterpreter.Cleanup(); } } -- cgit v1.2.1