blob: 521ab34b7ae8819dfdfd2526e9d07e287b8361ec [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
Sadik Armagan8853c1f2018-10-22 09:04:18 +01006#include "ParserFlatbuffersFixture.hpp"
7#include "../TfLiteParser.hpp"
8
9#include <string>
Sadik Armagan8853c1f2018-10-22 09:04:18 +010010
Sadik Armagan1625efc2021-06-10 18:24:34 +010011TEST_SUITE("TensorflowLiteParser_FullyConnected")
12{
Sadik Armagan8853c1f2018-10-22 09:04:18 +010013struct FullyConnectedFixture : public ParserFlatbuffersFixture
14{
15 explicit FullyConnectedFixture(const std::string& inputShape,
Finn Williamsd4fa5452021-03-01 12:31:41 +000016 const std::string& outputShape,
17 const std::string& filterShape,
18 const std::string& filterData,
19 const std::string biasShape = "",
20 const std::string biasData = "")
Sadik Armagan8853c1f2018-10-22 09:04:18 +010021 {
22 std::string inputTensors = "[ 0, 2 ]";
23 std::string biasTensor = "";
24 std::string biasBuffer = "";
25 if (biasShape.size() > 0 && biasData.size() > 0)
26 {
27 inputTensors = "[ 0, 2, 3 ]";
28 biasTensor = R"(
29 {
30 "shape": )" + biasShape + R"( ,
31 "type": "INT32",
32 "buffer": 3,
33 "name": "biasTensor",
34 "quantization": {
35 "min": [ 0.0 ],
36 "max": [ 255.0 ],
37 "scale": [ 1.0 ],
38 "zero_point": [ 0 ],
39 }
40 } )";
41 biasBuffer = R"(
42 { "data": )" + biasData + R"(, }, )";
43 }
44 m_JsonString = R"(
45 {
46 "version": 3,
47 "operator_codes": [ { "builtin_code": "FULLY_CONNECTED" } ],
48 "subgraphs": [ {
49 "tensors": [
50 {
51 "shape": )" + inputShape + R"(,
52 "type": "UINT8",
53 "buffer": 0,
54 "name": "inputTensor",
55 "quantization": {
56 "min": [ 0.0 ],
57 "max": [ 255.0 ],
58 "scale": [ 1.0 ],
59 "zero_point": [ 0 ],
60 }
61 },
62 {
63 "shape": )" + outputShape + R"(,
64 "type": "UINT8",
65 "buffer": 1,
66 "name": "outputTensor",
67 "quantization": {
68 "min": [ 0.0 ],
69 "max": [ 511.0 ],
70 "scale": [ 2.0 ],
71 "zero_point": [ 0 ],
72 }
73 },
74 {
75 "shape": )" + filterShape + R"(,
76 "type": "UINT8",
77 "buffer": 2,
78 "name": "filterTensor",
79 "quantization": {
80 "min": [ 0.0 ],
81 "max": [ 255.0 ],
82 "scale": [ 1.0 ],
83 "zero_point": [ 0 ],
84 }
85 }, )" + biasTensor + R"(
86 ],
87 "inputs": [ 0 ],
88 "outputs": [ 1 ],
89 "operators": [
90 {
91 "opcode_index": 0,
92 "inputs": )" + inputTensors + R"(,
93 "outputs": [ 1 ],
94 "builtin_options_type": "FullyConnectedOptions",
95 "builtin_options": {
96 "fused_activation_function": "NONE"
97 },
98 "custom_options_format": "FLEXBUFFERS"
99 }
100 ],
101 } ],
102 "buffers" : [
103 { },
104 { },
105 { "data": )" + filterData + R"(, }, )"
106 + biasBuffer + R"(
107 ]
108 }
109 )";
110 SetupSingleInputSingleOutput("inputTensor", "outputTensor");
111 }
112};
113
114struct FullyConnectedWithNoBiasFixture : FullyConnectedFixture
115{
116 FullyConnectedWithNoBiasFixture()
117 : FullyConnectedFixture("[ 1, 4, 1, 1 ]", // inputShape
118 "[ 1, 1 ]", // outputShape
Nattapat Chaimanowongd8eee592018-10-26 10:24:14 +0100119 "[ 1, 4 ]", // filterShape
Sadik Armagan8853c1f2018-10-22 09:04:18 +0100120 "[ 2, 3, 4, 5 ]") // filterData
121 {}
122};
123
Sadik Armagan1625efc2021-06-10 18:24:34 +0100124TEST_CASE_FIXTURE(FullyConnectedWithNoBiasFixture, "FullyConnectedWithNoBias")
Sadik Armagan8853c1f2018-10-22 09:04:18 +0100125{
Derek Lambertif90c56d2020-01-10 17:14:08 +0000126 RunTest<2, armnn::DataType::QAsymmU8>(
Sadik Armagan8853c1f2018-10-22 09:04:18 +0100127 0,
128 { 10, 20, 30, 40 },
129 { 400/2 });
130}
131
132struct FullyConnectedWithBiasFixture : FullyConnectedFixture
133{
134 FullyConnectedWithBiasFixture()
135 : FullyConnectedFixture("[ 1, 4, 1, 1 ]", // inputShape
136 "[ 1, 1 ]", // outputShape
Nattapat Chaimanowongd8eee592018-10-26 10:24:14 +0100137 "[ 1, 4 ]", // filterShape
Sadik Armagan8853c1f2018-10-22 09:04:18 +0100138 "[ 2, 3, 4, 5 ]", // filterData
139 "[ 1 ]", // biasShape
140 "[ 10, 0, 0, 0 ]" ) // biasData
141 {}
142};
143
Sadik Armagan1625efc2021-06-10 18:24:34 +0100144TEST_CASE_FIXTURE(FullyConnectedWithBiasFixture, "ParseFullyConnectedWithBias")
Sadik Armagan8853c1f2018-10-22 09:04:18 +0100145{
Derek Lambertif90c56d2020-01-10 17:14:08 +0000146 RunTest<2, armnn::DataType::QAsymmU8>(
Sadik Armagan8853c1f2018-10-22 09:04:18 +0100147 0,
148 { 10, 20, 30, 40 },
149 { (400+10)/2 });
150}
151
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +0100152struct FullyConnectedWithBiasMultipleOutputsFixture : FullyConnectedFixture
153{
154 FullyConnectedWithBiasMultipleOutputsFixture()
155 : FullyConnectedFixture("[ 1, 4, 2, 1 ]", // inputShape
156 "[ 2, 1 ]", // outputShape
157 "[ 1, 4 ]", // filterShape
158 "[ 2, 3, 4, 5 ]", // filterData
159 "[ 1 ]", // biasShape
160 "[ 10, 0, 0, 0 ]" ) // biasData
161 {}
162};
163
Sadik Armagan1625efc2021-06-10 18:24:34 +0100164TEST_CASE_FIXTURE(FullyConnectedWithBiasMultipleOutputsFixture, "FullyConnectedWithBiasMultipleOutputs")
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +0100165{
Derek Lambertif90c56d2020-01-10 17:14:08 +0000166 RunTest<2, armnn::DataType::QAsymmU8>(
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +0100167 0,
168 { 1, 2, 3, 4, 10, 20, 30, 40 },
169 { (40+10)/2, (400+10)/2 });
170}
171
Sadik Armagand109a4d2020-07-28 10:42:13 +0100172struct DynamicFullyConnectedWithBiasMultipleOutputsFixture : FullyConnectedFixture
173{
174 DynamicFullyConnectedWithBiasMultipleOutputsFixture()
175 : FullyConnectedFixture("[ 1, 4, 2, 1 ]", // inputShape
176 "[ ]", // outputShape
177 "[ 1, 4 ]", // filterShape
178 "[ 2, 3, 4, 5 ]", // filterData
179 "[ 1 ]", // biasShape
180 "[ 10, 0, 0, 0 ]" ) // biasData
181 { }
182};
183
Sadik Armagan1625efc2021-06-10 18:24:34 +0100184TEST_CASE_FIXTURE(
185 DynamicFullyConnectedWithBiasMultipleOutputsFixture, "DynamicFullyConnectedWithBiasMultipleOutputs")
Sadik Armagand109a4d2020-07-28 10:42:13 +0100186{
187 RunTest<2,
188 armnn::DataType::QAsymmU8,
189 armnn::DataType::QAsymmU8>(0,
190 { { "inputTensor", { 1, 2, 3, 4, 10, 20, 30, 40} } },
191 { { "outputTensor", { (40+10)/2, (400+10)/2 } } },
192 true);
193}
194
Finn Williamsd4fa5452021-03-01 12:31:41 +0000195
196struct FullyConnectedNonConstWeightsFixture : public ParserFlatbuffersFixture
197{
198 explicit FullyConnectedNonConstWeightsFixture(const std::string& inputShape,
199 const std::string& outputShape,
200 const std::string& filterShape,
201 const std::string biasShape = "")
202 {
203 std::string inputTensors = "[ 0, 1 ]";
204 std::string biasTensor = "";
205 std::string biasBuffer = "";
206 std::string outputs = "2";
207 if (biasShape.size() > 0)
208 {
209 inputTensors = "[ 0, 1, 2 ]";
210 biasTensor = R"(
211 {
212 "shape": )" + biasShape + R"(,
213 "type": "INT32",
214 "buffer": 2,
215 "name": "bias",
216 "quantization": {
217 "scale": [ 1.0 ],
218 "zero_point": [ 0 ],
219 "details_type": 0,
220 "quantized_dimension": 0
221 },
222 "is_variable": true
223 }, )";
224
mathad01bf7edb62021-04-20 16:12:45 +0100225 biasBuffer = R"(,{ "data": [] } )";
Finn Williamsd4fa5452021-03-01 12:31:41 +0000226 outputs = "3";
227 }
228 m_JsonString = R"(
229 {
230 "version": 3,
231 "operator_codes": [
232 {
233 "builtin_code": "FULLY_CONNECTED",
234 "version": 1
235 }
236 ],
237 "subgraphs": [
238 {
239 "tensors": [
240 {
241 "shape": )" + inputShape + R"(,
242 "type": "INT8",
243 "buffer": 0,
244 "name": "input_0",
245 "quantization": {
246 "scale": [ 1.0 ],
247 "zero_point": [ 0 ],
248 "details_type": 0,
249 "quantized_dimension": 0
250 },
Finn Williamsd4fa5452021-03-01 12:31:41 +0000251 },
252 {
253 "shape": )" + filterShape + R"(,
254 "type": "INT8",
255 "buffer": 1,
256 "name": "weights",
257 "quantization": {
258 "scale": [ 1.0 ],
259 "zero_point": [ 0 ],
260 "details_type": 0,
261 "quantized_dimension": 0
262 },
Finn Williamsd4fa5452021-03-01 12:31:41 +0000263 },
264 )" + biasTensor + R"(
265 {
266 "shape": )" + outputShape + R"(,
267 "type": "INT8",
268 "buffer": 0,
269 "name": "output",
270 "quantization": {
271 "scale": [
272 2.0
273 ],
274 "zero_point": [
275 0
276 ],
277 "details_type": 0,
278 "quantized_dimension": 0
279 },
Finn Williamsd4fa5452021-03-01 12:31:41 +0000280 }
281 ],
282 "inputs": )" + inputTensors + R"(,
283 "outputs": [ )" + outputs + R"( ],
284 "operators": [
285 {
286 "opcode_index": 0,
287 "inputs": )" + inputTensors + R"(,
288 "outputs": [ )" + outputs + R"( ],
289 "builtin_options_type": "FullyConnectedOptions",
290 "builtin_options": {
291 "fused_activation_function": "NONE",
292 "weights_format": "DEFAULT",
293 "keep_num_dims": false,
294 "asymmetric_quantize_inputs": false
295 },
296 "custom_options_format": "FLEXBUFFERS"
297 }
298 ]
299 }
300 ],
301 "description": "ArmnnDelegate: FullyConnected Operator Model",
302 "buffers": [
303 {
304 "data": []
305 },
306 {
mathad01bf7edb62021-04-20 16:12:45 +0100307 "data": []
Finn Williamsd4fa5452021-03-01 12:31:41 +0000308 }
309 )" + biasBuffer + R"(
310 ]
311 }
312 )";
313 Setup();
314 }
315};
316
317struct FullyConnectedNonConstWeights : FullyConnectedNonConstWeightsFixture
318{
319 FullyConnectedNonConstWeights()
320 : FullyConnectedNonConstWeightsFixture("[ 1, 4, 1, 1 ]", // inputShape
321 "[ 1, 1 ]", // outputShape
322 "[ 1, 4 ]", // filterShape
323 "[ 1 ]" ) // biasShape
324
325 {}
326};
327
Sadik Armagan1625efc2021-06-10 18:24:34 +0100328TEST_CASE_FIXTURE(FullyConnectedNonConstWeights, "ParseFullyConnectedNonConstWeights")
Finn Williamsd4fa5452021-03-01 12:31:41 +0000329{
330 RunTest<2, armnn::DataType::QAsymmS8,
331 armnn::DataType::Signed32,
332 armnn::DataType::QAsymmS8>(
333 0,
334 {{{"input_0", { 1, 2, 3, 4 }},{"weights", { 2, 3, 4, 5 }}}},
335 {{"bias", { 10 }}},
336 {{"output", { 25 }}});
337}
338
339struct FullyConnectedNonConstWeightsNoBias : FullyConnectedNonConstWeightsFixture
340{
341 FullyConnectedNonConstWeightsNoBias()
342 : FullyConnectedNonConstWeightsFixture("[ 1, 4, 1, 1 ]", // inputShape
343 "[ 1, 1 ]", // outputShape
344 "[ 1, 4 ]") // filterShape
345
346 {}
347};
348
Sadik Armagan1625efc2021-06-10 18:24:34 +0100349TEST_CASE_FIXTURE(FullyConnectedNonConstWeightsNoBias, "ParseFullyConnectedNonConstWeightsNoBias")
Finn Williamsd4fa5452021-03-01 12:31:41 +0000350{
351 RunTest<2, armnn::DataType::QAsymmS8,
352 armnn::DataType::QAsymmS8>(
353 0,
354 {{{"input_0", { 1, 2, 3, 4 }},{"weights", { 2, 3, 4, 5 }}}},
355 {{"output", { 20 }}});
356}
357
Sadik Armagan1625efc2021-06-10 18:24:34 +0100358}