// // Copyright © 2022-2024 Arm Ltd and Contributors. All rights reserved. // SPDX-License-Identifier: MIT // #pragma once #include "TestUtils.hpp" #include #include #include #include namespace { #if defined(ARMNN_POST_TFLITE_2_5) std::vector CreateCustomOptions(int, int, int, int, int, int, TfLitePadding); std::vector CreatePooling3dTfLiteModel( std::string poolType, tflite::TensorType tensorType, const std::vector& inputTensorShape, const std::vector& outputTensorShape, TfLitePadding padding = kTfLitePaddingSame, int32_t strideWidth = 0, int32_t strideHeight = 0, int32_t strideDepth = 0, int32_t filterWidth = 0, int32_t filterHeight = 0, int32_t filterDepth = 0, tflite::ActivationFunctionType fusedActivation = tflite::ActivationFunctionType_NONE, float quantScale = 1.0f, int quantOffset = 0) { using namespace tflite; flatbuffers::FlatBufferBuilder flatBufferBuilder; std::vector> buffers; buffers.push_back(CreateBuffer(flatBufferBuilder)); buffers.push_back(CreateBuffer(flatBufferBuilder)); buffers.push_back(CreateBuffer(flatBufferBuilder)); auto quantizationParameters = CreateQuantizationParameters(flatBufferBuilder, 0, 0, flatBufferBuilder.CreateVector({ quantScale }), flatBufferBuilder.CreateVector({ quantOffset })); // Create the input and output tensors std::array, 2> tensors; tensors[0] = CreateTensor(flatBufferBuilder, flatBufferBuilder.CreateVector(inputTensorShape.data(), inputTensorShape.size()), tensorType, 0, flatBufferBuilder.CreateString("input"), quantizationParameters); tensors[1] = CreateTensor(flatBufferBuilder, flatBufferBuilder.CreateVector(outputTensorShape.data(), outputTensorShape.size()), tensorType, 0, flatBufferBuilder.CreateString("output"), quantizationParameters); // Create the custom options from the function below std::vector customOperatorOptions = CreateCustomOptions(strideHeight, strideWidth, strideDepth, filterHeight, filterWidth, filterDepth, padding); // opCodeIndex is created as a uint8_t to avoid map lookup uint8_t opCodeIndex = 0; // Set the operator name based on the PoolType passed in from the test case std::string opName = ""; if (poolType == "kMax") { opName = "MaxPool3D"; } else { opName = "AveragePool3D"; } // To create a custom operator code you pass in the builtin code for custom operators and the name of the custom op flatbuffers::Offset operatorCode = CreateOperatorCodeDirect(flatBufferBuilder, tflite::BuiltinOperator_CUSTOM, opName.c_str()); // Create the Operator using the opCodeIndex and custom options. Also sets builtin options to none. const std::vector operatorInputs{ 0 }; const std::vector operatorOutputs{ 1 }; flatbuffers::Offset poolingOperator = CreateOperator(flatBufferBuilder, opCodeIndex, flatBufferBuilder.CreateVector(operatorInputs.data(), operatorInputs.size()), flatBufferBuilder.CreateVector(operatorOutputs.data(), operatorOutputs.size()), tflite::BuiltinOptions_NONE, 0, flatBufferBuilder.CreateVector(customOperatorOptions), tflite::CustomOptionsFormat_FLEXBUFFERS); // Create the subgraph using the operator created above. const std::vector subgraphInputs{ 0 }; const std::vector subgraphOutputs{ 1 }; 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(&poolingOperator, 1)); flatbuffers::Offset modelDescription = flatBufferBuilder.CreateString("ArmnnDelegate: Pooling3d Operator Model"); // Create the model using operatorCode and the subgraph. 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, armnnDelegate::FILE_IDENTIFIER); return std::vector(flatBufferBuilder.GetBufferPointer(), flatBufferBuilder.GetBufferPointer() + flatBufferBuilder.GetSize()); } template void Pooling3dTest(std::string poolType, tflite::TensorType tensorType, std::vector& inputShape, std::vector& outputShape, std::vector& inputValues, std::vector& expectedOutputValues, const std::vector& backends = {}, TfLitePadding padding = kTfLitePaddingSame, int32_t strideWidth = 0, int32_t strideHeight = 0, int32_t strideDepth = 0, int32_t filterWidth = 0, int32_t filterHeight = 0, int32_t filterDepth = 0, tflite::ActivationFunctionType fusedActivation = tflite::ActivationFunctionType_NONE, float quantScale = 1.0f, int quantOffset = 0) { using namespace delegateTestInterpreter; // Create the single op model buffer std::vector modelBuffer = CreatePooling3dTfLiteModel(poolType, tensorType, inputShape, outputShape, padding, strideWidth, strideHeight, strideDepth, filterWidth, filterHeight, filterDepth, fusedActivation, quantScale, quantOffset); std::string opType = ""; if (poolType == "kMax") { opType = "MaxPool3D"; } else { opType = "AveragePool3D"; } // Setup interpreter with just TFLite Runtime. auto tfLiteInterpreter = DelegateTestInterpreter(modelBuffer, opType); CHECK(tfLiteInterpreter.AllocateTensors() == kTfLiteOk); CHECK(tfLiteInterpreter.FillInputTensor(inputValues, 0) == 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, CaptureAvailableBackends(backends), opType); CHECK(armnnInterpreter.AllocateTensors() == kTfLiteOk); CHECK(armnnInterpreter.FillInputTensor(inputValues, 0) == 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(); } // Function to create the flexbuffer custom options for the custom pooling3d operator. std::vector CreateCustomOptions(int strideHeight, int strideWidth, int strideDepth, int filterHeight, int filterWidth, int filterDepth, TfLitePadding padding) { auto flex_builder = std::make_unique(); size_t map_start = flex_builder->StartMap(); flex_builder->String("data_format", "NDHWC"); // Padding is created as a key and padding type. Only VALID and SAME supported if (padding == kTfLitePaddingValid) { flex_builder->String("padding", "VALID"); } else { flex_builder->String("padding", "SAME"); } // Vector of filter dimensions in order ( 1, Depth, Height, Width, 1 ) auto start = flex_builder->StartVector("ksize"); flex_builder->Add(1); flex_builder->Add(filterDepth); flex_builder->Add(filterHeight); flex_builder->Add(filterWidth); flex_builder->Add(1); // EndVector( start, bool typed, bool fixed) flex_builder->EndVector(start, true, false); // Vector of stride dimensions in order ( 1, Depth, Height, Width, 1 ) auto stridesStart = flex_builder->StartVector("strides"); flex_builder->Add(1); flex_builder->Add(strideDepth); flex_builder->Add(strideHeight); flex_builder->Add(strideWidth); flex_builder->Add(1); // EndVector( stridesStart, bool typed, bool fixed) flex_builder->EndVector(stridesStart, true, false); flex_builder->EndMap(map_start); flex_builder->Finish(); return flex_builder->GetBuffer(); } #endif } // anonymous namespace