James Conroy | 3982548 | 2021-05-27 17:44:50 +0100 | [diff] [blame] | 1 | // |
Ryan OShea | 238ecd9 | 2023-03-07 11:44:23 +0000 | [diff] [blame^] | 2 | // Copyright © 2021, 2023 Arm Ltd and Contributors. All rights reserved. |
James Conroy | 3982548 | 2021-05-27 17:44:50 +0100 | [diff] [blame] | 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> CreatePreluTfLiteModel(tflite::BuiltinOperator preluOperatorCode, |
| 25 | tflite::TensorType tensorType, |
| 26 | const std::vector<int32_t>& inputShape, |
| 27 | const std::vector<int32_t>& alphaShape, |
| 28 | const std::vector<int32_t>& outputShape, |
| 29 | std::vector<float>& alphaData, |
| 30 | bool alphaIsConstant) |
| 31 | { |
| 32 | using namespace tflite; |
| 33 | flatbuffers::FlatBufferBuilder flatBufferBuilder; |
| 34 | |
| 35 | std::vector<flatbuffers::Offset<tflite::Buffer>> buffers; |
Ryan OShea | 238ecd9 | 2023-03-07 11:44:23 +0000 | [diff] [blame^] | 36 | buffers.push_back(CreateBuffer(flatBufferBuilder)); |
| 37 | buffers.push_back(CreateBuffer(flatBufferBuilder)); |
James Conroy | 3982548 | 2021-05-27 17:44:50 +0100 | [diff] [blame] | 38 | buffers.push_back(CreateBuffer(flatBufferBuilder, flatBufferBuilder.CreateVector( |
| 39 | reinterpret_cast<const uint8_t *>(alphaData.data()), sizeof(float) * alphaData.size()))); |
Ryan OShea | 238ecd9 | 2023-03-07 11:44:23 +0000 | [diff] [blame^] | 40 | buffers.push_back(CreateBuffer(flatBufferBuilder)); |
| 41 | |
James Conroy | 3982548 | 2021-05-27 17:44:50 +0100 | [diff] [blame] | 42 | |
| 43 | auto quantizationParameters = |
| 44 | CreateQuantizationParameters(flatBufferBuilder, |
| 45 | 0, |
| 46 | 0, |
| 47 | flatBufferBuilder.CreateVector<float>({ 1.0f }), |
| 48 | flatBufferBuilder.CreateVector<int64_t>({ 0 })); |
| 49 | |
| 50 | auto inputTensor = CreateTensor(flatBufferBuilder, |
| 51 | flatBufferBuilder.CreateVector<int32_t>(inputShape.data(), |
| 52 | inputShape.size()), |
| 53 | tensorType, |
Ryan OShea | 238ecd9 | 2023-03-07 11:44:23 +0000 | [diff] [blame^] | 54 | 1, |
James Conroy | 3982548 | 2021-05-27 17:44:50 +0100 | [diff] [blame] | 55 | flatBufferBuilder.CreateString("input"), |
| 56 | quantizationParameters); |
| 57 | |
| 58 | auto alphaTensor = CreateTensor(flatBufferBuilder, |
| 59 | flatBufferBuilder.CreateVector<int32_t>(alphaShape.data(), |
| 60 | alphaShape.size()), |
| 61 | tensorType, |
Ryan OShea | 238ecd9 | 2023-03-07 11:44:23 +0000 | [diff] [blame^] | 62 | 2, |
James Conroy | 3982548 | 2021-05-27 17:44:50 +0100 | [diff] [blame] | 63 | flatBufferBuilder.CreateString("alpha"), |
| 64 | quantizationParameters); |
| 65 | |
| 66 | auto outputTensor = CreateTensor(flatBufferBuilder, |
| 67 | flatBufferBuilder.CreateVector<int32_t>(outputShape.data(), |
| 68 | outputShape.size()), |
| 69 | tensorType, |
Ryan OShea | 238ecd9 | 2023-03-07 11:44:23 +0000 | [diff] [blame^] | 70 | 3, |
James Conroy | 3982548 | 2021-05-27 17:44:50 +0100 | [diff] [blame] | 71 | flatBufferBuilder.CreateString("output"), |
| 72 | quantizationParameters); |
| 73 | |
| 74 | std::vector<flatbuffers::Offset<Tensor>> tensors = { inputTensor, alphaTensor, outputTensor }; |
| 75 | |
| 76 | const std::vector<int> operatorInputs{0, 1}; |
| 77 | const std::vector<int> operatorOutputs{2}; |
| 78 | flatbuffers::Offset <Operator> preluOperator = |
| 79 | CreateOperator(flatBufferBuilder, |
| 80 | 0, |
| 81 | flatBufferBuilder.CreateVector<int32_t>(operatorInputs.data(), operatorInputs.size()), |
| 82 | flatBufferBuilder.CreateVector<int32_t>(operatorOutputs.data(), operatorOutputs.size())); |
| 83 | |
| 84 | std::vector<int> subgraphInputs{0}; |
| 85 | if (!alphaIsConstant) |
| 86 | { |
| 87 | subgraphInputs.push_back(1); |
| 88 | } |
| 89 | |
| 90 | const std::vector<int> subgraphOutputs{2}; |
| 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(&preluOperator, 1)); |
| 97 | |
| 98 | flatbuffers::Offset <flatbuffers::String> modelDescription = |
| 99 | flatBufferBuilder.CreateString("ArmnnDelegate: Prelu Operator Model"); |
| 100 | flatbuffers::Offset <OperatorCode> opCode = CreateOperatorCode(flatBufferBuilder, preluOperatorCode); |
| 101 | |
| 102 | flatbuffers::Offset <Model> flatbufferModel = |
| 103 | CreateModel(flatBufferBuilder, |
| 104 | TFLITE_SCHEMA_VERSION, |
| 105 | flatBufferBuilder.CreateVector(&opCode, 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 | void PreluTest(tflite::BuiltinOperator preluOperatorCode, |
| 117 | tflite::TensorType tensorType, |
| 118 | const std::vector<armnn::BackendId>& backends, |
| 119 | const std::vector<int32_t>& inputShape, |
| 120 | const std::vector<int32_t>& alphaShape, |
| 121 | std::vector<int32_t>& outputShape, |
| 122 | std::vector<float>& inputData, |
| 123 | std::vector<float>& alphaData, |
| 124 | std::vector<float>& expectedOutput, |
| 125 | bool alphaIsConstant) |
| 126 | { |
| 127 | using namespace tflite; |
| 128 | |
| 129 | std::vector<char> modelBuffer = CreatePreluTfLiteModel(preluOperatorCode, |
| 130 | tensorType, |
| 131 | inputShape, |
| 132 | alphaShape, |
| 133 | outputShape, |
| 134 | alphaData, |
| 135 | alphaIsConstant); |
| 136 | |
| 137 | const Model* tfLiteModel = GetModel(modelBuffer.data()); |
| 138 | |
| 139 | CHECK(tfLiteModel != nullptr); |
| 140 | |
| 141 | std::unique_ptr<Interpreter> armnnDelegateInterpreter; |
| 142 | |
| 143 | CHECK(InterpreterBuilder(tfLiteModel, ::tflite::ops::builtin::BuiltinOpResolver()) |
| 144 | (&armnnDelegateInterpreter) == kTfLiteOk); |
| 145 | CHECK(armnnDelegateInterpreter != nullptr); |
| 146 | CHECK(armnnDelegateInterpreter->AllocateTensors() == kTfLiteOk); |
| 147 | |
| 148 | std::unique_ptr<Interpreter> tfLiteInterpreter; |
| 149 | |
| 150 | CHECK(InterpreterBuilder(tfLiteModel, ::tflite::ops::builtin::BuiltinOpResolver()) |
| 151 | (&tfLiteInterpreter) == kTfLiteOk); |
| 152 | CHECK(tfLiteInterpreter != nullptr); |
| 153 | CHECK(tfLiteInterpreter->AllocateTensors() == kTfLiteOk); |
| 154 | |
| 155 | // Create the ArmNN Delegate |
| 156 | armnnDelegate::DelegateOptions delegateOptions(backends); |
| 157 | |
| 158 | std::unique_ptr<TfLiteDelegate, decltype(&armnnDelegate::TfLiteArmnnDelegateDelete)> |
| 159 | theArmnnDelegate(armnnDelegate::TfLiteArmnnDelegateCreate(delegateOptions), |
| 160 | armnnDelegate::TfLiteArmnnDelegateDelete); |
| 161 | CHECK(theArmnnDelegate != nullptr); |
| 162 | |
| 163 | // Modify armnnDelegateInterpreter to use armnnDelegate |
| 164 | CHECK(armnnDelegateInterpreter->ModifyGraphWithDelegate(theArmnnDelegate.get()) == kTfLiteOk); |
| 165 | |
| 166 | // Set input data |
| 167 | armnnDelegate::FillInput<float>(tfLiteInterpreter, 0, inputData); |
| 168 | armnnDelegate::FillInput<float>(armnnDelegateInterpreter, 0, inputData); |
| 169 | |
| 170 | // Set alpha data if not constant |
| 171 | if (!alphaIsConstant) { |
| 172 | armnnDelegate::FillInput<float>(tfLiteInterpreter, 1, alphaData); |
| 173 | armnnDelegate::FillInput<float>(armnnDelegateInterpreter, 1, alphaData); |
| 174 | } |
| 175 | |
| 176 | // Run EnqueueWorkload |
| 177 | CHECK(tfLiteInterpreter->Invoke() == kTfLiteOk); |
| 178 | CHECK(armnnDelegateInterpreter->Invoke() == kTfLiteOk); |
| 179 | |
| 180 | // Compare output data |
| 181 | auto tfLiteDelegateOutputId = tfLiteInterpreter->outputs()[0]; |
| 182 | |
| 183 | auto tfLiteDelegateOutputData = tfLiteInterpreter->typed_tensor<float>(tfLiteDelegateOutputId); |
| 184 | |
| 185 | auto armnnDelegateOutputId = armnnDelegateInterpreter->outputs()[0]; |
| 186 | auto armnnDelegateOutputData = armnnDelegateInterpreter->typed_tensor<float>(armnnDelegateOutputId); |
| 187 | |
| 188 | for (size_t i = 0; i < expectedOutput.size(); i++) |
| 189 | { |
| 190 | CHECK(expectedOutput[i] == armnnDelegateOutputData[i]); |
| 191 | CHECK(tfLiteDelegateOutputData[i] == expectedOutput[i]); |
| 192 | CHECK(tfLiteDelegateOutputData[i] == armnnDelegateOutputData[i]); |
| 193 | } |
| 194 | } |
| 195 | } // anonymous namespace |