Sadik Armagan | 4b227bb | 2021-01-22 10:53:38 +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 | std::vector<char> CreateNormalizationTfLiteModel(tflite::BuiltinOperator normalizationOperatorCode, |
| 25 | tflite::TensorType tensorType, |
| 26 | const std::vector<int32_t>& inputTensorShape, |
| 27 | const std::vector<int32_t>& outputTensorShape, |
| 28 | int32_t radius, |
| 29 | float bias, |
| 30 | float alpha, |
| 31 | float beta, |
| 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 outputTensor = CreateTensor(flatBufferBuilder, |
| 54 | flatBufferBuilder.CreateVector<int32_t>(outputTensorShape.data(), |
| 55 | outputTensorShape.size()), |
| 56 | tensorType, |
| 57 | 1, |
| 58 | flatBufferBuilder.CreateString("output"), |
| 59 | quantizationParameters); |
| 60 | |
| 61 | std::vector<flatbuffers::Offset<Tensor>> tensors = { inputTensor, outputTensor }; |
| 62 | |
| 63 | std::vector<flatbuffers::Offset<tflite::Buffer>> buffers; |
| 64 | buffers.push_back(CreateBuffer(flatBufferBuilder, flatBufferBuilder.CreateVector({}))); |
| 65 | buffers.push_back(CreateBuffer(flatBufferBuilder, flatBufferBuilder.CreateVector({}))); |
| 66 | |
Keith Davis | 244b5bf | 2021-01-31 18:36:58 +0000 | [diff] [blame] | 67 | std::vector<int32_t> operatorInputs = { 0 }; |
| 68 | std::vector<int> subgraphInputs = { 0 }; |
Sadik Armagan | 4b227bb | 2021-01-22 10:53:38 +0000 | [diff] [blame] | 69 | |
| 70 | tflite::BuiltinOptions operatorBuiltinOptionsType = BuiltinOptions_L2NormOptions; |
| 71 | flatbuffers::Offset<void> operatorBuiltinOptions = CreateL2NormOptions(flatBufferBuilder, |
| 72 | tflite::ActivationFunctionType_NONE).Union(); |
| 73 | |
| 74 | if (normalizationOperatorCode == tflite::BuiltinOperator_LOCAL_RESPONSE_NORMALIZATION) |
| 75 | { |
| 76 | operatorBuiltinOptionsType = BuiltinOptions_LocalResponseNormalizationOptions; |
| 77 | operatorBuiltinOptions = |
| 78 | CreateLocalResponseNormalizationOptions(flatBufferBuilder, radius, bias, alpha, beta).Union(); |
| 79 | } |
| 80 | |
| 81 | // create operator |
Keith Davis | 244b5bf | 2021-01-31 18:36:58 +0000 | [diff] [blame] | 82 | const std::vector<int32_t> operatorOutputs{ 1 }; |
Sadik Armagan | 4b227bb | 2021-01-22 10:53:38 +0000 | [diff] [blame] | 83 | flatbuffers::Offset <Operator> normalizationOperator = |
| 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 | |
Keith Davis | 244b5bf | 2021-01-31 18:36:58 +0000 | [diff] [blame] | 91 | const std::vector<int> subgraphOutputs{ 1 }; |
Sadik Armagan | 4b227bb | 2021-01-22 10:53:38 +0000 | [diff] [blame] | 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(), subgraphOutputs.size()), |
| 97 | flatBufferBuilder.CreateVector(&normalizationOperator, 1)); |
| 98 | |
| 99 | flatbuffers::Offset <flatbuffers::String> modelDescription = |
| 100 | flatBufferBuilder.CreateString("ArmnnDelegate: Normalization Operator Model"); |
| 101 | flatbuffers::Offset <OperatorCode> operatorCode = CreateOperatorCode(flatBufferBuilder, |
| 102 | normalizationOperatorCode); |
| 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 NormalizationTest(tflite::BuiltinOperator normalizationOperatorCode, |
| 120 | tflite::TensorType tensorType, |
| 121 | const std::vector<armnn::BackendId>& backends, |
| 122 | const std::vector<int32_t>& inputShape, |
| 123 | std::vector<int32_t>& outputShape, |
| 124 | std::vector<T>& inputValues, |
| 125 | std::vector<T>& expectedOutputValues, |
| 126 | int32_t radius = 0, |
| 127 | float bias = 0.f, |
| 128 | float alpha = 0.f, |
| 129 | float beta = 0.f, |
| 130 | float quantScale = 1.0f, |
| 131 | int quantOffset = 0) |
| 132 | { |
| 133 | using namespace tflite; |
| 134 | std::vector<char> modelBuffer = CreateNormalizationTfLiteModel(normalizationOperatorCode, |
| 135 | tensorType, |
| 136 | inputShape, |
| 137 | outputShape, |
| 138 | radius, |
| 139 | bias, |
| 140 | alpha, |
| 141 | beta, |
| 142 | quantScale, |
| 143 | quantOffset); |
| 144 | |
| 145 | const Model* tfLiteModel = GetModel(modelBuffer.data()); |
| 146 | CHECK(tfLiteModel != nullptr); |
| 147 | |
| 148 | std::unique_ptr<Interpreter> armnnDelegateInterpreter; |
| 149 | CHECK(InterpreterBuilder(tfLiteModel, ::tflite::ops::builtin::BuiltinOpResolver()) |
| 150 | (&armnnDelegateInterpreter) == kTfLiteOk); |
| 151 | CHECK(armnnDelegateInterpreter != nullptr); |
| 152 | CHECK(armnnDelegateInterpreter->AllocateTensors() == kTfLiteOk); |
| 153 | |
| 154 | std::unique_ptr<Interpreter> tfLiteInterpreter; |
| 155 | CHECK(InterpreterBuilder(tfLiteModel, ::tflite::ops::builtin::BuiltinOpResolver()) |
| 156 | (&tfLiteInterpreter) == kTfLiteOk); |
| 157 | CHECK(tfLiteInterpreter != nullptr); |
| 158 | CHECK(tfLiteInterpreter->AllocateTensors() == kTfLiteOk); |
| 159 | |
| 160 | // Create the ArmNN Delegate |
| 161 | armnnDelegate::DelegateOptions delegateOptions(backends); |
| 162 | std::unique_ptr<TfLiteDelegate, decltype(&armnnDelegate::TfLiteArmnnDelegateDelete)> |
| 163 | theArmnnDelegate(armnnDelegate::TfLiteArmnnDelegateCreate(delegateOptions), |
| 164 | armnnDelegate::TfLiteArmnnDelegateDelete); |
| 165 | CHECK(theArmnnDelegate != nullptr); |
| 166 | // Modify armnnDelegateInterpreter to use armnnDelegate |
| 167 | CHECK(armnnDelegateInterpreter->ModifyGraphWithDelegate(theArmnnDelegate.get()) == kTfLiteOk); |
| 168 | |
| 169 | // Set input data |
| 170 | armnnDelegate::FillInput<T>(tfLiteInterpreter, 0, inputValues); |
| 171 | armnnDelegate::FillInput<T>(armnnDelegateInterpreter, 0, inputValues); |
| 172 | |
| 173 | // Run EnqueueWorkload |
| 174 | CHECK(tfLiteInterpreter->Invoke() == kTfLiteOk); |
| 175 | CHECK(armnnDelegateInterpreter->Invoke() == kTfLiteOk); |
| 176 | |
| 177 | // Compare output data |
| 178 | armnnDelegate::CompareOutputData(tfLiteInterpreter, armnnDelegateInterpreter, outputShape, expectedOutputValues); |
| 179 | } |
| 180 | |
Keith Davis | 7c67fab | 2021-04-08 11:47:23 +0100 | [diff] [blame] | 181 | void L2NormalizationTest(std::vector<armnn::BackendId>& backends) |
| 182 | { |
| 183 | // Set input data |
| 184 | std::vector<int32_t> inputShape { 1, 1, 1, 10 }; |
| 185 | std::vector<int32_t> outputShape { 1, 1, 1, 10 }; |
| 186 | |
| 187 | std::vector<float> inputValues |
| 188 | { |
| 189 | 1.0f, |
| 190 | 2.0f, |
| 191 | 3.0f, |
| 192 | 4.0f, |
| 193 | 5.0f, |
| 194 | 6.0f, |
| 195 | 7.0f, |
| 196 | 8.0f, |
| 197 | 9.0f, |
| 198 | 10.0f |
| 199 | }; |
| 200 | |
| 201 | const float approxInvL2Norm = 0.050964719f; |
| 202 | std::vector<float> expectedOutputValues |
| 203 | { |
| 204 | 1.0f * approxInvL2Norm, |
| 205 | 2.0f * approxInvL2Norm, |
| 206 | 3.0f * approxInvL2Norm, |
| 207 | 4.0f * approxInvL2Norm, |
| 208 | 5.0f * approxInvL2Norm, |
| 209 | 6.0f * approxInvL2Norm, |
| 210 | 7.0f * approxInvL2Norm, |
| 211 | 8.0f * approxInvL2Norm, |
| 212 | 9.0f * approxInvL2Norm, |
| 213 | 10.0f * approxInvL2Norm |
| 214 | }; |
| 215 | |
| 216 | NormalizationTest<float>(tflite::BuiltinOperator_L2_NORMALIZATION, |
| 217 | ::tflite::TensorType_FLOAT32, |
| 218 | backends, |
| 219 | inputShape, |
| 220 | outputShape, |
| 221 | inputValues, |
| 222 | expectedOutputValues); |
| 223 | } |
| 224 | |
| 225 | void LocalResponseNormalizationTest(std::vector<armnn::BackendId>& backends, |
| 226 | int32_t radius, |
| 227 | float bias, |
| 228 | float alpha, |
| 229 | float beta) |
| 230 | { |
| 231 | // Set input data |
| 232 | std::vector<int32_t> inputShape { 2, 2, 2, 1 }; |
| 233 | std::vector<int32_t> outputShape { 2, 2, 2, 1 }; |
| 234 | |
| 235 | std::vector<float> inputValues |
| 236 | { |
| 237 | 1.0f, 2.0f, |
| 238 | 3.0f, 4.0f, |
| 239 | 5.0f, 6.0f, |
| 240 | 7.0f, 8.0f |
| 241 | }; |
| 242 | |
| 243 | std::vector<float> expectedOutputValues |
| 244 | { |
| 245 | 0.5f, 0.400000006f, 0.300000012f, 0.235294119f, |
| 246 | 0.192307696f, 0.16216217f, 0.140000001f, 0.123076923f |
| 247 | }; |
| 248 | |
| 249 | NormalizationTest<float>(tflite::BuiltinOperator_LOCAL_RESPONSE_NORMALIZATION, |
| 250 | ::tflite::TensorType_FLOAT32, |
| 251 | backends, |
| 252 | inputShape, |
| 253 | outputShape, |
| 254 | inputValues, |
| 255 | expectedOutputValues, |
| 256 | radius, |
| 257 | bias, |
| 258 | alpha, |
| 259 | beta); |
| 260 | } |
| 261 | |
Sadik Armagan | 4b227bb | 2021-01-22 10:53:38 +0000 | [diff] [blame] | 262 | } // anonymous namespace |