Narumol Prangnawarat | 50c87d3 | 2020-11-09 18:42:11 +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 | |
Jan Eilers | 3812fbc | 2020-11-17 19:06:35 +0000 | [diff] [blame] | 8 | #include "TestUtils.hpp" |
| 9 | |
Narumol Prangnawarat | 50c87d3 | 2020-11-09 18:42:11 +0000 | [diff] [blame] | 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 | std::vector<char> CreatePooling2dTfLiteModel( |
| 25 | tflite::BuiltinOperator poolingOperatorCode, |
| 26 | tflite::TensorType tensorType, |
| 27 | const std::vector <int32_t>& inputTensorShape, |
| 28 | const std::vector <int32_t>& outputTensorShape, |
| 29 | tflite::Padding padding = tflite::Padding_SAME, |
| 30 | int32_t strideWidth = 0, |
| 31 | int32_t strideHeight = 0, |
| 32 | int32_t filterWidth = 0, |
| 33 | int32_t filterHeight = 0, |
| 34 | tflite::ActivationFunctionType fusedActivation = tflite::ActivationFunctionType_NONE, |
| 35 | float quantScale = 1.0f, |
| 36 | int quantOffset = 0) |
| 37 | { |
| 38 | using namespace tflite; |
| 39 | flatbuffers::FlatBufferBuilder flatBufferBuilder; |
| 40 | |
| 41 | std::vector<flatbuffers::Offset<tflite::Buffer>> buffers; |
| 42 | buffers.push_back(CreateBuffer(flatBufferBuilder, flatBufferBuilder.CreateVector({}))); |
| 43 | |
| 44 | auto quantizationParameters = |
| 45 | CreateQuantizationParameters(flatBufferBuilder, |
| 46 | 0, |
| 47 | 0, |
| 48 | flatBufferBuilder.CreateVector<float>({ quantScale }), |
| 49 | flatBufferBuilder.CreateVector<int64_t>({ quantOffset })); |
| 50 | |
| 51 | std::array<flatbuffers::Offset<Tensor>, 2> tensors; |
| 52 | tensors[0] = CreateTensor(flatBufferBuilder, |
| 53 | flatBufferBuilder.CreateVector<int32_t>(inputTensorShape.data(), |
| 54 | inputTensorShape.size()), |
| 55 | tensorType, |
| 56 | 0, |
| 57 | flatBufferBuilder.CreateString("input"), |
| 58 | quantizationParameters); |
| 59 | |
| 60 | tensors[1] = CreateTensor(flatBufferBuilder, |
| 61 | flatBufferBuilder.CreateVector<int32_t>(outputTensorShape.data(), |
| 62 | outputTensorShape.size()), |
| 63 | tensorType, |
| 64 | 0, |
| 65 | flatBufferBuilder.CreateString("output"), |
| 66 | quantizationParameters); |
| 67 | |
| 68 | // create operator |
| 69 | tflite::BuiltinOptions operatorBuiltinOptionsType = BuiltinOptions_Pool2DOptions; |
| 70 | flatbuffers::Offset<void> operatorBuiltinOptions = CreatePool2DOptions(flatBufferBuilder, |
| 71 | padding, |
| 72 | strideWidth, |
| 73 | strideHeight, |
| 74 | filterWidth, |
| 75 | filterHeight, |
| 76 | fusedActivation).Union(); |
| 77 | |
Keith Davis | 892fafe | 2020-11-26 17:40:35 +0000 | [diff] [blame] | 78 | const std::vector<int32_t> operatorInputs{0}; |
| 79 | const std::vector<int32_t> operatorOutputs{1}; |
Narumol Prangnawarat | 50c87d3 | 2020-11-09 18:42:11 +0000 | [diff] [blame] | 80 | flatbuffers::Offset <Operator> poolingOperator = |
| 81 | CreateOperator(flatBufferBuilder, |
| 82 | 0, |
| 83 | flatBufferBuilder.CreateVector<int32_t>(operatorInputs.data(), operatorInputs.size()), |
| 84 | flatBufferBuilder.CreateVector<int32_t>(operatorOutputs.data(), operatorOutputs.size()), |
| 85 | operatorBuiltinOptionsType, |
| 86 | operatorBuiltinOptions); |
| 87 | |
Keith Davis | 892fafe | 2020-11-26 17:40:35 +0000 | [diff] [blame] | 88 | const std::vector<int> subgraphInputs{0}; |
| 89 | const std::vector<int> subgraphOutputs{1}; |
Narumol Prangnawarat | 50c87d3 | 2020-11-09 18:42:11 +0000 | [diff] [blame] | 90 | flatbuffers::Offset <SubGraph> subgraph = |
| 91 | CreateSubGraph(flatBufferBuilder, |
| 92 | flatBufferBuilder.CreateVector(tensors.data(), tensors.size()), |
| 93 | flatBufferBuilder.CreateVector<int32_t>(subgraphInputs.data(), subgraphInputs.size()), |
| 94 | flatBufferBuilder.CreateVector<int32_t>(subgraphOutputs.data(), subgraphOutputs.size()), |
| 95 | flatBufferBuilder.CreateVector(&poolingOperator, 1)); |
| 96 | |
| 97 | flatbuffers::Offset <flatbuffers::String> modelDescription = |
| 98 | flatBufferBuilder.CreateString("ArmnnDelegate: Pooling2d Operator Model"); |
| 99 | flatbuffers::Offset <OperatorCode> operatorCode = CreateOperatorCode(flatBufferBuilder, poolingOperatorCode); |
| 100 | |
| 101 | flatbuffers::Offset <Model> flatbufferModel = |
| 102 | CreateModel(flatBufferBuilder, |
| 103 | TFLITE_SCHEMA_VERSION, |
| 104 | flatBufferBuilder.CreateVector(&operatorCode, 1), |
| 105 | flatBufferBuilder.CreateVector(&subgraph, 1), |
| 106 | modelDescription, |
| 107 | flatBufferBuilder.CreateVector(buffers.data(), buffers.size())); |
| 108 | |
| 109 | flatBufferBuilder.Finish(flatbufferModel); |
| 110 | |
| 111 | return std::vector<char>(flatBufferBuilder.GetBufferPointer(), |
| 112 | flatBufferBuilder.GetBufferPointer() + flatBufferBuilder.GetSize()); |
| 113 | } |
| 114 | |
| 115 | template <typename T> |
| 116 | void Pooling2dTest(tflite::BuiltinOperator poolingOperatorCode, |
| 117 | tflite::TensorType tensorType, |
| 118 | std::vector<armnn::BackendId>& backends, |
| 119 | std::vector<int32_t>& inputShape, |
| 120 | std::vector<int32_t>& outputShape, |
| 121 | std::vector<T>& inputValues, |
| 122 | std::vector<T>& expectedOutputValues, |
| 123 | tflite::Padding padding = tflite::Padding_SAME, |
| 124 | int32_t strideWidth = 0, |
| 125 | int32_t strideHeight = 0, |
| 126 | int32_t filterWidth = 0, |
| 127 | int32_t filterHeight = 0, |
| 128 | tflite::ActivationFunctionType fusedActivation = tflite::ActivationFunctionType_NONE, |
| 129 | float quantScale = 1.0f, |
| 130 | int quantOffset = 0) |
| 131 | { |
| 132 | using namespace tflite; |
| 133 | std::vector<char> modelBuffer = CreatePooling2dTfLiteModel(poolingOperatorCode, |
| 134 | tensorType, |
| 135 | inputShape, |
| 136 | outputShape, |
| 137 | padding, |
| 138 | strideWidth, |
| 139 | strideHeight, |
| 140 | filterWidth, |
| 141 | filterHeight, |
| 142 | fusedActivation, |
| 143 | quantScale, |
| 144 | quantOffset); |
| 145 | |
| 146 | const Model* tfLiteModel = GetModel(modelBuffer.data()); |
| 147 | CHECK(tfLiteModel != nullptr); |
| 148 | // Create TfLite Interpreters |
| 149 | std::unique_ptr<Interpreter> armnnDelegateInterpreter; |
| 150 | CHECK(InterpreterBuilder(tfLiteModel, ::tflite::ops::builtin::BuiltinOpResolver()) |
| 151 | (&armnnDelegateInterpreter) == kTfLiteOk); |
| 152 | CHECK(armnnDelegateInterpreter != nullptr); |
| 153 | CHECK(armnnDelegateInterpreter->AllocateTensors() == kTfLiteOk); |
| 154 | |
| 155 | std::unique_ptr<Interpreter> tfLiteInterpreter; |
| 156 | CHECK(InterpreterBuilder(tfLiteModel, ::tflite::ops::builtin::BuiltinOpResolver()) |
| 157 | (&tfLiteInterpreter) == kTfLiteOk); |
| 158 | CHECK(tfLiteInterpreter != nullptr); |
| 159 | CHECK(tfLiteInterpreter->AllocateTensors() == kTfLiteOk); |
| 160 | |
| 161 | // Create the ArmNN Delegate |
| 162 | armnnDelegate::DelegateOptions delegateOptions(backends); |
| 163 | std::unique_ptr<TfLiteDelegate, decltype(&armnnDelegate::TfLiteArmnnDelegateDelete)> |
| 164 | theArmnnDelegate(armnnDelegate::TfLiteArmnnDelegateCreate(delegateOptions), |
| 165 | armnnDelegate::TfLiteArmnnDelegateDelete); |
| 166 | CHECK(theArmnnDelegate != nullptr); |
| 167 | // Modify armnnDelegateInterpreter to use armnnDelegate |
| 168 | CHECK(armnnDelegateInterpreter->ModifyGraphWithDelegate(theArmnnDelegate.get()) == kTfLiteOk); |
| 169 | |
| 170 | // Set input data |
| 171 | auto tfLiteDelegateInputId = tfLiteInterpreter->inputs()[0]; |
| 172 | auto tfLiteDelegateInputData = tfLiteInterpreter->typed_tensor<T>(tfLiteDelegateInputId); |
| 173 | for (unsigned int i = 0; i < inputValues.size(); ++i) |
| 174 | { |
| 175 | tfLiteDelegateInputData[i] = inputValues[i]; |
| 176 | } |
| 177 | |
| 178 | auto armnnDelegateInputId = armnnDelegateInterpreter->inputs()[0]; |
| 179 | auto armnnDelegateInputData = armnnDelegateInterpreter->typed_tensor<T>(armnnDelegateInputId); |
| 180 | for (unsigned int i = 0; i < inputValues.size(); ++i) |
| 181 | { |
| 182 | armnnDelegateInputData[i] = inputValues[i]; |
| 183 | } |
| 184 | |
| 185 | // Run EnqueueWorkload |
| 186 | CHECK(tfLiteInterpreter->Invoke() == kTfLiteOk); |
| 187 | CHECK(armnnDelegateInterpreter->Invoke() == kTfLiteOk); |
Narumol Prangnawarat | 50c87d3 | 2020-11-09 18:42:11 +0000 | [diff] [blame] | 188 | |
Jan Eilers | 3812fbc | 2020-11-17 19:06:35 +0000 | [diff] [blame] | 189 | armnnDelegate::CompareOutputData(tfLiteInterpreter, armnnDelegateInterpreter, outputShape, expectedOutputValues); |
Narumol Prangnawarat | 50c87d3 | 2020-11-09 18:42:11 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | } // anonymous namespace |
| 193 | |
| 194 | |
| 195 | |
| 196 | |