telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1 | // |
Finn Williams | b49ed18 | 2021-06-29 15:50:08 +0100 | [diff] [blame] | 2 | // Copyright © 2017 Arm Ltd and Contributors. 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 | |
Finn Williams | b49ed18 | 2021-06-29 15:50:08 +0100 | [diff] [blame] | 16 | #include "../TfLiteParser.hpp" |
Matteo Martincigh | c601aa6 | 2019-10-29 15:03:22 +0000 | [diff] [blame] | 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 | |
| 32 | using armnnTfLiteParser::ITfLiteParser; |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 33 | using armnnTfLiteParser::ITfLiteParserPtr; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 34 | |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 35 | using TensorRawPtr = const tflite::TensorT *; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 36 | struct ParserFlatbuffersFixture |
| 37 | { |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 38 | ParserFlatbuffersFixture() : |
Finn Williams | b49ed18 | 2021-06-29 15:50:08 +0100 | [diff] [blame] | 39 | m_Runtime(armnn::IRuntime::Create(armnn::IRuntime::CreationOptions())), |
| 40 | m_NetworkIdentifier(0), |
| 41 | m_DynamicNetworkIdentifier(1) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 42 | { |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 43 | ITfLiteParser::TfLiteParserOptions options; |
| 44 | options.m_StandInLayerForUnsupported = true; |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 45 | options.m_InferAndValidate = true; |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 46 | |
Finn Williams | b49ed18 | 2021-06-29 15:50:08 +0100 | [diff] [blame] | 47 | m_Parser = std::make_unique<armnnTfLiteParser::TfLiteParserImpl>( |
| 48 | 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; |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 53 | armnn::IRuntimePtr m_Runtime; |
| 54 | armnn::NetworkId m_NetworkIdentifier; |
Finn Williams | b49ed18 | 2021-06-29 15:50:08 +0100 | [diff] [blame] | 55 | armnn::NetworkId m_DynamicNetworkIdentifier; |
| 56 | bool m_TestDynamic; |
| 57 | std::unique_ptr<armnnTfLiteParser::TfLiteParserImpl> m_Parser; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 58 | |
| 59 | /// If the single-input-single-output overload of Setup() is called, these will store the input and output name |
| 60 | /// so they don't need to be passed to the single-input-single-output overload of RunTest(). |
| 61 | std::string m_SingleInputName; |
| 62 | std::string m_SingleOutputName; |
| 63 | |
Finn Williams | b49ed18 | 2021-06-29 15:50:08 +0100 | [diff] [blame] | 64 | void Setup(bool testDynamic = true) |
| 65 | { |
| 66 | m_TestDynamic = testDynamic; |
| 67 | loadNetwork(m_NetworkIdentifier, false); |
| 68 | |
| 69 | if (m_TestDynamic) |
| 70 | { |
| 71 | loadNetwork(m_DynamicNetworkIdentifier, true); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | std::unique_ptr<tflite::ModelT> MakeModelDynamic(std::vector<uint8_t> graphBinary) |
| 76 | { |
| 77 | const uint8_t* binaryContent = graphBinary.data(); |
| 78 | const size_t len = graphBinary.size(); |
| 79 | if (binaryContent == nullptr) |
| 80 | { |
| 81 | throw armnn::InvalidArgumentException(fmt::format("Invalid (null) binary content {}", |
| 82 | CHECK_LOCATION().AsString())); |
| 83 | } |
| 84 | flatbuffers::Verifier verifier(binaryContent, len); |
| 85 | if (verifier.VerifyBuffer<tflite::Model>() == false) |
| 86 | { |
| 87 | throw armnn::ParseException(fmt::format("Buffer doesn't conform to the expected Tensorflow Lite " |
| 88 | "flatbuffers format. size:{} {}", |
| 89 | len, |
| 90 | CHECK_LOCATION().AsString())); |
| 91 | } |
| 92 | auto model = tflite::UnPackModel(binaryContent); |
| 93 | |
| 94 | for (auto const& subgraph : model->subgraphs) |
| 95 | { |
| 96 | std::vector<int32_t> inputIds = subgraph->inputs; |
| 97 | for (unsigned int tensorIndex = 0; tensorIndex < subgraph->tensors.size(); ++tensorIndex) |
| 98 | { |
| 99 | if (std::find(inputIds.begin(), inputIds.end(), tensorIndex) != inputIds.end()) |
| 100 | { |
| 101 | continue; |
| 102 | } |
| 103 | for (auto const& tensor : subgraph->tensors) |
| 104 | { |
| 105 | if (tensor->shape_signature.size() != 0) |
| 106 | { |
| 107 | continue; |
| 108 | } |
| 109 | |
| 110 | for (unsigned int i = 0; i < tensor->shape.size(); ++i) |
| 111 | { |
| 112 | tensor->shape_signature.push_back(-1); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | return model; |
| 119 | } |
| 120 | |
| 121 | void loadNetwork(armnn::NetworkId networkId, bool loadDynamic) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 122 | { |
| 123 | bool ok = ReadStringToBinary(); |
| 124 | if (!ok) { |
| 125 | throw armnn::Exception("LoadNetwork failed while reading binary input"); |
| 126 | } |
| 127 | |
Finn Williams | b49ed18 | 2021-06-29 15:50:08 +0100 | [diff] [blame] | 128 | armnn::INetworkPtr network = loadDynamic ? m_Parser->LoadModel(MakeModelDynamic(m_GraphBinary)) |
| 129 | : m_Parser->CreateNetworkFromBinary(m_GraphBinary); |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 130 | |
| 131 | if (!network) { |
| 132 | throw armnn::Exception("The parser failed to create an ArmNN network"); |
| 133 | } |
| 134 | |
| 135 | auto optimized = Optimize(*network, { armnn::Compute::CpuRef }, |
| 136 | m_Runtime->GetDeviceSpec()); |
| 137 | std::string errorMessage; |
| 138 | |
Finn Williams | b49ed18 | 2021-06-29 15:50:08 +0100 | [diff] [blame] | 139 | armnn::Status ret = m_Runtime->LoadNetwork(networkId, move(optimized), errorMessage); |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 140 | |
| 141 | if (ret != armnn::Status::Success) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 142 | { |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 143 | throw armnn::Exception( |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 144 | fmt::format("The runtime failed to load the network. " |
| 145 | "Error was: {}. in {} [{}:{}]", |
| 146 | errorMessage, |
| 147 | __func__, |
| 148 | __FILE__, |
| 149 | __LINE__)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
| 153 | void SetupSingleInputSingleOutput(const std::string& inputName, const std::string& outputName) |
| 154 | { |
| 155 | // Store the input and output name so they don't need to be passed to the single-input-single-output RunTest(). |
| 156 | m_SingleInputName = inputName; |
| 157 | m_SingleOutputName = outputName; |
| 158 | Setup(); |
| 159 | } |
| 160 | |
| 161 | bool ReadStringToBinary() |
| 162 | { |
Rob Hughes | ff3c426 | 2019-12-20 17:43:16 +0000 | [diff] [blame] | 163 | std::string schemafile(g_TfLiteSchemaText, g_TfLiteSchemaText + g_TfLiteSchemaText_len); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 164 | |
| 165 | // parse schema first, so we can use it to parse the data after |
| 166 | flatbuffers::Parser parser; |
| 167 | |
Matthew Bentham | 6c8e8e7 | 2019-01-15 17:57:00 +0000 | [diff] [blame] | 168 | bool ok = parser.Parse(schemafile.c_str()); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 169 | ARMNN_ASSERT_MSG(ok, "Failed to parse schema file"); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 170 | |
| 171 | ok &= parser.Parse(m_JsonString.c_str()); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 172 | ARMNN_ASSERT_MSG(ok, "Failed to parse json input"); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 173 | |
| 174 | if (!ok) |
| 175 | { |
| 176 | return false; |
| 177 | } |
| 178 | |
| 179 | { |
| 180 | const uint8_t * bufferPtr = parser.builder_.GetBufferPointer(); |
| 181 | size_t size = static_cast<size_t>(parser.builder_.GetSize()); |
| 182 | m_GraphBinary.assign(bufferPtr, bufferPtr+size); |
| 183 | } |
| 184 | return ok; |
| 185 | } |
| 186 | |
| 187 | /// 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] | 188 | /// This 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, |
Rob Hughes | fc6bf05 | 2019-12-16 17:10:51 +0000 | [diff] [blame] | 190 | armnn::DataType ArmnnType> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 191 | void RunTest(size_t subgraphId, |
Rob Hughes | fc6bf05 | 2019-12-16 17:10:51 +0000 | [diff] [blame] | 192 | const std::vector<armnn::ResolveType<ArmnnType>>& inputData, |
| 193 | const std::vector<armnn::ResolveType<ArmnnType>>& expectedOutputData); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 194 | |
| 195 | /// Executes the network with the given input tensors and checks the results against the given output tensors. |
| 196 | /// This overload supports multiple inputs and multiple outputs, identified by name. |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 197 | template <std::size_t NumOutputDimensions, |
Rob Hughes | fc6bf05 | 2019-12-16 17:10:51 +0000 | [diff] [blame] | 198 | armnn::DataType ArmnnType> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 199 | void RunTest(size_t subgraphId, |
Rob Hughes | fc6bf05 | 2019-12-16 17:10:51 +0000 | [diff] [blame] | 200 | const std::map<std::string, std::vector<armnn::ResolveType<ArmnnType>>>& inputData, |
| 201 | const std::map<std::string, std::vector<armnn::ResolveType<ArmnnType>>>& expectedOutputData); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 202 | |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 203 | /// Multiple Inputs, Multiple Outputs w/ Variable Datatypes and different dimension sizes. |
| 204 | /// Executes the network with the given input tensors and checks the results against the given output tensors. |
| 205 | /// This overload supports multiple inputs and multiple outputs, identified by name along with the allowance for |
| 206 | /// the input datatype to be different to the output |
| 207 | template <std::size_t NumOutputDimensions, |
| 208 | armnn::DataType ArmnnType1, |
Rob Hughes | fc6bf05 | 2019-12-16 17:10:51 +0000 | [diff] [blame] | 209 | armnn::DataType ArmnnType2> |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 210 | void RunTest(size_t subgraphId, |
Rob Hughes | fc6bf05 | 2019-12-16 17:10:51 +0000 | [diff] [blame] | 211 | const std::map<std::string, std::vector<armnn::ResolveType<ArmnnType1>>>& inputData, |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 212 | const std::map<std::string, std::vector<armnn::ResolveType<ArmnnType2>>>& expectedOutputData, |
| 213 | bool isDynamic = false); |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 214 | |
Sadik Armagan | 2686849 | 2021-01-22 14:25:31 +0000 | [diff] [blame] | 215 | /// Multiple Inputs with different DataTypes, Multiple Outputs w/ Variable DataTypes |
| 216 | /// Executes the network with the given input tensors and checks the results against the given output tensors. |
| 217 | /// This overload supports multiple inputs and multiple outputs, identified by name along with the allowance for |
| 218 | /// the input datatype to be different to the output |
| 219 | template <std::size_t NumOutputDimensions, |
| 220 | armnn::DataType inputType1, |
| 221 | armnn::DataType inputType2, |
| 222 | armnn::DataType outputType> |
| 223 | void RunTest(size_t subgraphId, |
| 224 | const std::map<std::string, std::vector<armnn::ResolveType<inputType1>>>& input1Data, |
| 225 | const std::map<std::string, std::vector<armnn::ResolveType<inputType2>>>& input2Data, |
| 226 | const std::map<std::string, std::vector<armnn::ResolveType<outputType>>>& expectedOutputData); |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 227 | |
| 228 | /// Multiple Inputs, Multiple Outputs w/ Variable Datatypes and different dimension sizes. |
| 229 | /// Executes the network with the given input tensors and checks the results against the given output tensors. |
| 230 | /// This overload supports multiple inputs and multiple outputs, identified by name along with the allowance for |
| 231 | /// the input datatype to be different to the output |
| 232 | template<armnn::DataType ArmnnType1, |
Rob Hughes | fc6bf05 | 2019-12-16 17:10:51 +0000 | [diff] [blame] | 233 | armnn::DataType ArmnnType2> |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 234 | void RunTest(std::size_t subgraphId, |
Rob Hughes | fc6bf05 | 2019-12-16 17:10:51 +0000 | [diff] [blame] | 235 | const std::map<std::string, std::vector<armnn::ResolveType<ArmnnType1>>>& inputData, |
| 236 | const std::map<std::string, std::vector<armnn::ResolveType<ArmnnType2>>>& expectedOutputData); |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 237 | |
keidav01 | 222c753 | 2019-03-14 17:12:10 +0000 | [diff] [blame] | 238 | static inline std::string GenerateDetectionPostProcessJsonString( |
| 239 | const armnn::DetectionPostProcessDescriptor& descriptor) |
| 240 | { |
| 241 | flexbuffers::Builder detectPostProcess; |
| 242 | detectPostProcess.Map([&]() { |
| 243 | detectPostProcess.Bool("use_regular_nms", descriptor.m_UseRegularNms); |
| 244 | detectPostProcess.Int("max_detections", descriptor.m_MaxDetections); |
| 245 | detectPostProcess.Int("max_classes_per_detection", descriptor.m_MaxClassesPerDetection); |
| 246 | detectPostProcess.Int("detections_per_class", descriptor.m_DetectionsPerClass); |
| 247 | detectPostProcess.Int("num_classes", descriptor.m_NumClasses); |
| 248 | detectPostProcess.Float("nms_score_threshold", descriptor.m_NmsScoreThreshold); |
| 249 | detectPostProcess.Float("nms_iou_threshold", descriptor.m_NmsIouThreshold); |
| 250 | detectPostProcess.Float("h_scale", descriptor.m_ScaleH); |
| 251 | detectPostProcess.Float("w_scale", descriptor.m_ScaleW); |
| 252 | detectPostProcess.Float("x_scale", descriptor.m_ScaleX); |
| 253 | detectPostProcess.Float("y_scale", descriptor.m_ScaleY); |
| 254 | }); |
| 255 | detectPostProcess.Finish(); |
| 256 | |
| 257 | // Create JSON string |
| 258 | std::stringstream strStream; |
| 259 | std::vector<uint8_t> buffer = detectPostProcess.GetBuffer(); |
| 260 | std::copy(buffer.begin(), buffer.end(),std::ostream_iterator<int>(strStream,",")); |
| 261 | |
| 262 | return strStream.str(); |
| 263 | } |
| 264 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 265 | void CheckTensors(const TensorRawPtr& tensors, size_t shapeSize, const std::vector<int32_t>& shape, |
| 266 | tflite::TensorType tensorType, uint32_t buffer, const std::string& name, |
| 267 | const std::vector<float>& min, const std::vector<float>& max, |
| 268 | const std::vector<float>& scale, const std::vector<int64_t>& zeroPoint) |
| 269 | { |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 270 | CHECK(tensors); |
| 271 | CHECK_EQ(shapeSize, tensors->shape.size()); |
| 272 | CHECK(std::equal(shape.begin(), shape.end(), tensors->shape.begin(), tensors->shape.end())); |
| 273 | CHECK_EQ(tensorType, tensors->type); |
| 274 | CHECK_EQ(buffer, tensors->buffer); |
| 275 | CHECK_EQ(name, tensors->name); |
| 276 | CHECK(tensors->quantization); |
| 277 | CHECK(std::equal(min.begin(), min.end(), tensors->quantization.get()->min.begin(), |
| 278 | tensors->quantization.get()->min.end())); |
| 279 | CHECK(std::equal(max.begin(), max.end(), tensors->quantization.get()->max.begin(), |
| 280 | tensors->quantization.get()->max.end())); |
| 281 | CHECK(std::equal(scale.begin(), scale.end(), tensors->quantization.get()->scale.begin(), |
| 282 | tensors->quantization.get()->scale.end())); |
| 283 | CHECK(std::equal(zeroPoint.begin(), zeroPoint.end(), |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 284 | tensors->quantization.get()->zero_point.begin(), |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 285 | tensors->quantization.get()->zero_point.end())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 286 | } |
Sadik Armagan | 2686849 | 2021-01-22 14:25:31 +0000 | [diff] [blame] | 287 | |
| 288 | private: |
| 289 | /// Fills the InputTensors with given input data |
| 290 | template <armnn::DataType dataType> |
| 291 | void FillInputTensors(armnn::InputTensors& inputTensors, |
| 292 | const std::map<std::string, std::vector<armnn::ResolveType<dataType>>>& inputData, |
| 293 | size_t subgraphId); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 294 | }; |
| 295 | |
Sadik Armagan | 2686849 | 2021-01-22 14:25:31 +0000 | [diff] [blame] | 296 | /// Fills the InputTensors with given input data |
| 297 | template <armnn::DataType dataType> |
| 298 | void ParserFlatbuffersFixture::FillInputTensors( |
| 299 | armnn::InputTensors& inputTensors, |
| 300 | const std::map<std::string, std::vector<armnn::ResolveType<dataType>>>& inputData, |
| 301 | size_t subgraphId) |
| 302 | { |
| 303 | for (auto&& it : inputData) |
| 304 | { |
| 305 | armnn::BindingPointInfo bindingInfo = m_Parser->GetNetworkInputBindingInfo(subgraphId, it.first); |
| 306 | armnn::VerifyTensorInfoDataType(bindingInfo.second, dataType); |
| 307 | inputTensors.push_back({ bindingInfo.first, armnn::ConstTensor(bindingInfo.second, it.second.data()) }); |
| 308 | } |
| 309 | } |
| 310 | |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 311 | /// Single Input, Single Output |
| 312 | /// Executes the network with the given input tensor and checks the result against the given output tensor. |
| 313 | /// 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] | 314 | template <std::size_t NumOutputDimensions, |
Rob Hughes | fc6bf05 | 2019-12-16 17:10:51 +0000 | [diff] [blame] | 315 | armnn::DataType armnnType> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 316 | void ParserFlatbuffersFixture::RunTest(size_t subgraphId, |
Rob Hughes | fc6bf05 | 2019-12-16 17:10:51 +0000 | [diff] [blame] | 317 | const std::vector<armnn::ResolveType<armnnType>>& inputData, |
| 318 | const std::vector<armnn::ResolveType<armnnType>>& expectedOutputData) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 319 | { |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 320 | RunTest<NumOutputDimensions, armnnType>(subgraphId, |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 321 | { { m_SingleInputName, inputData } }, |
| 322 | { { m_SingleOutputName, expectedOutputData } }); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 323 | } |
| 324 | |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 325 | /// Multiple Inputs, Multiple Outputs |
| 326 | /// Executes the network with the given input tensors and checks the results against the given output tensors. |
| 327 | /// This overload supports multiple inputs and multiple outputs, identified by name. |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 328 | template <std::size_t NumOutputDimensions, |
Rob Hughes | fc6bf05 | 2019-12-16 17:10:51 +0000 | [diff] [blame] | 329 | armnn::DataType armnnType> |
Nattapat Chaimanowong | 649dd95 | 2019-01-22 16:10:44 +0000 | [diff] [blame] | 330 | void ParserFlatbuffersFixture::RunTest(size_t subgraphId, |
Rob Hughes | fc6bf05 | 2019-12-16 17:10:51 +0000 | [diff] [blame] | 331 | const std::map<std::string, std::vector<armnn::ResolveType<armnnType>>>& inputData, |
| 332 | const std::map<std::string, std::vector<armnn::ResolveType<armnnType>>>& expectedOutputData) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 333 | { |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 334 | RunTest<NumOutputDimensions, armnnType, armnnType>(subgraphId, inputData, expectedOutputData); |
| 335 | } |
| 336 | |
| 337 | /// Multiple Inputs, Multiple Outputs w/ Variable Datatypes |
| 338 | /// Executes the network with the given input tensors and checks the results against the given output tensors. |
| 339 | /// This overload supports multiple inputs and multiple outputs, identified by name along with the allowance for |
| 340 | /// the input datatype to be different to the output |
| 341 | template <std::size_t NumOutputDimensions, |
| 342 | armnn::DataType armnnType1, |
Rob Hughes | fc6bf05 | 2019-12-16 17:10:51 +0000 | [diff] [blame] | 343 | armnn::DataType armnnType2> |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 344 | void ParserFlatbuffersFixture::RunTest(size_t subgraphId, |
Rob Hughes | fc6bf05 | 2019-12-16 17:10:51 +0000 | [diff] [blame] | 345 | const std::map<std::string, std::vector<armnn::ResolveType<armnnType1>>>& inputData, |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 346 | const std::map<std::string, std::vector<armnn::ResolveType<armnnType2>>>& expectedOutputData, |
| 347 | bool isDynamic) |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 348 | { |
Rob Hughes | fc6bf05 | 2019-12-16 17:10:51 +0000 | [diff] [blame] | 349 | using DataType2 = armnn::ResolveType<armnnType2>; |
| 350 | |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 351 | // Setup the armnn input tensors from the given vectors. |
| 352 | armnn::InputTensors inputTensors; |
Sadik Armagan | 2686849 | 2021-01-22 14:25:31 +0000 | [diff] [blame] | 353 | FillInputTensors<armnnType1>(inputTensors, inputData, subgraphId); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 354 | |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 355 | // 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] | 356 | std::map<std::string, std::vector<DataType2>> outputStorage; |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 357 | armnn::OutputTensors outputTensors; |
| 358 | for (auto&& it : expectedOutputData) |
| 359 | { |
Narumol Prangnawarat | 386681a | 2019-04-29 16:40:55 +0100 | [diff] [blame] | 360 | armnn::LayerBindingId outputBindingId = m_Parser->GetNetworkOutputBindingInfo(subgraphId, it.first).first; |
| 361 | armnn::TensorInfo outputTensorInfo = m_Runtime->GetOutputTensorInfo(m_NetworkIdentifier, outputBindingId); |
| 362 | |
| 363 | // Check that output tensors have correct number of dimensions (NumOutputDimensions specified in test) |
| 364 | auto outputNumDimensions = outputTensorInfo.GetNumDimensions(); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 365 | CHECK_MESSAGE((outputNumDimensions == NumOutputDimensions), |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 366 | fmt::format("Number of dimensions expected {}, but got {} for output layer {}", |
| 367 | NumOutputDimensions, |
| 368 | outputNumDimensions, |
| 369 | it.first)); |
Narumol Prangnawarat | 386681a | 2019-04-29 16:40:55 +0100 | [diff] [blame] | 370 | |
| 371 | armnn::VerifyTensorInfoDataType(outputTensorInfo, armnnType2); |
Sadik Armagan | 483c811 | 2021-06-01 09:24:52 +0100 | [diff] [blame] | 372 | outputStorage.emplace(it.first, std::vector<DataType2>(outputTensorInfo.GetNumElements())); |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 373 | outputTensors.push_back( |
Narumol Prangnawarat | 386681a | 2019-04-29 16:40:55 +0100 | [diff] [blame] | 374 | { outputBindingId, armnn::Tensor(outputTensorInfo, outputStorage.at(it.first).data()) }); |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 375 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 376 | |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 377 | m_Runtime->EnqueueWorkload(m_NetworkIdentifier, inputTensors, outputTensors); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 378 | |
Aron Virginas-Tar | 1d67a690 | 2018-11-19 10:58:30 +0000 | [diff] [blame] | 379 | // Compare each output tensor to the expected values |
| 380 | for (auto&& it : expectedOutputData) |
| 381 | { |
Jim Flynn | b4d7eae | 2019-05-01 14:44:27 +0100 | [diff] [blame] | 382 | armnn::BindingPointInfo bindingInfo = m_Parser->GetNetworkOutputBindingInfo(subgraphId, it.first); |
Sadik Armagan | 483c811 | 2021-06-01 09:24:52 +0100 | [diff] [blame] | 383 | auto outputExpected = it.second; |
Bruno Goncalves | 9021125 | 2021-07-11 21:49:00 -0300 | [diff] [blame] | 384 | if (std::is_same<DataType2, uint8_t>::value) |
| 385 | { |
| 386 | auto result = CompareTensors(outputExpected, outputStorage[it.first], |
| 387 | bindingInfo.second.GetShape(), bindingInfo.second.GetShape(), |
| 388 | true, isDynamic); |
| 389 | CHECK_MESSAGE(result.m_Result, result.m_Message.str()); |
| 390 | } |
| 391 | else |
| 392 | { |
| 393 | auto result = CompareTensors(outputExpected, outputStorage[it.first], |
| 394 | bindingInfo.second.GetShape(), bindingInfo.second.GetShape(), |
| 395 | false, isDynamic); |
| 396 | CHECK_MESSAGE(result.m_Result, result.m_Message.str()); |
| 397 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 398 | } |
Finn Williams | b49ed18 | 2021-06-29 15:50:08 +0100 | [diff] [blame] | 399 | |
| 400 | if (isDynamic) |
| 401 | { |
| 402 | m_Runtime->EnqueueWorkload(m_DynamicNetworkIdentifier, inputTensors, outputTensors); |
| 403 | |
| 404 | // Compare each output tensor to the expected values |
| 405 | for (auto&& it : expectedOutputData) |
| 406 | { |
| 407 | armnn::BindingPointInfo bindingInfo = m_Parser->GetNetworkOutputBindingInfo(subgraphId, it.first); |
| 408 | auto outputExpected = it.second; |
| 409 | auto result = CompareTensors(outputExpected, outputStorage[it.first], |
| 410 | bindingInfo.second.GetShape(), bindingInfo.second.GetShape(), |
| 411 | false, isDynamic); |
| 412 | CHECK_MESSAGE(result.m_Result, result.m_Message.str()); |
| 413 | } |
| 414 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 415 | } |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 416 | |
| 417 | /// Multiple Inputs, Multiple Outputs w/ Variable Datatypes and different dimension sizes. |
| 418 | /// Executes the network with the given input tensors and checks the results against the given output tensors. |
| 419 | /// This overload supports multiple inputs and multiple outputs, identified by name along with the allowance for |
| 420 | /// the input datatype to be different to the output. |
| 421 | template <armnn::DataType armnnType1, |
Rob Hughes | fc6bf05 | 2019-12-16 17:10:51 +0000 | [diff] [blame] | 422 | armnn::DataType armnnType2> |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 423 | void ParserFlatbuffersFixture::RunTest(std::size_t subgraphId, |
Rob Hughes | fc6bf05 | 2019-12-16 17:10:51 +0000 | [diff] [blame] | 424 | const std::map<std::string, std::vector<armnn::ResolveType<armnnType1>>>& inputData, |
| 425 | const std::map<std::string, std::vector<armnn::ResolveType<armnnType2>>>& expectedOutputData) |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 426 | { |
Rob Hughes | fc6bf05 | 2019-12-16 17:10:51 +0000 | [diff] [blame] | 427 | using DataType2 = armnn::ResolveType<armnnType2>; |
| 428 | |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 429 | // Setup the armnn input tensors from the given vectors. |
| 430 | armnn::InputTensors inputTensors; |
Sadik Armagan | 2686849 | 2021-01-22 14:25:31 +0000 | [diff] [blame] | 431 | FillInputTensors<armnnType1>(inputTensors, inputData, subgraphId); |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 432 | |
| 433 | armnn::OutputTensors outputTensors; |
| 434 | outputTensors.reserve(expectedOutputData.size()); |
| 435 | std::map<std::string, std::vector<DataType2>> outputStorage; |
| 436 | for (auto&& it : expectedOutputData) |
| 437 | { |
Jim Flynn | b4d7eae | 2019-05-01 14:44:27 +0100 | [diff] [blame] | 438 | armnn::BindingPointInfo bindingInfo = m_Parser->GetNetworkOutputBindingInfo(subgraphId, it.first); |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 439 | armnn::VerifyTensorInfoDataType(bindingInfo.second, armnnType2); |
| 440 | |
| 441 | std::vector<DataType2> out(it.second.size()); |
| 442 | outputStorage.emplace(it.first, out); |
| 443 | outputTensors.push_back({ bindingInfo.first, |
| 444 | armnn::Tensor(bindingInfo.second, |
| 445 | outputStorage.at(it.first).data()) }); |
| 446 | } |
| 447 | |
| 448 | m_Runtime->EnqueueWorkload(m_NetworkIdentifier, inputTensors, outputTensors); |
| 449 | |
| 450 | // Checks the results. |
| 451 | for (auto&& it : expectedOutputData) |
| 452 | { |
Rob Hughes | fc6bf05 | 2019-12-16 17:10:51 +0000 | [diff] [blame] | 453 | std::vector<armnn::ResolveType<armnnType2>> out = outputStorage.at(it.first); |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 454 | { |
| 455 | for (unsigned int i = 0; i < out.size(); ++i) |
| 456 | { |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 457 | CHECK(doctest::Approx(it.second[i]).epsilon(0.000001f) == out[i]); |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | } |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 461 | } |
Sadik Armagan | 2686849 | 2021-01-22 14:25:31 +0000 | [diff] [blame] | 462 | |
| 463 | /// Multiple Inputs with different DataTypes, Multiple Outputs w/ Variable DataTypes |
| 464 | /// Executes the network with the given input tensors and checks the results against the given output tensors. |
| 465 | /// This overload supports multiple inputs and multiple outputs, identified by name along with the allowance for |
| 466 | /// the input datatype to be different to the output |
| 467 | template <std::size_t NumOutputDimensions, |
| 468 | armnn::DataType inputType1, |
| 469 | armnn::DataType inputType2, |
| 470 | armnn::DataType outputType> |
| 471 | void ParserFlatbuffersFixture::RunTest(size_t subgraphId, |
| 472 | const std::map<std::string, std::vector<armnn::ResolveType<inputType1>>>& input1Data, |
| 473 | const std::map<std::string, std::vector<armnn::ResolveType<inputType2>>>& input2Data, |
| 474 | const std::map<std::string, std::vector<armnn::ResolveType<outputType>>>& expectedOutputData) |
| 475 | { |
| 476 | using DataType2 = armnn::ResolveType<outputType>; |
| 477 | |
| 478 | // Setup the armnn input tensors from the given vectors. |
| 479 | armnn::InputTensors inputTensors; |
| 480 | FillInputTensors<inputType1>(inputTensors, input1Data, subgraphId); |
| 481 | FillInputTensors<inputType2>(inputTensors, input2Data, subgraphId); |
| 482 | |
| 483 | // 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] | 484 | std::map<std::string, std::vector<DataType2>> outputStorage; |
Sadik Armagan | 2686849 | 2021-01-22 14:25:31 +0000 | [diff] [blame] | 485 | armnn::OutputTensors outputTensors; |
| 486 | for (auto&& it : expectedOutputData) |
| 487 | { |
| 488 | armnn::LayerBindingId outputBindingId = m_Parser->GetNetworkOutputBindingInfo(subgraphId, it.first).first; |
| 489 | armnn::TensorInfo outputTensorInfo = m_Runtime->GetOutputTensorInfo(m_NetworkIdentifier, outputBindingId); |
| 490 | |
| 491 | // Check that output tensors have correct number of dimensions (NumOutputDimensions specified in test) |
| 492 | auto outputNumDimensions = outputTensorInfo.GetNumDimensions(); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 493 | CHECK_MESSAGE((outputNumDimensions == NumOutputDimensions), |
Sadik Armagan | 2686849 | 2021-01-22 14:25:31 +0000 | [diff] [blame] | 494 | fmt::format("Number of dimensions expected {}, but got {} for output layer {}", |
| 495 | NumOutputDimensions, |
| 496 | outputNumDimensions, |
| 497 | it.first)); |
| 498 | |
| 499 | armnn::VerifyTensorInfoDataType(outputTensorInfo, outputType); |
Sadik Armagan | 483c811 | 2021-06-01 09:24:52 +0100 | [diff] [blame] | 500 | outputStorage.emplace(it.first, std::vector<DataType2>(outputTensorInfo.GetNumElements())); |
Sadik Armagan | 2686849 | 2021-01-22 14:25:31 +0000 | [diff] [blame] | 501 | outputTensors.push_back( |
| 502 | { outputBindingId, armnn::Tensor(outputTensorInfo, outputStorage.at(it.first).data()) }); |
| 503 | } |
| 504 | |
| 505 | m_Runtime->EnqueueWorkload(m_NetworkIdentifier, inputTensors, outputTensors); |
| 506 | |
| 507 | // Compare each output tensor to the expected values |
| 508 | for (auto&& it : expectedOutputData) |
| 509 | { |
| 510 | armnn::BindingPointInfo bindingInfo = m_Parser->GetNetworkOutputBindingInfo(subgraphId, it.first); |
Sadik Armagan | 483c811 | 2021-06-01 09:24:52 +0100 | [diff] [blame] | 511 | auto outputExpected = it.second; |
Bruno Goncalves | 9021125 | 2021-07-11 21:49:00 -0300 | [diff] [blame] | 512 | if (std::is_same<DataType2, uint8_t>::value) |
| 513 | { |
| 514 | auto result = CompareTensors(outputExpected, outputStorage[it.first], |
| 515 | bindingInfo.second.GetShape(), bindingInfo.second.GetShape(), true); |
| 516 | CHECK_MESSAGE(result.m_Result, result.m_Message.str()); |
| 517 | } |
| 518 | else |
| 519 | { |
| 520 | auto result = CompareTensors(outputExpected, outputStorage[it.first], |
| 521 | bindingInfo.second.GetShape(), bindingInfo.second.GetShape()); |
| 522 | CHECK_MESSAGE(result.m_Result, result.m_Message.str()); |
| 523 | } |
Sadik Armagan | 2686849 | 2021-01-22 14:25:31 +0000 | [diff] [blame] | 524 | } |
| 525 | } |