Sadik Armagan | dc032fc | 2021-01-19 17:24:21 +0000 | [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 | namespace |
| 22 | { |
| 23 | |
| 24 | template <typename InputT, typename OutputT> |
| 25 | std::vector<char> CreateArgMinMaxTfLiteModel(tflite::BuiltinOperator argMinMaxOperatorCode, |
| 26 | tflite::TensorType tensorType, |
| 27 | const std::vector<int32_t>& inputTensorShape, |
| 28 | const std::vector<int32_t>& axisTensorShape, |
| 29 | const std::vector<int32_t>& outputTensorShape, |
| 30 | const std::vector<OutputT> axisValue, |
| 31 | tflite::TensorType outputType, |
| 32 | float quantScale = 1.0f, |
| 33 | int quantOffset = 0) |
| 34 | { |
| 35 | using namespace tflite; |
| 36 | flatbuffers::FlatBufferBuilder flatBufferBuilder; |
| 37 | |
| 38 | auto quantizationParameters = |
| 39 | CreateQuantizationParameters(flatBufferBuilder, |
| 40 | 0, |
| 41 | 0, |
| 42 | flatBufferBuilder.CreateVector<float>({ quantScale }), |
| 43 | flatBufferBuilder.CreateVector<int64_t>({ quantOffset })); |
| 44 | |
| 45 | auto inputTensor = CreateTensor(flatBufferBuilder, |
| 46 | flatBufferBuilder.CreateVector<int32_t>(inputTensorShape.data(), |
| 47 | inputTensorShape.size()), |
| 48 | tensorType, |
| 49 | 0, |
| 50 | flatBufferBuilder.CreateString("input"), |
| 51 | quantizationParameters); |
| 52 | |
| 53 | auto axisTensor = CreateTensor(flatBufferBuilder, |
| 54 | flatBufferBuilder.CreateVector<int32_t>(axisTensorShape.data(), |
| 55 | axisTensorShape.size()), |
| 56 | tflite::TensorType_INT32, |
| 57 | 1, |
| 58 | flatBufferBuilder.CreateString("axis")); |
| 59 | |
| 60 | auto outputTensor = CreateTensor(flatBufferBuilder, |
| 61 | flatBufferBuilder.CreateVector<int32_t>(outputTensorShape.data(), |
| 62 | outputTensorShape.size()), |
| 63 | outputType, |
| 64 | 2, |
| 65 | flatBufferBuilder.CreateString("output"), |
| 66 | quantizationParameters); |
| 67 | |
| 68 | std::vector<flatbuffers::Offset<Tensor>> tensors = { inputTensor, axisTensor, outputTensor }; |
| 69 | |
| 70 | std::vector<flatbuffers::Offset<tflite::Buffer>> buffers; |
| 71 | buffers.push_back(CreateBuffer(flatBufferBuilder, flatBufferBuilder.CreateVector({}))); |
| 72 | buffers.push_back( |
| 73 | CreateBuffer(flatBufferBuilder, |
| 74 | flatBufferBuilder.CreateVector(reinterpret_cast<const uint8_t*>(axisValue.data()), |
| 75 | sizeof(OutputT)))); |
| 76 | buffers.push_back(CreateBuffer(flatBufferBuilder, flatBufferBuilder.CreateVector({}))); |
| 77 | |
| 78 | std::vector<int32_t> operatorInputs = {{ 0, 1 }}; |
| 79 | std::vector<int> subgraphInputs = {{ 0, 1 }}; |
| 80 | |
| 81 | tflite::BuiltinOptions operatorBuiltinOptionsType = BuiltinOptions_ArgMaxOptions; |
| 82 | flatbuffers::Offset<void> operatorBuiltinOptions = CreateArgMaxOptions(flatBufferBuilder, outputType).Union(); |
| 83 | |
| 84 | if (argMinMaxOperatorCode == tflite::BuiltinOperator_ARG_MIN) |
| 85 | { |
| 86 | operatorBuiltinOptionsType = BuiltinOptions_ArgMinOptions; |
| 87 | operatorBuiltinOptions = CreateArgMinOptions(flatBufferBuilder, outputType).Union(); |
| 88 | } |
| 89 | |
| 90 | // create operator |
Keith Davis | bbc876c | 2021-01-27 13:12:03 +0000 | [diff] [blame] | 91 | const std::vector<int32_t> operatorOutputs{ 2 }; |
Sadik Armagan | dc032fc | 2021-01-19 17:24:21 +0000 | [diff] [blame] | 92 | flatbuffers::Offset <Operator> argMinMaxOperator = |
| 93 | CreateOperator(flatBufferBuilder, |
| 94 | 0, |
| 95 | flatBufferBuilder.CreateVector<int32_t>(operatorInputs.data(), operatorInputs.size()), |
| 96 | flatBufferBuilder.CreateVector<int32_t>(operatorOutputs.data(), operatorOutputs.size()), |
| 97 | operatorBuiltinOptionsType, |
| 98 | operatorBuiltinOptions); |
| 99 | |
Keith Davis | bbc876c | 2021-01-27 13:12:03 +0000 | [diff] [blame] | 100 | const std::vector<int> subgraphOutputs{ 2 }; |
Sadik Armagan | dc032fc | 2021-01-19 17:24:21 +0000 | [diff] [blame] | 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(&argMinMaxOperator, 1)); |
| 107 | |
| 108 | flatbuffers::Offset <flatbuffers::String> modelDescription = |
| 109 | flatBufferBuilder.CreateString("ArmnnDelegate: ArgMinMax Operator Model"); |
| 110 | flatbuffers::Offset <OperatorCode> operatorCode = CreateOperatorCode(flatBufferBuilder, |
| 111 | argMinMaxOperatorCode); |
| 112 | |
| 113 | flatbuffers::Offset <Model> flatbufferModel = |
| 114 | CreateModel(flatBufferBuilder, |
| 115 | TFLITE_SCHEMA_VERSION, |
| 116 | flatBufferBuilder.CreateVector(&operatorCode, 1), |
| 117 | flatBufferBuilder.CreateVector(&subgraph, 1), |
| 118 | modelDescription, |
| 119 | flatBufferBuilder.CreateVector(buffers.data(), buffers.size())); |
| 120 | |
| 121 | flatBufferBuilder.Finish(flatbufferModel); |
| 122 | |
| 123 | return std::vector<char>(flatBufferBuilder.GetBufferPointer(), |
| 124 | flatBufferBuilder.GetBufferPointer() + flatBufferBuilder.GetSize()); |
| 125 | } |
| 126 | |
| 127 | template <typename InputT, typename OutputT> |
| 128 | void ArgMinMaxTest(tflite::BuiltinOperator argMinMaxOperatorCode, |
| 129 | tflite::TensorType tensorType, |
| 130 | const std::vector<armnn::BackendId>& backends, |
| 131 | const std::vector<int32_t>& inputShape, |
| 132 | const std::vector<int32_t>& axisShape, |
| 133 | std::vector<int32_t>& outputShape, |
| 134 | std::vector<InputT>& inputValues, |
| 135 | std::vector<OutputT>& expectedOutputValues, |
| 136 | OutputT axisValue, |
| 137 | tflite::TensorType outputType, |
| 138 | float quantScale = 1.0f, |
| 139 | int quantOffset = 0) |
| 140 | { |
| 141 | using namespace tflite; |
| 142 | std::vector<char> modelBuffer = CreateArgMinMaxTfLiteModel<InputT, OutputT>(argMinMaxOperatorCode, |
| 143 | tensorType, |
| 144 | inputShape, |
| 145 | axisShape, |
| 146 | outputShape, |
| 147 | {axisValue}, |
| 148 | outputType, |
| 149 | quantScale, |
| 150 | quantOffset); |
| 151 | |
| 152 | const Model* tfLiteModel = GetModel(modelBuffer.data()); |
| 153 | CHECK(tfLiteModel != nullptr); |
| 154 | |
| 155 | std::unique_ptr<Interpreter> armnnDelegateInterpreter; |
| 156 | CHECK(InterpreterBuilder(tfLiteModel, ::tflite::ops::builtin::BuiltinOpResolver()) |
| 157 | (&armnnDelegateInterpreter) == kTfLiteOk); |
| 158 | CHECK(armnnDelegateInterpreter != nullptr); |
| 159 | CHECK(armnnDelegateInterpreter->AllocateTensors() == kTfLiteOk); |
| 160 | |
| 161 | std::unique_ptr<Interpreter> tfLiteInterpreter; |
| 162 | CHECK(InterpreterBuilder(tfLiteModel, ::tflite::ops::builtin::BuiltinOpResolver()) |
| 163 | (&tfLiteInterpreter) == kTfLiteOk); |
| 164 | CHECK(tfLiteInterpreter != nullptr); |
| 165 | CHECK(tfLiteInterpreter->AllocateTensors() == kTfLiteOk); |
| 166 | |
| 167 | // Create the ArmNN Delegate |
| 168 | armnnDelegate::DelegateOptions delegateOptions(backends); |
| 169 | std::unique_ptr<TfLiteDelegate, decltype(&armnnDelegate::TfLiteArmnnDelegateDelete)> |
| 170 | theArmnnDelegate(armnnDelegate::TfLiteArmnnDelegateCreate(delegateOptions), |
| 171 | armnnDelegate::TfLiteArmnnDelegateDelete); |
| 172 | CHECK(theArmnnDelegate != nullptr); |
| 173 | // Modify armnnDelegateInterpreter to use armnnDelegate |
| 174 | CHECK(armnnDelegateInterpreter->ModifyGraphWithDelegate(theArmnnDelegate.get()) == kTfLiteOk); |
| 175 | |
| 176 | // Set input data |
| 177 | armnnDelegate::FillInput<InputT>(tfLiteInterpreter, 0, inputValues); |
| 178 | armnnDelegate::FillInput<InputT>(armnnDelegateInterpreter, 0, inputValues); |
| 179 | |
| 180 | // Run EnqueueWorkload |
| 181 | CHECK(tfLiteInterpreter->Invoke() == kTfLiteOk); |
| 182 | CHECK(armnnDelegateInterpreter->Invoke() == kTfLiteOk); |
| 183 | |
| 184 | // Compare output data |
| 185 | auto tfLiteDelegateOutputId = tfLiteInterpreter->outputs()[0]; |
| 186 | auto tfLiteDelageOutputData = tfLiteInterpreter->typed_tensor<OutputT>(tfLiteDelegateOutputId); |
| 187 | auto armnnDelegateOutputId = armnnDelegateInterpreter->outputs()[0]; |
| 188 | auto armnnDelegateOutputData = armnnDelegateInterpreter->typed_tensor<OutputT>(armnnDelegateOutputId); |
| 189 | |
| 190 | for (size_t i = 0; i < expectedOutputValues.size(); i++) |
| 191 | { |
| 192 | CHECK(expectedOutputValues[i] == armnnDelegateOutputData[i]); |
| 193 | CHECK(tfLiteDelageOutputData[i] == expectedOutputValues[i]); |
| 194 | CHECK(tfLiteDelageOutputData[i] == armnnDelegateOutputData[i]); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | } // anonymous namespace |