ArmNN
 20.02
Transpose.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2019 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 BOOST_AUTO_TEST_SUITE(TensorflowLiteParser)
11 
12 struct TransposeFixture : public ParserFlatbuffersFixture
13 {
14  explicit TransposeFixture(const std::string & inputShape,
15  const std::string & permuteData,
16  const std::string & outputShape)
17  {
18  m_JsonString = R"(
19  {
20  "version": 3,
21  "operator_codes": [
22  {
23  "builtin_code": "TRANSPOSE",
24  "version": 1
25  }
26  ],
27  "subgraphs": [
28  {
29  "tensors": [
30  {
31  "shape": )" + inputShape + R"(,
32  "type": "FLOAT32",
33  "buffer": 0,
34  "name": "inputTensor",
35  "quantization": {
36  "min": [
37  0.0
38  ],
39  "max": [
40  255.0
41  ],
42  "details_type": 0,
43  "quantized_dimension": 0
44  },
45  "is_variable": false
46  },
47  {
48  "shape": )" + outputShape + R"(,
49  "type": "FLOAT32",
50  "buffer": 1,
51  "name": "outputTensor",
52  "quantization": {
53  "details_type": 0,
54  "quantized_dimension": 0
55  },
56  "is_variable": false
57  })";
58  m_JsonString += R"(,
59  {
60  "shape": [
61  3
62  ],
63  "type": "INT32",
64  "buffer": 2,
65  "name": "permuteTensor",
66  "quantization": {
67  "details_type": 0,
68  "quantized_dimension": 0
69  },
70  "is_variable": false
71  })";
72  m_JsonString += R"(],
73  "inputs": [
74  0
75  ],
76  "outputs": [
77  1
78  ],
79  "operators": [
80  {
81  "opcode_index": 0,
82  "inputs": [
83  0)";
84  m_JsonString += R"(,2)";
85  m_JsonString += R"(],
86  "outputs": [
87  1
88  ],
89  "builtin_options_type": "TransposeOptions",
90  "builtin_options": {
91  },
92  "custom_options_format": "FLEXBUFFERS"
93  }
94  ]
95  }
96  ],
97  "description": "TOCO Converted.",
98  "buffers": [
99  { },
100  { })";
101  if (!permuteData.empty())
102  {
103  m_JsonString += R"(,{"data": )" + permuteData + R"( })";
104  }
105  m_JsonString += R"(
106  ]
107  }
108  )";
109  Setup();
110  }
111 };
112 
113 // Note that this assumes the Tensorflow permutation vector implementation as opposed to the armnn implemenation.
114 struct TransposeFixtureWithPermuteData : TransposeFixture
115 {
116  TransposeFixtureWithPermuteData() : TransposeFixture("[ 2, 2, 3 ]",
117  "[ 0, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0 ]",
118  "[ 2, 3, 2 ]") {}
119 };
120 
121 BOOST_FIXTURE_TEST_CASE(TransposeWithPermuteData, TransposeFixtureWithPermuteData)
122 {
123  RunTest<3, armnn::DataType::Float32>(
124  0,
125  {{"inputTensor", { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }}},
126  {{"outputTensor", { 1, 4, 2, 5, 3, 6, 7, 10, 8, 11, 9, 12 }}});
127 
128  BOOST_TEST((m_Parser->GetNetworkOutputBindingInfo(0, "outputTensor").second.GetShape()
129  == armnn::TensorShape({2,3,2})));
130 }
131 
132 // Tensorflow default permutation behavior assumes no permute argument will create permute vector [n-1...0],
133 // where n is the number of dimensions of the input tensor
134 // In this case we should get output shape 3,2,2 given default permutation vector 2,1,0
135 struct TransposeFixtureWithoutPermuteData : TransposeFixture
136 {
137  TransposeFixtureWithoutPermuteData() : TransposeFixture("[ 2, 2, 3 ]",
138  "[ 2, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 ]",
139  "[ 3, 2, 2 ]") {}
140 };
141 
142 BOOST_FIXTURE_TEST_CASE(TransposeWithoutPermuteDims, TransposeFixtureWithoutPermuteData)
143 {
144  RunTest<3, armnn::DataType::Float32>(
145  0,
146  {{"inputTensor", { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }}},
147  {{"outputTensor", { 1, 7, 4, 10, 2, 8, 5, 11, 3, 9, 6, 12 }}});
148 
149  BOOST_TEST((m_Parser->GetNetworkOutputBindingInfo(0, "outputTensor").second.GetShape()
150  == armnn::TensorShape({3,2,2})));
151 }
152 
BOOST_AUTO_TEST_SUITE(TensorflowLiteParser)
BOOST_FIXTURE_TEST_CASE(TransposeWithPermuteData, TransposeFixtureWithPermuteData)
Definition: Transpose.cpp:121
BOOST_AUTO_TEST_SUITE_END()