blob: 1ae4eb51c40433c6ebbcf2338c955601bcf2413d [file] [log] [blame]
telsoa01c577f2c2018-08-31 09:22:23 +01001//
Finn Williamsb49ed182021-06-29 15:50:08 +01002// Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa01c577f2c2018-08-31 09:22:23 +01004//
Sadik Armagan1625efc2021-06-10 18:24:34 +01005
telsoa01c577f2c2018-08-31 09:22:23 +01006#include "ParserFlatbuffersFixture.hpp"
telsoa01c577f2c2018-08-31 09:22:23 +01007
Kevin May7d96b162021-02-03 17:38:41 +00008using armnnTfLiteParser::TfLiteParserImpl;
9using ModelPtr = TfLiteParserImpl::ModelPtr;
telsoa01c577f2c2018-08-31 09:22:23 +010010
Sadik Armagan1625efc2021-06-10 18:24:34 +010011TEST_SUITE("TensorflowLiteParser_GetInputsOutputs")
12{
telsoa01c577f2c2018-08-31 09:22:23 +010013struct GetInputsOutputsMainFixture : public ParserFlatbuffersFixture
14{
15 explicit GetInputsOutputsMainFixture(const std::string& inputs, const std::string& outputs)
16 {
17 m_JsonString = R"(
18 {
19 "version": 3,
20 "operator_codes": [ { "builtin_code": "AVERAGE_POOL_2D" }, { "builtin_code": "CONV_2D" } ],
21 "subgraphs": [
22 {
23 "tensors": [
24 {
25 "shape": [ 1, 1, 1, 1 ] ,
26 "type": "UINT8",
27 "buffer": 0,
28 "name": "OutputTensor",
29 "quantization": {
30 "min": [ 0.0 ],
31 "max": [ 255.0 ],
32 "scale": [ 1.0 ],
33 "zero_point": [ 0 ]
34 }
35 },
36 {
37 "shape": [ 1, 2, 2, 1 ] ,
38 "type": "UINT8",
39 "buffer": 1,
40 "name": "InputTensor",
41 "quantization": {
42 "min": [ -1.2 ],
43 "max": [ 25.5 ],
44 "scale": [ 0.25 ],
45 "zero_point": [ 10 ]
46 }
47 }
48 ],
49 "inputs": [ 1 ],
50 "outputs": [ 0 ],
51 "operators": [ {
52 "opcode_index": 0,
53 "inputs": )"
54 + inputs
55 + R"(,
56 "outputs": )"
57 + outputs
58 + R"(,
59 "builtin_options_type": "Pool2DOptions",
60 "builtin_options":
61 {
62 "padding": "VALID",
63 "stride_w": 2,
64 "stride_h": 2,
65 "filter_width": 2,
66 "filter_height": 2,
67 "fused_activation_function": "NONE"
68 },
69 "custom_options_format": "FLEXBUFFERS"
70 } ]
71 },
72 {
73 "tensors": [
74 {
75 "shape": [ 1, 3, 3, 1 ],
76 "type": "UINT8",
77 "buffer": 0,
78 "name": "ConvInputTensor",
79 "quantization": {
80 "scale": [ 1.0 ],
81 "zero_point": [ 0 ],
82 }
83 },
84 {
85 "shape": [ 1, 1, 1, 1 ],
86 "type": "UINT8",
87 "buffer": 1,
88 "name": "ConvOutputTensor",
89 "quantization": {
90 "min": [ 0.0 ],
91 "max": [ 511.0 ],
92 "scale": [ 2.0 ],
93 "zero_point": [ 0 ],
94 }
95 },
96 {
97 "shape": [ 1, 3, 3, 1 ],
98 "type": "UINT8",
99 "buffer": 2,
100 "name": "filterTensor",
101 "quantization": {
102 "min": [ 0.0 ],
103 "max": [ 255.0 ],
104 "scale": [ 1.0 ],
105 "zero_point": [ 0 ],
106 }
107 }
108 ],
109 "inputs": [ 0 ],
110 "outputs": [ 1 ],
111 "operators": [
112 {
113 "opcode_index": 0,
114 "inputs": [ 0, 2 ],
115 "outputs": [ 1 ],
116 "builtin_options_type": "Conv2DOptions",
117 "builtin_options": {
118 "padding": "VALID",
119 "stride_w": 1,
120 "stride_h": 1,
121 "fused_activation_function": "NONE"
122 },
123 "custom_options_format": "FLEXBUFFERS"
124 }
125 ],
126 }
127 ],
128 "description": "Test Subgraph Inputs Outputs",
129 "buffers" : [
130 { },
131 { },
132 { "data": [ 2,1,0, 6,2,1, 4,1,2 ], },
133 { },
134 ]
135 })";
136
137 ReadStringToBinary();
138 }
139
140};
141
142struct GetEmptyInputsOutputsFixture : GetInputsOutputsMainFixture
143{
144 GetEmptyInputsOutputsFixture() : GetInputsOutputsMainFixture("[ ]", "[ ]") {}
145};
146
147struct GetInputsOutputsFixture : GetInputsOutputsMainFixture
148{
149 GetInputsOutputsFixture() : GetInputsOutputsMainFixture("[ 1 ]", "[ 0 ]") {}
150};
151
Sadik Armagan1625efc2021-06-10 18:24:34 +0100152TEST_CASE_FIXTURE(GetEmptyInputsOutputsFixture, "GetEmptyInputs")
telsoa01c577f2c2018-08-31 09:22:23 +0100153{
Kevin May7d96b162021-02-03 17:38:41 +0000154 TfLiteParserImpl::ModelPtr model = TfLiteParserImpl::LoadModelFromBinary(m_GraphBinary.data(),
155 m_GraphBinary.size());
156 TfLiteParserImpl::TensorRawPtrVector tensors = TfLiteParserImpl::GetInputs(model, 0, 0);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100157 CHECK_EQ(0, tensors.size());
telsoa01c577f2c2018-08-31 09:22:23 +0100158}
159
Sadik Armagan1625efc2021-06-10 18:24:34 +0100160TEST_CASE_FIXTURE(GetEmptyInputsOutputsFixture, "GetEmptyOutputs")
telsoa01c577f2c2018-08-31 09:22:23 +0100161{
Kevin May7d96b162021-02-03 17:38:41 +0000162 TfLiteParserImpl::ModelPtr model = TfLiteParserImpl::LoadModelFromBinary(m_GraphBinary.data(),
163 m_GraphBinary.size());
164 TfLiteParserImpl::TensorRawPtrVector tensors = TfLiteParserImpl::GetOutputs(model, 0, 0);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100165 CHECK_EQ(0, tensors.size());
telsoa01c577f2c2018-08-31 09:22:23 +0100166}
167
Sadik Armagan1625efc2021-06-10 18:24:34 +0100168TEST_CASE_FIXTURE(GetInputsOutputsFixture, "GetInputs")
telsoa01c577f2c2018-08-31 09:22:23 +0100169{
Kevin May7d96b162021-02-03 17:38:41 +0000170 TfLiteParserImpl::ModelPtr model = TfLiteParserImpl::LoadModelFromBinary(m_GraphBinary.data(),
171 m_GraphBinary.size());
172 TfLiteParserImpl::TensorRawPtrVector tensors = TfLiteParserImpl::GetInputs(model, 0, 0);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100173 CHECK_EQ(1, tensors.size());
telsoa01c577f2c2018-08-31 09:22:23 +0100174 CheckTensors(tensors[0], 4, { 1, 2, 2, 1 }, tflite::TensorType::TensorType_UINT8, 1,
175 "InputTensor", { -1.2f }, { 25.5f }, { 0.25f }, { 10 });
176}
177
Sadik Armagan1625efc2021-06-10 18:24:34 +0100178TEST_CASE_FIXTURE(GetInputsOutputsFixture, "GetOutputs")
telsoa01c577f2c2018-08-31 09:22:23 +0100179{
Kevin May7d96b162021-02-03 17:38:41 +0000180 TfLiteParserImpl::ModelPtr model = TfLiteParserImpl::LoadModelFromBinary(m_GraphBinary.data(),
181 m_GraphBinary.size());
182 TfLiteParserImpl::TensorRawPtrVector tensors = TfLiteParserImpl::GetOutputs(model, 0, 0);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100183 CHECK_EQ(1, tensors.size());
telsoa01c577f2c2018-08-31 09:22:23 +0100184 CheckTensors(tensors[0], 4, { 1, 1, 1, 1 }, tflite::TensorType::TensorType_UINT8, 0,
185 "OutputTensor", { 0.0f }, { 255.0f }, { 1.0f }, { 0 });
186}
187
Sadik Armagan1625efc2021-06-10 18:24:34 +0100188TEST_CASE_FIXTURE(GetInputsOutputsFixture, "GetInputsMultipleInputs")
telsoa01c577f2c2018-08-31 09:22:23 +0100189{
Kevin May7d96b162021-02-03 17:38:41 +0000190 TfLiteParserImpl::ModelPtr model = TfLiteParserImpl::LoadModelFromBinary(m_GraphBinary.data(),
191 m_GraphBinary.size());
192 TfLiteParserImpl::TensorRawPtrVector tensors = TfLiteParserImpl::GetInputs(model, 1, 0);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100193 CHECK_EQ(2, tensors.size());
telsoa01c577f2c2018-08-31 09:22:23 +0100194 CheckTensors(tensors[0], 4, { 1, 3, 3, 1 }, tflite::TensorType::TensorType_UINT8, 0,
195 "ConvInputTensor", { }, { }, { 1.0f }, { 0 });
196 CheckTensors(tensors[1], 4, { 1, 3, 3, 1 }, tflite::TensorType::TensorType_UINT8, 2,
197 "filterTensor", { 0.0f }, { 255.0f }, { 1.0f }, { 0 });
198}
199
Sadik Armagan1625efc2021-06-10 18:24:34 +0100200TEST_CASE_FIXTURE(GetInputsOutputsFixture, "GetOutputs2")
telsoa01c577f2c2018-08-31 09:22:23 +0100201{
Kevin May7d96b162021-02-03 17:38:41 +0000202 TfLiteParserImpl::ModelPtr model = TfLiteParserImpl::LoadModelFromBinary(m_GraphBinary.data(),
203 m_GraphBinary.size());
204 TfLiteParserImpl::TensorRawPtrVector tensors = TfLiteParserImpl::GetOutputs(model, 1, 0);
Sadik Armagan1625efc2021-06-10 18:24:34 +0100205 CHECK_EQ(1, tensors.size());
telsoa01c577f2c2018-08-31 09:22:23 +0100206 CheckTensors(tensors[0], 4, { 1, 1, 1, 1 }, tflite::TensorType::TensorType_UINT8, 1,
207 "ConvOutputTensor", { 0.0f }, { 511.0f }, { 2.0f }, { 0 });
208}
209
Sadik Armagan1625efc2021-06-10 18:24:34 +0100210TEST_CASE("GetInputsNullModel")
telsoa01c577f2c2018-08-31 09:22:23 +0100211{
Sadik Armagan1625efc2021-06-10 18:24:34 +0100212 CHECK_THROWS_AS(TfLiteParserImpl::GetInputs(nullptr, 0, 0), armnn::ParseException);
telsoa01c577f2c2018-08-31 09:22:23 +0100213}
214
Sadik Armagan1625efc2021-06-10 18:24:34 +0100215TEST_CASE("GetOutputsNullModel")
telsoa01c577f2c2018-08-31 09:22:23 +0100216{
Sadik Armagan1625efc2021-06-10 18:24:34 +0100217 CHECK_THROWS_AS(TfLiteParserImpl::GetOutputs(nullptr, 0, 0), armnn::ParseException);
telsoa01c577f2c2018-08-31 09:22:23 +0100218}
219
Sadik Armagan1625efc2021-06-10 18:24:34 +0100220TEST_CASE_FIXTURE(GetInputsOutputsFixture, "GetInputsInvalidSubgraph")
telsoa01c577f2c2018-08-31 09:22:23 +0100221{
Kevin May7d96b162021-02-03 17:38:41 +0000222 TfLiteParserImpl::ModelPtr model = TfLiteParserImpl::LoadModelFromBinary(m_GraphBinary.data(),
223 m_GraphBinary.size());
Sadik Armagan1625efc2021-06-10 18:24:34 +0100224 CHECK_THROWS_AS(TfLiteParserImpl::GetInputs(model, 2, 0), armnn::ParseException);
telsoa01c577f2c2018-08-31 09:22:23 +0100225}
226
Sadik Armagan1625efc2021-06-10 18:24:34 +0100227TEST_CASE_FIXTURE(GetInputsOutputsFixture, "GetOutputsInvalidSubgraph")
telsoa01c577f2c2018-08-31 09:22:23 +0100228{
Kevin May7d96b162021-02-03 17:38:41 +0000229 TfLiteParserImpl::ModelPtr model = TfLiteParserImpl::LoadModelFromBinary(m_GraphBinary.data(),
230 m_GraphBinary.size());
Sadik Armagan1625efc2021-06-10 18:24:34 +0100231 CHECK_THROWS_AS(TfLiteParserImpl::GetOutputs(model, 2, 0), armnn::ParseException);
telsoa01c577f2c2018-08-31 09:22:23 +0100232}
233
Sadik Armagan1625efc2021-06-10 18:24:34 +0100234TEST_CASE_FIXTURE(GetInputsOutputsFixture, "GetInputsInvalidOperator")
telsoa01c577f2c2018-08-31 09:22:23 +0100235{
Kevin May7d96b162021-02-03 17:38:41 +0000236 TfLiteParserImpl::ModelPtr model = TfLiteParserImpl::LoadModelFromBinary(m_GraphBinary.data(),
237 m_GraphBinary.size());
Sadik Armagan1625efc2021-06-10 18:24:34 +0100238 CHECK_THROWS_AS(TfLiteParserImpl::GetInputs(model, 0, 1), armnn::ParseException);
telsoa01c577f2c2018-08-31 09:22:23 +0100239}
240
Sadik Armagan1625efc2021-06-10 18:24:34 +0100241TEST_CASE_FIXTURE(GetInputsOutputsFixture, "GetOutputsInvalidOperator")
telsoa01c577f2c2018-08-31 09:22:23 +0100242{
Kevin May7d96b162021-02-03 17:38:41 +0000243 TfLiteParserImpl::ModelPtr model = TfLiteParserImpl::LoadModelFromBinary(m_GraphBinary.data(),
244 m_GraphBinary.size());
Sadik Armagan1625efc2021-06-10 18:24:34 +0100245 CHECK_THROWS_AS(TfLiteParserImpl::GetOutputs(model, 0, 1), armnn::ParseException);
telsoa01c577f2c2018-08-31 09:22:23 +0100246}
247
Sadik Armagan1625efc2021-06-10 18:24:34 +0100248}