Ryan OShea | 49ed0df | 2022-09-21 16:09:41 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2022 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 | std::vector<char> CreateBatchMatMulTfLiteModel( |
| 25 | tflite::BuiltinOperator bmmOperatorCode, |
| 26 | tflite::TensorType tensorType, |
| 27 | const std::vector <int32_t>& LHSInputTensorShape, |
| 28 | const std::vector <int32_t>& RHSInputTensorShape, |
| 29 | const std::vector <int32_t>& outputTensorShape, |
| 30 | bool adjX = false, |
| 31 | bool adjY = false, |
| 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 | std::array<flatbuffers::Offset<Tensor>, 3> tensors; |
| 49 | tensors[0] = CreateTensor(flatBufferBuilder, |
| 50 | flatBufferBuilder.CreateVector<int32_t>(LHSInputTensorShape.data(), |
| 51 | LHSInputTensorShape.size()), |
| 52 | tensorType, |
| 53 | 0, |
| 54 | flatBufferBuilder.CreateString("LHSInput"), |
| 55 | quantizationParameters); |
| 56 | |
| 57 | tensors[1] = CreateTensor(flatBufferBuilder, |
| 58 | flatBufferBuilder.CreateVector<int32_t>(RHSInputTensorShape.data(), |
| 59 | RHSInputTensorShape.size()), |
| 60 | tensorType, |
| 61 | 0, |
| 62 | flatBufferBuilder.CreateString("RHSInput"), |
| 63 | quantizationParameters); |
| 64 | |
| 65 | tensors[2] = CreateTensor(flatBufferBuilder, |
| 66 | flatBufferBuilder.CreateVector<int32_t>(outputTensorShape.data(), |
| 67 | outputTensorShape.size()), |
| 68 | tensorType, |
| 69 | 0, |
| 70 | flatBufferBuilder.CreateString("output"), |
| 71 | quantizationParameters); |
| 72 | |
| 73 | // create operator |
| 74 | tflite::BuiltinOptions operatorBuiltinOptionsType = BuiltinOptions_BatchMatMulOptions; |
| 75 | flatbuffers::Offset<void> operatorBuiltinOptions = CreateBatchMatMulOptions(flatBufferBuilder, |
| 76 | adjX, |
| 77 | adjY).Union(); |
| 78 | |
| 79 | const std::vector<int32_t> operatorInputs{{0, 1}}; |
| 80 | const std::vector<int32_t> operatorOutputs{2}; |
| 81 | flatbuffers::Offset <Operator> bmmOperator = |
| 82 | CreateOperator(flatBufferBuilder, |
| 83 | 0, |
| 84 | flatBufferBuilder.CreateVector<int32_t>(operatorInputs.data(), operatorInputs.size()), |
| 85 | flatBufferBuilder.CreateVector<int32_t>(operatorOutputs.data(), |
| 86 | operatorOutputs.size()), |
| 87 | operatorBuiltinOptionsType, |
| 88 | operatorBuiltinOptions); |
| 89 | |
| 90 | const std::vector<int> subgraphInputs{{0, 1}}; |
| 91 | const std::vector<int> subgraphOutputs{2}; |
| 92 | flatbuffers::Offset <SubGraph> subgraph = |
| 93 | CreateSubGraph(flatBufferBuilder, |
| 94 | flatBufferBuilder.CreateVector(tensors.data(), tensors.size()), |
| 95 | flatBufferBuilder.CreateVector<int32_t>(subgraphInputs.data(), subgraphInputs.size()), |
| 96 | flatBufferBuilder.CreateVector<int32_t>(subgraphOutputs.data(), |
| 97 | subgraphOutputs.size()), |
| 98 | flatBufferBuilder.CreateVector(&bmmOperator, 1)); |
| 99 | |
| 100 | flatbuffers::Offset <flatbuffers::String> modelDescription = |
| 101 | flatBufferBuilder.CreateString("ArmnnDelegate: BatchMatMul Operator Model"); |
| 102 | flatbuffers::Offset <OperatorCode> operatorCode = CreateOperatorCode(flatBufferBuilder, bmmOperatorCode); |
| 103 | |
| 104 | flatbuffers::Offset <Model> flatbufferModel = |
| 105 | CreateModel(flatBufferBuilder, |
| 106 | TFLITE_SCHEMA_VERSION, |
| 107 | flatBufferBuilder.CreateVector(&operatorCode, 1), |
| 108 | flatBufferBuilder.CreateVector(&subgraph, 1), |
| 109 | modelDescription, |
| 110 | flatBufferBuilder.CreateVector(buffers.data(), buffers.size())); |
| 111 | |
| 112 | flatBufferBuilder.Finish(flatbufferModel); |
| 113 | |
| 114 | return std::vector<char>(flatBufferBuilder.GetBufferPointer(), |
| 115 | flatBufferBuilder.GetBufferPointer() + flatBufferBuilder.GetSize()); |
| 116 | } |
| 117 | |
| 118 | template <typename T> |
| 119 | void BatchMatMulTest(tflite::BuiltinOperator bmmOperatorCode, |
| 120 | tflite::TensorType tensorType, |
| 121 | std::vector<armnn::BackendId>& backends, |
| 122 | std::vector<int32_t>& LHSInputShape, |
| 123 | std::vector<int32_t>& RHSInputShape, |
| 124 | std::vector<int32_t>& outputShape, |
| 125 | std::vector<T>& LHSInputValues, |
| 126 | std::vector<T>& RHSInputValues, |
| 127 | std::vector<T>& expectedOutputValues, |
| 128 | bool adjX = false, |
| 129 | bool adjY = false, |
| 130 | float quantScale = 1.0f, |
| 131 | int quantOffset = 0) |
| 132 | { |
| 133 | using namespace tflite; |
| 134 | std::vector<char> modelBuffer = CreateBatchMatMulTfLiteModel(bmmOperatorCode, |
| 135 | tensorType, |
| 136 | LHSInputShape, |
| 137 | RHSInputShape, |
| 138 | outputShape, |
| 139 | adjX, |
| 140 | adjY, |
| 141 | quantScale, |
| 142 | quantOffset); |
| 143 | |
| 144 | const Model* tfLiteModel = GetModel(modelBuffer.data()); |
| 145 | CHECK(tfLiteModel != nullptr); |
| 146 | // Create TfLite Interpreters |
| 147 | std::unique_ptr<Interpreter> armnnDelegateInterpreter; |
| 148 | CHECK(InterpreterBuilder(tfLiteModel, ::tflite::ops::builtin::BuiltinOpResolver()) |
| 149 | (&armnnDelegateInterpreter) == kTfLiteOk); |
| 150 | CHECK(armnnDelegateInterpreter != nullptr); |
| 151 | CHECK(armnnDelegateInterpreter->AllocateTensors() == kTfLiteOk); |
| 152 | |
| 153 | std::unique_ptr<Interpreter> tfLiteInterpreter; |
| 154 | CHECK(InterpreterBuilder(tfLiteModel, ::tflite::ops::builtin::BuiltinOpResolver()) |
| 155 | (&tfLiteInterpreter) == kTfLiteOk); |
| 156 | CHECK(tfLiteInterpreter != nullptr); |
| 157 | CHECK(tfLiteInterpreter->AllocateTensors() == kTfLiteOk); |
| 158 | |
| 159 | // Create the ArmNN Delegate |
| 160 | armnnDelegate::DelegateOptions delegateOptions(backends); |
| 161 | std::unique_ptr<TfLiteDelegate, decltype(&armnnDelegate::TfLiteArmnnDelegateDelete)> |
| 162 | theArmnnDelegate(armnnDelegate::TfLiteArmnnDelegateCreate(delegateOptions), |
| 163 | armnnDelegate::TfLiteArmnnDelegateDelete); |
| 164 | CHECK(theArmnnDelegate != nullptr); |
| 165 | // Modify armnnDelegateInterpreter to use armnnDelegate |
| 166 | CHECK(armnnDelegateInterpreter->ModifyGraphWithDelegate(theArmnnDelegate.get()) == kTfLiteOk); |
| 167 | |
| 168 | // Set input data |
| 169 | auto tfLiteDelegateLHSInputId = tfLiteInterpreter->inputs()[0]; |
| 170 | auto tfLiteDelegateLHSInputData = tfLiteInterpreter->typed_tensor<T>(tfLiteDelegateLHSInputId); |
| 171 | auto tfLiteDelegateRHSInputId = tfLiteInterpreter->inputs()[1]; |
| 172 | auto tfLiteDelegateRHSInputData = tfLiteInterpreter->typed_tensor<T>(tfLiteDelegateRHSInputId); |
| 173 | for (unsigned int i = 0; i < LHSInputValues.size(); ++i) |
| 174 | { |
| 175 | tfLiteDelegateLHSInputData[i] = LHSInputValues[i]; |
| 176 | } |
| 177 | for (unsigned int i = 0; i < RHSInputValues.size(); ++i) |
| 178 | { |
| 179 | tfLiteDelegateRHSInputData[i] = RHSInputValues[i]; |
| 180 | } |
| 181 | |
| 182 | auto armnnDelegateLHSInputId = armnnDelegateInterpreter->inputs()[0]; |
| 183 | auto armnnDelegateLHSInputData = armnnDelegateInterpreter->typed_tensor<T>(armnnDelegateLHSInputId); |
| 184 | auto armnnDelegateRHSInputId = armnnDelegateInterpreter->inputs()[1]; |
| 185 | auto armnnDelegateRHSInputData = armnnDelegateInterpreter->typed_tensor<T>(armnnDelegateRHSInputId); |
| 186 | for (unsigned int i = 0; i < LHSInputValues.size(); ++i) |
| 187 | { |
| 188 | armnnDelegateLHSInputData[i] = LHSInputValues[i]; |
| 189 | } |
| 190 | for (unsigned int i = 0; i < RHSInputValues.size(); ++i) |
| 191 | { |
| 192 | armnnDelegateRHSInputData[i] = RHSInputValues[i]; |
| 193 | } |
| 194 | // Run EnqueueWorkload |
| 195 | CHECK(tfLiteInterpreter->Invoke() == kTfLiteOk); |
| 196 | CHECK(armnnDelegateInterpreter->Invoke() == kTfLiteOk); |
| 197 | |
| 198 | armnnDelegate::CompareOutputData(tfLiteInterpreter, armnnDelegateInterpreter, |
| 199 | outputShape, expectedOutputValues); |
| 200 | } |
| 201 | |
| 202 | } // anonymous namespace |
| 203 | |
| 204 | |
| 205 | |
| 206 | |