Narumol Prangnawarat | 958024b | 2020-12-17 12:17:58 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2020 Arm Ltd and Contributors. All rights reserved. |
| 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 <tensorflow/lite/interpreter.h> |
| 14 | #include <tensorflow/lite/kernels/register.h> |
| 15 | #include <tensorflow/lite/model.h> |
| 16 | #include <tensorflow/lite/schema/schema_generated.h> |
| 17 | #include <tensorflow/lite/version.h> |
| 18 | |
| 19 | #include <doctest/doctest.h> |
| 20 | |
| 21 | namespace |
| 22 | { |
| 23 | |
| 24 | template <typename T> |
| 25 | std::vector<char> CreatePadTfLiteModel( |
| 26 | tflite::BuiltinOperator padOperatorCode, |
| 27 | tflite::TensorType tensorType, |
Matthew Sloyan | af3a4ef | 2021-10-22 15:48:12 +0100 | [diff] [blame] | 28 | tflite::MirrorPadMode paddingMode, |
Narumol Prangnawarat | 958024b | 2020-12-17 12:17:58 +0000 | [diff] [blame] | 29 | const std::vector<int32_t>& inputTensorShape, |
| 30 | const std::vector<int32_t>& paddingTensorShape, |
| 31 | const std::vector<int32_t>& outputTensorShape, |
| 32 | const std::vector<int32_t>& paddingDim, |
| 33 | const std::vector<T> paddingValue, |
| 34 | float quantScale = 1.0f, |
| 35 | int quantOffset = 0) |
| 36 | { |
| 37 | using namespace tflite; |
| 38 | flatbuffers::FlatBufferBuilder flatBufferBuilder; |
| 39 | |
| 40 | auto quantizationParameters = |
| 41 | CreateQuantizationParameters(flatBufferBuilder, |
| 42 | 0, |
| 43 | 0, |
| 44 | flatBufferBuilder.CreateVector<float>({ quantScale }), |
| 45 | flatBufferBuilder.CreateVector<int64_t>({ quantOffset })); |
| 46 | |
| 47 | auto inputTensor = CreateTensor(flatBufferBuilder, |
| 48 | flatBufferBuilder.CreateVector<int32_t>(inputTensorShape.data(), |
| 49 | inputTensorShape.size()), |
| 50 | tensorType, |
| 51 | 0, |
| 52 | flatBufferBuilder.CreateString("input"), |
| 53 | quantizationParameters); |
| 54 | |
| 55 | auto paddingTensor = CreateTensor(flatBufferBuilder, |
| 56 | flatBufferBuilder.CreateVector<int32_t>(paddingTensorShape.data(), |
| 57 | paddingTensorShape.size()), |
| 58 | tflite::TensorType_INT32, |
| 59 | 1, |
| 60 | flatBufferBuilder.CreateString("padding")); |
| 61 | |
| 62 | auto outputTensor = CreateTensor(flatBufferBuilder, |
| 63 | flatBufferBuilder.CreateVector<int32_t>(outputTensorShape.data(), |
| 64 | outputTensorShape.size()), |
| 65 | tensorType, |
| 66 | 2, |
| 67 | flatBufferBuilder.CreateString("output"), |
| 68 | quantizationParameters); |
| 69 | |
| 70 | std::vector<flatbuffers::Offset<Tensor>> tensors = { inputTensor, paddingTensor, outputTensor}; |
| 71 | |
| 72 | std::vector<flatbuffers::Offset<tflite::Buffer>> buffers; |
| 73 | buffers.push_back(CreateBuffer(flatBufferBuilder, flatBufferBuilder.CreateVector({}))); |
| 74 | buffers.push_back( |
| 75 | CreateBuffer(flatBufferBuilder, |
| 76 | flatBufferBuilder.CreateVector(reinterpret_cast<const uint8_t*>(paddingDim.data()), |
Narumol Prangnawarat | 4cf0fe3 | 2020-12-18 16:13:06 +0000 | [diff] [blame] | 77 | sizeof(int32_t) * paddingDim.size()))); |
Narumol Prangnawarat | 958024b | 2020-12-17 12:17:58 +0000 | [diff] [blame] | 78 | buffers.push_back(CreateBuffer(flatBufferBuilder, flatBufferBuilder.CreateVector({}))); |
| 79 | |
| 80 | std::vector<int32_t> operatorInputs; |
| 81 | std::vector<int> subgraphInputs; |
| 82 | |
| 83 | tflite::BuiltinOptions operatorBuiltinOptionsType = BuiltinOptions_PadOptions; |
| 84 | flatbuffers::Offset<void> operatorBuiltinOptions; |
| 85 | |
| 86 | if (padOperatorCode == tflite::BuiltinOperator_PAD) |
| 87 | { |
| 88 | operatorInputs = {{ 0, 1 }}; |
| 89 | subgraphInputs = {{ 0, 1 }}; |
| 90 | operatorBuiltinOptions = CreatePadOptions(flatBufferBuilder).Union(); |
Matthew Sloyan | af3a4ef | 2021-10-22 15:48:12 +0100 | [diff] [blame] | 91 | } |
| 92 | else if(padOperatorCode == tflite::BuiltinOperator_MIRROR_PAD) |
| 93 | { |
| 94 | operatorInputs = {{ 0, 1 }}; |
| 95 | subgraphInputs = {{ 0, 1 }}; |
Narumol Prangnawarat | 958024b | 2020-12-17 12:17:58 +0000 | [diff] [blame] | 96 | |
Matthew Sloyan | af3a4ef | 2021-10-22 15:48:12 +0100 | [diff] [blame] | 97 | operatorBuiltinOptionsType = BuiltinOptions_MirrorPadOptions; |
| 98 | operatorBuiltinOptions = CreateMirrorPadOptions(flatBufferBuilder, paddingMode).Union(); |
Narumol Prangnawarat | 958024b | 2020-12-17 12:17:58 +0000 | [diff] [blame] | 99 | } |
| 100 | else if (padOperatorCode == tflite::BuiltinOperator_PADV2) |
| 101 | { |
| 102 | buffers.push_back( |
| 103 | CreateBuffer(flatBufferBuilder, |
| 104 | flatBufferBuilder.CreateVector(reinterpret_cast<const uint8_t*>(paddingValue.data()), |
| 105 | sizeof(T)))); |
| 106 | |
| 107 | const std::vector<int32_t> shape = { 1 }; |
| 108 | auto padValueTensor = CreateTensor(flatBufferBuilder, |
| 109 | flatBufferBuilder.CreateVector<int32_t>(shape.data(), |
| 110 | shape.size()), |
| 111 | tensorType, |
| 112 | 3, |
| 113 | flatBufferBuilder.CreateString("paddingValue"), |
| 114 | quantizationParameters); |
| 115 | |
| 116 | tensors.push_back(padValueTensor); |
| 117 | |
| 118 | operatorInputs = {{ 0, 1, 3 }}; |
| 119 | subgraphInputs = {{ 0, 1, 3 }}; |
| 120 | |
| 121 | operatorBuiltinOptionsType = BuiltinOptions_PadV2Options; |
| 122 | operatorBuiltinOptions = CreatePadV2Options(flatBufferBuilder).Union(); |
| 123 | } |
| 124 | |
| 125 | // create operator |
Keith Davis | bbc876c | 2021-01-27 13:12:03 +0000 | [diff] [blame] | 126 | const std::vector<int32_t> operatorOutputs{ 2 }; |
Matthew Sloyan | af3a4ef | 2021-10-22 15:48:12 +0100 | [diff] [blame] | 127 | flatbuffers::Offset <Operator> paddingOperator = |
Narumol Prangnawarat | 958024b | 2020-12-17 12:17:58 +0000 | [diff] [blame] | 128 | CreateOperator(flatBufferBuilder, |
| 129 | 0, |
| 130 | flatBufferBuilder.CreateVector<int32_t>(operatorInputs.data(), operatorInputs.size()), |
| 131 | flatBufferBuilder.CreateVector<int32_t>(operatorOutputs.data(), operatorOutputs.size()), |
| 132 | operatorBuiltinOptionsType, |
| 133 | operatorBuiltinOptions); |
| 134 | |
Keith Davis | bbc876c | 2021-01-27 13:12:03 +0000 | [diff] [blame] | 135 | const std::vector<int> subgraphOutputs{ 2 }; |
Narumol Prangnawarat | 958024b | 2020-12-17 12:17:58 +0000 | [diff] [blame] | 136 | flatbuffers::Offset <SubGraph> subgraph = |
| 137 | CreateSubGraph(flatBufferBuilder, |
| 138 | flatBufferBuilder.CreateVector(tensors.data(), tensors.size()), |
| 139 | flatBufferBuilder.CreateVector<int32_t>(subgraphInputs.data(), subgraphInputs.size()), |
| 140 | flatBufferBuilder.CreateVector<int32_t>(subgraphOutputs.data(), subgraphOutputs.size()), |
Matthew Sloyan | af3a4ef | 2021-10-22 15:48:12 +0100 | [diff] [blame] | 141 | flatBufferBuilder.CreateVector(&paddingOperator, 1)); |
Narumol Prangnawarat | 958024b | 2020-12-17 12:17:58 +0000 | [diff] [blame] | 142 | |
| 143 | flatbuffers::Offset <flatbuffers::String> modelDescription = |
| 144 | flatBufferBuilder.CreateString("ArmnnDelegate: Pad Operator Model"); |
| 145 | flatbuffers::Offset <OperatorCode> operatorCode = CreateOperatorCode(flatBufferBuilder, |
| 146 | padOperatorCode); |
| 147 | |
| 148 | flatbuffers::Offset <Model> flatbufferModel = |
| 149 | CreateModel(flatBufferBuilder, |
| 150 | TFLITE_SCHEMA_VERSION, |
| 151 | flatBufferBuilder.CreateVector(&operatorCode, 1), |
| 152 | flatBufferBuilder.CreateVector(&subgraph, 1), |
| 153 | modelDescription, |
| 154 | flatBufferBuilder.CreateVector(buffers.data(), buffers.size())); |
| 155 | |
| 156 | flatBufferBuilder.Finish(flatbufferModel); |
| 157 | |
| 158 | return std::vector<char>(flatBufferBuilder.GetBufferPointer(), |
| 159 | flatBufferBuilder.GetBufferPointer() + flatBufferBuilder.GetSize()); |
| 160 | } |
| 161 | |
| 162 | template <typename T> |
| 163 | void PadTest(tflite::BuiltinOperator padOperatorCode, |
| 164 | tflite::TensorType tensorType, |
| 165 | const std::vector<armnn::BackendId>& backends, |
| 166 | const std::vector<int32_t>& inputShape, |
| 167 | const std::vector<int32_t>& paddingShape, |
| 168 | std::vector<int32_t>& outputShape, |
| 169 | std::vector<T>& inputValues, |
| 170 | std::vector<int32_t>& paddingDim, |
| 171 | std::vector<T>& expectedOutputValues, |
| 172 | T paddingValue, |
| 173 | float quantScale = 1.0f, |
Matthew Sloyan | af3a4ef | 2021-10-22 15:48:12 +0100 | [diff] [blame] | 174 | int quantOffset = 0, |
| 175 | tflite::MirrorPadMode paddingMode = tflite::MirrorPadMode_SYMMETRIC) |
Narumol Prangnawarat | 958024b | 2020-12-17 12:17:58 +0000 | [diff] [blame] | 176 | { |
| 177 | using namespace tflite; |
| 178 | std::vector<char> modelBuffer = CreatePadTfLiteModel<T>(padOperatorCode, |
| 179 | tensorType, |
Matthew Sloyan | af3a4ef | 2021-10-22 15:48:12 +0100 | [diff] [blame] | 180 | paddingMode, |
Narumol Prangnawarat | 958024b | 2020-12-17 12:17:58 +0000 | [diff] [blame] | 181 | inputShape, |
| 182 | paddingShape, |
| 183 | outputShape, |
| 184 | paddingDim, |
| 185 | {paddingValue}, |
| 186 | quantScale, |
| 187 | quantOffset); |
| 188 | |
| 189 | const Model* tfLiteModel = GetModel(modelBuffer.data()); |
| 190 | CHECK(tfLiteModel != nullptr); |
| 191 | |
| 192 | std::unique_ptr<Interpreter> armnnDelegateInterpreter; |
| 193 | CHECK(InterpreterBuilder(tfLiteModel, ::tflite::ops::builtin::BuiltinOpResolver()) |
| 194 | (&armnnDelegateInterpreter) == kTfLiteOk); |
| 195 | CHECK(armnnDelegateInterpreter != nullptr); |
| 196 | CHECK(armnnDelegateInterpreter->AllocateTensors() == kTfLiteOk); |
| 197 | |
| 198 | std::unique_ptr<Interpreter> tfLiteInterpreter; |
| 199 | CHECK(InterpreterBuilder(tfLiteModel, ::tflite::ops::builtin::BuiltinOpResolver()) |
| 200 | (&tfLiteInterpreter) == kTfLiteOk); |
| 201 | CHECK(tfLiteInterpreter != nullptr); |
| 202 | CHECK(tfLiteInterpreter->AllocateTensors() == kTfLiteOk); |
| 203 | |
| 204 | // Create the ArmNN Delegate |
| 205 | armnnDelegate::DelegateOptions delegateOptions(backends); |
| 206 | std::unique_ptr<TfLiteDelegate, decltype(&armnnDelegate::TfLiteArmnnDelegateDelete)> |
| 207 | theArmnnDelegate(armnnDelegate::TfLiteArmnnDelegateCreate(delegateOptions), |
| 208 | armnnDelegate::TfLiteArmnnDelegateDelete); |
| 209 | CHECK(theArmnnDelegate != nullptr); |
| 210 | // Modify armnnDelegateInterpreter to use armnnDelegate |
| 211 | CHECK(armnnDelegateInterpreter->ModifyGraphWithDelegate(theArmnnDelegate.get()) == kTfLiteOk); |
| 212 | |
| 213 | // Set input data |
| 214 | armnnDelegate::FillInput<T>(tfLiteInterpreter, 0, inputValues); |
| 215 | armnnDelegate::FillInput<T>(armnnDelegateInterpreter, 0, inputValues); |
| 216 | |
| 217 | // Run EnqueueWorkload |
| 218 | CHECK(tfLiteInterpreter->Invoke() == kTfLiteOk); |
| 219 | CHECK(armnnDelegateInterpreter->Invoke() == kTfLiteOk); |
| 220 | |
| 221 | armnnDelegate::CompareOutputData<T>(tfLiteInterpreter, armnnDelegateInterpreter, outputShape, expectedOutputValues); |
| 222 | } |
| 223 | |
Keith Davis | bbc876c | 2021-01-27 13:12:03 +0000 | [diff] [blame] | 224 | } // anonymous namespace |