blob: 0e72522c794f211b686421414b9bbb616302307d [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
6#include <boost/test/unit_test.hpp>
7#include "ParserFlatbuffersFixture.hpp"
8#include "../TfLiteParser.hpp"
9#include <sstream>
10
Kevin May7d96b162021-02-03 17:38:41 +000011using armnnTfLiteParser::TfLiteParserImpl;
telsoa01c577f2c2018-08-31 09:22:23 +010012
13BOOST_AUTO_TEST_SUITE(TensorflowLiteParser)
14
15struct GetBufferFixture : public ParserFlatbuffersFixture
16{
17 explicit GetBufferFixture()
18 {
19 m_JsonString = R"(
20 {
21 "version": 3,
22 "operator_codes": [ { "builtin_code": "CONV_2D" } ],
23 "subgraphs": [ {
24 "tensors": [
25 {
26 "shape": [ 1, 3, 3, 1 ],
27 "type": "UINT8",
28 "buffer": 0,
29 "name": "inputTensor",
30 "quantization": {
31 "min": [ 0.0 ],
32 "max": [ 255.0 ],
33 "scale": [ 1.0 ],
34 "zero_point": [ 0 ],
35 }
36 },
37 {
38 "shape": [ 1, 1, 1, 1 ],
39 "type": "UINT8",
40 "buffer": 1,
41 "name": "outputTensor",
42 "quantization": {
43 "min": [ 0.0 ],
44 "max": [ 511.0 ],
45 "scale": [ 2.0 ],
46 "zero_point": [ 0 ],
47 }
48 },
49 {
50 "shape": [ 1, 3, 3, 1 ],
51 "type": "UINT8",
52 "buffer": 2,
53 "name": "filterTensor",
54 "quantization": {
55 "min": [ 0.0 ],
56 "max": [ 255.0 ],
57 "scale": [ 1.0 ],
58 "zero_point": [ 0 ],
59 }
60 }
61 ],
62 "inputs": [ 0 ],
63 "outputs": [ 1 ],
64 "operators": [
65 {
66 "opcode_index": 0,
67 "inputs": [ 0, 2 ],
68 "outputs": [ 1 ],
69 "builtin_options_type": "Conv2DOptions",
70 "builtin_options": {
71 "padding": "VALID",
72 "stride_w": 1,
73 "stride_h": 1,
74 "fused_activation_function": "NONE"
75 },
76 "custom_options_format": "FLEXBUFFERS"
77 }
78 ],
79 } ],
80 "buffers" : [
81 { },
82 { },
83 { "data": [ 2,1,0, 6,2,1, 4,1,2 ], },
84 { },
85 ]
86 }
87 )";
88 ReadStringToBinary();
89 }
90
Kevin May7d96b162021-02-03 17:38:41 +000091 void CheckBufferContents(const TfLiteParserImpl::ModelPtr& model,
telsoa01c577f2c2018-08-31 09:22:23 +010092 std::vector<int32_t> bufferValues, size_t bufferIndex)
93 {
94 for(long unsigned int i=0; i<bufferValues.size(); i++)
95 {
Kevin May7d96b162021-02-03 17:38:41 +000096 BOOST_CHECK_EQUAL(TfLiteParserImpl::GetBuffer(model, bufferIndex)->data[i], bufferValues[i]);
telsoa01c577f2c2018-08-31 09:22:23 +010097 }
98 }
99};
100
101BOOST_FIXTURE_TEST_CASE(GetBufferCheckContents, GetBufferFixture)
102{
103 //Check contents of buffer are correct
Kevin May7d96b162021-02-03 17:38:41 +0000104 TfLiteParserImpl::ModelPtr model = TfLiteParserImpl::LoadModelFromBinary(m_GraphBinary.data(),
105 m_GraphBinary.size());
telsoa01c577f2c2018-08-31 09:22:23 +0100106 std::vector<int32_t> bufferValues = {2,1,0,6,2,1,4,1,2};
107 CheckBufferContents(model, bufferValues, 2);
108}
109
110BOOST_FIXTURE_TEST_CASE(GetBufferCheckEmpty, GetBufferFixture)
111{
112 //Check if test fixture buffers are empty or not
Kevin May7d96b162021-02-03 17:38:41 +0000113 TfLiteParserImpl::ModelPtr model = TfLiteParserImpl::LoadModelFromBinary(m_GraphBinary.data(),
114 m_GraphBinary.size());
115 BOOST_CHECK(TfLiteParserImpl::GetBuffer(model, 0)->data.empty());
116 BOOST_CHECK(TfLiteParserImpl::GetBuffer(model, 1)->data.empty());
117 BOOST_CHECK(!TfLiteParserImpl::GetBuffer(model, 2)->data.empty());
118 BOOST_CHECK(TfLiteParserImpl::GetBuffer(model, 3)->data.empty());
telsoa01c577f2c2018-08-31 09:22:23 +0100119}
120
121BOOST_FIXTURE_TEST_CASE(GetBufferCheckParseException, GetBufferFixture)
122{
123 //Check if armnn::ParseException thrown when invalid buffer index used
Kevin May7d96b162021-02-03 17:38:41 +0000124 TfLiteParserImpl::ModelPtr model = TfLiteParserImpl::LoadModelFromBinary(m_GraphBinary.data(),
125 m_GraphBinary.size());
126 BOOST_CHECK_THROW(TfLiteParserImpl::GetBuffer(model, 4), armnn::Exception);
telsoa01c577f2c2018-08-31 09:22:23 +0100127}
128
129BOOST_AUTO_TEST_SUITE_END()