xref: /aosp_15_r20/external/armnn/src/armnnDeserializer/test/DeserializeFullyConnected.cpp (revision 89c4ff92f2867872bb9e2354d150bf0c8c502810)
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "ParserFlatbuffersSerializeFixture.hpp"
7 #include <armnnDeserializer/IDeserializer.hpp>
8 
9 #include <string>
10 
11 TEST_SUITE("DeserializeParser_FullyConnected")
12 {
13 struct FullyConnectedFixture : public ParserFlatbuffersSerializeFixture
14 {
FullyConnectedFixtureFullyConnectedFixture15     explicit FullyConnectedFixture(const std::string & inputShape1,
16                                    const std::string & outputShape,
17                                    const std::string & weightsShape,
18                                    const std::string & dataType)
19     {
20         m_JsonString = R"(
21         {
22             inputIds: [0],
23             outputIds: [2],
24             layers: [{
25                 layer_type: "InputLayer",
26                 layer: {
27                     base: {
28                         layerBindingId: 0,
29                         base: {
30                             index: 0,
31                             layerName: "InputLayer",
32                             layerType: "Input",
33                             inputSlots: [{
34                                 index: 0,
35                                 connection: {sourceLayerIndex:0, outputSlotIndex:0 },
36                                 }],
37                             outputSlots: [{
38                                 index: 0,
39                                 tensorInfo: {
40                                     dimensions: )" + inputShape1 + R"(,
41                                     dataType: )" + dataType + R"(,
42                                     quantizationScale: 1.0,
43                                     quantizationOffset: 0
44                                     },
45                                 }]
46                             },
47                         }
48                     },
49                 },
50             {
51             layer_type: "FullyConnectedLayer",
52             layer : {
53                 base: {
54                     index:1,
55                     layerName: "FullyConnectedLayer",
56                     layerType: "FullyConnected",
57                     inputSlots: [{
58                             index: 0,
59                             connection: {sourceLayerIndex:0, outputSlotIndex:0 },
60                         }],
61                     outputSlots: [{
62                         index: 0,
63                         tensorInfo: {
64                             dimensions: )" + outputShape + R"(,
65                             dataType: )" + dataType + R"(,
66                             quantizationScale: 2.0,
67                             quantizationOffset: 0
68                         },
69                         }],
70                     },
71                 descriptor: {
72                     biasEnabled: false,
73                     transposeWeightsMatrix: true
74                     },
75                 weights: {
76                     info: {
77                              dimensions: )" + weightsShape + R"(,
78                              dataType: )" + dataType + R"(,
79                              quantizationScale: 1.0,
80                              quantizationOffset: 0
81                          },
82                     data_type: ByteData,
83                     data: {
84                         data: [
85                             2, 3, 4, 5
86                             ],
87                         }
88                     }
89                 },
90             },
91             {
92             layer_type: "OutputLayer",
93             layer: {
94                 base:{
95                     layerBindingId: 0,
96                     base: {
97                         index: 2,
98                         layerName: "OutputLayer",
99                         layerType: "Output",
100                         inputSlots: [{
101                             index: 0,
102                             connection: {sourceLayerIndex:1, outputSlotIndex:0 },
103                         }],
104                         outputSlots: [ {
105                             index: 0,
106                             tensorInfo: {
107                                 dimensions: )" + outputShape + R"(,
108                                 dataType: )" + dataType + R"(
109                             },
110                         }],
111                     }
112                 }},
113             }]
114         }
115         )";
116         Setup();
117     }
118 };
119 
120 
121 struct FullyConnectedFixtureConstantAsInput : public ParserFlatbuffersSerializeFixture
122 {
FullyConnectedFixtureConstantAsInputFullyConnectedFixtureConstantAsInput123     explicit FullyConnectedFixtureConstantAsInput()
124     {
125         m_JsonString = R"(
126     {
127       "layers": [
128         {
129           "layer_type": "InputLayer",
130           "layer": {
131             "base": {
132               "base": {
133                 "index": 0,
134                 "layerName": "InputLayer",
135                 "layerType": "Input",
136                 "inputSlots": [
137 
138                 ],
139                 "outputSlots": [
140                   {
141                     "index": 0,
142                     "tensorInfo": {
143                       "dimensions": [
144                         1,
145                         4,
146                         1,
147                         1
148                       ],
149                       "dataType": "QAsymmU8",
150                       "quantizationScale": 1.0,
151                       "quantizationOffset": 0,
152                       "quantizationDim": 0,
153                       "dimensionality": 1,
154                       "dimensionSpecificity": [
155                         true,
156                         true,
157                         true,
158                         true
159                       ]
160                     }
161                   }
162                 ]
163               },
164               "layerBindingId": 0
165             }
166           }
167         },
168         {
169           "layer_type": "FullyConnectedLayer",
170           "layer": {
171             "base": {
172               "index": 1,
173               "layerName": "FullyConnectedLayer",
174               "layerType": "FullyConnected",
175               "inputSlots": [
176                 {
177                   "index": 0,
178                   "connection": {
179                     "sourceLayerIndex": 0,
180                     "outputSlotIndex": 0
181                   }
182                 },
183                 {
184                   "index": 1,
185                   "connection": {
186                     "sourceLayerIndex": 2,
187                     "outputSlotIndex": 0
188                   }
189                 }
190               ],
191               "outputSlots": [
192                 {
193                   "index": 0,
194                   "tensorInfo": {
195                     "dimensions": [
196                       1,
197                       1
198                     ],
199                     "dataType": "QAsymmU8",
200                     "quantizationScale": 2.0,
201                     "quantizationOffset": 0,
202                     "quantizationDim": 0,
203                     "dimensionality": 1,
204                     "dimensionSpecificity": [
205                       true,
206                       true
207                     ]
208                   }
209                 }
210               ]
211             },
212             "descriptor": {
213               "biasEnabled": false,
214               "transposeWeightsMatrix": true,
215               "constantWeights": true
216             }
217           }
218         },
219         {
220           "layer_type": "ConstantLayer",
221           "layer": {
222             "base": {
223               "index": 2,
224               "layerName": "",
225               "layerType": "Constant",
226               "inputSlots": [
227 
228               ],
229               "outputSlots": [
230                 {
231                   "index": 0,
232                   "tensorInfo": {
233                     "dimensions": [
234                       1,
235                       4
236                     ],
237                     "dataType": "QAsymmU8",
238                     "quantizationScale": 1.0,
239                     "quantizationOffset": 0,
240                     "quantizationDim": 0,
241                     "dimensionality": 1,
242                     "dimensionSpecificity": [
243                       true,
244                       true
245                     ],
246                     "isConstant": true,
247                   }
248                 }
249               ]
250             },
251             "input": {
252               "info": {
253                 "dimensions": [
254                   1,
255                   4
256                 ],
257                 "dataType": "QAsymmU8",
258                 "quantizationScale": 1.0,
259                 "quantizationOffset": 0,
260                 "quantizationDim": 0,
261                 "dimensionality": 1,
262                 "dimensionSpecificity": [
263                   true,
264                   true
265                 ]
266               },
267               "data_type": "ByteData",
268               "data": {
269                 "data": [
270                   2,
271                   3,
272                   4,
273                   5
274                 ]
275               }
276             }
277           }
278         },
279         {
280           "layer_type": "OutputLayer",
281           "layer": {
282             "base": {
283               "base": {
284                 "index": 3,
285                 "layerName": "OutputLayer",
286                 "layerType": "Output",
287                 "inputSlots": [
288                   {
289                     "index": 0,
290                     "connection": {
291                       "sourceLayerIndex": 1,
292                       "outputSlotIndex": 0
293                     }
294                   }
295                 ],
296                 "outputSlots": [
297 
298                 ]
299               },
300               "layerBindingId": 0
301             }
302           }
303         }
304       ],
305       "inputIds": [
306         0
307       ],
308       "outputIds": [
309         0
310       ],
311       "featureVersions": {
312         "bindingIdsScheme": 1,
313         "weightsLayoutScheme": 1,
314         "constantTensorsAsInputs": 1
315       }
316     }
317     )";
318         Setup();
319     }
320 };
321 
322 struct FullyConnectedWithNoBiasFixture : FullyConnectedFixture
323 {
FullyConnectedWithNoBiasFixtureFullyConnectedWithNoBiasFixture324     FullyConnectedWithNoBiasFixture()
325             : FullyConnectedFixture("[ 1, 4, 1, 1 ]",     // inputShape
326                                     "[ 1, 1 ]",           // outputShape
327                                     "[ 1, 4 ]",           // filterShape
328                                     "QuantisedAsymm8")    // filterData
329     {}
330 };
331 
332 TEST_CASE_FIXTURE(FullyConnectedWithNoBiasFixture, "FullyConnectedWithNoBias")
333 {
334     // Weights and biases used to be always constant and were stored as members of the layer. This has changed and
335     // they are now passed as inputs (ConstantLayer) but the old way can still be used for now.
336     RunTest<2, armnn::DataType::QAsymmU8>(
337             0,
338             {{"InputLayer",  { 10, 20, 30, 40 }}},
339             {{"OutputLayer", { 400/2 }}});
340 }
341 
342 struct FullyConnectedWithNoBiasFixtureConstantAsInput : FullyConnectedFixtureConstantAsInput
343 {
FullyConnectedWithNoBiasFixtureConstantAsInputFullyConnectedWithNoBiasFixtureConstantAsInput344     FullyConnectedWithNoBiasFixtureConstantAsInput()
345             : FullyConnectedFixtureConstantAsInput()
346     {}
347 };
348 
349 TEST_CASE_FIXTURE(FullyConnectedWithNoBiasFixtureConstantAsInput, "FullyConnectedWithNoBiasConstantAsInput")
350 {
351     RunTest<2, armnn::DataType::QAsymmU8>(
352             0,
353             {{"InputLayer",  { 10, 20, 30, 40 }}},
354             {{"OutputLayer", { 400/2 }}});
355 }
356 
357 }
358