From 62483bee640e7d8accf6ac77b24c6e9828841851 Mon Sep 17 00:00:00 2001 From: Sadik Armagan Date: Fri, 23 Oct 2020 17:14:43 +0100 Subject: IVGCVSW-5366 'Add a do nothing SubGraph class' IVGCVSW-5373 'Implement the ABS operator in the Delegate' * Added a Switch statement into the VisitNode() function * Separated the Visit functions into the categorized source files * Implemented VisitElementwiseUnary() function * Added tests for ABS and SQRT Signed-off-by: Sadik Armagan Change-Id: If9654d0a8d8ff7dcd6fb5cbe0dc312941772affb --- delegate/src/test/AbsTest.cpp | 98 +++++++++++++++++++++++++++++++++ delegate/src/test/ArmnnDelegateTest.cpp | 12 ++-- delegate/src/test/SqrtTest.cpp | 97 ++++++++++++++++++++++++++++++++ 3 files changed, 199 insertions(+), 8 deletions(-) create mode 100644 delegate/src/test/AbsTest.cpp create mode 100644 delegate/src/test/SqrtTest.cpp (limited to 'delegate/src/test') diff --git a/delegate/src/test/AbsTest.cpp b/delegate/src/test/AbsTest.cpp new file mode 100644 index 0000000000..f9c345e6d2 --- /dev/null +++ b/delegate/src/test/AbsTest.cpp @@ -0,0 +1,98 @@ +// +// Copyright © 2020 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#include "ElementwiseUnaryTestHelper.hpp" + +#include + +#include +#include +#include +#include +#include +#include + +#include + +namespace armnnDelegate +{ + +TEST_SUITE("AbsTest") +{ + +TEST_CASE ("AbsTestFloat32") +{ + using namespace tflite; + + const std::vector inputShape { { 3, 1, 2} }; + std::vector modelBuffer = CreateElementwiseUnaryTfLiteModel(BuiltinOperator_ABS, + ::tflite::TensorType_FLOAT32, + inputShape); + 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 + auto delegateOptions = TfLiteArmnnDelegateOptionsDefault(); + auto armnnDelegate = TfLiteArmnnDelegateCreate(delegateOptions); + CHECK(armnnDelegate != nullptr); + // Modify armnnDelegateInterpreter to use armnnDelegate + CHECK(armnnDelegateInterpreter->ModifyGraphWithDelegate(armnnDelegate) == kTfLiteOk); + + // Set input data + std::vector inputValues + { + -0.1f, -0.2f, -0.3f, + 0.1f, 0.2f, 0.3f + }; + auto tfLiteDelegateInputId = tfLiteInterpreter->inputs()[0]; + auto tfLiteDelageInputData = tfLiteInterpreter->typed_tensor(tfLiteDelegateInputId); + for (unsigned int i = 0; i < inputValues.size(); ++i) + { + tfLiteDelageInputData[i] = inputValues[i]; + } + + auto armnnDelegateInputId = armnnDelegateInterpreter->inputs()[0]; + auto armnnDelegateInputData = armnnDelegateInterpreter->typed_tensor(armnnDelegateInputId); + for (unsigned int i = 0; i < inputValues.size(); ++i) + { + armnnDelegateInputData[i] = inputValues[i]; + } + + // Run EnqueWorkload + CHECK(tfLiteInterpreter->Invoke() == kTfLiteOk); + CHECK(armnnDelegateInterpreter->Invoke() == kTfLiteOk); + + // Compare output data + auto tfLiteDelegateOutputId = tfLiteInterpreter->outputs()[0]; + auto tfLiteDelageOutputData = tfLiteInterpreter->typed_tensor(tfLiteDelegateOutputId); + + auto armnnDelegateOutputId = armnnDelegateInterpreter->outputs()[0]; + auto armnnDelegateOutputData = armnnDelegateInterpreter->typed_tensor(armnnDelegateOutputId); + + for (size_t i = 0; i < inputValues.size(); i++) + { + CHECK(std::abs(inputValues[i]) == armnnDelegateOutputData[i]); + CHECK(tfLiteDelageOutputData[i] == armnnDelegateOutputData[i]); + } +} + +} + +} // namespace armnnDelegate + + + diff --git a/delegate/src/test/ArmnnDelegateTest.cpp b/delegate/src/test/ArmnnDelegateTest.cpp index 8bd58f6286..fdf786ff99 100644 --- a/delegate/src/test/ArmnnDelegateTest.cpp +++ b/delegate/src/test/ArmnnDelegateTest.cpp @@ -3,20 +3,17 @@ // SPDX-License-Identifier: MIT // -#include - -#ifndef DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN #define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN -#endif #include +#include + #include "tensorflow/lite/kernels/builtin_op_kernels.h" #include -namespace +namespace armnnDelegate { - TEST_SUITE("ArmnnDelegate") { @@ -50,5 +47,4 @@ TEST_CASE ("ArmnnDelegate Registered") } -} // anonymous namespace - +} // namespace armnnDelegate diff --git a/delegate/src/test/SqrtTest.cpp b/delegate/src/test/SqrtTest.cpp new file mode 100644 index 0000000000..df3534dcdb --- /dev/null +++ b/delegate/src/test/SqrtTest.cpp @@ -0,0 +1,97 @@ +// +// Copyright © 2020 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#include "ElementwiseUnaryTestHelper.hpp" + +#include + +#include +#include +#include +#include +#include +#include + +#include + +namespace armnnDelegate +{ + +TEST_SUITE("SqrtTest") +{ + +TEST_CASE ("SqrtTestFloat32") +{ + using namespace tflite; + const std::vector inputShape { { 3, 1, 2} }; + std::vector modelBuffer = CreateElementwiseUnaryTfLiteModel(BuiltinOperator_SQRT, + ::tflite::TensorType_FLOAT32, + inputShape); + + 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 + auto delegateOptions = TfLiteArmnnDelegateOptionsDefault(); + auto armnnDelegate = TfLiteArmnnDelegateCreate(delegateOptions); + CHECK(armnnDelegate != nullptr); + // Modify armnnDelegateInterpreter to use armnnDelegate + CHECK(armnnDelegateInterpreter->ModifyGraphWithDelegate(armnnDelegate) == kTfLiteOk); + + // Set input data + std::vector inputValues + { + 9.0f, 4.25f, 81.9f, + 0.1f, 0.9f, 169.0f + }; + + auto tfLiteDelegateInputId = tfLiteInterpreter->inputs()[0]; + auto tfLiteDelageInputData = tfLiteInterpreter->typed_tensor(tfLiteDelegateInputId); + for (unsigned int i = 0; i < inputValues.size(); ++i) + { + tfLiteDelageInputData[i] = inputValues[i]; + } + + auto armnnDelegateInputId = armnnDelegateInterpreter->inputs()[0]; + auto armnnDelegateInputData = armnnDelegateInterpreter->typed_tensor(armnnDelegateInputId); + for (unsigned int i = 0; i < inputValues.size(); ++i) + { + armnnDelegateInputData[i] = inputValues[i]; + } + + // Run EnqueWorkload + CHECK(tfLiteInterpreter->Invoke() == kTfLiteOk); + CHECK(armnnDelegateInterpreter->Invoke() == kTfLiteOk); + + // Compare output data + auto tfLiteDelegateOutputId = tfLiteInterpreter->outputs()[0]; + auto tfLiteDelageOutputData = tfLiteInterpreter->typed_tensor(tfLiteDelegateOutputId); + auto armnnDelegateOutputId = armnnDelegateInterpreter->outputs()[0]; + auto armnnDelegateOutputData = armnnDelegateInterpreter->typed_tensor(armnnDelegateOutputId); + for (size_t i = 0; i < inputValues.size(); i++) + { + CHECK(std::sqrt(inputValues[i]) == armnnDelegateOutputData[i]); + CHECK(tfLiteDelageOutputData[i] == armnnDelegateOutputData[i]); + } + +} + +} + +} // namespace armnnDelegate + + + -- cgit v1.2.1