blob: 6b82bb1f976e534b31a42a5ecb66312a8066dc85 [file] [log] [blame]
telsoa01c577f2c2018-08-31 09:22:23 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa01c577f2c2018-08-31 09:22:23 +01004//
5#include <boost/test/unit_test.hpp>
6#include "ParserFlatbuffersFixture.hpp"
7#include "../TfLiteParser.hpp"
8
9using armnnTfLiteParser::TfLiteParser;
10using ModelPtr = TfLiteParser::ModelPtr;
11
12BOOST_AUTO_TEST_SUITE(TensorflowLiteParser)
13
14struct GetTensorIdsFixture : public ParserFlatbuffersFixture
15{
16 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
82struct GetEmptyTensorIdsFixture : GetTensorIdsFixture
83{
84 GetEmptyTensorIdsFixture() : GetTensorIdsFixture("[ ]", "[ ]") {}
85};
86
87struct GetInputOutputTensorIdsFixture : GetTensorIdsFixture
88{
89 GetInputOutputTensorIdsFixture() : GetTensorIdsFixture("[ 0, 1, 2 ]", "[ 3 ]") {}
90};
91
92BOOST_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
101BOOST_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
110BOOST_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
119BOOST_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
128BOOST_FIXTURE_TEST_CASE(GetInputTensorIdsNullModel, GetInputOutputTensorIdsFixture)
129{
130 BOOST_CHECK_THROW(TfLiteParser::GetInputTensorIds(nullptr, 0, 0), armnn::ParseException);
131}
132
133BOOST_FIXTURE_TEST_CASE(GetOutputTensorIdsNullModel, GetInputOutputTensorIdsFixture)
134{
135 BOOST_CHECK_THROW(TfLiteParser::GetOutputTensorIds(nullptr, 0, 0), armnn::ParseException);
136}
137
Derek Lambertiff05cc52019-04-26 13:05:17 +0100138BOOST_FIXTURE_TEST_CASE(GetInputTensorIdsInvalidSubgraph, GetInputOutputTensorIdsFixture)
telsoa01c577f2c2018-08-31 09:22:23 +0100139{
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
Derek Lambertiff05cc52019-04-26 13:05:17 +0100144BOOST_FIXTURE_TEST_CASE(GetOutputTensorIdsInvalidSubgraph, GetInputOutputTensorIdsFixture)
telsoa01c577f2c2018-08-31 09:22:23 +0100145{
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
150BOOST_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
156BOOST_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
162BOOST_AUTO_TEST_SUITE_END()