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> |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 11 | #include <DelegateTestInterpreter.hpp> |
Ryan OShea | d21abaf | 2022-06-10 14:49:11 +0100 | [diff] [blame] | 12 | |
| 13 | #include <flatbuffers/flatbuffers.h> |
| 14 | #include <flatbuffers/flexbuffers.h> |
Ryan OShea | d21abaf | 2022-06-10 14:49:11 +0100 | [diff] [blame] | 15 | #include <tensorflow/lite/kernels/register.h> |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 16 | #include <tensorflow/lite/kernels/custom_ops_register.h> |
Ryan OShea | d21abaf | 2022-06-10 14:49:11 +0100 | [diff] [blame] | 17 | #include <tensorflow/lite/version.h> |
| 18 | |
| 19 | #include <doctest/doctest.h> |
| 20 | |
| 21 | namespace |
| 22 | { |
| 23 | #if defined(ARMNN_POST_TFLITE_2_5) |
| 24 | |
| 25 | std::vector<uint8_t> CreateCustomOptions(int, int, int, int, int, int, TfLitePadding); |
| 26 | |
| 27 | std::vector<char> CreatePooling3dTfLiteModel( |
| 28 | std::string poolType, |
| 29 | tflite::TensorType tensorType, |
| 30 | const std::vector<int32_t>& inputTensorShape, |
| 31 | const std::vector<int32_t>& outputTensorShape, |
| 32 | TfLitePadding padding = kTfLitePaddingSame, |
| 33 | int32_t strideWidth = 0, |
| 34 | int32_t strideHeight = 0, |
| 35 | int32_t strideDepth = 0, |
| 36 | int32_t filterWidth = 0, |
| 37 | int32_t filterHeight = 0, |
| 38 | int32_t filterDepth = 0, |
| 39 | tflite::ActivationFunctionType fusedActivation = tflite::ActivationFunctionType_NONE, |
| 40 | float quantScale = 1.0f, |
| 41 | int quantOffset = 0) |
| 42 | { |
| 43 | using namespace tflite; |
| 44 | flatbuffers::FlatBufferBuilder flatBufferBuilder; |
| 45 | |
| 46 | std::vector<flatbuffers::Offset<tflite::Buffer>> buffers; |
Ryan OShea | 238ecd9 | 2023-03-07 11:44:23 +0000 | [diff] [blame] | 47 | buffers.push_back(CreateBuffer(flatBufferBuilder)); |
| 48 | buffers.push_back(CreateBuffer(flatBufferBuilder)); |
| 49 | buffers.push_back(CreateBuffer(flatBufferBuilder)); |
| 50 | |
Ryan OShea | d21abaf | 2022-06-10 14:49:11 +0100 | [diff] [blame] | 51 | |
| 52 | auto quantizationParameters = |
| 53 | CreateQuantizationParameters(flatBufferBuilder, |
| 54 | 0, |
| 55 | 0, |
| 56 | flatBufferBuilder.CreateVector<float>({ quantScale }), |
| 57 | flatBufferBuilder.CreateVector<int64_t>({ quantOffset })); |
| 58 | |
| 59 | // Create the input and output tensors |
| 60 | std::array<flatbuffers::Offset<Tensor>, 2> tensors; |
| 61 | tensors[0] = CreateTensor(flatBufferBuilder, |
| 62 | flatBufferBuilder.CreateVector<int32_t>(inputTensorShape.data(), |
| 63 | inputTensorShape.size()), |
| 64 | tensorType, |
| 65 | 0, |
| 66 | flatBufferBuilder.CreateString("input"), |
| 67 | quantizationParameters); |
| 68 | |
| 69 | tensors[1] = CreateTensor(flatBufferBuilder, |
| 70 | flatBufferBuilder.CreateVector<int32_t>(outputTensorShape.data(), |
| 71 | outputTensorShape.size()), |
| 72 | tensorType, |
| 73 | 0, |
| 74 | flatBufferBuilder.CreateString("output"), |
| 75 | quantizationParameters); |
| 76 | |
| 77 | // Create the custom options from the function below |
| 78 | std::vector<uint8_t> customOperatorOptions = CreateCustomOptions(strideHeight, strideWidth, strideDepth, |
| 79 | filterHeight, filterWidth, filterDepth, padding); |
| 80 | // opCodeIndex is created as a uint8_t to avoid map lookup |
| 81 | uint8_t opCodeIndex = 0; |
| 82 | // Set the operator name based on the PoolType passed in from the test case |
| 83 | std::string opName = ""; |
| 84 | if (poolType == "kMax") |
| 85 | { |
| 86 | opName = "MaxPool3D"; |
| 87 | } |
| 88 | else |
| 89 | { |
| 90 | opName = "AveragePool3D"; |
| 91 | } |
| 92 | // To create a custom operator code you pass in the builtin code for custom operators and the name of the custom op |
| 93 | flatbuffers::Offset<OperatorCode> operatorCode = CreateOperatorCodeDirect(flatBufferBuilder, |
| 94 | tflite::BuiltinOperator_CUSTOM, |
| 95 | opName.c_str()); |
| 96 | |
| 97 | // Create the Operator using the opCodeIndex and custom options. Also sets builtin options to none. |
| 98 | const std::vector<int32_t> operatorInputs{ 0 }; |
| 99 | const std::vector<int32_t> operatorOutputs{ 1 }; |
| 100 | flatbuffers::Offset<Operator> poolingOperator = |
| 101 | CreateOperator(flatBufferBuilder, |
| 102 | opCodeIndex, |
| 103 | flatBufferBuilder.CreateVector<int32_t>(operatorInputs.data(), operatorInputs.size()), |
| 104 | flatBufferBuilder.CreateVector<int32_t>(operatorOutputs.data(), operatorOutputs.size()), |
| 105 | tflite::BuiltinOptions_NONE, |
| 106 | 0, |
| 107 | flatBufferBuilder.CreateVector<uint8_t>(customOperatorOptions), |
| 108 | tflite::CustomOptionsFormat_FLEXBUFFERS); |
| 109 | |
| 110 | // Create the subgraph using the operator created above. |
| 111 | const std::vector<int> subgraphInputs{ 0 }; |
| 112 | const std::vector<int> subgraphOutputs{ 1 }; |
| 113 | flatbuffers::Offset<SubGraph> subgraph = |
| 114 | CreateSubGraph(flatBufferBuilder, |
| 115 | flatBufferBuilder.CreateVector(tensors.data(), tensors.size()), |
| 116 | flatBufferBuilder.CreateVector<int32_t>(subgraphInputs.data(), subgraphInputs.size()), |
| 117 | flatBufferBuilder.CreateVector<int32_t>(subgraphOutputs.data(), subgraphOutputs.size()), |
| 118 | flatBufferBuilder.CreateVector(&poolingOperator, 1)); |
| 119 | |
| 120 | flatbuffers::Offset<flatbuffers::String> modelDescription = |
| 121 | flatBufferBuilder.CreateString("ArmnnDelegate: Pooling3d Operator Model"); |
| 122 | |
| 123 | // Create the model using operatorCode and the subgraph. |
| 124 | flatbuffers::Offset<Model> flatbufferModel = |
| 125 | CreateModel(flatBufferBuilder, |
| 126 | TFLITE_SCHEMA_VERSION, |
| 127 | flatBufferBuilder.CreateVector(&operatorCode, 1), |
| 128 | flatBufferBuilder.CreateVector(&subgraph, 1), |
| 129 | modelDescription, |
| 130 | flatBufferBuilder.CreateVector(buffers.data(), buffers.size())); |
| 131 | |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 132 | flatBufferBuilder.Finish(flatbufferModel, armnnDelegate::FILE_IDENTIFIER); |
Ryan OShea | d21abaf | 2022-06-10 14:49:11 +0100 | [diff] [blame] | 133 | |
| 134 | return std::vector<char>(flatBufferBuilder.GetBufferPointer(), |
| 135 | flatBufferBuilder.GetBufferPointer() + flatBufferBuilder.GetSize()); |
| 136 | } |
| 137 | |
| 138 | template<typename T> |
| 139 | void Pooling3dTest(std::string poolType, |
| 140 | tflite::TensorType tensorType, |
| 141 | std::vector<armnn::BackendId>& backends, |
| 142 | std::vector<int32_t>& inputShape, |
| 143 | std::vector<int32_t>& outputShape, |
| 144 | std::vector<T>& inputValues, |
| 145 | std::vector<T>& expectedOutputValues, |
| 146 | TfLitePadding padding = kTfLitePaddingSame, |
| 147 | int32_t strideWidth = 0, |
| 148 | int32_t strideHeight = 0, |
| 149 | int32_t strideDepth = 0, |
| 150 | int32_t filterWidth = 0, |
| 151 | int32_t filterHeight = 0, |
| 152 | int32_t filterDepth = 0, |
| 153 | tflite::ActivationFunctionType fusedActivation = tflite::ActivationFunctionType_NONE, |
| 154 | float quantScale = 1.0f, |
| 155 | int quantOffset = 0) |
| 156 | { |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 157 | using namespace delegateTestInterpreter; |
Ryan OShea | d21abaf | 2022-06-10 14:49:11 +0100 | [diff] [blame] | 158 | // Create the single op model buffer |
| 159 | std::vector<char> modelBuffer = CreatePooling3dTfLiteModel(poolType, |
| 160 | tensorType, |
| 161 | inputShape, |
| 162 | outputShape, |
| 163 | padding, |
| 164 | strideWidth, |
| 165 | strideHeight, |
| 166 | strideDepth, |
| 167 | filterWidth, |
| 168 | filterHeight, |
| 169 | filterDepth, |
| 170 | fusedActivation, |
| 171 | quantScale, |
| 172 | quantOffset); |
| 173 | |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 174 | std::string opType = ""; |
Ryan OShea | d21abaf | 2022-06-10 14:49:11 +0100 | [diff] [blame] | 175 | if (poolType == "kMax") |
| 176 | { |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 177 | opType = "MaxPool3D"; |
Ryan OShea | d21abaf | 2022-06-10 14:49:11 +0100 | [diff] [blame] | 178 | } |
| 179 | else |
| 180 | { |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 181 | opType = "AveragePool3D"; |
Ryan OShea | d21abaf | 2022-06-10 14:49:11 +0100 | [diff] [blame] | 182 | } |
| 183 | |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 184 | // Setup interpreter with just TFLite Runtime. |
| 185 | auto tfLiteInterpreter = DelegateTestInterpreter(modelBuffer, opType); |
| 186 | CHECK(tfLiteInterpreter.AllocateTensors() == kTfLiteOk); |
| 187 | CHECK(tfLiteInterpreter.FillInputTensor<T>(inputValues, 0) == kTfLiteOk); |
| 188 | CHECK(tfLiteInterpreter.Invoke() == kTfLiteOk); |
| 189 | std::vector<T> tfLiteOutputValues = tfLiteInterpreter.GetOutputResult<T>(0); |
| 190 | std::vector<int32_t> tfLiteOutputShape = tfLiteInterpreter.GetOutputShape(0); |
Ryan OShea | d21abaf | 2022-06-10 14:49:11 +0100 | [diff] [blame] | 191 | |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 192 | // Setup interpreter with Arm NN Delegate applied. |
| 193 | auto armnnInterpreter = DelegateTestInterpreter(modelBuffer, backends, opType); |
| 194 | CHECK(armnnInterpreter.AllocateTensors() == kTfLiteOk); |
| 195 | CHECK(armnnInterpreter.FillInputTensor<T>(inputValues, 0) == kTfLiteOk); |
| 196 | CHECK(armnnInterpreter.Invoke() == kTfLiteOk); |
| 197 | std::vector<T> armnnOutputValues = armnnInterpreter.GetOutputResult<T>(0); |
| 198 | std::vector<int32_t> armnnOutputShape = armnnInterpreter.GetOutputShape(0); |
Ryan OShea | d21abaf | 2022-06-10 14:49:11 +0100 | [diff] [blame] | 199 | |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 200 | armnnDelegate::CompareOutputData<T>(tfLiteOutputValues, armnnOutputValues, expectedOutputValues); |
| 201 | armnnDelegate::CompareOutputShape(tfLiteOutputShape, armnnOutputShape, outputShape); |
Ryan OShea | d21abaf | 2022-06-10 14:49:11 +0100 | [diff] [blame] | 202 | |
Matthew Sloyan | ebe392d | 2023-03-30 10:12:08 +0100 | [diff] [blame] | 203 | tfLiteInterpreter.Cleanup(); |
| 204 | armnnInterpreter.Cleanup(); |
Ryan OShea | d21abaf | 2022-06-10 14:49:11 +0100 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | // Function to create the flexbuffer custom options for the custom pooling3d operator. |
| 208 | std::vector<uint8_t> CreateCustomOptions(int strideHeight, int strideWidth, int strideDepth, |
| 209 | int filterHeight, int filterWidth, int filterDepth, TfLitePadding padding) |
| 210 | { |
| 211 | auto flex_builder = std::make_unique<flexbuffers::Builder>(); |
| 212 | size_t map_start = flex_builder->StartMap(); |
| 213 | flex_builder->String("data_format", "NDHWC"); |
| 214 | // Padding is created as a key and padding type. Only VALID and SAME supported |
| 215 | if (padding == kTfLitePaddingValid) |
| 216 | { |
| 217 | flex_builder->String("padding", "VALID"); |
| 218 | } |
| 219 | else |
| 220 | { |
| 221 | flex_builder->String("padding", "SAME"); |
| 222 | } |
| 223 | |
| 224 | // Vector of filter dimensions in order ( 1, Depth, Height, Width, 1 ) |
| 225 | auto start = flex_builder->StartVector("ksize"); |
| 226 | flex_builder->Add(1); |
| 227 | flex_builder->Add(filterDepth); |
| 228 | flex_builder->Add(filterHeight); |
| 229 | flex_builder->Add(filterWidth); |
| 230 | flex_builder->Add(1); |
| 231 | // EndVector( start, bool typed, bool fixed) |
| 232 | flex_builder->EndVector(start, true, false); |
| 233 | |
| 234 | // Vector of stride dimensions in order ( 1, Depth, Height, Width, 1 ) |
| 235 | auto stridesStart = flex_builder->StartVector("strides"); |
| 236 | flex_builder->Add(1); |
| 237 | flex_builder->Add(strideDepth); |
| 238 | flex_builder->Add(strideHeight); |
| 239 | flex_builder->Add(strideWidth); |
| 240 | flex_builder->Add(1); |
| 241 | // EndVector( stridesStart, bool typed, bool fixed) |
| 242 | flex_builder->EndVector(stridesStart, true, false); |
| 243 | |
| 244 | flex_builder->EndMap(map_start); |
| 245 | flex_builder->Finish(); |
| 246 | |
| 247 | return flex_builder->GetBuffer(); |
| 248 | } |
| 249 | #endif |
| 250 | } // anonymous namespace |
| 251 | |
| 252 | |
| 253 | |
| 254 | |