blob: 333e17fafd92f36c7125022f83d47036cc8ed3d5 [file] [log] [blame]
Sadik Armagan8853c1f2018-10-22 09:04:18 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include <boost/test/unit_test.hpp>
7#include "ParserFlatbuffersFixture.hpp"
8#include "../TfLiteParser.hpp"
9
10#include <string>
Sadik Armagan8853c1f2018-10-22 09:04:18 +010011
12BOOST_AUTO_TEST_SUITE(TensorflowLiteParser)
13
14struct FullyConnectedFixture : public ParserFlatbuffersFixture
15{
16 explicit FullyConnectedFixture(const std::string& inputShape,
Finn Williamsd4fa5452021-03-01 12:31:41 +000017 const std::string& outputShape,
18 const std::string& filterShape,
19 const std::string& filterData,
20 const std::string biasShape = "",
21 const std::string biasData = "")
Sadik Armagan8853c1f2018-10-22 09:04:18 +010022 {
23 std::string inputTensors = "[ 0, 2 ]";
24 std::string biasTensor = "";
25 std::string biasBuffer = "";
26 if (biasShape.size() > 0 && biasData.size() > 0)
27 {
28 inputTensors = "[ 0, 2, 3 ]";
29 biasTensor = R"(
30 {
31 "shape": )" + biasShape + R"( ,
32 "type": "INT32",
33 "buffer": 3,
34 "name": "biasTensor",
35 "quantization": {
36 "min": [ 0.0 ],
37 "max": [ 255.0 ],
38 "scale": [ 1.0 ],
39 "zero_point": [ 0 ],
40 }
41 } )";
42 biasBuffer = R"(
43 { "data": )" + biasData + R"(, }, )";
44 }
45 m_JsonString = R"(
46 {
47 "version": 3,
48 "operator_codes": [ { "builtin_code": "FULLY_CONNECTED" } ],
49 "subgraphs": [ {
50 "tensors": [
51 {
52 "shape": )" + inputShape + R"(,
53 "type": "UINT8",
54 "buffer": 0,
55 "name": "inputTensor",
56 "quantization": {
57 "min": [ 0.0 ],
58 "max": [ 255.0 ],
59 "scale": [ 1.0 ],
60 "zero_point": [ 0 ],
61 }
62 },
63 {
64 "shape": )" + outputShape + R"(,
65 "type": "UINT8",
66 "buffer": 1,
67 "name": "outputTensor",
68 "quantization": {
69 "min": [ 0.0 ],
70 "max": [ 511.0 ],
71 "scale": [ 2.0 ],
72 "zero_point": [ 0 ],
73 }
74 },
75 {
76 "shape": )" + filterShape + R"(,
77 "type": "UINT8",
78 "buffer": 2,
79 "name": "filterTensor",
80 "quantization": {
81 "min": [ 0.0 ],
82 "max": [ 255.0 ],
83 "scale": [ 1.0 ],
84 "zero_point": [ 0 ],
85 }
86 }, )" + biasTensor + R"(
87 ],
88 "inputs": [ 0 ],
89 "outputs": [ 1 ],
90 "operators": [
91 {
92 "opcode_index": 0,
93 "inputs": )" + inputTensors + R"(,
94 "outputs": [ 1 ],
95 "builtin_options_type": "FullyConnectedOptions",
96 "builtin_options": {
97 "fused_activation_function": "NONE"
98 },
99 "custom_options_format": "FLEXBUFFERS"
100 }
101 ],
102 } ],
103 "buffers" : [
104 { },
105 { },
106 { "data": )" + filterData + R"(, }, )"
107 + biasBuffer + R"(
108 ]
109 }
110 )";
111 SetupSingleInputSingleOutput("inputTensor", "outputTensor");
112 }
113};
114
115struct FullyConnectedWithNoBiasFixture : FullyConnectedFixture
116{
117 FullyConnectedWithNoBiasFixture()
118 : FullyConnectedFixture("[ 1, 4, 1, 1 ]", // inputShape
119 "[ 1, 1 ]", // outputShape
Nattapat Chaimanowongd8eee592018-10-26 10:24:14 +0100120 "[ 1, 4 ]", // filterShape
Sadik Armagan8853c1f2018-10-22 09:04:18 +0100121 "[ 2, 3, 4, 5 ]") // filterData
122 {}
123};
124
125BOOST_FIXTURE_TEST_CASE(FullyConnectedWithNoBias, FullyConnectedWithNoBiasFixture)
126{
Derek Lambertif90c56d2020-01-10 17:14:08 +0000127 RunTest<2, armnn::DataType::QAsymmU8>(
Sadik Armagan8853c1f2018-10-22 09:04:18 +0100128 0,
129 { 10, 20, 30, 40 },
130 { 400/2 });
131}
132
133struct FullyConnectedWithBiasFixture : FullyConnectedFixture
134{
135 FullyConnectedWithBiasFixture()
136 : FullyConnectedFixture("[ 1, 4, 1, 1 ]", // inputShape
137 "[ 1, 1 ]", // outputShape
Nattapat Chaimanowongd8eee592018-10-26 10:24:14 +0100138 "[ 1, 4 ]", // filterShape
Sadik Armagan8853c1f2018-10-22 09:04:18 +0100139 "[ 2, 3, 4, 5 ]", // filterData
140 "[ 1 ]", // biasShape
141 "[ 10, 0, 0, 0 ]" ) // biasData
142 {}
143};
144
145BOOST_FIXTURE_TEST_CASE(ParseFullyConnectedWithBias, FullyConnectedWithBiasFixture)
146{
Derek Lambertif90c56d2020-01-10 17:14:08 +0000147 RunTest<2, armnn::DataType::QAsymmU8>(
Sadik Armagan8853c1f2018-10-22 09:04:18 +0100148 0,
149 { 10, 20, 30, 40 },
150 { (400+10)/2 });
151}
152
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +0100153struct FullyConnectedWithBiasMultipleOutputsFixture : FullyConnectedFixture
154{
155 FullyConnectedWithBiasMultipleOutputsFixture()
156 : FullyConnectedFixture("[ 1, 4, 2, 1 ]", // inputShape
157 "[ 2, 1 ]", // outputShape
158 "[ 1, 4 ]", // filterShape
159 "[ 2, 3, 4, 5 ]", // filterData
160 "[ 1 ]", // biasShape
161 "[ 10, 0, 0, 0 ]" ) // biasData
162 {}
163};
164
165BOOST_FIXTURE_TEST_CASE(FullyConnectedWithBiasMultipleOutputs, FullyConnectedWithBiasMultipleOutputsFixture)
166{
Derek Lambertif90c56d2020-01-10 17:14:08 +0000167 RunTest<2, armnn::DataType::QAsymmU8>(
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +0100168 0,
169 { 1, 2, 3, 4, 10, 20, 30, 40 },
170 { (40+10)/2, (400+10)/2 });
171}
172
Sadik Armagand109a4d2020-07-28 10:42:13 +0100173struct DynamicFullyConnectedWithBiasMultipleOutputsFixture : FullyConnectedFixture
174{
175 DynamicFullyConnectedWithBiasMultipleOutputsFixture()
176 : FullyConnectedFixture("[ 1, 4, 2, 1 ]", // inputShape
177 "[ ]", // outputShape
178 "[ 1, 4 ]", // filterShape
179 "[ 2, 3, 4, 5 ]", // filterData
180 "[ 1 ]", // biasShape
181 "[ 10, 0, 0, 0 ]" ) // biasData
182 { }
183};
184
185BOOST_FIXTURE_TEST_CASE(
186 DynamicFullyConnectedWithBiasMultipleOutputs,
187 DynamicFullyConnectedWithBiasMultipleOutputsFixture)
188{
189 RunTest<2,
190 armnn::DataType::QAsymmU8,
191 armnn::DataType::QAsymmU8>(0,
192 { { "inputTensor", { 1, 2, 3, 4, 10, 20, 30, 40} } },
193 { { "outputTensor", { (40+10)/2, (400+10)/2 } } },
194 true);
195}
196
Finn Williamsd4fa5452021-03-01 12:31:41 +0000197
198struct FullyConnectedNonConstWeightsFixture : public ParserFlatbuffersFixture
199{
200 explicit FullyConnectedNonConstWeightsFixture(const std::string& inputShape,
201 const std::string& outputShape,
202 const std::string& filterShape,
203 const std::string biasShape = "")
204 {
205 std::string inputTensors = "[ 0, 1 ]";
206 std::string biasTensor = "";
207 std::string biasBuffer = "";
208 std::string outputs = "2";
209 if (biasShape.size() > 0)
210 {
211 inputTensors = "[ 0, 1, 2 ]";
212 biasTensor = R"(
213 {
214 "shape": )" + biasShape + R"(,
215 "type": "INT32",
216 "buffer": 2,
217 "name": "bias",
218 "quantization": {
219 "scale": [ 1.0 ],
220 "zero_point": [ 0 ],
221 "details_type": 0,
222 "quantized_dimension": 0
223 },
224 "is_variable": true
225 }, )";
226
227 biasBuffer = R"(,{ "data": [ 10, 0, 0, 0 ] } )";
228 outputs = "3";
229 }
230 m_JsonString = R"(
231 {
232 "version": 3,
233 "operator_codes": [
234 {
235 "builtin_code": "FULLY_CONNECTED",
236 "version": 1
237 }
238 ],
239 "subgraphs": [
240 {
241 "tensors": [
242 {
243 "shape": )" + inputShape + R"(,
244 "type": "INT8",
245 "buffer": 0,
246 "name": "input_0",
247 "quantization": {
248 "scale": [ 1.0 ],
249 "zero_point": [ 0 ],
250 "details_type": 0,
251 "quantized_dimension": 0
252 },
253 "is_variable": false
254 },
255 {
256 "shape": )" + filterShape + R"(,
257 "type": "INT8",
258 "buffer": 1,
259 "name": "weights",
260 "quantization": {
261 "scale": [ 1.0 ],
262 "zero_point": [ 0 ],
263 "details_type": 0,
264 "quantized_dimension": 0
265 },
266 "is_variable": true
267 },
268 )" + biasTensor + R"(
269 {
270 "shape": )" + outputShape + R"(,
271 "type": "INT8",
272 "buffer": 0,
273 "name": "output",
274 "quantization": {
275 "scale": [
276 2.0
277 ],
278 "zero_point": [
279 0
280 ],
281 "details_type": 0,
282 "quantized_dimension": 0
283 },
284 "is_variable": false
285 }
286 ],
287 "inputs": )" + inputTensors + R"(,
288 "outputs": [ )" + outputs + R"( ],
289 "operators": [
290 {
291 "opcode_index": 0,
292 "inputs": )" + inputTensors + R"(,
293 "outputs": [ )" + outputs + R"( ],
294 "builtin_options_type": "FullyConnectedOptions",
295 "builtin_options": {
296 "fused_activation_function": "NONE",
297 "weights_format": "DEFAULT",
298 "keep_num_dims": false,
299 "asymmetric_quantize_inputs": false
300 },
301 "custom_options_format": "FLEXBUFFERS"
302 }
303 ]
304 }
305 ],
306 "description": "ArmnnDelegate: FullyConnected Operator Model",
307 "buffers": [
308 {
309 "data": []
310 },
311 {
312 "data": [ 2, 3, 4, 5 ]
313 }
314 )" + biasBuffer + R"(
315 ]
316 }
317 )";
318 Setup();
319 }
320};
321
322struct FullyConnectedNonConstWeights : FullyConnectedNonConstWeightsFixture
323{
324 FullyConnectedNonConstWeights()
325 : FullyConnectedNonConstWeightsFixture("[ 1, 4, 1, 1 ]", // inputShape
326 "[ 1, 1 ]", // outputShape
327 "[ 1, 4 ]", // filterShape
328 "[ 1 ]" ) // biasShape
329
330 {}
331};
332
333BOOST_FIXTURE_TEST_CASE(ParseFullyConnectedNonConstWeights, FullyConnectedNonConstWeights)
334{
335 RunTest<2, armnn::DataType::QAsymmS8,
336 armnn::DataType::Signed32,
337 armnn::DataType::QAsymmS8>(
338 0,
339 {{{"input_0", { 1, 2, 3, 4 }},{"weights", { 2, 3, 4, 5 }}}},
340 {{"bias", { 10 }}},
341 {{"output", { 25 }}});
342}
343
344struct FullyConnectedNonConstWeightsNoBias : FullyConnectedNonConstWeightsFixture
345{
346 FullyConnectedNonConstWeightsNoBias()
347 : FullyConnectedNonConstWeightsFixture("[ 1, 4, 1, 1 ]", // inputShape
348 "[ 1, 1 ]", // outputShape
349 "[ 1, 4 ]") // filterShape
350
351 {}
352};
353
354BOOST_FIXTURE_TEST_CASE(ParseFullyConnectedNonConstWeightsNoBias, FullyConnectedNonConstWeightsNoBias)
355{
356 RunTest<2, armnn::DataType::QAsymmS8,
357 armnn::DataType::QAsymmS8>(
358 0,
359 {{{"input_0", { 1, 2, 3, 4 }},{"weights", { 2, 3, 4, 5 }}}},
360 {{"output", { 20 }}});
361}
362
Sadik Armagan8853c1f2018-10-22 09:04:18 +0100363BOOST_AUTO_TEST_SUITE_END()