xref: /aosp_15_r20/external/armnn/src/armnnTfLiteParser/test/LoadScopeDynamicTensor.cpp (revision 89c4ff92f2867872bb9e2354d150bf0c8c502810)
1 //
2 // Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "armnnTfLiteParser/ITfLiteParser.hpp"
7 #include "ParserFlatbuffersFixture.hpp"
8 
9 
10 TEST_SUITE("TensorflowLiteParser_LoadScopeDynamicTensor")
11 {
12 struct LoadScopeDynamicTensorFixture : public ParserFlatbuffersFixture
13 {
LoadScopeDynamicTensorFixtureLoadScopeDynamicTensorFixture14     explicit LoadScopeDynamicTensorFixture(const std::string& shape0,
15                                            const std::string& shape1,
16                                            const std::string& shape2)
17     {
18         m_JsonString = R"(
19         {
20             "version": 3,
21             "operator_codes": [
22                 {
23                     "builtin_code": "AVERAGE_POOL_2D",
24                     "version": 1
25                 },
26                 {
27                     "builtin_code": "SOFTMAX",
28                     "version": 1
29                 }
30             ],
31             "subgraphs": [
32                 {
33                     "tensors": [
34                         {
35                             "shape": )" + shape0 + R"(,
36                             "type": "FLOAT32",
37                             "buffer": 1,
38                             "name": "input0",
39                             "quantization": {
40                                 "details_type": 0,
41                                 "quantized_dimension": 0
42                             },
43                             "is_variable": false
44                         },
45                         {
46                             "shape": )" + shape1 + R"(,
47                             "type": "FLOAT32",
48                             "buffer": 3,
49                             "name": "output",
50                             "quantization": {
51                                 "details_type": 0,
52                                 "quantized_dimension": 0
53                             },
54                             "is_variable": false
55                         },
56                         {
57                             "shape": )" + shape2 + R"(,
58                             "type": "FLOAT32",
59                             "buffer": 2,
60                             "name": "model/average_pooling2d/AvgPool",
61                             "quantization": {
62                                 "details_type": 0,
63                                 "quantized_dimension": 0
64                             },
65                             "is_variable": false
66                         }
67                     ],
68                     "inputs": [
69                         0
70                     ],
71                     "outputs": [
72                         1
73                     ],
74                     "operators": [
75                         {
76                             "opcode_index": 1,
77                             "inputs": [
78                                 2
79                             ],
80                             "outputs": [
81                                 1
82                             ],
83                             "builtin_options_type": "SoftmaxOptions",
84                             "builtin_options": {
85                                 "beta": 1.0
86                             },
87                             "custom_options_format": "FLEXBUFFERS"
88                         },
89                         {
90                             "opcode_index": 0,
91                             "inputs": [
92                                 0
93                             ],
94                             "outputs": [
95                                 2
96                             ],
97                             "builtin_options_type": "Pool2DOptions",
98                             "builtin_options": {
99                                 "padding": "VALID",
100                                 "stride_w": 2,
101                                 "stride_h": 2,
102                                 "filter_width": 2,
103                                 "filter_height": 2,
104                                 "fused_activation_function": "NONE"
105                             },
106                             "custom_options_format": "FLEXBUFFERS"
107                         }
108                     ],
109                     "name": "main"
110                 }
111             ],
112             "description": "MLIR Converted.",
113             "buffers": [
114                 {
115                 },
116                 {
117                 },
118                 {
119                 },
120                 {
121                 }
122             ]
123         }
124         )";
125         Setup();
126     }
127 };
128 
129 struct LoadScopeDynamicTensor0Fixture : LoadScopeDynamicTensorFixture
130 {
LoadScopeDynamicTensor0FixtureLoadScopeDynamicTensor0Fixture131     LoadScopeDynamicTensor0Fixture() : LoadScopeDynamicTensorFixture("[ 1, 2, 3, 2 ]", "[]", "[]") {}
132 };
133 
134 struct LoadScopeDynamicTensor1Fixture : LoadScopeDynamicTensorFixture
135 {
LoadScopeDynamicTensor1FixtureLoadScopeDynamicTensor1Fixture136     LoadScopeDynamicTensor1Fixture() : LoadScopeDynamicTensorFixture("[ 1, 2, 4, 1 ]", "[ 1, 1, 2, 1 ]", "[]") {}
137 };
138 
139 struct LoadScopeDynamicTensor2Fixture : LoadScopeDynamicTensorFixture
140 {
LoadScopeDynamicTensor2FixtureLoadScopeDynamicTensor2Fixture141     LoadScopeDynamicTensor2Fixture() : LoadScopeDynamicTensorFixture("[ 1, 3, 3, 2 ]", "[ ]", "[ 1, 1, 1, 2 ]") {}
142 };
143 
144 TEST_CASE_FIXTURE(LoadScopeDynamicTensor0Fixture, "LoadScopeDynamicTensor0")
145 {
146     RunTest<4, armnn::DataType::Float32, armnn::DataType::Float32>(
147         0,
148         { {"input0", { 0.f, 1.f, 2.f, 3.f, 4.f, 5.f, 6.f, 7.f, 8.f, 9.f, 10.f, 11.f }} },
149         { {"output", { 0.26894143f, 0.7310586f }} },
150         true);
151 }
152 
153 TEST_CASE_FIXTURE(LoadScopeDynamicTensor1Fixture, "LoadScopeDynamicTensor1")
154 {
155     RunTest<4, armnn::DataType::Float32, armnn::DataType::Float32>(
156         0,
157         { {"input0", { 0.f, 1.f, 2.f, 3.f, 4.f, 5.f, 6.f, 7.f }} },
158         { {"output", { 1.f, 1.f }} },
159         true);
160 }
161 
162 TEST_CASE_FIXTURE(LoadScopeDynamicTensor2Fixture, "LoadScopeDynamicTensor2")
163 {
164   RunTest<4, armnn::DataType::Float32, armnn::DataType::Float32>(
165         0,
166         { {"input0", { 0.f, 1.f, 2.f, 3.f, 4.f, 5.f, 6.f, 7.f, 8.f, 0.f, 1.f, 2.f, 3.f, 4.f, 5.f, 6.f, 7.f, 8.f }} },
167         { {"output", { 0.7772999f, 0.22270015f }} },
168         true);
169 }
170 
171 struct LoadScopeDynamicTensorBroadcastingFixture : public ParserFlatbuffersFixture
172 {
LoadScopeDynamicTensorBroadcastingFixtureLoadScopeDynamicTensorBroadcastingFixture173     explicit LoadScopeDynamicTensorBroadcastingFixture(const std::string& inputShape0,
174                                                        const std::string& inputShape1,
175                                                        const std::string& inputShape2,
176                                                        const std::string& addShape,
177                                                        const std::string& outputShape)
178     {
179         m_JsonString = R"(
180         {
181             "version": 3,
182             "operator_codes": [
183                 {
184                     "builtin_code": "ADD",
185                     "version": 1
186                 },
187                 {
188                     "builtin_code": "SUB",
189                     "version": 1
190                 }
191             ],
192             "subgraphs": [
193                 {
194                     "tensors": [
195                         {
196                             "shape": )" + inputShape0 + R"(,
197                             "type": "FLOAT32",
198                             "buffer": 1,
199                             "name": "input0",
200                             "quantization": {
201                                 "details_type": 0,
202                                 "quantized_dimension": 0
203                             },
204                             "is_variable": false
205                         },
206                         {
207                             "shape": )" + inputShape1 + R"(,
208                             "type": "FLOAT32",
209                             "buffer": 2,
210                             "name": "input1",
211                             "quantization": {
212                                 "details_type": 0,
213                                 "quantized_dimension": 0
214                             },
215                             "is_variable": false
216                         },
217                         {
218                             "shape": )" + outputShape + R"(,
219                             "type": "FLOAT32",
220                             "buffer": 5,
221                             "name": "output",
222                             "quantization": {
223                                 "details_type": 0,
224                                 "quantized_dimension": 0
225                             },
226                             "is_variable": false
227                         },
228 
229                         {
230                             "shape": )" + addShape + R"(,
231                             "type": "FLOAT32",
232                             "buffer": 4,
233                             "name": "model/add/add",
234                             "quantization": {
235                                 "details_type": 0,
236                                 "quantized_dimension": 0
237                             },
238                             "is_variable": false
239                         },
240                         {
241                             "shape": )" + inputShape2 + R"(,
242                             "type": "FLOAT32",
243                             "buffer": 3,
244                             "name": "input2",
245                             "quantization": {
246                                 "details_type": 0,
247                                 "quantized_dimension": 0
248                             },
249                             "is_variable": false
250                         },
251                     ],
252                     "inputs": [
253                         0,
254                         1,
255                         4
256                     ],
257                     "outputs": [
258                         2
259                     ],
260                     "operators": [
261                         {
262                             "opcode_index": 0,
263                             "inputs": [
264                                 0,
265                                 1
266                             ],
267                             "outputs": [
268                                 3
269                             ],
270                             "builtin_options_type": "AddOptions",
271                             "builtin_options": {
272                                 "fused_activation_function": "NONE"
273                             },
274                             "custom_options_format": "FLEXBUFFERS"
275                         },
276                         {
277                             "opcode_index": 1,
278                             "inputs": [
279                                 3,
280                                 4
281                             ],
282                             "outputs": [
283                                 2
284                             ],
285                             "builtin_options_type": "SubOptions",
286                             "builtin_options": {
287                                 "fused_activation_function": "NONE"
288                             },
289                             "custom_options_format": "FLEXBUFFERS"
290                         }
291                     ],
292                     "name": "main"
293                 }
294             ],
295             "buffers": [
296                 {
297                 },
298                 {
299                 },
300                 {
301                 },
302                 {
303                 },
304                 {
305                 },
306                 {
307                 }
308             ]
309         }
310         )";
311         Setup();
312     }
313 };
314 
315 struct LoadScopeDynamicTensorBroadcasting3DFixture : LoadScopeDynamicTensorBroadcastingFixture
316 {
LoadScopeDynamicTensorBroadcasting3DFixtureLoadScopeDynamicTensorBroadcasting3DFixture317     LoadScopeDynamicTensorBroadcasting3DFixture() : LoadScopeDynamicTensorBroadcastingFixture("[ 1, 2, 3, 2 ]",
318                                                                                               "[ 2, 3, 2 ]",
319                                                                                               "[ 2, 3, 2 ]",
320                                                                                               "[ 1, 2, 3, 2 ]", "[]") {}
321 };
322 
323 struct LoadScopeDynamicTensorBroadcasting2DFixture : LoadScopeDynamicTensorBroadcastingFixture
324 {
LoadScopeDynamicTensorBroadcasting2DFixtureLoadScopeDynamicTensorBroadcasting2DFixture325     LoadScopeDynamicTensorBroadcasting2DFixture() : LoadScopeDynamicTensorBroadcastingFixture("[ 1, 2, 3, 2 ]",
326                                                                                               "[ 3, 2 ]",
327                                                                                               "[ 3, 2 ]",
328                                                                                               "[]", "[]") {}
329 };
330 
331 struct LoadScopeDynamicTensorBroadcasting1DFixture : LoadScopeDynamicTensorBroadcastingFixture
332 {
LoadScopeDynamicTensorBroadcasting1DFixtureLoadScopeDynamicTensorBroadcasting1DFixture333     LoadScopeDynamicTensorBroadcasting1DFixture() : LoadScopeDynamicTensorBroadcastingFixture("[ 1, 2, 3, 2 ]",
334                                                                                               "[ 1 ]",
335                                                                                               "[ 1 ]",
336                                                                                               "[]",
337                                                                                               "[ 1, 2, 3, 2 ]") {}
338 };
339 
340 TEST_CASE_FIXTURE(LoadScopeDynamicTensorBroadcasting3DFixture, "LoadScopeDynamicTensorBroadcasting3D")
341 {
342     RunTest<4, armnn::DataType::Float32, armnn::DataType::Float32>(
343         0,
344         { {"input0", { 0.f, 1.f, 2.f, 3.f, 4.f, 5.f, 6.f, 7.f, 8.f, 9.f, 10.f, 11.f }},
345           {"input1", { 3.f, 4.f, 5.f, 6.f, 7.f, 8.f, 9.f, 10.f, 11.f, 12.f, 13.f, 14.f }},
346           {"input2", { 1.f, 2.f, 3.f, 4.f, 5.f, 6.f, 7.f, 8.f, 9.f, 10.f, 11.f, 12.f }}
347         },
348         { {"output", { 2.f, 3.f, 4.f, 5.f, 6.f, 7.f, 8.f, 9.f, 10.f, 11.f, 12.f, 13.f }} },
349         true);
350 }
351 
352 TEST_CASE_FIXTURE(LoadScopeDynamicTensorBroadcasting2DFixture, "LoadScopeDynamicTensorBroadcasting2D")
353 {
354     RunTest<4, armnn::DataType::Float32, armnn::DataType::Float32>(
355         0,
356         { {"input0", { 0.f, 1.f, 2.f, 3.f, 4.f, 5.f, 6.f, 7.f, 8.f, 9.f, 10.f, 11.f }},
357           {"input1", { 3.f, 4.f, 5.f, 6.f, 7.f, 8.f }},
358           {"input2", { -1.f, -2.f, 3.f, 4.f, 5.f, 6.f }}
359         },
360         { {"output", { 4.f, 7.f, 4.f, 5.f, 6.f, 7.f, 10.f, 13.f, 10.f, 11.f, 12.f, 13.f }} },
361         true);
362 }
363 
364 TEST_CASE_FIXTURE(LoadScopeDynamicTensorBroadcasting1DFixture, "LoadScopeDynamicTensorBroadcasting1D")
365 {
366     RunTest<4, armnn::DataType::Float32, armnn::DataType::Float32>(
367         0,
368         { {"input0", { 0.f, 1.f, 2.f, 3.f, 4.f, 5.f, 6.f, 7.f, 8.f, 9.f, 10.f, 11.f }},
369           {"input1", { 5.f }},
370           {"input2", { 1.f }}
371         },
372         { {"output", { 4.f, 5.f, 6.f, 7.f, 8.f, 9.f, 10.f, 11.f, 12.f, 13.f, 14.f, 15.f }} },
373         true);
374 }
375 
376 }
377