Matthew Sloyan | c8eb955 | 2020-11-26 10:54:22 +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 | std::vector<char> CreateLogicalBinaryTfLiteModel(tflite::BuiltinOperator logicalOperatorCode, |
| 25 | tflite::TensorType tensorType, |
| 26 | const std::vector <int32_t>& input0TensorShape, |
| 27 | const std::vector <int32_t>& input1TensorShape, |
| 28 | const std::vector <int32_t>& outputTensorShape, |
| 29 | float quantScale = 1.0f, |
| 30 | int quantOffset = 0) |
| 31 | { |
| 32 | using namespace tflite; |
| 33 | flatbuffers::FlatBufferBuilder flatBufferBuilder; |
| 34 | |
| 35 | std::vector<flatbuffers::Offset<tflite::Buffer>> buffers; |
| 36 | buffers.push_back(CreateBuffer(flatBufferBuilder, flatBufferBuilder.CreateVector({}))); |
| 37 | |
| 38 | auto quantizationParameters = |
| 39 | CreateQuantizationParameters(flatBufferBuilder, |
| 40 | 0, |
| 41 | 0, |
| 42 | flatBufferBuilder.CreateVector<float>({ quantScale }), |
| 43 | flatBufferBuilder.CreateVector<int64_t>({ quantOffset })); |
| 44 | |
| 45 | |
| 46 | std::array<flatbuffers::Offset<Tensor>, 3> tensors; |
| 47 | tensors[0] = CreateTensor(flatBufferBuilder, |
| 48 | flatBufferBuilder.CreateVector<int32_t>(input0TensorShape.data(), |
| 49 | input0TensorShape.size()), |
| 50 | tensorType, |
| 51 | 0, |
| 52 | flatBufferBuilder.CreateString("input_0"), |
| 53 | quantizationParameters); |
| 54 | tensors[1] = CreateTensor(flatBufferBuilder, |
| 55 | flatBufferBuilder.CreateVector<int32_t>(input1TensorShape.data(), |
| 56 | input1TensorShape.size()), |
| 57 | tensorType, |
| 58 | 0, |
| 59 | flatBufferBuilder.CreateString("input_1"), |
| 60 | quantizationParameters); |
| 61 | tensors[2] = CreateTensor(flatBufferBuilder, |
| 62 | flatBufferBuilder.CreateVector<int32_t>(outputTensorShape.data(), |
| 63 | outputTensorShape.size()), |
| 64 | tensorType, |
| 65 | 0, |
| 66 | flatBufferBuilder.CreateString("output"), |
| 67 | quantizationParameters); |
| 68 | |
| 69 | // create operator |
| 70 | tflite::BuiltinOptions operatorBuiltinOptionsType = tflite::BuiltinOptions_NONE; |
| 71 | flatbuffers::Offset<void> operatorBuiltinOptions = 0; |
| 72 | switch (logicalOperatorCode) |
| 73 | { |
| 74 | case BuiltinOperator_LOGICAL_AND: |
| 75 | { |
| 76 | operatorBuiltinOptionsType = BuiltinOptions_LogicalAndOptions; |
| 77 | operatorBuiltinOptions = CreateLogicalAndOptions(flatBufferBuilder).Union(); |
| 78 | break; |
| 79 | } |
| 80 | case BuiltinOperator_LOGICAL_OR: |
| 81 | { |
| 82 | operatorBuiltinOptionsType = BuiltinOptions_LogicalOrOptions; |
| 83 | operatorBuiltinOptions = CreateLogicalOrOptions(flatBufferBuilder).Union(); |
| 84 | break; |
| 85 | } |
| 86 | default: |
| 87 | break; |
| 88 | } |
| 89 | const std::vector<int32_t> operatorInputs{ {0, 1} }; |
| 90 | const std::vector<int32_t> operatorOutputs{ 2 }; |
| 91 | flatbuffers::Offset <Operator> logicalBinaryOperator = |
| 92 | CreateOperator(flatBufferBuilder, |
| 93 | 0, |
| 94 | flatBufferBuilder.CreateVector<int32_t>(operatorInputs.data(), operatorInputs.size()), |
| 95 | flatBufferBuilder.CreateVector<int32_t>(operatorOutputs.data(), operatorOutputs.size()), |
| 96 | operatorBuiltinOptionsType, |
| 97 | operatorBuiltinOptions); |
| 98 | |
| 99 | const std::vector<int> subgraphInputs{ {0, 1} }; |
| 100 | const std::vector<int> subgraphOutputs{ 2 }; |
| 101 | flatbuffers::Offset <SubGraph> subgraph = |
| 102 | CreateSubGraph(flatBufferBuilder, |
| 103 | flatBufferBuilder.CreateVector(tensors.data(), tensors.size()), |
| 104 | flatBufferBuilder.CreateVector<int32_t>(subgraphInputs.data(), subgraphInputs.size()), |
| 105 | flatBufferBuilder.CreateVector<int32_t>(subgraphOutputs.data(), subgraphOutputs.size()), |
| 106 | flatBufferBuilder.CreateVector(&logicalBinaryOperator, 1)); |
| 107 | |
| 108 | flatbuffers::Offset <flatbuffers::String> modelDescription = |
| 109 | flatBufferBuilder.CreateString("ArmnnDelegate: Logical Binary Operator Model"); |
| 110 | flatbuffers::Offset <OperatorCode> operatorCode = CreateOperatorCode(flatBufferBuilder, logicalOperatorCode); |
| 111 | |
| 112 | flatbuffers::Offset <Model> flatbufferModel = |
| 113 | CreateModel(flatBufferBuilder, |
| 114 | TFLITE_SCHEMA_VERSION, |
| 115 | flatBufferBuilder.CreateVector(&operatorCode, 1), |
| 116 | flatBufferBuilder.CreateVector(&subgraph, 1), |
| 117 | modelDescription, |
| 118 | flatBufferBuilder.CreateVector(buffers.data(), buffers.size())); |
| 119 | |
| 120 | flatBufferBuilder.Finish(flatbufferModel); |
| 121 | |
| 122 | return std::vector<char>(flatBufferBuilder.GetBufferPointer(), |
| 123 | flatBufferBuilder.GetBufferPointer() + flatBufferBuilder.GetSize()); |
| 124 | } |
| 125 | |
| 126 | template <typename T> |
| 127 | void LogicalBinaryTest(tflite::BuiltinOperator logicalOperatorCode, |
| 128 | tflite::TensorType tensorType, |
| 129 | std::vector<armnn::BackendId>& backends, |
| 130 | std::vector<int32_t>& input0Shape, |
| 131 | std::vector<int32_t>& input1Shape, |
| 132 | std::vector<int32_t>& expectedOutputShape, |
| 133 | std::vector<T>& input0Values, |
| 134 | std::vector<T>& input1Values, |
| 135 | std::vector<T>& expectedOutputValues, |
| 136 | float quantScale = 1.0f, |
| 137 | int quantOffset = 0) |
| 138 | { |
| 139 | using namespace tflite; |
| 140 | std::vector<char> modelBuffer = CreateLogicalBinaryTfLiteModel(logicalOperatorCode, |
| 141 | tensorType, |
| 142 | input0Shape, |
| 143 | input1Shape, |
| 144 | expectedOutputShape, |
| 145 | quantScale, |
| 146 | quantOffset); |
| 147 | |
| 148 | const Model* tfLiteModel = GetModel(modelBuffer.data()); |
| 149 | // Create TfLite Interpreters |
| 150 | std::unique_ptr<Interpreter> armnnDelegateInterpreter; |
| 151 | CHECK(InterpreterBuilder(tfLiteModel, ::tflite::ops::builtin::BuiltinOpResolver()) |
| 152 | (&armnnDelegateInterpreter) == kTfLiteOk); |
| 153 | CHECK(armnnDelegateInterpreter != nullptr); |
| 154 | CHECK(armnnDelegateInterpreter->AllocateTensors() == kTfLiteOk); |
| 155 | |
| 156 | std::unique_ptr<Interpreter> tfLiteInterpreter; |
| 157 | CHECK(InterpreterBuilder(tfLiteModel, ::tflite::ops::builtin::BuiltinOpResolver()) |
| 158 | (&tfLiteInterpreter) == kTfLiteOk); |
| 159 | CHECK(tfLiteInterpreter != nullptr); |
| 160 | CHECK(tfLiteInterpreter->AllocateTensors() == kTfLiteOk); |
| 161 | |
| 162 | // Create the ArmNN Delegate |
| 163 | armnnDelegate::DelegateOptions delegateOptions(backends); |
| 164 | std::unique_ptr<TfLiteDelegate, decltype(&armnnDelegate::TfLiteArmnnDelegateDelete)> |
| 165 | theArmnnDelegate(armnnDelegate::TfLiteArmnnDelegateCreate(delegateOptions), |
| 166 | armnnDelegate::TfLiteArmnnDelegateDelete); |
| 167 | CHECK(theArmnnDelegate != nullptr); |
| 168 | // Modify armnnDelegateInterpreter to use armnnDelegate |
| 169 | CHECK(armnnDelegateInterpreter->ModifyGraphWithDelegate(theArmnnDelegate.get()) == kTfLiteOk); |
| 170 | |
| 171 | // Set input data for the armnn interpreter |
| 172 | armnnDelegate::FillInput(armnnDelegateInterpreter, 0, input0Values); |
| 173 | armnnDelegate::FillInput(armnnDelegateInterpreter, 1, input1Values); |
| 174 | |
| 175 | // Set input data for the tflite interpreter |
| 176 | armnnDelegate::FillInput(tfLiteInterpreter, 0, input0Values); |
| 177 | armnnDelegate::FillInput(tfLiteInterpreter, 1, input1Values); |
| 178 | |
| 179 | // Run EnqueWorkload |
| 180 | CHECK(tfLiteInterpreter->Invoke() == kTfLiteOk); |
| 181 | CHECK(armnnDelegateInterpreter->Invoke() == kTfLiteOk); |
| 182 | |
| 183 | // Compare output data, comparing Boolean values is handled differently and needs to call the CompareData function |
| 184 | // directly. This is because Boolean types get converted to a bit representation in a vector. |
| 185 | auto tfLiteDelegateOutputId = tfLiteInterpreter->outputs()[0]; |
| 186 | auto tfLiteDelegateOutputData = tfLiteInterpreter->typed_tensor<T>(tfLiteDelegateOutputId); |
| 187 | auto armnnDelegateOutputId = armnnDelegateInterpreter->outputs()[0]; |
| 188 | auto armnnDelegateOutputData = armnnDelegateInterpreter->typed_tensor<T>(armnnDelegateOutputId); |
| 189 | |
| 190 | armnnDelegate::CompareData(expectedOutputValues, armnnDelegateOutputData, expectedOutputValues.size()); |
| 191 | armnnDelegate::CompareData(expectedOutputValues, tfLiteDelegateOutputData, expectedOutputValues.size()); |
| 192 | armnnDelegate::CompareData(tfLiteDelegateOutputData, armnnDelegateOutputData, expectedOutputValues.size()); |
| 193 | |
| 194 | armnnDelegateInterpreter.reset(nullptr); |
| 195 | tfLiteInterpreter.reset(nullptr); |
| 196 | } |
| 197 | |
| 198 | } // anonymous namespace |