Kevin May | 8ab2d7a | 2021-05-07 09:32:51 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2021 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 | #include <string> |
| 22 | |
| 23 | namespace |
| 24 | { |
| 25 | |
| 26 | std::vector<char> CreateUnpackTfLiteModel(tflite::BuiltinOperator unpackOperatorCode, |
| 27 | tflite::TensorType tensorType, |
| 28 | std::vector<int32_t>& inputTensorShape, |
| 29 | const std::vector <int32_t>& outputTensorShape, |
| 30 | const int32_t outputTensorNum, |
| 31 | unsigned int axis = 0, |
| 32 | float quantScale = 1.0f, |
| 33 | int quantOffset = 0) |
| 34 | { |
| 35 | using namespace tflite; |
| 36 | flatbuffers::FlatBufferBuilder flatBufferBuilder; |
| 37 | |
| 38 | std::vector<flatbuffers::Offset<tflite::Buffer>> buffers; |
| 39 | buffers.push_back(CreateBuffer(flatBufferBuilder, flatBufferBuilder.CreateVector({}))); |
| 40 | |
| 41 | auto quantizationParameters = |
| 42 | CreateQuantizationParameters(flatBufferBuilder, |
| 43 | 0, |
| 44 | 0, |
| 45 | flatBufferBuilder.CreateVector<float>({ quantScale }), |
| 46 | flatBufferBuilder.CreateVector<int64_t>({ quantOffset })); |
| 47 | |
| 48 | const std::vector<int32_t> operatorInputs{ 0 }; |
| 49 | std::vector<int32_t> operatorOutputs{}; |
| 50 | const std::vector<int> subgraphInputs{ 0 }; |
| 51 | std::vector<int> subgraphOutputs{}; |
| 52 | |
| 53 | std::vector<flatbuffers::Offset<Tensor>> tensors(outputTensorNum + 1); |
| 54 | |
| 55 | // Create input tensor |
| 56 | tensors[0] = CreateTensor(flatBufferBuilder, |
| 57 | flatBufferBuilder.CreateVector<int32_t>(inputTensorShape.data(), |
| 58 | inputTensorShape.size()), |
| 59 | tensorType, |
| 60 | 0, |
| 61 | flatBufferBuilder.CreateString("input"), |
| 62 | quantizationParameters); |
| 63 | |
| 64 | for (int i = 0; i < outputTensorNum; ++i) |
| 65 | { |
| 66 | tensors[i + 1] = CreateTensor(flatBufferBuilder, |
| 67 | flatBufferBuilder.CreateVector<int32_t>(outputTensorShape.data(), |
| 68 | outputTensorShape.size()), |
| 69 | tensorType, |
| 70 | 0, |
| 71 | flatBufferBuilder.CreateString("output" + std::to_string(i)), |
| 72 | quantizationParameters); |
| 73 | |
| 74 | operatorOutputs.push_back(i + 1); |
| 75 | subgraphOutputs.push_back(i + 1); |
| 76 | } |
| 77 | |
| 78 | // create operator |
| 79 | tflite::BuiltinOptions operatorBuiltinOptionsType = tflite::BuiltinOptions_UnpackOptions; |
| 80 | flatbuffers::Offset<void> operatorBuiltinOptions = |
| 81 | CreateUnpackOptions(flatBufferBuilder, outputTensorNum, axis).Union(); |
| 82 | |
| 83 | flatbuffers::Offset <Operator> unpackOperator = |
| 84 | CreateOperator(flatBufferBuilder, |
| 85 | 0, |
| 86 | flatBufferBuilder.CreateVector<int32_t>(operatorInputs.data(), operatorInputs.size()), |
| 87 | flatBufferBuilder.CreateVector<int32_t>(operatorOutputs.data(), operatorOutputs.size()), |
| 88 | operatorBuiltinOptionsType, |
| 89 | operatorBuiltinOptions); |
| 90 | |
| 91 | flatbuffers::Offset <SubGraph> subgraph = |
| 92 | CreateSubGraph(flatBufferBuilder, |
| 93 | flatBufferBuilder.CreateVector(tensors.data(), tensors.size()), |
| 94 | flatBufferBuilder.CreateVector<int32_t>(subgraphInputs.data(), subgraphInputs.size()), |
| 95 | flatBufferBuilder.CreateVector<int32_t>(subgraphOutputs.data(), subgraphOutputs.size()), |
| 96 | flatBufferBuilder.CreateVector(&unpackOperator, 1)); |
| 97 | |
| 98 | flatbuffers::Offset <flatbuffers::String> modelDescription = |
| 99 | flatBufferBuilder.CreateString("ArmnnDelegate: Unpack Operator Model"); |
| 100 | flatbuffers::Offset <OperatorCode> operatorCode = CreateOperatorCode(flatBufferBuilder, unpackOperatorCode); |
| 101 | |
| 102 | flatbuffers::Offset <Model> flatbufferModel = |
| 103 | CreateModel(flatBufferBuilder, |
| 104 | TFLITE_SCHEMA_VERSION, |
| 105 | flatBufferBuilder.CreateVector(&operatorCode, 1), |
| 106 | flatBufferBuilder.CreateVector(&subgraph, 1), |
| 107 | modelDescription, |
| 108 | flatBufferBuilder.CreateVector(buffers.data(), buffers.size())); |
| 109 | |
| 110 | flatBufferBuilder.Finish(flatbufferModel); |
| 111 | |
| 112 | return std::vector<char>(flatBufferBuilder.GetBufferPointer(), |
| 113 | flatBufferBuilder.GetBufferPointer() + flatBufferBuilder.GetSize()); |
| 114 | } |
| 115 | |
| 116 | template <typename T> |
| 117 | void UnpackTest(tflite::BuiltinOperator unpackOperatorCode, |
| 118 | tflite::TensorType tensorType, |
| 119 | std::vector<armnn::BackendId>& backends, |
| 120 | std::vector<int32_t>& inputShape, |
| 121 | std::vector<int32_t>& expectedOutputShape, |
| 122 | std::vector<T>& inputValues, |
| 123 | std::vector<std::vector<T>>& expectedOutputValues, |
| 124 | unsigned int axis = 0, |
| 125 | float quantScale = 1.0f, |
| 126 | int quantOffset = 0) |
| 127 | { |
| 128 | using namespace tflite; |
| 129 | std::vector<char> modelBuffer = CreateUnpackTfLiteModel(unpackOperatorCode, |
| 130 | tensorType, |
| 131 | inputShape, |
| 132 | expectedOutputShape, |
| 133 | expectedOutputValues.size(), |
| 134 | axis, |
| 135 | quantScale, |
| 136 | quantOffset); |
| 137 | |
| 138 | const Model* tfLiteModel = GetModel(modelBuffer.data()); |
| 139 | |
| 140 | // Create TfLite Interpreters |
| 141 | std::unique_ptr<Interpreter> armnnDelegateInterpreter; |
| 142 | CHECK(InterpreterBuilder(tfLiteModel, ::tflite::ops::builtin::BuiltinOpResolver()) |
| 143 | (&armnnDelegateInterpreter) == kTfLiteOk); |
| 144 | CHECK(armnnDelegateInterpreter != nullptr); |
| 145 | CHECK(armnnDelegateInterpreter->AllocateTensors() == kTfLiteOk); |
| 146 | |
| 147 | std::unique_ptr<Interpreter> tfLiteInterpreter; |
| 148 | CHECK(InterpreterBuilder(tfLiteModel, ::tflite::ops::builtin::BuiltinOpResolver()) |
| 149 | (&tfLiteInterpreter) == kTfLiteOk); |
| 150 | CHECK(tfLiteInterpreter != nullptr); |
| 151 | CHECK(tfLiteInterpreter->AllocateTensors() == kTfLiteOk); |
| 152 | |
| 153 | // Create the ArmNN Delegate |
| 154 | armnnDelegate::DelegateOptions delegateOptions(backends); |
| 155 | std::unique_ptr<TfLiteDelegate, decltype(&armnnDelegate::TfLiteArmnnDelegateDelete)> |
| 156 | theArmnnDelegate(armnnDelegate::TfLiteArmnnDelegateCreate(delegateOptions), |
| 157 | armnnDelegate::TfLiteArmnnDelegateDelete); |
| 158 | CHECK(theArmnnDelegate != nullptr); |
| 159 | |
| 160 | // Modify armnnDelegateInterpreter to use armnnDelegate |
| 161 | CHECK(armnnDelegateInterpreter->ModifyGraphWithDelegate(theArmnnDelegate.get()) == kTfLiteOk); |
| 162 | |
| 163 | // Set input data |
| 164 | armnnDelegate::FillInput<T>(tfLiteInterpreter, 0, inputValues); |
| 165 | armnnDelegate::FillInput<T>(armnnDelegateInterpreter, 0, inputValues); |
| 166 | |
| 167 | |
| 168 | // Run EnqueueWorkload |
| 169 | CHECK(tfLiteInterpreter->Invoke() == kTfLiteOk); |
| 170 | CHECK(armnnDelegateInterpreter->Invoke() == kTfLiteOk); |
| 171 | |
| 172 | // Compare output data |
| 173 | for (unsigned int i = 0; i < expectedOutputValues.size(); ++i) |
| 174 | { |
| 175 | armnnDelegate::CompareOutputData<T>(tfLiteInterpreter, |
| 176 | armnnDelegateInterpreter, |
| 177 | expectedOutputShape, |
| 178 | expectedOutputValues[i], |
| 179 | i); |
| 180 | } |
| 181 | |
| 182 | armnnDelegateInterpreter.reset(nullptr); |
| 183 | } |
| 184 | |
| 185 | } // anonymous namespace |