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