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