blob: 9a5d6c0ba81b750eb3e08717415d5fe74f99674f [file] [log] [blame]
Idriss Chaouch564c13d2023-09-01 17:58:38 +01001//
2// Copyright © 2023 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include "ParserFlatbuffersFixture.hpp"
7
8TEST_SUITE("TensorflowLiteParser_BroadcastTo")
9{
10struct BroadcastToFixture : public ParserFlatbuffersFixture
11{
12 explicit BroadcastToFixture(const std::string& inputShape1,
13 const std::string& inputShape2,
14 const std::string& shapeShape,
15 const std::string& outputBroadcastToShape,
16 const std::string& outputShape,
17 const std::string& shapeData,
18 const bool checkThrows,
19 const std::string& scale = "1.0",
20 const std::string& offset = "0")
21 {
22 m_JsonString = R"(
23 {
24 "version": 3,
25 "operator_codes": [
26 {
27 "deprecated_builtin_code": 127,
28 "version": 2,
29 "builtin_code": "BROADCAST_TO"
30 },
31 {
32 "version": 1,
33 "builtin_code": "MUL"
34 }
35 ],
36 "subgraphs": [
37 {
38 "tensors": [
39 {
40 "shape": )" + inputShape1 + R"(,
41 "type": "FLOAT32",
42 "buffer": 1,
43 "name": "inputTensor1",
44 "quantization":{
45 "min": [ 0.0 ],
46 "max": [ 255.0 ],
47 "scale": [ )" + scale + R"( ],
48 "zero_point": [ )" + offset + R"( ],
49 },
50 "is_variable": false,
51 "shape_signature": [
52 -1,
53 3
54 ],
55 "has_rank": true
56 },
57 {
58 "shape": )" + inputShape2 + R"(,
59 "type": "FLOAT32",
60 "buffer": 2,
61 "name": "inputTensor2",
62 "quantization": {
63 "min": [ 0.0 ],
64 "max": [ 255.0 ],
65 "scale": [ )" + scale + R"( ],
66 "zero_point": [ )" + offset + R"( ],
67 },
68 "is_variable": false,
69 "shape_signature": [
70 -1,
71 3
72 ],
73 "has_rank": true
74 },
75 {
76 "shape": )" + shapeShape + R"(,
77 "type": "INT32",
78 "buffer": 3,
79 "name": "shape",
80 "quantization": {
81 "details_type": "NONE",
82 "quantized_dimension": 0
83 },
84 "is_variable": false,
85 "shape_signature": [
86 -1
87 ],
88 "has_rank": true
89 },
90 {
91 "shape": )" + outputBroadcastToShape + R"(,
92 "type": "FLOAT32",
93 "buffer": 4,
94 "name": "model/tf.broadcast_to/BroadcastTo",
95 "quantization": {
96 "min": [ 0.0 ],
97 "max": [ 255.0 ],
98 "scale": [ )" + scale + R"( ],
99 "zero_point": [ )" + offset + R"( ],
100 },
101 "is_variable": false,
102 "has_rank": false
103 },
104 {
105 "shape": )" + outputShape + R"(,
106 "type": "FLOAT32",
107 "buffer": 5,
108 "name": "outputTensor",
109 "quantization": {
110 "min": [ 0.0 ],
111 "max": [ 255.0 ],
112 "scale": [ )" + scale + R"( ],
113 "zero_point": [ )" + offset + R"( ],
114 },
115 "is_variable": false,
116 "has_rank": false
117 }
118 ],
119 "inputs": [
120 0,
121 1,
122 2
123 ],
124 "outputs": [
125 4
126 ],
127 "operators": [
128 {
129 "opcode_index": 0,
130 "inputs": [
131 0,
132 2
133 ],
134 "outputs": [
135 3
136 ],
137 "builtin_options_type": "NONE",
138 "custom_options_format": "FLEXBUFFERS"
139 },
140 {
141 "opcode_index": 1,
142 "inputs": [
143 1,
144 3
145 ],
146 "outputs": [
147 4
148 ],
149 "builtin_options_type": "MulOptions",
150 "builtin_options": {
151 "fused_activation_function": "NONE"
152 },
153 "custom_options_format": "FLEXBUFFERS"
154 }
155 ],
156 "name": "main"
157 }
158 ],
159 "description": "MLIR Converted.",
160 "buffers": [
161 {
162 },
163 {
164 },
165 {
166 },
167 {
168 )" + shapeData + R"(
169 },
170 {
171 },
172 {
173 },
174 {
175 },
176 {
177 }
178 ],
179 "metadata": [
180 {
181 "name": "min_runtime_version",
182 "buffer": 6
183 },
184 {
185 "name": "CONVERSION_METADATA",
186 "buffer": 7
187 }
188 ],
189 "signature_defs": [
190
191 ]
192 }
193 )";
194 if(checkThrows)
195 {
196 CHECK_THROWS_AS(Setup(), armnn::ParseException);
197 }
198 else
199 {
200 Setup();
201 }
202 }
203};
204
205struct BroadcastToSimpleFixture : public ParserFlatbuffersFixture
206{
207 explicit BroadcastToSimpleFixture(const std::string& inputShape,
208 const std::string& shapeShape,
209 const std::string& outputShape,
210 const std::string& shapeData,
211 const std::string& dataType,
212 const std::string& scale = "1.0",
213 const std::string& offset = "0")
214 {
215 m_JsonString = R"(
216 {
217 "version": 3,
218 "operator_codes": [
219 {
220 "deprecated_builtin_code": 127,
221 "version": 2,
222 "builtin_code": "BROADCAST_TO"
223 }
224 ],
225 "subgraphs": [
226 {
227 "tensors": [
228 {
229 "shape": )" + inputShape + R"(,
230 "type": )" + dataType + R"(,
231 "buffer": 1,
232 "name": "input1",
233 "quantization": {
234 "min": [ 0.0 ],
235 "max": [ 255.0 ],
236 "scale": [ )" + scale + R"( ],
237 "zero_point": [ )" + offset + R"( ],
238 },
239 "is_variable": false,
240 "shape_signature": [
241 -1,
242 3
243 ],
244 "has_rank": true
245 },
246 {
247 "shape": )" + shapeShape + R"(,
248 "type": "INT32",
249 "buffer": 2,
250 "name": "shape",
251 "quantization": {
252 "details_type": "NONE",
253 "quantized_dimension": 0
254 },
255 "is_variable": false,
256 "shape_signature": [
257 -1
258 ],
259 "has_rank": true
260 },
261 {
262 "shape": )" + outputShape + R"(,
263 "type": )" + dataType + R"(,
264 "buffer": 3,
265 "name": "Identity",
266 "quantization": {
267 "min": [ 0.0 ],
268 "max": [ 255.0 ],
269 "scale": [ )" + scale + R"( ],
270 "zero_point": [ )" + offset + R"( ],
271 },
272 "is_variable": false,
273 "has_rank": false
274 }
275 ],
276 "inputs": [
277 0,
278 1
279 ],
280 "outputs": [
281 2
282 ],
283 "operators": [
284 {
285 "opcode_index": 0,
286 "inputs": [
287 0,
288 1
289 ],
290 "outputs": [
291 2
292 ],
293 "builtin_options_type": "NONE",
294 "custom_options_format": "FLEXBUFFERS"
295 }
296 ],
297 "name": "main"
298 }
299 ],
300 "description": "MLIR Converted.",
301 "buffers": [
302 {
303 },
304 {
305 },
306 {
307 "data": )" + shapeData + R"(,
308 },
309 {
310 },
311 {
312 },
313 {
314 }
315 ],
316 "metadata": [
317 {
318 "name": "min_runtime_version",
319 "buffer": 4
320 },
321 {
322 "name": "CONVERSION_METADATA",
323 "buffer": 5
324 }
325 ],
326 "signature_defs": [
327
328 ]
329 }
330 )";
331 SetupSingleInputSingleOutput("inputTensor", "outputTensor");
332 }
333};
334
335struct SimpleBroadcastToSimpleFixtureFloat32 : public BroadcastToSimpleFixture
336{
337 SimpleBroadcastToSimpleFixtureFloat32() : BroadcastToSimpleFixture("[1, 4]",
338 "[2]",
339 "[3, 4]",
340 "[3, 0, 0, 0, 4, 0, 0, 0]",
341 "FLOAT32") {}
342};
343
344TEST_CASE_FIXTURE(SimpleBroadcastToSimpleFixtureFloat32, "SimpleParseBroadcastToFloat32")
345{
346 RunTest<2, armnn::DataType::Float32, armnn::DataType::Float32>
347 (0, {{ "input1", { 1.f, 2.f, 3.f, 4.f }}},
348 {{ "Identity", { 1.f, 2.f, 3.f, 4.f,
349 1.f, 2.f, 3.f, 4.f,
350 1.f, 2.f, 3.f, 4.f}}});
351
352}
353struct SimpleBroadcastToSimpleFixtureSigned32 : public BroadcastToSimpleFixture
354{
355 SimpleBroadcastToSimpleFixtureSigned32() : BroadcastToSimpleFixture("[1, 4]",
356 "[2]",
357 "[3, 4]",
358 "[3, 0, 0, 0, 4, 0, 0, 0]",
359 "INT32") {}
360};
361
362TEST_CASE_FIXTURE(SimpleBroadcastToSimpleFixtureSigned32, "SimpleParseBroadcastToSigned32")
363{
364 RunTest<2, armnn::DataType::Signed32, armnn::DataType::Signed32>
365 (0, {{ "input1", { 1, 2, 3, -4 }}},
366 {{ "Identity", { 1, 2, 3, -4,
367 1, 2, 3, -4,
368 1, 2, 3, -4}}});
369
370}
371
372struct SimpleBroadcastToSimpleFixtureQAsymmU8 : public BroadcastToSimpleFixture
373{
374 SimpleBroadcastToSimpleFixtureQAsymmU8() : BroadcastToSimpleFixture("[1, 4]",
375 "[2]",
376 "[3, 4]",
377 "[3, 0, 0, 0, 4, 0, 0, 0]",
378 "UINT8") {}
379};
380
381TEST_CASE_FIXTURE(SimpleBroadcastToSimpleFixtureQAsymmU8, "SimpleParseBroadcastToQAsymmU8")
382{
383 RunTest<2, armnn::DataType::QAsymmU8, armnn::DataType::QAsymmU8>
384 (0, {{ "input1", { 1, 2, 3, 4 }}},
385 {{ "Identity", { 1, 2, 3, 4,
386 1, 2, 3, 4,
387 1, 2, 3, 4}}});
388
389}
390
391struct SimpleBroadcastToFixture : public BroadcastToFixture
392{
393 SimpleBroadcastToFixture() : BroadcastToFixture("[1, 4]",
394 "[3, 4]",
395 "[2]",
396 "[3, 4]",
397 "[3, 4]",
398 "\"data\":[3, 0, 0, 0, 4, 0, 0, 0]",
399 false) {}
400};
401
402TEST_CASE_FIXTURE(SimpleBroadcastToFixture, "ParseBroadcastTo")
403{
404 RunTest<2, armnn::DataType::Float32, armnn::DataType::Float32, armnn::DataType::Float32>
405 (
406 0, {{ "inputTensor1", { 1, 2, 3, 4 }}},
407 {{"inputTensor2", {1, 1, 1, 1,
408 1, 1, 1, 1,
409 1, 1, 1, 1}}},
410 {{ "outputTensor", { 1, 2, 3, 4,
411 1, 2, 3, 4,
412 1, 2, 3, 4}}}
413 );
414
415}
416
417struct DynamicBroadcastToFixture : public BroadcastToFixture
418{
419 DynamicBroadcastToFixture() : BroadcastToFixture("[1, 4]",
420 "[3, 4]",
421 "[2]",
422 "[3, 4]",
423 "[3, 4]",
424 "\"data\":[3, 0, 0, 0, 4, 0, 0, 0]",
425 false) {}
426};
427
428TEST_CASE_FIXTURE(DynamicBroadcastToFixture, "DynamicParseBroadcastTo")
429{
430 RunTest<2, armnn::DataType::Float32, armnn::DataType::Float32, armnn::DataType::Float32>
431 (
432 0, {{ "inputTensor1", { 1, 2, 3, 4 }}},
433 {{"inputTensor2", {1, 1, 1, 1,
434 1, 1, 1, 1,
435 1, 1, 1, 1}}},
436 {{ "outputTensor", { 1, 2, 3, 4,
437 1, 2, 3, 4,
438 1, 2, 3, 4}}}
439 );
440
441}
442
443struct DynamicBroadcastToFixtureNoOutputShape : public BroadcastToFixture
444{
445 DynamicBroadcastToFixtureNoOutputShape() : BroadcastToFixture("[1, 4]",
446 "[3, 4]",
447 "[2]",
448 "[]",
449 "[3, 4]",
450 "\"data\":[3, 0, 0, 0, 4, 0, 0, 0]",
451 false) {}
452};
453
454TEST_CASE_FIXTURE(DynamicBroadcastToFixtureNoOutputShape, "DynamicParseBroadcastToNoOutputShape")
455{
456 RunTest<2, armnn::DataType::Float32, armnn::DataType::Float32, armnn::DataType::Float32>
457 (
458 0, {{ "inputTensor1", { 1, 2, 3, 4 }}},
459 {{"inputTensor2", {1, 1, 1, 1,
460 1, 1, 1, 1,
461 1, 1, 1, 1}}},
462 {{ "outputTensor", { 1, 2, 3, 4,
463 1, 2, 3, 4,
464 1, 2, 3, 4}}}
465 );
466
467}
468
469struct DynamicBroadcastToFixtureNoData : public BroadcastToFixture
470{
471 DynamicBroadcastToFixtureNoData() : BroadcastToFixture("[1, 4]",
472 "[3, 4]",
473 "[2]",
474 "[3, 4]",
475 "[3, 4]",
476 "",
477 false) {}
478};
479
480TEST_CASE_FIXTURE(DynamicBroadcastToFixtureNoData, "DynamicParseBroadcastToNoData")
481{
482 RunTest<2, armnn::DataType::Float32, armnn::DataType::Float32, armnn::DataType::Float32>
483 (
484 0, {{ "inputTensor1", { 1, 2, 3, 4 }}},
485 {{"inputTensor2", {1, 1, 1, 1,
486 1, 1, 1, 1,
487 1, 1, 1, 1}}},
488 {{ "outputTensor", { 1, 2, 3, 4,
489 1, 2, 3, 4,
490 1, 2, 3, 4}}}
491 );
492
493}
494
495struct DynamicBroadcastToFixtureNoDataNoOutputShape : public BroadcastToFixture
496{
497 DynamicBroadcastToFixtureNoDataNoOutputShape() : BroadcastToFixture("[1, 4]",
498 "[3, 4]",
499 "[2]",
500 "[]",
501 "[3, 4]",
502 "", true) { }
503};
504
505TEST_CASE_FIXTURE(DynamicBroadcastToFixtureNoDataNoOutputShape, "DynamicParseBroadcastToNoDataNoOutputShape")
506{
507}
508}