telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4 | // |
| 5 | #include <boost/test/unit_test.hpp> |
| 6 | #include "ParserFlatbuffersFixture.hpp" |
| 7 | #include "../TfLiteParser.hpp" |
| 8 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 9 | using armnnTfLiteParser::TfLiteParserImpl; |
| 10 | using ModelPtr = TfLiteParserImpl::ModelPtr; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 11 | |
| 12 | BOOST_AUTO_TEST_SUITE(TensorflowLiteParser) |
| 13 | |
| 14 | struct 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 | |
| 82 | struct GetEmptyTensorIdsFixture : GetTensorIdsFixture |
| 83 | { |
| 84 | GetEmptyTensorIdsFixture() : GetTensorIdsFixture("[ ]", "[ ]") {} |
| 85 | }; |
| 86 | |
| 87 | struct GetInputOutputTensorIdsFixture : GetTensorIdsFixture |
| 88 | { |
| 89 | GetInputOutputTensorIdsFixture() : GetTensorIdsFixture("[ 0, 1, 2 ]", "[ 3 ]") {} |
| 90 | }; |
| 91 | |
| 92 | BOOST_FIXTURE_TEST_CASE(GetEmptyInputTensorIds, GetEmptyTensorIdsFixture) |
| 93 | { |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 94 | TfLiteParserImpl::ModelPtr model = TfLiteParserImpl::LoadModelFromBinary(m_GraphBinary.data(), |
| 95 | m_GraphBinary.size()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 96 | std::vector<int32_t> expectedIds = { }; |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 97 | std::vector<int32_t> inputTensorIds = TfLiteParserImpl::GetInputTensorIds(model, 0, 0); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 98 | BOOST_CHECK_EQUAL_COLLECTIONS(expectedIds.begin(), expectedIds.end(), |
| 99 | inputTensorIds.begin(), inputTensorIds.end()); |
| 100 | } |
| 101 | |
| 102 | BOOST_FIXTURE_TEST_CASE(GetEmptyOutputTensorIds, GetEmptyTensorIdsFixture) |
| 103 | { |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 104 | TfLiteParserImpl::ModelPtr model = TfLiteParserImpl::LoadModelFromBinary(m_GraphBinary.data(), |
| 105 | m_GraphBinary.size()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 106 | std::vector<int32_t> expectedIds = { }; |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 107 | std::vector<int32_t> outputTensorIds = TfLiteParserImpl::GetOutputTensorIds(model, 0, 0); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 108 | BOOST_CHECK_EQUAL_COLLECTIONS(expectedIds.begin(), expectedIds.end(), |
| 109 | outputTensorIds.begin(), outputTensorIds.end()); |
| 110 | } |
| 111 | |
| 112 | BOOST_FIXTURE_TEST_CASE(GetInputTensorIds, GetInputOutputTensorIdsFixture) |
| 113 | { |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 114 | TfLiteParserImpl::ModelPtr model = TfLiteParserImpl::LoadModelFromBinary(m_GraphBinary.data(), |
| 115 | m_GraphBinary.size()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 116 | std::vector<int32_t> expectedInputIds = { 0, 1, 2 }; |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 117 | std::vector<int32_t> inputTensorIds = TfLiteParserImpl::GetInputTensorIds(model, 0, 0); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 118 | BOOST_CHECK_EQUAL_COLLECTIONS(expectedInputIds.begin(), expectedInputIds.end(), |
| 119 | inputTensorIds.begin(), inputTensorIds.end()); |
| 120 | } |
| 121 | |
| 122 | BOOST_FIXTURE_TEST_CASE(GetOutputTensorIds, GetInputOutputTensorIdsFixture) |
| 123 | { |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 124 | TfLiteParserImpl::ModelPtr model = TfLiteParserImpl::LoadModelFromBinary(m_GraphBinary.data(), |
| 125 | m_GraphBinary.size()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 126 | std::vector<int32_t> expectedOutputIds = { 3 }; |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 127 | std::vector<int32_t> outputTensorIds = TfLiteParserImpl::GetOutputTensorIds(model, 0, 0); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 128 | BOOST_CHECK_EQUAL_COLLECTIONS(expectedOutputIds.begin(), expectedOutputIds.end(), |
| 129 | outputTensorIds.begin(), outputTensorIds.end()); |
| 130 | } |
| 131 | |
| 132 | BOOST_FIXTURE_TEST_CASE(GetInputTensorIdsNullModel, GetInputOutputTensorIdsFixture) |
| 133 | { |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 134 | BOOST_CHECK_THROW(TfLiteParserImpl::GetInputTensorIds(nullptr, 0, 0), armnn::ParseException); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | BOOST_FIXTURE_TEST_CASE(GetOutputTensorIdsNullModel, GetInputOutputTensorIdsFixture) |
| 138 | { |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 139 | BOOST_CHECK_THROW(TfLiteParserImpl::GetOutputTensorIds(nullptr, 0, 0), armnn::ParseException); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 140 | } |
| 141 | |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 142 | BOOST_FIXTURE_TEST_CASE(GetInputTensorIdsInvalidSubgraph, GetInputOutputTensorIdsFixture) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 143 | { |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 144 | TfLiteParserImpl::ModelPtr model = TfLiteParserImpl::LoadModelFromBinary(m_GraphBinary.data(), |
| 145 | m_GraphBinary.size()); |
| 146 | BOOST_CHECK_THROW(TfLiteParserImpl::GetInputTensorIds(model, 1, 0), armnn::ParseException); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 147 | } |
| 148 | |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 149 | BOOST_FIXTURE_TEST_CASE(GetOutputTensorIdsInvalidSubgraph, GetInputOutputTensorIdsFixture) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 150 | { |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 151 | TfLiteParserImpl::ModelPtr model = TfLiteParserImpl::LoadModelFromBinary(m_GraphBinary.data(), |
| 152 | m_GraphBinary.size()); |
| 153 | BOOST_CHECK_THROW(TfLiteParserImpl::GetOutputTensorIds(model, 1, 0), armnn::ParseException); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | BOOST_FIXTURE_TEST_CASE(GetInputTensorIdsInvalidOperator, GetInputOutputTensorIdsFixture) |
| 157 | { |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 158 | TfLiteParserImpl::ModelPtr model = TfLiteParserImpl::LoadModelFromBinary(m_GraphBinary.data(), |
| 159 | m_GraphBinary.size()); |
| 160 | BOOST_CHECK_THROW(TfLiteParserImpl::GetInputTensorIds(model, 0, 1), armnn::ParseException); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | BOOST_FIXTURE_TEST_CASE(GetOutputTensorIdsInvalidOperator, GetInputOutputTensorIdsFixture) |
| 164 | { |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 165 | TfLiteParserImpl::ModelPtr model = TfLiteParserImpl::LoadModelFromBinary(m_GraphBinary.data(), |
| 166 | m_GraphBinary.size()); |
| 167 | BOOST_CHECK_THROW(TfLiteParserImpl::GetOutputTensorIds(model, 0, 1), armnn::ParseException); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | BOOST_AUTO_TEST_SUITE_END() |