ArmNN
 21.05
GetBuffer.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include <boost/test/unit_test.hpp>
8 #include "../TfLiteParser.hpp"
9 #include <sstream>
10 
12 
13 BOOST_AUTO_TEST_SUITE(TensorflowLiteParser)
14 
15 struct 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  )";
89  }
90 
91  void CheckBufferContents(const TfLiteParserImpl::ModelPtr& model,
92  std::vector<int32_t> bufferValues, size_t bufferIndex)
93  {
94  for(long unsigned int i=0; i<bufferValues.size(); i++)
95  {
96  BOOST_CHECK_EQUAL(TfLiteParserImpl::GetBuffer(model, bufferIndex)->data[i], bufferValues[i]);
97  }
98  }
99 };
100 
101 BOOST_FIXTURE_TEST_CASE(GetBufferCheckContents, GetBufferFixture)
102 {
103  //Check contents of buffer are correct
104  TfLiteParserImpl::ModelPtr model = TfLiteParserImpl::LoadModelFromBinary(m_GraphBinary.data(),
105  m_GraphBinary.size());
106  std::vector<int32_t> bufferValues = {2,1,0,6,2,1,4,1,2};
107  CheckBufferContents(model, bufferValues, 2);
108 }
109 
110 BOOST_FIXTURE_TEST_CASE(GetBufferCheckEmpty, GetBufferFixture)
111 {
112  //Check if test fixture buffers are empty or not
113  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());
119 }
120 
121 BOOST_FIXTURE_TEST_CASE(GetBufferCheckParseException, GetBufferFixture)
122 {
123  //Check if armnn::ParseException thrown when invalid buffer index used
124  TfLiteParserImpl::ModelPtr model = TfLiteParserImpl::LoadModelFromBinary(m_GraphBinary.data(),
125  m_GraphBinary.size());
126  BOOST_CHECK_THROW(TfLiteParserImpl::GetBuffer(model, 4), armnn::Exception);
127 }
128 
BOOST_AUTO_TEST_SUITE(TensorflowLiteParser)
BOOST_FIXTURE_TEST_CASE(GetBufferCheckContents, GetBufferFixture)
Definition: GetBuffer.cpp:101
std::unique_ptr< onnx::ModelProto > ModelPtr
Definition: OnnxParser.hpp:23
BOOST_AUTO_TEST_SUITE_END()
Base class for all ArmNN exceptions so that users can filter to just those.
Definition: Exceptions.hpp:46