blob: a8b102271a8284174031caa09e88865e926926ce [file] [log] [blame]
Matthew Sloyan0d35a932020-11-09 12:25:05 +00001//
Ryan OShea238ecd92023-03-07 11:44:23 +00002// Copyright © 2020, 2023 Arm Ltd and Contributors. All rights reserved.
Matthew Sloyan0d35a932020-11-09 12:25:05 +00003// 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> CreateQuantizationTfLiteModel(tflite::BuiltinOperator quantizationOperatorCode,
23 tflite::TensorType inputTensorType,
24 tflite::TensorType outputTensorType,
25 const std::vector <int32_t>& inputTensorShape,
26 const std::vector <int32_t>& outputTensorShape,
27 float quantScale = 1.0f,
28 int quantOffset = 0)
29{
30 using namespace tflite;
31 flatbuffers::FlatBufferBuilder flatBufferBuilder;
32
33 std::vector<flatbuffers::Offset<tflite::Buffer>> buffers;
Ryan OShea238ecd92023-03-07 11:44:23 +000034 buffers.push_back(CreateBuffer(flatBufferBuilder));
35 buffers.push_back(CreateBuffer(flatBufferBuilder));
36 buffers.push_back(CreateBuffer(flatBufferBuilder));
37
Matthew Sloyan0d35a932020-11-09 12:25:05 +000038
39 auto quantizationParameters =
40 CreateQuantizationParameters(flatBufferBuilder,
41 0,
42 0,
43 flatBufferBuilder.CreateVector<float>({ quantScale }),
44 flatBufferBuilder.CreateVector<int64_t>({ quantOffset }),
45 QuantizationDetails_CustomQuantization);
46
47 std::array<flatbuffers::Offset<Tensor>, 2> tensors;
48 tensors[0] = CreateTensor(flatBufferBuilder,
49 flatBufferBuilder.CreateVector<int32_t>(inputTensorShape.data(),
50 inputTensorShape.size()),
51 inputTensorType,
Ryan OShea238ecd92023-03-07 11:44:23 +000052 1,
Matthew Sloyan0d35a932020-11-09 12:25:05 +000053 flatBufferBuilder.CreateString("input"),
54 quantizationParameters);
55 tensors[1] = CreateTensor(flatBufferBuilder,
56 flatBufferBuilder.CreateVector<int32_t>(outputTensorShape.data(),
57 outputTensorShape.size()),
58 outputTensorType,
Ryan OShea238ecd92023-03-07 11:44:23 +000059 2,
Matthew Sloyan0d35a932020-11-09 12:25:05 +000060 flatBufferBuilder.CreateString("output"),
61 quantizationParameters);
62
63 // create operator
64 tflite::BuiltinOptions operatorBuiltinOptionsType = tflite::BuiltinOptions_NONE;
65 flatbuffers::Offset<void> operatorBuiltinOptions = 0;
66 switch (quantizationOperatorCode)
67 {
68 case BuiltinOperator_QUANTIZE:
69 {
70 operatorBuiltinOptionsType = BuiltinOptions_QuantizeOptions;
71 operatorBuiltinOptions = CreateQuantizeOptions(flatBufferBuilder).Union();
72 break;
73 }
74 case BuiltinOperator_DEQUANTIZE:
75 {
76 operatorBuiltinOptionsType = BuiltinOptions_DequantizeOptions;
77 operatorBuiltinOptions = CreateDequantizeOptions(flatBufferBuilder).Union();
78 break;
79 }
80 default:
81 break;
82 }
83
Keith Davis892fafe2020-11-26 17:40:35 +000084 const std::vector<int32_t> operatorInputs{0};
85 const std::vector<int32_t> operatorOutputs{1};
Matthew Sloyan0d35a932020-11-09 12:25:05 +000086 flatbuffers::Offset <Operator> quantizationOperator =
87 CreateOperator(flatBufferBuilder,
88 0,
89 flatBufferBuilder.CreateVector<int32_t>(operatorInputs.data(), operatorInputs.size()),
90 flatBufferBuilder.CreateVector<int32_t>(operatorOutputs.data(), operatorOutputs.size()),
91 operatorBuiltinOptionsType,
92 operatorBuiltinOptions);
93
Keith Davis892fafe2020-11-26 17:40:35 +000094 const std::vector<int> subgraphInputs{0};
95 const std::vector<int> subgraphOutputs{1};
Matthew Sloyan0d35a932020-11-09 12:25:05 +000096 flatbuffers::Offset <SubGraph> subgraph =
97 CreateSubGraph(flatBufferBuilder,
98 flatBufferBuilder.CreateVector(tensors.data(), tensors.size()),
99 flatBufferBuilder.CreateVector<int32_t>(subgraphInputs.data(), subgraphInputs.size()),
100 flatBufferBuilder.CreateVector<int32_t>(subgraphOutputs.data(), subgraphOutputs.size()),
101 flatBufferBuilder.CreateVector(&quantizationOperator, 1));
102
103 flatbuffers::Offset <flatbuffers::String> modelDescription =
104 flatBufferBuilder.CreateString("ArmnnDelegate: Quantization Operator Model");
105 flatbuffers::Offset <OperatorCode> operatorCode = CreateOperatorCode(flatBufferBuilder, quantizationOperatorCode);
106
107 flatbuffers::Offset <Model> flatbufferModel =
108 CreateModel(flatBufferBuilder,
109 TFLITE_SCHEMA_VERSION,
110 flatBufferBuilder.CreateVector(&operatorCode, 1),
111 flatBufferBuilder.CreateVector(&subgraph, 1),
112 modelDescription,
113 flatBufferBuilder.CreateVector(buffers.data(), buffers.size()));
114
115 flatBufferBuilder.Finish(flatbufferModel);
116
117 return std::vector<char>(flatBufferBuilder.GetBufferPointer(),
118 flatBufferBuilder.GetBufferPointer() + flatBufferBuilder.GetSize());
119}
120
121template <typename InputT, typename OutputT>
122void QuantizationTest(tflite::BuiltinOperator quantizeOperatorCode,
123 tflite::TensorType inputTensorType,
124 tflite::TensorType outputTensorType,
125 std::vector<armnn::BackendId>& backends,
126 std::vector<int32_t>& inputShape,
127 std::vector<int32_t>& outputShape,
128 std::vector<InputT>& inputValues,
129 std::vector<OutputT>& expectedOutputValues,
130 float quantScale = 1.0f,
131 int quantOffset = 0)
132{
133 using namespace tflite;
134 std::vector<char> modelBuffer = CreateQuantizationTfLiteModel(quantizeOperatorCode,
135 inputTensorType,
136 outputTensorType,
137 inputShape,
138 outputShape,
139 quantScale,
140 quantOffset);
141
142 const Model* tfLiteModel = GetModel(modelBuffer.data());
143
144 // Create TfLite Interpreters
145 std::unique_ptr<Interpreter> armnnDelegateInterpreter;
146 CHECK(InterpreterBuilder(tfLiteModel, ::tflite::ops::builtin::BuiltinOpResolver())
147 (&armnnDelegateInterpreter) == kTfLiteOk);
148 CHECK(armnnDelegateInterpreter != nullptr);
149 CHECK(armnnDelegateInterpreter->AllocateTensors() == kTfLiteOk);
150
151 std::unique_ptr<Interpreter> tfLiteInterpreter;
152 CHECK(InterpreterBuilder(tfLiteModel, ::tflite::ops::builtin::BuiltinOpResolver())
153 (&tfLiteInterpreter) == kTfLiteOk);
154 CHECK(tfLiteInterpreter != nullptr);
155 CHECK(tfLiteInterpreter->AllocateTensors() == kTfLiteOk);
156
157 // Create the ArmNN Delegate
158 armnnDelegate::DelegateOptions delegateOptions(backends);
159 std::unique_ptr<TfLiteDelegate, decltype(&armnnDelegate::TfLiteArmnnDelegateDelete)>
160 theArmnnDelegate(armnnDelegate::TfLiteArmnnDelegateCreate(delegateOptions),
161 armnnDelegate::TfLiteArmnnDelegateDelete);
162 CHECK(theArmnnDelegate != nullptr);
163
164 // Modify armnnDelegateInterpreter to use armnnDelegate
165 CHECK(armnnDelegateInterpreter->ModifyGraphWithDelegate(theArmnnDelegate.get()) == kTfLiteOk);
166
167 // Set input data
168 auto tfLiteDelegateInputId = tfLiteInterpreter->inputs()[0];
169 auto tfLiteDelageInputData = tfLiteInterpreter->typed_tensor<InputT>(tfLiteDelegateInputId);
170 for (unsigned int i = 0; i < inputValues.size(); ++i)
171 {
172 tfLiteDelageInputData[i] = inputValues[i];
173 }
174
175 auto armnnDelegateInputId = armnnDelegateInterpreter->inputs()[0];
176 auto armnnDelegateInputData = armnnDelegateInterpreter->typed_tensor<InputT>(armnnDelegateInputId);
177 for (unsigned int i = 0; i < inputValues.size(); ++i)
178 {
179 armnnDelegateInputData[i] = inputValues[i];
180 }
181
182 // Run EnqueWorkload
183 CHECK(tfLiteInterpreter->Invoke() == kTfLiteOk);
184 CHECK(armnnDelegateInterpreter->Invoke() == kTfLiteOk);
185
186 // Compare output data
187 auto tfLiteDelegateOutputId = tfLiteInterpreter->outputs()[0];
188 auto tfLiteDelageOutputData = tfLiteInterpreter->typed_tensor<OutputT>(tfLiteDelegateOutputId);
189 auto armnnDelegateOutputId = armnnDelegateInterpreter->outputs()[0];
190 auto armnnDelegateOutputData = armnnDelegateInterpreter->typed_tensor<OutputT>(armnnDelegateOutputId);
191
192 for (size_t i = 0; i < expectedOutputValues.size(); i++)
193 {
194 CHECK(expectedOutputValues[i] == armnnDelegateOutputData[i]);
195 CHECK(tfLiteDelageOutputData[i] == expectedOutputValues[i]);
196 CHECK(tfLiteDelageOutputData[i] == armnnDelegateOutputData[i]);
197 }
198}
199
200} // anonymous namespace