blob: a34465081460e56cd46d9b2df36a00be592cb3c3 [file] [log] [blame]
Narumol Prangnawarat50c87d32020-11-09 18:42:11 +00001//
2// Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#pragma once
7
8#include <armnn_delegate.hpp>
9
10#include <flatbuffers/flatbuffers.h>
11#include <tensorflow/lite/interpreter.h>
12#include <tensorflow/lite/kernels/register.h>
13#include <tensorflow/lite/model.h>
14#include <tensorflow/lite/schema/schema_generated.h>
15#include <tensorflow/lite/version.h>
16
17#include <doctest/doctest.h>
18
19namespace
20{
21
22std::vector<char> CreatePooling2dTfLiteModel(
23 tflite::BuiltinOperator poolingOperatorCode,
24 tflite::TensorType tensorType,
25 const std::vector <int32_t>& inputTensorShape,
26 const std::vector <int32_t>& outputTensorShape,
27 tflite::Padding padding = tflite::Padding_SAME,
28 int32_t strideWidth = 0,
29 int32_t strideHeight = 0,
30 int32_t filterWidth = 0,
31 int32_t filterHeight = 0,
32 tflite::ActivationFunctionType fusedActivation = tflite::ActivationFunctionType_NONE,
33 float quantScale = 1.0f,
34 int quantOffset = 0)
35{
36 using namespace tflite;
37 flatbuffers::FlatBufferBuilder flatBufferBuilder;
38
39 std::vector<flatbuffers::Offset<tflite::Buffer>> buffers;
40 buffers.push_back(CreateBuffer(flatBufferBuilder, flatBufferBuilder.CreateVector({})));
41
42 auto quantizationParameters =
43 CreateQuantizationParameters(flatBufferBuilder,
44 0,
45 0,
46 flatBufferBuilder.CreateVector<float>({ quantScale }),
47 flatBufferBuilder.CreateVector<int64_t>({ quantOffset }));
48
49 std::array<flatbuffers::Offset<Tensor>, 2> tensors;
50 tensors[0] = CreateTensor(flatBufferBuilder,
51 flatBufferBuilder.CreateVector<int32_t>(inputTensorShape.data(),
52 inputTensorShape.size()),
53 tensorType,
54 0,
55 flatBufferBuilder.CreateString("input"),
56 quantizationParameters);
57
58 tensors[1] = CreateTensor(flatBufferBuilder,
59 flatBufferBuilder.CreateVector<int32_t>(outputTensorShape.data(),
60 outputTensorShape.size()),
61 tensorType,
62 0,
63 flatBufferBuilder.CreateString("output"),
64 quantizationParameters);
65
66 // create operator
67 tflite::BuiltinOptions operatorBuiltinOptionsType = BuiltinOptions_Pool2DOptions;
68 flatbuffers::Offset<void> operatorBuiltinOptions = CreatePool2DOptions(flatBufferBuilder,
69 padding,
70 strideWidth,
71 strideHeight,
72 filterWidth,
73 filterHeight,
74 fusedActivation).Union();
75
76 const std::vector<int32_t> operatorInputs{{0}};
77 const std::vector<int32_t> operatorOutputs{{1}};
78 flatbuffers::Offset <Operator> poolingOperator =
79 CreateOperator(flatBufferBuilder,
80 0,
81 flatBufferBuilder.CreateVector<int32_t>(operatorInputs.data(), operatorInputs.size()),
82 flatBufferBuilder.CreateVector<int32_t>(operatorOutputs.data(), operatorOutputs.size()),
83 operatorBuiltinOptionsType,
84 operatorBuiltinOptions);
85
86 const std::vector<int> subgraphInputs{{0}};
87 const std::vector<int> subgraphOutputs{{1}};
88 flatbuffers::Offset <SubGraph> subgraph =
89 CreateSubGraph(flatBufferBuilder,
90 flatBufferBuilder.CreateVector(tensors.data(), tensors.size()),
91 flatBufferBuilder.CreateVector<int32_t>(subgraphInputs.data(), subgraphInputs.size()),
92 flatBufferBuilder.CreateVector<int32_t>(subgraphOutputs.data(), subgraphOutputs.size()),
93 flatBufferBuilder.CreateVector(&poolingOperator, 1));
94
95 flatbuffers::Offset <flatbuffers::String> modelDescription =
96 flatBufferBuilder.CreateString("ArmnnDelegate: Pooling2d Operator Model");
97 flatbuffers::Offset <OperatorCode> operatorCode = CreateOperatorCode(flatBufferBuilder, poolingOperatorCode);
98
99 flatbuffers::Offset <Model> flatbufferModel =
100 CreateModel(flatBufferBuilder,
101 TFLITE_SCHEMA_VERSION,
102 flatBufferBuilder.CreateVector(&operatorCode, 1),
103 flatBufferBuilder.CreateVector(&subgraph, 1),
104 modelDescription,
105 flatBufferBuilder.CreateVector(buffers.data(), buffers.size()));
106
107 flatBufferBuilder.Finish(flatbufferModel);
108
109 return std::vector<char>(flatBufferBuilder.GetBufferPointer(),
110 flatBufferBuilder.GetBufferPointer() + flatBufferBuilder.GetSize());
111}
112
113template <typename T>
114void Pooling2dTest(tflite::BuiltinOperator poolingOperatorCode,
115 tflite::TensorType tensorType,
116 std::vector<armnn::BackendId>& backends,
117 std::vector<int32_t>& inputShape,
118 std::vector<int32_t>& outputShape,
119 std::vector<T>& inputValues,
120 std::vector<T>& expectedOutputValues,
121 tflite::Padding padding = tflite::Padding_SAME,
122 int32_t strideWidth = 0,
123 int32_t strideHeight = 0,
124 int32_t filterWidth = 0,
125 int32_t filterHeight = 0,
126 tflite::ActivationFunctionType fusedActivation = tflite::ActivationFunctionType_NONE,
127 float quantScale = 1.0f,
128 int quantOffset = 0)
129{
130 using namespace tflite;
131 std::vector<char> modelBuffer = CreatePooling2dTfLiteModel(poolingOperatorCode,
132 tensorType,
133 inputShape,
134 outputShape,
135 padding,
136 strideWidth,
137 strideHeight,
138 filterWidth,
139 filterHeight,
140 fusedActivation,
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 tfLiteDelegateInputId = tfLiteInterpreter->inputs()[0];
170 auto tfLiteDelegateInputData = tfLiteInterpreter->typed_tensor<T>(tfLiteDelegateInputId);
171 for (unsigned int i = 0; i < inputValues.size(); ++i)
172 {
173 tfLiteDelegateInputData[i] = inputValues[i];
174 }
175
176 auto armnnDelegateInputId = armnnDelegateInterpreter->inputs()[0];
177 auto armnnDelegateInputData = armnnDelegateInterpreter->typed_tensor<T>(armnnDelegateInputId);
178 for (unsigned int i = 0; i < inputValues.size(); ++i)
179 {
180 armnnDelegateInputData[i] = inputValues[i];
181 }
182
183 // Run EnqueueWorkload
184 CHECK(tfLiteInterpreter->Invoke() == kTfLiteOk);
185 CHECK(armnnDelegateInterpreter->Invoke() == kTfLiteOk);
186 // Compare output data
187 auto tfLiteDelegateOutputId = tfLiteInterpreter->outputs()[0];
188 auto tfLiteDelegateOutputData = tfLiteInterpreter->typed_tensor<T>(tfLiteDelegateOutputId);
189 auto tfLiteDelegateOutputTensor = tfLiteInterpreter->tensor(tfLiteDelegateOutputId);
190 auto armnnDelegateOutputId = armnnDelegateInterpreter->outputs()[0];
191 auto armnnDelegateOutputData = armnnDelegateInterpreter->typed_tensor<T>(armnnDelegateOutputId);
192 auto armnnDelegateOutputTensor = armnnDelegateInterpreter->tensor(armnnDelegateOutputId);
193
194 for (size_t i = 0; i < tfLiteDelegateOutputTensor->dims->size; i++)
195 {
196 CHECK(outputShape[i] == armnnDelegateOutputTensor->dims->data[i]);
197 CHECK(tfLiteDelegateOutputTensor->dims->data[i] == armnnDelegateOutputTensor->dims->data[i]);
198 }
199
200 for (size_t i = 0; i < expectedOutputValues.size(); i++)
201 {
202 CHECK(expectedOutputValues[i] == armnnDelegateOutputData[i]);
203 CHECK(tfLiteDelegateOutputData[i] == expectedOutputValues[i]);
204 CHECK(tfLiteDelegateOutputData[i] == armnnDelegateOutputData[i]);
205 }
206}
207
208} // anonymous namespace
209
210
211
212