xref: /aosp_15_r20/external/armnn/src/armnnTfLiteParser/test/Unpack.cpp (revision 89c4ff92f2867872bb9e2354d150bf0c8c502810)
1 //
2 // Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "ParserFlatbuffersFixture.hpp"
7 
8 
9 TEST_SUITE("TensorflowLiteParser_Unpack")
10 {
11 struct UnpackFixture : public ParserFlatbuffersFixture
12 {
UnpackFixtureUnpackFixture13     explicit UnpackFixture(const std::string& inputShape,
14                            const unsigned int numberOfOutputs,
15                            const std::string& outputShape,
16                            const std::string& axis,
17                            const std::string& num,
18                            const std::string& dataType,
19                            const std::string& outputScale,
20                            const std::string& outputOffset)
21     {
22         // As input index is 0, output indexes start at 1
23         std::string outputIndexes = "1";
24         for(unsigned int i = 1; i < numberOfOutputs; i++)
25         {
26             outputIndexes += ", " + std::to_string(i+1);
27         }
28         m_JsonString = R"(
29             {
30                 "version": 3,
31                 "operator_codes": [ { "builtin_code": "UNPACK" } ],
32                 "subgraphs": [ {
33                     "tensors": [
34                         {
35                             "shape": )" + inputShape + R"(,
36                             "type": )" + dataType + R"(,
37                             "buffer": 0,
38                             "name": "inputTensor",
39                             "quantization": {
40                                 "min": [ 0.0 ],
41                                 "max": [ 255.0 ],
42                                 "scale": [ 1.0 ],
43                                 "zero_point": [ 0 ],
44                             }
45                         },)";
46         // Append the required number of outputs for this UnpackFixture.
47         // As input index is 0, output indexes start at 1.
48         for(unsigned int i = 0; i < numberOfOutputs; i++)
49         {
50             m_JsonString += R"(
51                         {
52                             "shape": )" + outputShape + R"( ,
53                                 "type": )" + dataType + R"(,
54                                 "buffer": )" + std::to_string(i + 1) + R"(,
55                                 "name": "outputTensor)" + std::to_string(i + 1) + R"(",
56                                 "quantization": {
57                                 "min": [ 0.0 ],
58                                 "max": [ 255.0 ],
59                                 "scale": [ )" + outputScale + R"( ],
60                                 "zero_point": [ )" + outputOffset + R"( ],
61                             }
62                         },)";
63         }
64         m_JsonString += R"(
65                     ],
66                     "inputs": [ 0 ],
67                     "outputs": [ )" + outputIndexes + R"( ],
68                     "operators": [
69                         {
70                             "opcode_index": 0,
71                             "inputs": [ 0 ],
72                             "outputs": [ )" + outputIndexes + R"( ],
73                             "builtin_options_type": "UnpackOptions",
74                             "builtin_options": {
75                                 "axis": )" + axis;
76 
77                     if(!num.empty())
78                     {
79                         m_JsonString += R"(,
80                                 "num" : )" + num;
81                     }
82 
83                     m_JsonString += R"(
84                             },
85                             "custom_options_format": "FLEXBUFFERS"
86                         }
87                     ],
88                 } ],
89                 "buffers" : [
90                     { },
91                     { }
92                 ]
93             }
94         )";
95         Setup();
96     }
97 };
98 
99 struct DefaultUnpackAxisZeroFixture : UnpackFixture
100 {
DefaultUnpackAxisZeroFixtureDefaultUnpackAxisZeroFixture101     DefaultUnpackAxisZeroFixture() : UnpackFixture("[ 4, 1, 6 ]", 4, "[ 1, 6 ]", "0", "", "FLOAT32", "1.0", "0") {}
102 };
103 
104 struct DefaultUnpackAxisZeroUint8Fixture : UnpackFixture
105 {
DefaultUnpackAxisZeroUint8FixtureDefaultUnpackAxisZeroUint8Fixture106     DefaultUnpackAxisZeroUint8Fixture() : UnpackFixture("[ 4, 1, 6 ]", 4, "[ 1, 6 ]", "0", "", "UINT8", "0.1", "0") {}
107 };
108 
109 TEST_CASE_FIXTURE(DefaultUnpackAxisZeroFixture, "UnpackAxisZeroNumIsDefaultNotSpecified")
110 {
111     RunTest<2, armnn::DataType::Float32>(
112         0,
113         { {"inputTensor", { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f,
114                             7.0f, 8.0f, 9.0f, 10.0f, 11.0f, 12.0f,
115                             13.0f, 14.0f, 15.0f, 16.0f, 17.0f, 18.0f,
116                             19.0f, 20.0f, 21.0f, 22.0f, 23.0f, 24.0f } } },
117         { {"outputTensor1", { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f }},
118           {"outputTensor2", { 7.0f, 8.0f, 9.0f, 10.0f, 11.0f, 12.0f }},
119           {"outputTensor3", { 13.0f, 14.0f, 15.0f, 16.0f, 17.0f, 18.0f }},
120           {"outputTensor4", { 19.0f, 20.0f, 21.0f, 22.0f, 23.0f, 24.0f }} });
121 }
122 
123 TEST_CASE_FIXTURE(DefaultUnpackAxisZeroUint8Fixture, "UnpackAxisZeroNumIsDefaultNotSpecifiedUint8")
124 {
125     RunTest<2, armnn::DataType::QAsymmU8>(
126         0,
127         { {"inputTensor", { 1, 2, 3, 4, 5, 6,
128                             7, 8, 9, 10, 11, 12,
129                             13, 14, 15, 16, 17, 18,
130                             19, 20, 21, 22, 23, 24 } } },
131         { {"outputTensor1", { 10, 20, 30, 40, 50, 60 }},
132           {"outputTensor2", { 70, 80, 90, 100, 110, 120 }},
133           {"outputTensor3", { 130, 140, 150, 160, 170, 180 }},
134           {"outputTensor4", { 190, 200, 210, 220, 230, 240 }} });
135 }
136 
137 struct DefaultUnpackLastAxisFixture : UnpackFixture
138 {
DefaultUnpackLastAxisFixtureDefaultUnpackLastAxisFixture139     DefaultUnpackLastAxisFixture() : UnpackFixture("[ 4, 1, 6 ]", 6, "[ 4, 1 ]", "2", "6", "FLOAT32", "1.0", "0") {}
140 };
141 
142 struct DefaultUnpackLastAxisUint8Fixture : UnpackFixture
143 {
DefaultUnpackLastAxisUint8FixtureDefaultUnpackLastAxisUint8Fixture144     DefaultUnpackLastAxisUint8Fixture() : UnpackFixture("[ 4, 1, 6 ]", 6, "[ 4, 1 ]", "2", "6", "UINT8", "0.1", "0") {}
145 };
146 
147 TEST_CASE_FIXTURE(DefaultUnpackLastAxisFixture, "UnpackLastAxisNumSix")
148 {
149     RunTest<2, armnn::DataType::Float32>(
150         0,
151         { {"inputTensor", { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f,
152                             7.0f, 8.0f, 9.0f, 10.0f, 11.0f, 12.0f,
153                             13.0f, 14.0f, 15.0f, 16.0f, 17.0f, 18.0f,
154                             19.0f, 20.0f, 21.0f, 22.0f, 23.0f, 24.0f } } },
155         { {"outputTensor1", { 1.0f, 7.0f, 13.0f, 19.0f }},
156           {"outputTensor2", { 2.0f, 8.0f, 14.0f, 20.0f }},
157           {"outputTensor3", { 3.0f, 9.0f, 15.0f, 21.0f }},
158           {"outputTensor4", { 4.0f, 10.0f, 16.0f, 22.0f }},
159           {"outputTensor5", { 5.0f, 11.0f, 17.0f, 23.0f }},
160           {"outputTensor6", { 6.0f, 12.0f, 18.0f, 24.0f }} });
161 }
162 
163 TEST_CASE_FIXTURE(DefaultUnpackLastAxisUint8Fixture, "UnpackLastAxisNumSixUint8") {
164     RunTest<2, armnn::DataType::QAsymmU8>(
165         0,
166         {{"inputTensor", { 1, 2, 3, 4, 5, 6,
167                            7, 8, 9, 10, 11, 12,
168                            13, 14, 15, 16, 17, 18,
169                            19, 20, 21, 22, 23, 24 }}},
170         {{"outputTensor1", { 10, 70, 130, 190 }},
171          {"outputTensor2", { 20, 80, 140, 200 }},
172          {"outputTensor3", { 30, 90, 150, 210 }},
173          {"outputTensor4", { 40, 100, 160, 220 }},
174          {"outputTensor5", { 50, 110, 170, 230 }},
175          {"outputTensor6", { 60, 120, 180, 240 }}});
176 }
177 
178 }
179