1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #include <boost/test/unit_test.hpp>
6 #include "ParserFlatbuffersFixture.hpp"
7 #include "../TfLiteParser.hpp"
8
9 using armnnTfLiteParser::TfLiteParser;
10 using ModelPtr = TfLiteParser::ModelPtr;
11
12 BOOST_AUTO_TEST_SUITE(TensorflowLiteParser)
13
14 struct GetTensorIdsFixture : public ParserFlatbuffersFixture
15 {
GetTensorIdsFixtureGetTensorIdsFixture16 explicit GetTensorIdsFixture(const std::string& inputs, const std::string& outputs)
17 {
18 m_JsonString = R"(
19 {
20 "version": 3,
21 "operator_codes": [ { "builtin_code": "AVERAGE_POOL_2D" } ],
22 "subgraphs": [
23 {
24 "tensors": [
25 {
26 "shape": [ 1, 1, 1, 1 ] ,
27 "type": "UINT8",
28 "buffer": 0,
29 "name": "OutputTensor",
30 "quantization": {
31 "min": [ 0.0 ],
32 "max": [ 255.0 ],
33 "scale": [ 1.0 ],
34 "zero_point": [ 0 ]
35 }
36 },
37 {
38 "shape": [ 1, 2, 2, 1 ] ,
39 "type": "UINT8",
40 "buffer": 1,
41 "name": "InputTensor",
42 "quantization": {
43 "min": [ 0.0 ],
44 "max": [ 255.0 ],
45 "scale": [ 1.0 ],
46 "zero_point": [ 0 ]
47 }
48 }
49 ],
50 "inputs": [ 1 ],
51 "outputs": [ 0 ],
52 "operators": [ {
53 "opcode_index": 0,
54 "inputs": )"
55 + inputs
56 + R"(,
57 "outputs": )"
58 + outputs
59 + R"(,
60 "builtin_options_type": "Pool2DOptions",
61 "builtin_options":
62 {
63 "padding": "VALID",
64 "stride_w": 2,
65 "stride_h": 2,
66 "filter_width": 2,
67 "filter_height": 2,
68 "fused_activation_function": "NONE"
69 },
70 "custom_options_format": "FLEXBUFFERS"
71 } ]
72 }
73 ],
74 "description": "Test loading a model",
75 "buffers" : [ {}, {} ]
76 })";
77
78 ReadStringToBinary();
79 }
80 };
81
82 struct GetEmptyTensorIdsFixture : GetTensorIdsFixture
83 {
GetEmptyTensorIdsFixtureGetEmptyTensorIdsFixture84 GetEmptyTensorIdsFixture() : GetTensorIdsFixture("[ ]", "[ ]") {}
85 };
86
87 struct GetInputOutputTensorIdsFixture : GetTensorIdsFixture
88 {
GetInputOutputTensorIdsFixtureGetInputOutputTensorIdsFixture89 GetInputOutputTensorIdsFixture() : GetTensorIdsFixture("[ 0, 1, 2 ]", "[ 3 ]") {}
90 };
91
BOOST_FIXTURE_TEST_CASE(GetEmptyInputTensorIds,GetEmptyTensorIdsFixture)92 BOOST_FIXTURE_TEST_CASE(GetEmptyInputTensorIds, GetEmptyTensorIdsFixture)
93 {
94 TfLiteParser::ModelPtr model = TfLiteParser::LoadModelFromBinary(m_GraphBinary.data(), m_GraphBinary.size());
95 std::vector<int32_t> expectedIds = { };
96 std::vector<int32_t> inputTensorIds = TfLiteParser::GetInputTensorIds(model, 0, 0);
97 BOOST_CHECK_EQUAL_COLLECTIONS(expectedIds.begin(), expectedIds.end(),
98 inputTensorIds.begin(), inputTensorIds.end());
99 }
100
BOOST_FIXTURE_TEST_CASE(GetEmptyOutputTensorIds,GetEmptyTensorIdsFixture)101 BOOST_FIXTURE_TEST_CASE(GetEmptyOutputTensorIds, GetEmptyTensorIdsFixture)
102 {
103 TfLiteParser::ModelPtr model = TfLiteParser::LoadModelFromBinary(m_GraphBinary.data(), m_GraphBinary.size());
104 std::vector<int32_t> expectedIds = { };
105 std::vector<int32_t> outputTensorIds = TfLiteParser::GetOutputTensorIds(model, 0, 0);
106 BOOST_CHECK_EQUAL_COLLECTIONS(expectedIds.begin(), expectedIds.end(),
107 outputTensorIds.begin(), outputTensorIds.end());
108 }
109
BOOST_FIXTURE_TEST_CASE(GetInputTensorIds,GetInputOutputTensorIdsFixture)110 BOOST_FIXTURE_TEST_CASE(GetInputTensorIds, GetInputOutputTensorIdsFixture)
111 {
112 TfLiteParser::ModelPtr model = TfLiteParser::LoadModelFromBinary(m_GraphBinary.data(), m_GraphBinary.size());
113 std::vector<int32_t> expectedInputIds = { 0, 1, 2 };
114 std::vector<int32_t> inputTensorIds = TfLiteParser::GetInputTensorIds(model, 0, 0);
115 BOOST_CHECK_EQUAL_COLLECTIONS(expectedInputIds.begin(), expectedInputIds.end(),
116 inputTensorIds.begin(), inputTensorIds.end());
117 }
118
BOOST_FIXTURE_TEST_CASE(GetOutputTensorIds,GetInputOutputTensorIdsFixture)119 BOOST_FIXTURE_TEST_CASE(GetOutputTensorIds, GetInputOutputTensorIdsFixture)
120 {
121 TfLiteParser::ModelPtr model = TfLiteParser::LoadModelFromBinary(m_GraphBinary.data(), m_GraphBinary.size());
122 std::vector<int32_t> expectedOutputIds = { 3 };
123 std::vector<int32_t> outputTensorIds = TfLiteParser::GetOutputTensorIds(model, 0, 0);
124 BOOST_CHECK_EQUAL_COLLECTIONS(expectedOutputIds.begin(), expectedOutputIds.end(),
125 outputTensorIds.begin(), outputTensorIds.end());
126 }
127
BOOST_FIXTURE_TEST_CASE(GetInputTensorIdsNullModel,GetInputOutputTensorIdsFixture)128 BOOST_FIXTURE_TEST_CASE(GetInputTensorIdsNullModel, GetInputOutputTensorIdsFixture)
129 {
130 BOOST_CHECK_THROW(TfLiteParser::GetInputTensorIds(nullptr, 0, 0), armnn::ParseException);
131 }
132
BOOST_FIXTURE_TEST_CASE(GetOutputTensorIdsNullModel,GetInputOutputTensorIdsFixture)133 BOOST_FIXTURE_TEST_CASE(GetOutputTensorIdsNullModel, GetInputOutputTensorIdsFixture)
134 {
135 BOOST_CHECK_THROW(TfLiteParser::GetOutputTensorIds(nullptr, 0, 0), armnn::ParseException);
136 }
137
BOOST_FIXTURE_TEST_CASE(GetInputTensorIdsInvalidSubgraph,GetInputOutputTensorIdsFixture)138 BOOST_FIXTURE_TEST_CASE(GetInputTensorIdsInvalidSubgraph, GetInputOutputTensorIdsFixture)
139 {
140 TfLiteParser::ModelPtr model = TfLiteParser::LoadModelFromBinary(m_GraphBinary.data(), m_GraphBinary.size());
141 BOOST_CHECK_THROW(TfLiteParser::GetInputTensorIds(model, 1, 0), armnn::ParseException);
142 }
143
BOOST_FIXTURE_TEST_CASE(GetOutputTensorIdsInvalidSubgraph,GetInputOutputTensorIdsFixture)144 BOOST_FIXTURE_TEST_CASE(GetOutputTensorIdsInvalidSubgraph, GetInputOutputTensorIdsFixture)
145 {
146 TfLiteParser::ModelPtr model = TfLiteParser::LoadModelFromBinary(m_GraphBinary.data(), m_GraphBinary.size());
147 BOOST_CHECK_THROW(TfLiteParser::GetOutputTensorIds(model, 1, 0), armnn::ParseException);
148 }
149
BOOST_FIXTURE_TEST_CASE(GetInputTensorIdsInvalidOperator,GetInputOutputTensorIdsFixture)150 BOOST_FIXTURE_TEST_CASE(GetInputTensorIdsInvalidOperator, GetInputOutputTensorIdsFixture)
151 {
152 TfLiteParser::ModelPtr model = TfLiteParser::LoadModelFromBinary(m_GraphBinary.data(), m_GraphBinary.size());
153 BOOST_CHECK_THROW(TfLiteParser::GetInputTensorIds(model, 0, 1), armnn::ParseException);
154 }
155
BOOST_FIXTURE_TEST_CASE(GetOutputTensorIdsInvalidOperator,GetInputOutputTensorIdsFixture)156 BOOST_FIXTURE_TEST_CASE(GetOutputTensorIdsInvalidOperator, GetInputOutputTensorIdsFixture)
157 {
158 TfLiteParser::ModelPtr model = TfLiteParser::LoadModelFromBinary(m_GraphBinary.data(), m_GraphBinary.size());
159 BOOST_CHECK_THROW(TfLiteParser::GetOutputTensorIds(model, 0, 1), armnn::ParseException);
160 }
161
162 BOOST_AUTO_TEST_SUITE_END()
163