Ryan OShea | d21abaf | 2022-06-10 14:49:11 +0100 | [diff] [blame] | 1 | // |
Ryan OShea | 238ecd9 | 2023-03-07 11:44:23 +0000 | [diff] [blame^] | 2 | // Copyright © 2022-2023 Arm Ltd and Contributors. All rights reserved. |
Ryan OShea | d21abaf | 2022-06-10 14:49:11 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
| 8 | #include "TestUtils.hpp" |
| 9 | |
| 10 | #include <armnn_delegate.hpp> |
| 11 | |
| 12 | #include <flatbuffers/flatbuffers.h> |
| 13 | #include <flatbuffers/flexbuffers.h> |
| 14 | #include <tensorflow/lite/interpreter.h> |
| 15 | #include <tensorflow/lite/kernels/custom_ops_register.h> |
| 16 | #include <tensorflow/lite/kernels/register.h> |
| 17 | #include <tensorflow/lite/model.h> |
| 18 | #include <tensorflow/lite/schema/schema_generated.h> |
| 19 | #include <tensorflow/lite/version.h> |
| 20 | |
| 21 | #include <doctest/doctest.h> |
| 22 | |
| 23 | namespace |
| 24 | { |
| 25 | #if defined(ARMNN_POST_TFLITE_2_5) |
| 26 | |
| 27 | std::vector<uint8_t> CreateCustomOptions(int, int, int, int, int, int, TfLitePadding); |
| 28 | |
| 29 | std::vector<char> CreatePooling3dTfLiteModel( |
| 30 | std::string poolType, |
| 31 | tflite::TensorType tensorType, |
| 32 | const std::vector<int32_t>& inputTensorShape, |
| 33 | const std::vector<int32_t>& outputTensorShape, |
| 34 | TfLitePadding padding = kTfLitePaddingSame, |
| 35 | int32_t strideWidth = 0, |
| 36 | int32_t strideHeight = 0, |
| 37 | int32_t strideDepth = 0, |
| 38 | int32_t filterWidth = 0, |
| 39 | int32_t filterHeight = 0, |
| 40 | int32_t filterDepth = 0, |
| 41 | tflite::ActivationFunctionType fusedActivation = tflite::ActivationFunctionType_NONE, |
| 42 | float quantScale = 1.0f, |
| 43 | int quantOffset = 0) |
| 44 | { |
| 45 | using namespace tflite; |
| 46 | flatbuffers::FlatBufferBuilder flatBufferBuilder; |
| 47 | |
| 48 | std::vector<flatbuffers::Offset<tflite::Buffer>> buffers; |
Ryan OShea | 238ecd9 | 2023-03-07 11:44:23 +0000 | [diff] [blame^] | 49 | buffers.push_back(CreateBuffer(flatBufferBuilder)); |
| 50 | buffers.push_back(CreateBuffer(flatBufferBuilder)); |
| 51 | buffers.push_back(CreateBuffer(flatBufferBuilder)); |
| 52 | |
Ryan OShea | d21abaf | 2022-06-10 14:49:11 +0100 | [diff] [blame] | 53 | |
| 54 | auto quantizationParameters = |
| 55 | CreateQuantizationParameters(flatBufferBuilder, |
| 56 | 0, |
| 57 | 0, |
| 58 | flatBufferBuilder.CreateVector<float>({ quantScale }), |
| 59 | flatBufferBuilder.CreateVector<int64_t>({ quantOffset })); |
| 60 | |
| 61 | // Create the input and output tensors |
| 62 | std::array<flatbuffers::Offset<Tensor>, 2> tensors; |
| 63 | tensors[0] = CreateTensor(flatBufferBuilder, |
| 64 | flatBufferBuilder.CreateVector<int32_t>(inputTensorShape.data(), |
| 65 | inputTensorShape.size()), |
| 66 | tensorType, |
| 67 | 0, |
| 68 | flatBufferBuilder.CreateString("input"), |
| 69 | quantizationParameters); |
| 70 | |
| 71 | tensors[1] = CreateTensor(flatBufferBuilder, |
| 72 | flatBufferBuilder.CreateVector<int32_t>(outputTensorShape.data(), |
| 73 | outputTensorShape.size()), |
| 74 | tensorType, |
| 75 | 0, |
| 76 | flatBufferBuilder.CreateString("output"), |
| 77 | quantizationParameters); |
| 78 | |
| 79 | // Create the custom options from the function below |
| 80 | std::vector<uint8_t> customOperatorOptions = CreateCustomOptions(strideHeight, strideWidth, strideDepth, |
| 81 | filterHeight, filterWidth, filterDepth, padding); |
| 82 | // opCodeIndex is created as a uint8_t to avoid map lookup |
| 83 | uint8_t opCodeIndex = 0; |
| 84 | // Set the operator name based on the PoolType passed in from the test case |
| 85 | std::string opName = ""; |
| 86 | if (poolType == "kMax") |
| 87 | { |
| 88 | opName = "MaxPool3D"; |
| 89 | } |
| 90 | else |
| 91 | { |
| 92 | opName = "AveragePool3D"; |
| 93 | } |
| 94 | // To create a custom operator code you pass in the builtin code for custom operators and the name of the custom op |
| 95 | flatbuffers::Offset<OperatorCode> operatorCode = CreateOperatorCodeDirect(flatBufferBuilder, |
| 96 | tflite::BuiltinOperator_CUSTOM, |
| 97 | opName.c_str()); |
| 98 | |
| 99 | // Create the Operator using the opCodeIndex and custom options. Also sets builtin options to none. |
| 100 | const std::vector<int32_t> operatorInputs{ 0 }; |
| 101 | const std::vector<int32_t> operatorOutputs{ 1 }; |
| 102 | flatbuffers::Offset<Operator> poolingOperator = |
| 103 | CreateOperator(flatBufferBuilder, |
| 104 | opCodeIndex, |
| 105 | flatBufferBuilder.CreateVector<int32_t>(operatorInputs.data(), operatorInputs.size()), |
| 106 | flatBufferBuilder.CreateVector<int32_t>(operatorOutputs.data(), operatorOutputs.size()), |
| 107 | tflite::BuiltinOptions_NONE, |
| 108 | 0, |
| 109 | flatBufferBuilder.CreateVector<uint8_t>(customOperatorOptions), |
| 110 | tflite::CustomOptionsFormat_FLEXBUFFERS); |
| 111 | |
| 112 | // Create the subgraph using the operator created above. |
| 113 | const std::vector<int> subgraphInputs{ 0 }; |
| 114 | const std::vector<int> subgraphOutputs{ 1 }; |
| 115 | flatbuffers::Offset<SubGraph> subgraph = |
| 116 | CreateSubGraph(flatBufferBuilder, |
| 117 | flatBufferBuilder.CreateVector(tensors.data(), tensors.size()), |
| 118 | flatBufferBuilder.CreateVector<int32_t>(subgraphInputs.data(), subgraphInputs.size()), |
| 119 | flatBufferBuilder.CreateVector<int32_t>(subgraphOutputs.data(), subgraphOutputs.size()), |
| 120 | flatBufferBuilder.CreateVector(&poolingOperator, 1)); |
| 121 | |
| 122 | flatbuffers::Offset<flatbuffers::String> modelDescription = |
| 123 | flatBufferBuilder.CreateString("ArmnnDelegate: Pooling3d Operator Model"); |
| 124 | |
| 125 | // Create the model using operatorCode and the subgraph. |
| 126 | flatbuffers::Offset<Model> flatbufferModel = |
| 127 | CreateModel(flatBufferBuilder, |
| 128 | TFLITE_SCHEMA_VERSION, |
| 129 | flatBufferBuilder.CreateVector(&operatorCode, 1), |
| 130 | flatBufferBuilder.CreateVector(&subgraph, 1), |
| 131 | modelDescription, |
| 132 | flatBufferBuilder.CreateVector(buffers.data(), buffers.size())); |
| 133 | |
| 134 | flatBufferBuilder.Finish(flatbufferModel); |
| 135 | |
| 136 | return std::vector<char>(flatBufferBuilder.GetBufferPointer(), |
| 137 | flatBufferBuilder.GetBufferPointer() + flatBufferBuilder.GetSize()); |
| 138 | } |
| 139 | |
| 140 | template<typename T> |
| 141 | void Pooling3dTest(std::string poolType, |
| 142 | tflite::TensorType tensorType, |
| 143 | std::vector<armnn::BackendId>& backends, |
| 144 | std::vector<int32_t>& inputShape, |
| 145 | std::vector<int32_t>& outputShape, |
| 146 | std::vector<T>& inputValues, |
| 147 | std::vector<T>& expectedOutputValues, |
| 148 | TfLitePadding padding = kTfLitePaddingSame, |
| 149 | int32_t strideWidth = 0, |
| 150 | int32_t strideHeight = 0, |
| 151 | int32_t strideDepth = 0, |
| 152 | int32_t filterWidth = 0, |
| 153 | int32_t filterHeight = 0, |
| 154 | int32_t filterDepth = 0, |
| 155 | tflite::ActivationFunctionType fusedActivation = tflite::ActivationFunctionType_NONE, |
| 156 | float quantScale = 1.0f, |
| 157 | int quantOffset = 0) |
| 158 | { |
| 159 | using namespace tflite; |
| 160 | // Create the single op model buffer |
| 161 | std::vector<char> modelBuffer = CreatePooling3dTfLiteModel(poolType, |
| 162 | tensorType, |
| 163 | inputShape, |
| 164 | outputShape, |
| 165 | padding, |
| 166 | strideWidth, |
| 167 | strideHeight, |
| 168 | strideDepth, |
| 169 | filterWidth, |
| 170 | filterHeight, |
| 171 | filterDepth, |
| 172 | fusedActivation, |
| 173 | quantScale, |
| 174 | quantOffset); |
| 175 | |
| 176 | const Model* tfLiteModel = GetModel(modelBuffer.data()); |
| 177 | CHECK(tfLiteModel != nullptr); |
| 178 | // Create TfLite Interpreters |
| 179 | std::unique_ptr<Interpreter> armnnDelegateInterpreter; |
| 180 | |
| 181 | // Custom ops need to be added to the BuiltinOp resolver before the interpreter is created |
| 182 | // Based on the poolType from the test case add the custom operator using the name and the tflite |
| 183 | // registration function |
| 184 | tflite::ops::builtin::BuiltinOpResolver armnn_op_resolver; |
| 185 | if (poolType == "kMax") |
| 186 | { |
| 187 | armnn_op_resolver.AddCustom("MaxPool3D", tflite::ops::custom::Register_MAX_POOL_3D()); |
| 188 | } |
| 189 | else |
| 190 | { |
| 191 | armnn_op_resolver.AddCustom("AveragePool3D", tflite::ops::custom::Register_AVG_POOL_3D()); |
| 192 | } |
| 193 | |
| 194 | CHECK(InterpreterBuilder(tfLiteModel, armnn_op_resolver) |
| 195 | (&armnnDelegateInterpreter) == kTfLiteOk); |
| 196 | CHECK(armnnDelegateInterpreter != nullptr); |
| 197 | CHECK(armnnDelegateInterpreter->AllocateTensors() == kTfLiteOk); |
| 198 | |
| 199 | std::unique_ptr<Interpreter> tfLiteInterpreter; |
| 200 | |
| 201 | // Custom ops need to be added to the BuiltinOp resolver before the interpreter is created |
| 202 | // Based on the poolType from the test case add the custom operator using the name and the tflite |
| 203 | // registration function |
| 204 | tflite::ops::builtin::BuiltinOpResolver tflite_op_resolver; |
| 205 | if (poolType == "kMax") |
| 206 | { |
| 207 | tflite_op_resolver.AddCustom("MaxPool3D", tflite::ops::custom::Register_MAX_POOL_3D()); |
| 208 | } |
| 209 | else |
| 210 | { |
| 211 | tflite_op_resolver.AddCustom("AveragePool3D", tflite::ops::custom::Register_AVG_POOL_3D()); |
| 212 | } |
| 213 | |
| 214 | CHECK(InterpreterBuilder(tfLiteModel, tflite_op_resolver) |
| 215 | (&tfLiteInterpreter) == kTfLiteOk); |
| 216 | CHECK(tfLiteInterpreter != nullptr); |
| 217 | CHECK(tfLiteInterpreter->AllocateTensors() == kTfLiteOk); |
| 218 | |
| 219 | // Create the ArmNN Delegate |
| 220 | armnnDelegate::DelegateOptions delegateOptions(backends); |
| 221 | std::unique_ptr<TfLiteDelegate, decltype(&armnnDelegate::TfLiteArmnnDelegateDelete)> |
| 222 | theArmnnDelegate(armnnDelegate::TfLiteArmnnDelegateCreate(delegateOptions), |
| 223 | armnnDelegate::TfLiteArmnnDelegateDelete); |
| 224 | CHECK(theArmnnDelegate != nullptr); |
| 225 | |
| 226 | // Modify armnnDelegateInterpreter to use armnnDelegate |
| 227 | CHECK(armnnDelegateInterpreter->ModifyGraphWithDelegate(theArmnnDelegate.get()) == kTfLiteOk); |
| 228 | |
| 229 | // Set input data |
| 230 | auto tfLiteDelegateInputId = tfLiteInterpreter->inputs()[0]; |
| 231 | auto tfLiteDelegateInputData = tfLiteInterpreter->typed_tensor<T>(tfLiteDelegateInputId); |
| 232 | for (unsigned int i = 0; i < inputValues.size(); ++i) |
| 233 | { |
| 234 | tfLiteDelegateInputData[i] = inputValues[i]; |
| 235 | } |
| 236 | |
| 237 | auto armnnDelegateInputId = armnnDelegateInterpreter->inputs()[0]; |
| 238 | auto armnnDelegateInputData = armnnDelegateInterpreter->typed_tensor<T>(armnnDelegateInputId); |
| 239 | for (unsigned int i = 0; i < inputValues.size(); ++i) |
| 240 | { |
| 241 | armnnDelegateInputData[i] = inputValues[i]; |
| 242 | } |
| 243 | |
| 244 | // Run EnqueueWorkload |
| 245 | CHECK(tfLiteInterpreter->Invoke() == kTfLiteOk); |
| 246 | CHECK(armnnDelegateInterpreter->Invoke() == kTfLiteOk); |
| 247 | |
| 248 | armnnDelegate::CompareOutputData(tfLiteInterpreter, armnnDelegateInterpreter, outputShape, expectedOutputValues); |
| 249 | } |
| 250 | |
| 251 | // Function to create the flexbuffer custom options for the custom pooling3d operator. |
| 252 | std::vector<uint8_t> CreateCustomOptions(int strideHeight, int strideWidth, int strideDepth, |
| 253 | int filterHeight, int filterWidth, int filterDepth, TfLitePadding padding) |
| 254 | { |
| 255 | auto flex_builder = std::make_unique<flexbuffers::Builder>(); |
| 256 | size_t map_start = flex_builder->StartMap(); |
| 257 | flex_builder->String("data_format", "NDHWC"); |
| 258 | // Padding is created as a key and padding type. Only VALID and SAME supported |
| 259 | if (padding == kTfLitePaddingValid) |
| 260 | { |
| 261 | flex_builder->String("padding", "VALID"); |
| 262 | } |
| 263 | else |
| 264 | { |
| 265 | flex_builder->String("padding", "SAME"); |
| 266 | } |
| 267 | |
| 268 | // Vector of filter dimensions in order ( 1, Depth, Height, Width, 1 ) |
| 269 | auto start = flex_builder->StartVector("ksize"); |
| 270 | flex_builder->Add(1); |
| 271 | flex_builder->Add(filterDepth); |
| 272 | flex_builder->Add(filterHeight); |
| 273 | flex_builder->Add(filterWidth); |
| 274 | flex_builder->Add(1); |
| 275 | // EndVector( start, bool typed, bool fixed) |
| 276 | flex_builder->EndVector(start, true, false); |
| 277 | |
| 278 | // Vector of stride dimensions in order ( 1, Depth, Height, Width, 1 ) |
| 279 | auto stridesStart = flex_builder->StartVector("strides"); |
| 280 | flex_builder->Add(1); |
| 281 | flex_builder->Add(strideDepth); |
| 282 | flex_builder->Add(strideHeight); |
| 283 | flex_builder->Add(strideWidth); |
| 284 | flex_builder->Add(1); |
| 285 | // EndVector( stridesStart, bool typed, bool fixed) |
| 286 | flex_builder->EndVector(stridesStart, true, false); |
| 287 | |
| 288 | flex_builder->EndMap(map_start); |
| 289 | flex_builder->Finish(); |
| 290 | |
| 291 | return flex_builder->GetBuffer(); |
| 292 | } |
| 293 | #endif |
| 294 | } // anonymous namespace |
| 295 | |
| 296 | |
| 297 | |
| 298 | |