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 | |
| 6 | #pragma once |
| 7 | |
Matthew Bentham | 6c8e8e7 | 2019-01-15 17:57:00 +0000 | [diff] [blame] | 8 | #include "Schema.hpp" |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 9 | #include <boost/filesystem.hpp> |
| 10 | #include <boost/assert.hpp> |
| 11 | #include <boost/format.hpp> |
| 12 | #include <experimental/filesystem> |
| 13 | #include <armnn/IRuntime.hpp> |
| 14 | #include <armnn/TypesUtils.hpp> |
| 15 | #include "test/TensorHelpers.hpp" |
| 16 | |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 17 | #include "TypeUtils.hpp" |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 18 | #include "armnnTfLiteParser/ITfLiteParser.hpp" |
| 19 | |
Aron Virginas-Tar | c9cc804 | 2018-11-01 16:15:57 +0000 | [diff] [blame] | 20 | #include <backendsCommon/BackendRegistry.hpp> |
Aron Virginas-Tar | 54e9572 | 2018-10-25 11:47:31 +0100 | [diff] [blame] | 21 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 22 | #include "flatbuffers/idl.h" |
| 23 | #include "flatbuffers/util.h" |
| 24 | |
| 25 | #include <schema_generated.h> |
| 26 | #include <iostream> |
| 27 | |
| 28 | using armnnTfLiteParser::ITfLiteParser; |
| 29 | using TensorRawPtr = const tflite::TensorT *; |
| 30 | |
| 31 | struct ParserFlatbuffersFixture |
| 32 | { |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 33 | ParserFlatbuffersFixture() : |
| 34 | m_Parser(ITfLiteParser::Create()), |
| 35 | m_Runtime(armnn::IRuntime::Create(armnn::IRuntime::CreationOptions())), |
| 36 | m_NetworkIdentifier(-1) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 37 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | std::vector<uint8_t> m_GraphBinary; |
| 41 | std::string m_JsonString; |
| 42 | std::unique_ptr<ITfLiteParser, void (*)(ITfLiteParser *parser)> m_Parser; |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 43 | armnn::IRuntimePtr m_Runtime; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 44 | armnn::NetworkId m_NetworkIdentifier; |
| 45 | |
| 46 | /// If the single-input-single-output overload of Setup() is called, these will store the input and output name |
| 47 | /// so they don't need to be passed to the single-input-single-output overload of RunTest(). |
| 48 | std::string m_SingleInputName; |
| 49 | std::string m_SingleOutputName; |
| 50 | |
| 51 | void Setup() |
| 52 | { |
| 53 | bool ok = ReadStringToBinary(); |
| 54 | if (!ok) { |
| 55 | throw armnn::Exception("LoadNetwork failed while reading binary input"); |
| 56 | } |
| 57 | |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 58 | armnn::INetworkPtr network = |
| 59 | m_Parser->CreateNetworkFromBinary(m_GraphBinary); |
| 60 | |
| 61 | if (!network) { |
| 62 | throw armnn::Exception("The parser failed to create an ArmNN network"); |
| 63 | } |
| 64 | |
| 65 | auto optimized = Optimize(*network, { armnn::Compute::CpuRef }, |
| 66 | m_Runtime->GetDeviceSpec()); |
| 67 | std::string errorMessage; |
| 68 | |
| 69 | armnn::Status ret = m_Runtime->LoadNetwork(m_NetworkIdentifier, move(optimized), errorMessage); |
| 70 | |
| 71 | if (ret != armnn::Status::Success) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 72 | { |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 73 | throw armnn::Exception( |
| 74 | boost::str( |
| 75 | boost::format("The runtime failed to load the network. " |
| 76 | "Error was: %1%. in %2% [%3%:%4%]") % |
| 77 | errorMessage % |
| 78 | __func__ % |
| 79 | __FILE__ % |
| 80 | __LINE__)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
| 84 | void SetupSingleInputSingleOutput(const std::string& inputName, const std::string& outputName) |
| 85 | { |
| 86 | // Store the input and output name so they don't need to be passed to the single-input-single-output RunTest(). |
| 87 | m_SingleInputName = inputName; |
| 88 | m_SingleOutputName = outputName; |
| 89 | Setup(); |
| 90 | } |
| 91 | |
| 92 | bool ReadStringToBinary() |
| 93 | { |
Matthew Bentham | 6c8e8e7 | 2019-01-15 17:57:00 +0000 | [diff] [blame] | 94 | std::string schemafile(&tflite_schema_start, &tflite_schema_end); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 95 | |
| 96 | // parse schema first, so we can use it to parse the data after |
| 97 | flatbuffers::Parser parser; |
| 98 | |
Matthew Bentham | 6c8e8e7 | 2019-01-15 17:57:00 +0000 | [diff] [blame] | 99 | bool ok = parser.Parse(schemafile.c_str()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 100 | BOOST_ASSERT_MSG(ok, "Failed to parse schema file"); |
| 101 | |
| 102 | ok &= parser.Parse(m_JsonString.c_str()); |
| 103 | BOOST_ASSERT_MSG(ok, "Failed to parse json input"); |
| 104 | |
| 105 | if (!ok) |
| 106 | { |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | { |
| 111 | const uint8_t * bufferPtr = parser.builder_.GetBufferPointer(); |
| 112 | size_t size = static_cast<size_t>(parser.builder_.GetSize()); |
| 113 | m_GraphBinary.assign(bufferPtr, bufferPtr+size); |
| 114 | } |
| 115 | return ok; |
| 116 | } |
| 117 | |
| 118 | /// Executes the network with the given input tensor and checks the result against the given output tensor. |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 119 | /// This assumes the network has a single input and a single output. |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 120 | template <std::size_t NumOutputDimensions, |
| 121 | armnn::DataType ArmnnType, |
| 122 | typename DataType = armnn::ResolveType<ArmnnType>> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 123 | void RunTest(size_t subgraphId, |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 124 | const std::vector<DataType>& inputData, |
| 125 | const std::vector<DataType>& expectedOutputData); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 126 | |
| 127 | /// Executes the network with the given input tensors and checks the results against the given output tensors. |
| 128 | /// This overload supports multiple inputs and multiple outputs, identified by name. |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 129 | template <std::size_t NumOutputDimensions, |
| 130 | armnn::DataType ArmnnType, |
| 131 | typename DataType = armnn::ResolveType<ArmnnType>> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 132 | void RunTest(size_t subgraphId, |
| 133 | const std::map<std::string, std::vector<DataType>>& inputData, |
| 134 | const std::map<std::string, std::vector<DataType>>& expectedOutputData); |
| 135 | |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 136 | /// Multiple Inputs, Multiple Outputs w/ Variable Datatypes and different dimension sizes. |
| 137 | /// Executes the network with the given input tensors and checks the results against the given output tensors. |
| 138 | /// This overload supports multiple inputs and multiple outputs, identified by name along with the allowance for |
| 139 | /// the input datatype to be different to the output |
| 140 | template <std::size_t NumOutputDimensions, |
| 141 | armnn::DataType ArmnnType1, |
| 142 | armnn::DataType ArmnnType2, |
| 143 | typename DataType1 = armnn::ResolveType<ArmnnType1>, |
| 144 | typename DataType2 = armnn::ResolveType<ArmnnType2>> |
| 145 | void RunTest(size_t subgraphId, |
| 146 | const std::map<std::string, std::vector<DataType1>>& inputData, |
| 147 | const std::map<std::string, std::vector<DataType2>>& expectedOutputData); |
| 148 | |
| 149 | |
| 150 | /// Multiple Inputs, Multiple Outputs w/ Variable Datatypes and different dimension sizes. |
| 151 | /// Executes the network with the given input tensors and checks the results against the given output tensors. |
| 152 | /// This overload supports multiple inputs and multiple outputs, identified by name along with the allowance for |
| 153 | /// the input datatype to be different to the output |
| 154 | template<armnn::DataType ArmnnType1, |
| 155 | armnn::DataType ArmnnType2, |
| 156 | typename DataType1 = armnn::ResolveType<ArmnnType1>, |
| 157 | typename DataType2 = armnn::ResolveType<ArmnnType2>> |
| 158 | void RunTest(std::size_t subgraphId, |
| 159 | const std::map<std::string, std::vector<DataType1>>& inputData, |
| 160 | const std::map<std::string, std::vector<DataType2>>& expectedOutputData); |
| 161 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 162 | void CheckTensors(const TensorRawPtr& tensors, size_t shapeSize, const std::vector<int32_t>& shape, |
| 163 | tflite::TensorType tensorType, uint32_t buffer, const std::string& name, |
| 164 | const std::vector<float>& min, const std::vector<float>& max, |
| 165 | const std::vector<float>& scale, const std::vector<int64_t>& zeroPoint) |
| 166 | { |
| 167 | BOOST_CHECK(tensors); |
| 168 | BOOST_CHECK_EQUAL(shapeSize, tensors->shape.size()); |
| 169 | BOOST_CHECK_EQUAL_COLLECTIONS(shape.begin(), shape.end(), tensors->shape.begin(), tensors->shape.end()); |
| 170 | BOOST_CHECK_EQUAL(tensorType, tensors->type); |
| 171 | BOOST_CHECK_EQUAL(buffer, tensors->buffer); |
| 172 | BOOST_CHECK_EQUAL(name, tensors->name); |
| 173 | BOOST_CHECK(tensors->quantization); |
| 174 | BOOST_CHECK_EQUAL_COLLECTIONS(min.begin(), min.end(), tensors->quantization.get()->min.begin(), |
| 175 | tensors->quantization.get()->min.end()); |
| 176 | BOOST_CHECK_EQUAL_COLLECTIONS(max.begin(), max.end(), tensors->quantization.get()->max.begin(), |
| 177 | tensors->quantization.get()->max.end()); |
| 178 | BOOST_CHECK_EQUAL_COLLECTIONS(scale.begin(), scale.end(), tensors->quantization.get()->scale.begin(), |
| 179 | tensors->quantization.get()->scale.end()); |
| 180 | BOOST_CHECK_EQUAL_COLLECTIONS(zeroPoint.begin(), zeroPoint.end(), |
| 181 | tensors->quantization.get()->zero_point.begin(), |
| 182 | tensors->quantization.get()->zero_point.end()); |
| 183 | } |
| 184 | }; |
| 185 | |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 186 | /// Single Input, Single Output |
| 187 | /// Executes the network with the given input tensor and checks the result against the given output tensor. |
| 188 | /// This overload assumes the network has a single input and a single output. |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 189 | template <std::size_t NumOutputDimensions, |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 190 | armnn::DataType armnnType, |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 191 | typename DataType> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 192 | void ParserFlatbuffersFixture::RunTest(size_t subgraphId, |
| 193 | const std::vector<DataType>& inputData, |
| 194 | const std::vector<DataType>& expectedOutputData) |
| 195 | { |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 196 | RunTest<NumOutputDimensions, armnnType>(subgraphId, |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 197 | { { m_SingleInputName, inputData } }, |
| 198 | { { m_SingleOutputName, expectedOutputData } }); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 199 | } |
| 200 | |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 201 | /// Multiple Inputs, Multiple Outputs |
| 202 | /// Executes the network with the given input tensors and checks the results against the given output tensors. |
| 203 | /// This overload supports multiple inputs and multiple outputs, identified by name. |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 204 | template <std::size_t NumOutputDimensions, |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 205 | armnn::DataType armnnType, |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 206 | typename DataType> |
| 207 | void ParserFlatbuffersFixture::RunTest(size_t subgraphId, |
| 208 | const std::map<std::string, std::vector<DataType>>& inputData, |
| 209 | const std::map<std::string, std::vector<DataType>>& expectedOutputData) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 210 | { |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 211 | RunTest<NumOutputDimensions, armnnType, armnnType>(subgraphId, inputData, expectedOutputData); |
| 212 | } |
| 213 | |
| 214 | /// Multiple Inputs, Multiple Outputs w/ Variable Datatypes |
| 215 | /// Executes the network with the given input tensors and checks the results against the given output tensors. |
| 216 | /// This overload supports multiple inputs and multiple outputs, identified by name along with the allowance for |
| 217 | /// the input datatype to be different to the output |
| 218 | template <std::size_t NumOutputDimensions, |
| 219 | armnn::DataType armnnType1, |
| 220 | armnn::DataType armnnType2, |
| 221 | typename DataType1, |
| 222 | typename DataType2> |
| 223 | void ParserFlatbuffersFixture::RunTest(size_t subgraphId, |
| 224 | const std::map<std::string, std::vector<DataType1>>& inputData, |
| 225 | const std::map<std::string, std::vector<DataType2>>& expectedOutputData) |
| 226 | { |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 227 | using BindingPointInfo = std::pair<armnn::LayerBindingId, armnn::TensorInfo>; |
| 228 | |
| 229 | // Setup the armnn input tensors from the given vectors. |
| 230 | armnn::InputTensors inputTensors; |
| 231 | for (auto&& it : inputData) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 232 | { |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 233 | BindingPointInfo bindingInfo = m_Parser->GetNetworkInputBindingInfo(subgraphId, it.first); |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 234 | armnn::VerifyTensorInfoDataType(bindingInfo.second, armnnType1); |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 235 | inputTensors.push_back({ bindingInfo.first, armnn::ConstTensor(bindingInfo.second, it.second.data()) }); |
| 236 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 237 | |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 238 | // Allocate storage for the output tensors to be written to and setup the armnn output tensors. |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 239 | std::map<std::string, boost::multi_array<DataType2, NumOutputDimensions>> outputStorage; |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 240 | armnn::OutputTensors outputTensors; |
| 241 | for (auto&& it : expectedOutputData) |
| 242 | { |
| 243 | BindingPointInfo bindingInfo = m_Parser->GetNetworkOutputBindingInfo(subgraphId, it.first); |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 244 | armnn::VerifyTensorInfoDataType(bindingInfo.second, armnnType2); |
| 245 | outputStorage.emplace(it.first, MakeTensor<DataType2, NumOutputDimensions>(bindingInfo.second)); |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 246 | outputTensors.push_back( |
| 247 | { bindingInfo.first, armnn::Tensor(bindingInfo.second, outputStorage.at(it.first).data()) }); |
| 248 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 249 | |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 250 | m_Runtime->EnqueueWorkload(m_NetworkIdentifier, inputTensors, outputTensors); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 251 | |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 252 | // Compare each output tensor to the expected values |
| 253 | for (auto&& it : expectedOutputData) |
| 254 | { |
| 255 | BindingPointInfo bindingInfo = m_Parser->GetNetworkOutputBindingInfo(subgraphId, it.first); |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 256 | auto outputExpected = MakeTensor<DataType2, NumOutputDimensions>(bindingInfo.second, it.second); |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 257 | BOOST_TEST(CompareTensors(outputExpected, outputStorage[it.first])); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 258 | } |
| 259 | } |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 260 | |
| 261 | /// Multiple Inputs, Multiple Outputs w/ Variable Datatypes and different dimension sizes. |
| 262 | /// Executes the network with the given input tensors and checks the results against the given output tensors. |
| 263 | /// This overload supports multiple inputs and multiple outputs, identified by name along with the allowance for |
| 264 | /// the input datatype to be different to the output. |
| 265 | template <armnn::DataType armnnType1, |
| 266 | armnn::DataType armnnType2, |
| 267 | typename DataType1, |
| 268 | typename DataType2> |
| 269 | void ParserFlatbuffersFixture::RunTest(std::size_t subgraphId, |
| 270 | const std::map<std::string, std::vector<DataType1>>& inputData, |
| 271 | const std::map<std::string, std::vector<DataType2>>& expectedOutputData) |
| 272 | { |
| 273 | using BindingPointInfo = std::pair<armnn::LayerBindingId, armnn::TensorInfo>; |
| 274 | |
| 275 | // Setup the armnn input tensors from the given vectors. |
| 276 | armnn::InputTensors inputTensors; |
| 277 | for (auto&& it : inputData) |
| 278 | { |
| 279 | BindingPointInfo bindingInfo = m_Parser->GetNetworkInputBindingInfo(subgraphId, it.first); |
| 280 | armnn::VerifyTensorInfoDataType(bindingInfo.second, armnnType1); |
| 281 | |
| 282 | inputTensors.push_back({ bindingInfo.first, armnn::ConstTensor(bindingInfo.second, it.second.data()) }); |
| 283 | } |
| 284 | |
| 285 | armnn::OutputTensors outputTensors; |
| 286 | outputTensors.reserve(expectedOutputData.size()); |
| 287 | std::map<std::string, std::vector<DataType2>> outputStorage; |
| 288 | for (auto&& it : expectedOutputData) |
| 289 | { |
| 290 | BindingPointInfo bindingInfo = m_Parser->GetNetworkOutputBindingInfo(subgraphId, it.first); |
| 291 | armnn::VerifyTensorInfoDataType(bindingInfo.second, armnnType2); |
| 292 | |
| 293 | std::vector<DataType2> out(it.second.size()); |
| 294 | outputStorage.emplace(it.first, out); |
| 295 | outputTensors.push_back({ bindingInfo.first, |
| 296 | armnn::Tensor(bindingInfo.second, |
| 297 | outputStorage.at(it.first).data()) }); |
| 298 | } |
| 299 | |
| 300 | m_Runtime->EnqueueWorkload(m_NetworkIdentifier, inputTensors, outputTensors); |
| 301 | |
| 302 | // Checks the results. |
| 303 | for (auto&& it : expectedOutputData) |
| 304 | { |
| 305 | std::vector<DataType2> out = outputStorage.at(it.first); |
| 306 | { |
| 307 | for (unsigned int i = 0; i < out.size(); ++i) |
| 308 | { |
| 309 | BOOST_TEST(it.second[i] == out[i], boost::test_tools::tolerance(0.000001f)); |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | } |