// // 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 { template std::vector CreateElementwiseBinaryTfLiteModel(tflite::BuiltinOperator binaryOperatorCode, tflite::ActivationFunctionType activationType, tflite::TensorType tensorType, const std::vector & input0TensorShape, const std::vector & input1TensorShape, const std::vector & outputTensorShape, std::vector& input1Values, bool constantInput = false, float quantScale = 1.0f, int quantOffset = 0) { using namespace tflite; flatbuffers::FlatBufferBuilder flatBufferBuilder; std::vector> buffers; buffers.push_back(CreateBuffer(flatBufferBuilder, flatBufferBuilder.CreateVector({}))); if (constantInput) { buffers.push_back( CreateBuffer(flatBufferBuilder, flatBufferBuilder.CreateVector(reinterpret_cast(input1Values.data()), sizeof(T) * input1Values.size()))); } else { buffers.push_back(CreateBuffer(flatBufferBuilder, flatBufferBuilder.CreateVector({}))); } 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(input0TensorShape.data(), input0TensorShape.size()), tensorType, 0, flatBufferBuilder.CreateString("input_0"), quantizationParameters); tensors[1] = CreateTensor(flatBufferBuilder, flatBufferBuilder.CreateVector(input1TensorShape.data(), input1TensorShape.size()), tensorType, 1, flatBufferBuilder.CreateString("input_1"), quantizationParameters); tensors[2] = CreateTensor(flatBufferBuilder, flatBufferBuilder.CreateVector(outputTensorShape.data(), outputTensorShape.size()), tensorType, 2, flatBufferBuilder.CreateString("output"), quantizationParameters); // create operator tflite::BuiltinOptions operatorBuiltinOptionsType = tflite::BuiltinOptions_NONE; flatbuffers::Offset operatorBuiltinOptions = 0; switch (binaryOperatorCode) { case BuiltinOperator_ADD: { operatorBuiltinOptionsType = BuiltinOptions_AddOptions; operatorBuiltinOptions = CreateAddOptions(flatBufferBuilder, activationType).Union(); break; } case BuiltinOperator_DIV: { operatorBuiltinOptionsType = BuiltinOptions_DivOptions; operatorBuiltinOptions = CreateDivOptions(flatBufferBuilder, activationType).Union(); break; } case BuiltinOperator_MAXIMUM: { operatorBuiltinOptionsType = BuiltinOptions_MaximumMinimumOptions; operatorBuiltinOptions = CreateMaximumMinimumOptions(flatBufferBuilder).Union(); break; } case BuiltinOperator_MINIMUM: { operatorBuiltinOptionsType = BuiltinOptions_MaximumMinimumOptions; operatorBuiltinOptions = CreateMaximumMinimumOptions(flatBufferBuilder).Union(); break; } case BuiltinOperator_MUL: { operatorBuiltinOptionsType = BuiltinOptions_MulOptions; operatorBuiltinOptions = CreateMulOptions(flatBufferBuilder, activationType).Union(); break; } case BuiltinOperator_SUB: { operatorBuiltinOptionsType = BuiltinOptions_SubOptions; operatorBuiltinOptions = CreateSubOptions(flatBufferBuilder, activationType).Union(); break; } case BuiltinOperator_FLOOR_DIV: { operatorBuiltinOptionsType = tflite::BuiltinOptions_FloorDivOptions; operatorBuiltinOptions = CreateSubOptions(flatBufferBuilder, activationType).Union(); break; } default: break; } const std::vector operatorInputs{0, 1}; const std::vector operatorOutputs{2}; flatbuffers::Offset elementwiseBinaryOperator = 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(&elementwiseBinaryOperator, 1)); flatbuffers::Offset modelDescription = flatBufferBuilder.CreateString("ArmnnDelegate: Elementwise Binary Operator Model"); flatbuffers::Offset operatorCode = CreateOperatorCode(flatBufferBuilder, binaryOperatorCode); 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 ElementwiseBinaryTest(tflite::BuiltinOperator binaryOperatorCode, tflite::ActivationFunctionType activationType, tflite::TensorType tensorType, std::vector& backends, std::vector& input0Shape, std::vector& input1Shape, std::vector& outputShape, std::vector& input0Values, std::vector& input1Values, std::vector& expectedOutputValues, float quantScale = 1.0f, int quantOffset = 0, bool constantInput = false) { using namespace tflite; std::vector modelBuffer = CreateElementwiseBinaryTfLiteModel(binaryOperatorCode, activationType, tensorType, input0Shape, input1Shape, outputShape, input1Values, constantInput, quantScale, quantOffset); 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 armnnDelegate::FillInput(tfLiteInterpreter, 0, input0Values); armnnDelegate::FillInput(armnnDelegateInterpreter, 0, input0Values); if (!constantInput) { armnnDelegate::FillInput(tfLiteInterpreter, 1, input1Values); armnnDelegate::FillInput(armnnDelegateInterpreter, 1, input1Values); } // Run EnqueWorkload CHECK(tfLiteInterpreter->Invoke() == kTfLiteOk); CHECK(armnnDelegateInterpreter->Invoke() == kTfLiteOk); // Compare output data armnnDelegate::CompareOutputData(tfLiteInterpreter, armnnDelegateInterpreter, outputShape, expectedOutputValues); armnnDelegateInterpreter.reset(nullptr); } } // anonymous namespace