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