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> |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 14 | #include <armnn/utility/Assert.hpp> |
keidav01 | 222c753 | 2019-03-14 17:12:10 +0000 | [diff] [blame] | 15 | |
Matteo Martincigh | c601aa6 | 2019-10-29 15:03:22 +0000 | [diff] [blame] | 16 | #include <armnnTfLiteParser/ITfLiteParser.hpp> |
| 17 | |
| 18 | #include <ResolveType.hpp> |
| 19 | |
| 20 | #include <test/TensorHelpers.hpp> |
| 21 | |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 22 | #include <fmt/format.h> |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 23 | #include <doctest/doctest.h> |
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; |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 46 | options.m_InferAndValidate = true; |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 47 | |
| 48 | m_Parser.reset(ITfLiteParser::CreateRaw(armnn::Optional<ITfLiteParser::TfLiteParserOptions>(options))); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | std::vector<uint8_t> m_GraphBinary; |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 52 | std::string m_JsonString; |
| 53 | ITfLiteParserPtr m_Parser; |
| 54 | armnn::IRuntimePtr m_Runtime; |
| 55 | armnn::NetworkId m_NetworkIdentifier; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 56 | |
| 57 | /// If the single-input-single-output overload of Setup() is called, these will store the input and output name |
| 58 | /// so they don't need to be passed to the single-input-single-output overload of RunTest(). |
| 59 | std::string m_SingleInputName; |
| 60 | std::string m_SingleOutputName; |
| 61 | |
| 62 | void Setup() |
| 63 | { |
| 64 | bool ok = ReadStringToBinary(); |
| 65 | if (!ok) { |
| 66 | throw armnn::Exception("LoadNetwork failed while reading binary input"); |
| 67 | } |
| 68 | |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 69 | armnn::INetworkPtr network = |
| 70 | m_Parser->CreateNetworkFromBinary(m_GraphBinary); |
| 71 | |
| 72 | if (!network) { |
| 73 | throw armnn::Exception("The parser failed to create an ArmNN network"); |
| 74 | } |
| 75 | |
| 76 | auto optimized = Optimize(*network, { armnn::Compute::CpuRef }, |
| 77 | m_Runtime->GetDeviceSpec()); |
| 78 | std::string errorMessage; |
| 79 | |
| 80 | armnn::Status ret = m_Runtime->LoadNetwork(m_NetworkIdentifier, move(optimized), errorMessage); |
| 81 | |
| 82 | if (ret != armnn::Status::Success) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 83 | { |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 84 | throw armnn::Exception( |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 85 | fmt::format("The runtime failed to load the network. " |
| 86 | "Error was: {}. in {} [{}:{}]", |
| 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 | { |
Rob Hughes | ff3c426 | 2019-12-20 17:43:16 +0000 | [diff] [blame] | 104 | std::string schemafile(g_TfLiteSchemaText, g_TfLiteSchemaText + g_TfLiteSchemaText_len); |
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()); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 110 | ARMNN_ASSERT_MSG(ok, "Failed to parse schema file"); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 111 | |
| 112 | ok &= parser.Parse(m_JsonString.c_str()); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 113 | ARMNN_ASSERT_MSG(ok, "Failed to parse json input"); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 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, |
Rob Hughes | fc6bf05 | 2019-12-16 17:10:51 +0000 | [diff] [blame] | 131 | armnn::DataType ArmnnType> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 132 | void RunTest(size_t subgraphId, |
Rob Hughes | fc6bf05 | 2019-12-16 17:10:51 +0000 | [diff] [blame] | 133 | const std::vector<armnn::ResolveType<ArmnnType>>& inputData, |
| 134 | const std::vector<armnn::ResolveType<ArmnnType>>& expectedOutputData); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 135 | |
| 136 | /// Executes the network with the given input tensors and checks the results against the given output tensors. |
| 137 | /// This overload supports multiple inputs and multiple outputs, identified by name. |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 138 | template <std::size_t NumOutputDimensions, |
Rob Hughes | fc6bf05 | 2019-12-16 17:10:51 +0000 | [diff] [blame] | 139 | armnn::DataType ArmnnType> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 140 | void RunTest(size_t subgraphId, |
Rob Hughes | fc6bf05 | 2019-12-16 17:10:51 +0000 | [diff] [blame] | 141 | const std::map<std::string, std::vector<armnn::ResolveType<ArmnnType>>>& inputData, |
| 142 | const std::map<std::string, std::vector<armnn::ResolveType<ArmnnType>>>& expectedOutputData); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 143 | |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 144 | /// Multiple Inputs, Multiple Outputs w/ Variable Datatypes and different dimension sizes. |
| 145 | /// Executes the network with the given input tensors and checks the results against the given output tensors. |
| 146 | /// This overload supports multiple inputs and multiple outputs, identified by name along with the allowance for |
| 147 | /// the input datatype to be different to the output |
| 148 | template <std::size_t NumOutputDimensions, |
| 149 | armnn::DataType ArmnnType1, |
Rob Hughes | fc6bf05 | 2019-12-16 17:10:51 +0000 | [diff] [blame] | 150 | armnn::DataType ArmnnType2> |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 151 | void RunTest(size_t subgraphId, |
Rob Hughes | fc6bf05 | 2019-12-16 17:10:51 +0000 | [diff] [blame] | 152 | const std::map<std::string, std::vector<armnn::ResolveType<ArmnnType1>>>& inputData, |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 153 | const std::map<std::string, std::vector<armnn::ResolveType<ArmnnType2>>>& expectedOutputData, |
| 154 | bool isDynamic = false); |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 155 | |
Sadik Armagan | 2686849 | 2021-01-22 14:25:31 +0000 | [diff] [blame] | 156 | /// Multiple Inputs with different DataTypes, Multiple Outputs w/ Variable DataTypes |
| 157 | /// Executes the network with the given input tensors and checks the results against the given output tensors. |
| 158 | /// This overload supports multiple inputs and multiple outputs, identified by name along with the allowance for |
| 159 | /// the input datatype to be different to the output |
| 160 | template <std::size_t NumOutputDimensions, |
| 161 | armnn::DataType inputType1, |
| 162 | armnn::DataType inputType2, |
| 163 | armnn::DataType outputType> |
| 164 | void RunTest(size_t subgraphId, |
| 165 | const std::map<std::string, std::vector<armnn::ResolveType<inputType1>>>& input1Data, |
| 166 | const std::map<std::string, std::vector<armnn::ResolveType<inputType2>>>& input2Data, |
| 167 | const std::map<std::string, std::vector<armnn::ResolveType<outputType>>>& expectedOutputData); |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 168 | |
| 169 | /// Multiple Inputs, Multiple Outputs w/ Variable Datatypes and different dimension sizes. |
| 170 | /// Executes the network with the given input tensors and checks the results against the given output tensors. |
| 171 | /// This overload supports multiple inputs and multiple outputs, identified by name along with the allowance for |
| 172 | /// the input datatype to be different to the output |
| 173 | template<armnn::DataType ArmnnType1, |
Rob Hughes | fc6bf05 | 2019-12-16 17:10:51 +0000 | [diff] [blame] | 174 | armnn::DataType ArmnnType2> |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 175 | void RunTest(std::size_t subgraphId, |
Rob Hughes | fc6bf05 | 2019-12-16 17:10:51 +0000 | [diff] [blame] | 176 | const std::map<std::string, std::vector<armnn::ResolveType<ArmnnType1>>>& inputData, |
| 177 | const std::map<std::string, std::vector<armnn::ResolveType<ArmnnType2>>>& expectedOutputData); |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 178 | |
keidav01 | 222c753 | 2019-03-14 17:12:10 +0000 | [diff] [blame] | 179 | static inline std::string GenerateDetectionPostProcessJsonString( |
| 180 | const armnn::DetectionPostProcessDescriptor& descriptor) |
| 181 | { |
| 182 | flexbuffers::Builder detectPostProcess; |
| 183 | detectPostProcess.Map([&]() { |
| 184 | detectPostProcess.Bool("use_regular_nms", descriptor.m_UseRegularNms); |
| 185 | detectPostProcess.Int("max_detections", descriptor.m_MaxDetections); |
| 186 | detectPostProcess.Int("max_classes_per_detection", descriptor.m_MaxClassesPerDetection); |
| 187 | detectPostProcess.Int("detections_per_class", descriptor.m_DetectionsPerClass); |
| 188 | detectPostProcess.Int("num_classes", descriptor.m_NumClasses); |
| 189 | detectPostProcess.Float("nms_score_threshold", descriptor.m_NmsScoreThreshold); |
| 190 | detectPostProcess.Float("nms_iou_threshold", descriptor.m_NmsIouThreshold); |
| 191 | detectPostProcess.Float("h_scale", descriptor.m_ScaleH); |
| 192 | detectPostProcess.Float("w_scale", descriptor.m_ScaleW); |
| 193 | detectPostProcess.Float("x_scale", descriptor.m_ScaleX); |
| 194 | detectPostProcess.Float("y_scale", descriptor.m_ScaleY); |
| 195 | }); |
| 196 | detectPostProcess.Finish(); |
| 197 | |
| 198 | // Create JSON string |
| 199 | std::stringstream strStream; |
| 200 | std::vector<uint8_t> buffer = detectPostProcess.GetBuffer(); |
| 201 | std::copy(buffer.begin(), buffer.end(),std::ostream_iterator<int>(strStream,",")); |
| 202 | |
| 203 | return strStream.str(); |
| 204 | } |
| 205 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 206 | void CheckTensors(const TensorRawPtr& tensors, size_t shapeSize, const std::vector<int32_t>& shape, |
| 207 | tflite::TensorType tensorType, uint32_t buffer, const std::string& name, |
| 208 | const std::vector<float>& min, const std::vector<float>& max, |
| 209 | const std::vector<float>& scale, const std::vector<int64_t>& zeroPoint) |
| 210 | { |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 211 | CHECK(tensors); |
| 212 | CHECK_EQ(shapeSize, tensors->shape.size()); |
| 213 | CHECK(std::equal(shape.begin(), shape.end(), tensors->shape.begin(), tensors->shape.end())); |
| 214 | CHECK_EQ(tensorType, tensors->type); |
| 215 | CHECK_EQ(buffer, tensors->buffer); |
| 216 | CHECK_EQ(name, tensors->name); |
| 217 | CHECK(tensors->quantization); |
| 218 | CHECK(std::equal(min.begin(), min.end(), tensors->quantization.get()->min.begin(), |
| 219 | tensors->quantization.get()->min.end())); |
| 220 | CHECK(std::equal(max.begin(), max.end(), tensors->quantization.get()->max.begin(), |
| 221 | tensors->quantization.get()->max.end())); |
| 222 | CHECK(std::equal(scale.begin(), scale.end(), tensors->quantization.get()->scale.begin(), |
| 223 | tensors->quantization.get()->scale.end())); |
| 224 | CHECK(std::equal(zeroPoint.begin(), zeroPoint.end(), |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 225 | tensors->quantization.get()->zero_point.begin(), |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 226 | tensors->quantization.get()->zero_point.end())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 227 | } |
Sadik Armagan | 2686849 | 2021-01-22 14:25:31 +0000 | [diff] [blame] | 228 | |
| 229 | private: |
| 230 | /// Fills the InputTensors with given input data |
| 231 | template <armnn::DataType dataType> |
| 232 | void FillInputTensors(armnn::InputTensors& inputTensors, |
| 233 | const std::map<std::string, std::vector<armnn::ResolveType<dataType>>>& inputData, |
| 234 | size_t subgraphId); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 235 | }; |
| 236 | |
Sadik Armagan | 2686849 | 2021-01-22 14:25:31 +0000 | [diff] [blame] | 237 | /// Fills the InputTensors with given input data |
| 238 | template <armnn::DataType dataType> |
| 239 | void ParserFlatbuffersFixture::FillInputTensors( |
| 240 | armnn::InputTensors& inputTensors, |
| 241 | const std::map<std::string, std::vector<armnn::ResolveType<dataType>>>& inputData, |
| 242 | size_t subgraphId) |
| 243 | { |
| 244 | for (auto&& it : inputData) |
| 245 | { |
| 246 | armnn::BindingPointInfo bindingInfo = m_Parser->GetNetworkInputBindingInfo(subgraphId, it.first); |
| 247 | armnn::VerifyTensorInfoDataType(bindingInfo.second, dataType); |
| 248 | inputTensors.push_back({ bindingInfo.first, armnn::ConstTensor(bindingInfo.second, it.second.data()) }); |
| 249 | } |
| 250 | } |
| 251 | |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 252 | /// Single Input, Single Output |
| 253 | /// Executes the network with the given input tensor and checks the result against the given output tensor. |
| 254 | /// 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] | 255 | template <std::size_t NumOutputDimensions, |
Rob Hughes | fc6bf05 | 2019-12-16 17:10:51 +0000 | [diff] [blame] | 256 | armnn::DataType armnnType> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 257 | void ParserFlatbuffersFixture::RunTest(size_t subgraphId, |
Rob Hughes | fc6bf05 | 2019-12-16 17:10:51 +0000 | [diff] [blame] | 258 | const std::vector<armnn::ResolveType<armnnType>>& inputData, |
| 259 | const std::vector<armnn::ResolveType<armnnType>>& expectedOutputData) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 260 | { |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 261 | RunTest<NumOutputDimensions, armnnType>(subgraphId, |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 262 | { { m_SingleInputName, inputData } }, |
| 263 | { { m_SingleOutputName, expectedOutputData } }); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 264 | } |
| 265 | |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 266 | /// Multiple Inputs, Multiple Outputs |
| 267 | /// Executes the network with the given input tensors and checks the results against the given output tensors. |
| 268 | /// This overload supports multiple inputs and multiple outputs, identified by name. |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 269 | template <std::size_t NumOutputDimensions, |
Rob Hughes | fc6bf05 | 2019-12-16 17:10:51 +0000 | [diff] [blame] | 270 | armnn::DataType armnnType> |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 271 | void ParserFlatbuffersFixture::RunTest(size_t subgraphId, |
Rob Hughes | fc6bf05 | 2019-12-16 17:10:51 +0000 | [diff] [blame] | 272 | const std::map<std::string, std::vector<armnn::ResolveType<armnnType>>>& inputData, |
| 273 | const std::map<std::string, std::vector<armnn::ResolveType<armnnType>>>& expectedOutputData) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 274 | { |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 275 | RunTest<NumOutputDimensions, armnnType, armnnType>(subgraphId, inputData, expectedOutputData); |
| 276 | } |
| 277 | |
| 278 | /// Multiple Inputs, Multiple Outputs w/ Variable Datatypes |
| 279 | /// Executes the network with the given input tensors and checks the results against the given output tensors. |
| 280 | /// This overload supports multiple inputs and multiple outputs, identified by name along with the allowance for |
| 281 | /// the input datatype to be different to the output |
| 282 | template <std::size_t NumOutputDimensions, |
| 283 | armnn::DataType armnnType1, |
Rob Hughes | fc6bf05 | 2019-12-16 17:10:51 +0000 | [diff] [blame] | 284 | armnn::DataType armnnType2> |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 285 | void ParserFlatbuffersFixture::RunTest(size_t subgraphId, |
Rob Hughes | fc6bf05 | 2019-12-16 17:10:51 +0000 | [diff] [blame] | 286 | const std::map<std::string, std::vector<armnn::ResolveType<armnnType1>>>& inputData, |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 287 | const std::map<std::string, std::vector<armnn::ResolveType<armnnType2>>>& expectedOutputData, |
| 288 | bool isDynamic) |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 289 | { |
Rob Hughes | fc6bf05 | 2019-12-16 17:10:51 +0000 | [diff] [blame] | 290 | using DataType2 = armnn::ResolveType<armnnType2>; |
| 291 | |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 292 | // Setup the armnn input tensors from the given vectors. |
| 293 | armnn::InputTensors inputTensors; |
Sadik Armagan | 2686849 | 2021-01-22 14:25:31 +0000 | [diff] [blame] | 294 | FillInputTensors<armnnType1>(inputTensors, inputData, subgraphId); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 295 | |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 296 | // Allocate storage for the output tensors to be written to and setup the armnn output tensors. |
Sadik Armagan | 483c811 | 2021-06-01 09:24:52 +0100 | [diff] [blame] | 297 | std::map<std::string, std::vector<DataType2>> outputStorage; |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 298 | armnn::OutputTensors outputTensors; |
| 299 | for (auto&& it : expectedOutputData) |
| 300 | { |
Narumol Prangnawarat | 386681a | 2019-04-29 16:40:55 +0100 | [diff] [blame] | 301 | armnn::LayerBindingId outputBindingId = m_Parser->GetNetworkOutputBindingInfo(subgraphId, it.first).first; |
| 302 | armnn::TensorInfo outputTensorInfo = m_Runtime->GetOutputTensorInfo(m_NetworkIdentifier, outputBindingId); |
| 303 | |
| 304 | // Check that output tensors have correct number of dimensions (NumOutputDimensions specified in test) |
| 305 | auto outputNumDimensions = outputTensorInfo.GetNumDimensions(); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 306 | CHECK_MESSAGE((outputNumDimensions == NumOutputDimensions), |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 307 | fmt::format("Number of dimensions expected {}, but got {} for output layer {}", |
| 308 | NumOutputDimensions, |
| 309 | outputNumDimensions, |
| 310 | it.first)); |
Narumol Prangnawarat | 386681a | 2019-04-29 16:40:55 +0100 | [diff] [blame] | 311 | |
| 312 | armnn::VerifyTensorInfoDataType(outputTensorInfo, armnnType2); |
Sadik Armagan | 483c811 | 2021-06-01 09:24:52 +0100 | [diff] [blame] | 313 | outputStorage.emplace(it.first, std::vector<DataType2>(outputTensorInfo.GetNumElements())); |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 314 | outputTensors.push_back( |
Narumol Prangnawarat | 386681a | 2019-04-29 16:40:55 +0100 | [diff] [blame] | 315 | { outputBindingId, armnn::Tensor(outputTensorInfo, outputStorage.at(it.first).data()) }); |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 316 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 317 | |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 318 | m_Runtime->EnqueueWorkload(m_NetworkIdentifier, inputTensors, outputTensors); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 319 | |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 320 | // Compare each output tensor to the expected values |
| 321 | for (auto&& it : expectedOutputData) |
| 322 | { |
Jim Flynn | b4d7eae | 2019-05-01 14:44:27 +0100 | [diff] [blame] | 323 | armnn::BindingPointInfo bindingInfo = m_Parser->GetNetworkOutputBindingInfo(subgraphId, it.first); |
Sadik Armagan | 483c811 | 2021-06-01 09:24:52 +0100 | [diff] [blame] | 324 | auto outputExpected = it.second; |
| 325 | auto result = CompareTensors(outputExpected, outputStorage[it.first], |
| 326 | bindingInfo.second.GetShape(), bindingInfo.second.GetShape(), |
| 327 | false, isDynamic); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 328 | CHECK_MESSAGE(result.m_Result, result.m_Message.str()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 329 | } |
| 330 | } |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 331 | |
| 332 | /// Multiple Inputs, Multiple Outputs w/ Variable Datatypes and different dimension sizes. |
| 333 | /// Executes the network with the given input tensors and checks the results against the given output tensors. |
| 334 | /// This overload supports multiple inputs and multiple outputs, identified by name along with the allowance for |
| 335 | /// the input datatype to be different to the output. |
| 336 | template <armnn::DataType armnnType1, |
Rob Hughes | fc6bf05 | 2019-12-16 17:10:51 +0000 | [diff] [blame] | 337 | armnn::DataType armnnType2> |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 338 | void ParserFlatbuffersFixture::RunTest(std::size_t subgraphId, |
Rob Hughes | fc6bf05 | 2019-12-16 17:10:51 +0000 | [diff] [blame] | 339 | const std::map<std::string, std::vector<armnn::ResolveType<armnnType1>>>& inputData, |
| 340 | const std::map<std::string, std::vector<armnn::ResolveType<armnnType2>>>& expectedOutputData) |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 341 | { |
Rob Hughes | fc6bf05 | 2019-12-16 17:10:51 +0000 | [diff] [blame] | 342 | using DataType2 = armnn::ResolveType<armnnType2>; |
| 343 | |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 344 | // Setup the armnn input tensors from the given vectors. |
| 345 | armnn::InputTensors inputTensors; |
Sadik Armagan | 2686849 | 2021-01-22 14:25:31 +0000 | [diff] [blame] | 346 | FillInputTensors<armnnType1>(inputTensors, inputData, subgraphId); |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 347 | |
| 348 | armnn::OutputTensors outputTensors; |
| 349 | outputTensors.reserve(expectedOutputData.size()); |
| 350 | std::map<std::string, std::vector<DataType2>> outputStorage; |
| 351 | for (auto&& it : expectedOutputData) |
| 352 | { |
Jim Flynn | b4d7eae | 2019-05-01 14:44:27 +0100 | [diff] [blame] | 353 | armnn::BindingPointInfo bindingInfo = m_Parser->GetNetworkOutputBindingInfo(subgraphId, it.first); |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 354 | armnn::VerifyTensorInfoDataType(bindingInfo.second, armnnType2); |
| 355 | |
| 356 | std::vector<DataType2> out(it.second.size()); |
| 357 | outputStorage.emplace(it.first, out); |
| 358 | outputTensors.push_back({ bindingInfo.first, |
| 359 | armnn::Tensor(bindingInfo.second, |
| 360 | outputStorage.at(it.first).data()) }); |
| 361 | } |
| 362 | |
| 363 | m_Runtime->EnqueueWorkload(m_NetworkIdentifier, inputTensors, outputTensors); |
| 364 | |
| 365 | // Checks the results. |
| 366 | for (auto&& it : expectedOutputData) |
| 367 | { |
Rob Hughes | fc6bf05 | 2019-12-16 17:10:51 +0000 | [diff] [blame] | 368 | std::vector<armnn::ResolveType<armnnType2>> out = outputStorage.at(it.first); |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 369 | { |
| 370 | for (unsigned int i = 0; i < out.size(); ++i) |
| 371 | { |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 372 | CHECK(doctest::Approx(it.second[i]).epsilon(0.000001f) == out[i]); |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | } |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 376 | } |
Sadik Armagan | 2686849 | 2021-01-22 14:25:31 +0000 | [diff] [blame] | 377 | |
| 378 | /// Multiple Inputs with different DataTypes, Multiple Outputs w/ Variable DataTypes |
| 379 | /// Executes the network with the given input tensors and checks the results against the given output tensors. |
| 380 | /// This overload supports multiple inputs and multiple outputs, identified by name along with the allowance for |
| 381 | /// the input datatype to be different to the output |
| 382 | template <std::size_t NumOutputDimensions, |
| 383 | armnn::DataType inputType1, |
| 384 | armnn::DataType inputType2, |
| 385 | armnn::DataType outputType> |
| 386 | void ParserFlatbuffersFixture::RunTest(size_t subgraphId, |
| 387 | const std::map<std::string, std::vector<armnn::ResolveType<inputType1>>>& input1Data, |
| 388 | const std::map<std::string, std::vector<armnn::ResolveType<inputType2>>>& input2Data, |
| 389 | const std::map<std::string, std::vector<armnn::ResolveType<outputType>>>& expectedOutputData) |
| 390 | { |
| 391 | using DataType2 = armnn::ResolveType<outputType>; |
| 392 | |
| 393 | // Setup the armnn input tensors from the given vectors. |
| 394 | armnn::InputTensors inputTensors; |
| 395 | FillInputTensors<inputType1>(inputTensors, input1Data, subgraphId); |
| 396 | FillInputTensors<inputType2>(inputTensors, input2Data, subgraphId); |
| 397 | |
| 398 | // Allocate storage for the output tensors to be written to and setup the armnn output tensors. |
Sadik Armagan | 483c811 | 2021-06-01 09:24:52 +0100 | [diff] [blame] | 399 | std::map<std::string, std::vector<DataType2>> outputStorage; |
Sadik Armagan | 2686849 | 2021-01-22 14:25:31 +0000 | [diff] [blame] | 400 | armnn::OutputTensors outputTensors; |
| 401 | for (auto&& it : expectedOutputData) |
| 402 | { |
| 403 | armnn::LayerBindingId outputBindingId = m_Parser->GetNetworkOutputBindingInfo(subgraphId, it.first).first; |
| 404 | armnn::TensorInfo outputTensorInfo = m_Runtime->GetOutputTensorInfo(m_NetworkIdentifier, outputBindingId); |
| 405 | |
| 406 | // Check that output tensors have correct number of dimensions (NumOutputDimensions specified in test) |
| 407 | auto outputNumDimensions = outputTensorInfo.GetNumDimensions(); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 408 | CHECK_MESSAGE((outputNumDimensions == NumOutputDimensions), |
Sadik Armagan | 2686849 | 2021-01-22 14:25:31 +0000 | [diff] [blame] | 409 | fmt::format("Number of dimensions expected {}, but got {} for output layer {}", |
| 410 | NumOutputDimensions, |
| 411 | outputNumDimensions, |
| 412 | it.first)); |
| 413 | |
| 414 | armnn::VerifyTensorInfoDataType(outputTensorInfo, outputType); |
Sadik Armagan | 483c811 | 2021-06-01 09:24:52 +0100 | [diff] [blame] | 415 | outputStorage.emplace(it.first, std::vector<DataType2>(outputTensorInfo.GetNumElements())); |
Sadik Armagan | 2686849 | 2021-01-22 14:25:31 +0000 | [diff] [blame] | 416 | outputTensors.push_back( |
| 417 | { outputBindingId, armnn::Tensor(outputTensorInfo, outputStorage.at(it.first).data()) }); |
| 418 | } |
| 419 | |
| 420 | m_Runtime->EnqueueWorkload(m_NetworkIdentifier, inputTensors, outputTensors); |
| 421 | |
| 422 | // Compare each output tensor to the expected values |
| 423 | for (auto&& it : expectedOutputData) |
| 424 | { |
| 425 | armnn::BindingPointInfo bindingInfo = m_Parser->GetNetworkOutputBindingInfo(subgraphId, it.first); |
Sadik Armagan | 483c811 | 2021-06-01 09:24:52 +0100 | [diff] [blame] | 426 | auto outputExpected = it.second; |
| 427 | auto result = CompareTensors(outputExpected, outputStorage[it.first], |
| 428 | bindingInfo.second.GetShape(), bindingInfo.second.GetShape(), false); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 429 | CHECK_MESSAGE(result.m_Result, result.m_Message.str()); |
Sadik Armagan | 2686849 | 2021-01-22 14:25:31 +0000 | [diff] [blame] | 430 | } |
| 431 | } |