blob: 108b878e2062aa447cf1b2bc699e8ef3ceda3777 [file] [log] [blame]
Sadik Armagan8853c1f2018-10-22 09:04:18 +01001//
Finn Williamsb49ed182021-06-29 15:50:08 +01002// Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003// SPDX-License-Identifier: MIT
4//
5
Sadik Armagan8853c1f2018-10-22 09:04:18 +01006#include "ParserFlatbuffersFixture.hpp"
Sadik Armagan8853c1f2018-10-22 09:04:18 +01007
Sadik Armagan8853c1f2018-10-22 09:04:18 +01008
Sadik Armagan1625efc2021-06-10 18:24:34 +01009TEST_SUITE("TensorflowLiteParser_FullyConnected")
10{
Sadik Armagan8853c1f2018-10-22 09:04:18 +010011struct FullyConnectedFixture : public ParserFlatbuffersFixture
12{
13 explicit FullyConnectedFixture(const std::string& inputShape,
Finn Williamsd4fa5452021-03-01 12:31:41 +000014 const std::string& outputShape,
15 const std::string& filterShape,
16 const std::string& filterData,
17 const std::string biasShape = "",
Mike Kelly5880b912022-01-28 16:18:54 +000018 const std::string biasData = "",
19 const std::string dataType = "UINT8",
20 const std::string weightsDataType = "UINT8",
21 const std::string biasDataType = "INT32")
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"( ,
Mike Kelly5880b912022-01-28 16:18:54 +000032 "type": )" + biasDataType + R"(,
Sadik Armagan8853c1f2018-10-22 09:04:18 +010033 "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"(,
Mike Kelly5880b912022-01-28 16:18:54 +000053 "type": )" + dataType + R"(,
Sadik Armagan8853c1f2018-10-22 09:04:18 +010054 "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"(,
Mike Kelly5880b912022-01-28 16:18:54 +000065 "type": )" + dataType + R"(,
Sadik Armagan8853c1f2018-10-22 09:04:18 +010066 "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"(,
Mike Kelly5880b912022-01-28 16:18:54 +000077 "type": )" + weightsDataType + R"(,
Sadik Armagan8853c1f2018-10-22 09:04:18 +010078 "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
Sadik Armagan1625efc2021-06-10 18:24:34 +0100125TEST_CASE_FIXTURE(FullyConnectedWithNoBiasFixture, "FullyConnectedWithNoBias")
Sadik Armagan8853c1f2018-10-22 09:04:18 +0100126{
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
Sadik Armagan1625efc2021-06-10 18:24:34 +0100145TEST_CASE_FIXTURE(FullyConnectedWithBiasFixture, "ParseFullyConnectedWithBias")
Sadik Armagan8853c1f2018-10-22 09:04:18 +0100146{
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
Sadik Armagan1625efc2021-06-10 18:24:34 +0100165TEST_CASE_FIXTURE(FullyConnectedWithBiasMultipleOutputsFixture, "FullyConnectedWithBiasMultipleOutputs")
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +0100166{
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
Sadik Armagan1625efc2021-06-10 18:24:34 +0100185TEST_CASE_FIXTURE(
186 DynamicFullyConnectedWithBiasMultipleOutputsFixture, "DynamicFullyConnectedWithBiasMultipleOutputs")
Sadik Armagand109a4d2020-07-28 10:42:13 +0100187{
188 RunTest<2,
189 armnn::DataType::QAsymmU8,
190 armnn::DataType::QAsymmU8>(0,
191 { { "inputTensor", { 1, 2, 3, 4, 10, 20, 30, 40} } },
192 { { "outputTensor", { (40+10)/2, (400+10)/2 } } },
193 true);
194}
195
Finn Williamsd4fa5452021-03-01 12:31:41 +0000196
197struct FullyConnectedNonConstWeightsFixture : public ParserFlatbuffersFixture
198{
199 explicit FullyConnectedNonConstWeightsFixture(const std::string& inputShape,
200 const std::string& outputShape,
201 const std::string& filterShape,
202 const std::string biasShape = "")
203 {
204 std::string inputTensors = "[ 0, 1 ]";
205 std::string biasTensor = "";
206 std::string biasBuffer = "";
207 std::string outputs = "2";
208 if (biasShape.size() > 0)
209 {
210 inputTensors = "[ 0, 1, 2 ]";
211 biasTensor = R"(
212 {
213 "shape": )" + biasShape + R"(,
214 "type": "INT32",
215 "buffer": 2,
216 "name": "bias",
217 "quantization": {
218 "scale": [ 1.0 ],
219 "zero_point": [ 0 ],
220 "details_type": 0,
221 "quantized_dimension": 0
222 },
223 "is_variable": true
224 }, )";
225
mathad01bf7edb62021-04-20 16:12:45 +0100226 biasBuffer = R"(,{ "data": [] } )";
Finn Williamsd4fa5452021-03-01 12:31:41 +0000227 outputs = "3";
228 }
229 m_JsonString = R"(
230 {
231 "version": 3,
232 "operator_codes": [
233 {
234 "builtin_code": "FULLY_CONNECTED",
235 "version": 1
236 }
237 ],
238 "subgraphs": [
239 {
240 "tensors": [
241 {
242 "shape": )" + inputShape + R"(,
243 "type": "INT8",
244 "buffer": 0,
245 "name": "input_0",
246 "quantization": {
247 "scale": [ 1.0 ],
248 "zero_point": [ 0 ],
249 "details_type": 0,
250 "quantized_dimension": 0
251 },
Finn Williamsd4fa5452021-03-01 12:31:41 +0000252 },
253 {
254 "shape": )" + filterShape + R"(,
255 "type": "INT8",
256 "buffer": 1,
257 "name": "weights",
258 "quantization": {
259 "scale": [ 1.0 ],
260 "zero_point": [ 0 ],
261 "details_type": 0,
262 "quantized_dimension": 0
263 },
Finn Williamsd4fa5452021-03-01 12:31:41 +0000264 },
265 )" + biasTensor + R"(
266 {
267 "shape": )" + outputShape + R"(,
268 "type": "INT8",
269 "buffer": 0,
270 "name": "output",
271 "quantization": {
272 "scale": [
273 2.0
274 ],
275 "zero_point": [
276 0
277 ],
278 "details_type": 0,
279 "quantized_dimension": 0
280 },
Finn Williamsd4fa5452021-03-01 12:31:41 +0000281 }
282 ],
283 "inputs": )" + inputTensors + R"(,
284 "outputs": [ )" + outputs + R"( ],
285 "operators": [
286 {
287 "opcode_index": 0,
288 "inputs": )" + inputTensors + R"(,
289 "outputs": [ )" + outputs + R"( ],
290 "builtin_options_type": "FullyConnectedOptions",
291 "builtin_options": {
292 "fused_activation_function": "NONE",
293 "weights_format": "DEFAULT",
294 "keep_num_dims": false,
295 "asymmetric_quantize_inputs": false
296 },
297 "custom_options_format": "FLEXBUFFERS"
298 }
299 ]
300 }
301 ],
302 "description": "ArmnnDelegate: FullyConnected Operator Model",
303 "buffers": [
304 {
305 "data": []
306 },
307 {
mathad01bf7edb62021-04-20 16:12:45 +0100308 "data": []
Finn Williamsd4fa5452021-03-01 12:31:41 +0000309 }
310 )" + biasBuffer + R"(
311 ]
312 }
313 )";
314 Setup();
315 }
316};
317
318struct FullyConnectedNonConstWeights : FullyConnectedNonConstWeightsFixture
319{
320 FullyConnectedNonConstWeights()
321 : FullyConnectedNonConstWeightsFixture("[ 1, 4, 1, 1 ]", // inputShape
322 "[ 1, 1 ]", // outputShape
323 "[ 1, 4 ]", // filterShape
324 "[ 1 ]" ) // biasShape
325
326 {}
327};
328
Sadik Armagan1625efc2021-06-10 18:24:34 +0100329TEST_CASE_FIXTURE(FullyConnectedNonConstWeights, "ParseFullyConnectedNonConstWeights")
Finn Williamsd4fa5452021-03-01 12:31:41 +0000330{
331 RunTest<2, armnn::DataType::QAsymmS8,
332 armnn::DataType::Signed32,
333 armnn::DataType::QAsymmS8>(
334 0,
335 {{{"input_0", { 1, 2, 3, 4 }},{"weights", { 2, 3, 4, 5 }}}},
336 {{"bias", { 10 }}},
337 {{"output", { 25 }}});
338}
339
340struct FullyConnectedNonConstWeightsNoBias : FullyConnectedNonConstWeightsFixture
341{
342 FullyConnectedNonConstWeightsNoBias()
343 : FullyConnectedNonConstWeightsFixture("[ 1, 4, 1, 1 ]", // inputShape
344 "[ 1, 1 ]", // outputShape
345 "[ 1, 4 ]") // filterShape
346
347 {}
348};
349
Sadik Armagan1625efc2021-06-10 18:24:34 +0100350TEST_CASE_FIXTURE(FullyConnectedNonConstWeightsNoBias, "ParseFullyConnectedNonConstWeightsNoBias")
Finn Williamsd4fa5452021-03-01 12:31:41 +0000351{
352 RunTest<2, armnn::DataType::QAsymmS8,
353 armnn::DataType::QAsymmS8>(
354 0,
355 {{{"input_0", { 1, 2, 3, 4 }},{"weights", { 2, 3, 4, 5 }}}},
356 {{"output", { 20 }}});
357}
358
Mike Kelly5880b912022-01-28 16:18:54 +0000359struct FullyConnectedWeightsBiasFloat : FullyConnectedFixture
360{
361 FullyConnectedWeightsBiasFloat()
362 : FullyConnectedFixture("[ 1, 4, 1, 1 ]", // inputShape
363 "[ 1, 1 ]", // outputShape
364 "[ 1, 4 ]", // filterShape
365 "[ 2, 3, 4, 5 ]", // filterData
366 "[ 1 ]", // biasShape
367 "[ 10, 0, 0, 0 ]", // filterShape
368 "FLOAT32", // input and output dataType
369 "INT8", // weights dataType
370 "FLOAT32") // bias dataType
371 {}
372};
373
374TEST_CASE_FIXTURE(FullyConnectedWeightsBiasFloat, "FullyConnectedWeightsBiasFloat")
375{
376 RunTest<2, armnn::DataType::Float32>(
377 0,
378 { 10, 20, 30, 40 },
379 { 400 });
380}
381
Sadik Armagan1625efc2021-06-10 18:24:34 +0100382}