telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [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 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 4 | // |
Matteo Martincigh | c601aa6 | 2019-10-29 15:03:22 +0000 | [diff] [blame] | 5 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 6 | #pragma once |
Matteo Martincigh | c601aa6 | 2019-10-29 15:03:22 +0000 | [diff] [blame] | 7 | |
David Beck | f0b4845 | 2018-10-19 15:20:56 +0100 | [diff] [blame] | 8 | #include <armnn/ArmNN.hpp> |
Matteo Martincigh | c601aa6 | 2019-10-29 15:03:22 +0000 | [diff] [blame] | 9 | #include <armnn/BackendRegistry.hpp> |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 10 | #include <armnn/utility/Assert.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 11 | |
Aron Virginas-Tar | 64e4ccb | 2019-02-12 11:27:53 +0000 | [diff] [blame] | 12 | #if defined(ARMNN_SERIALIZER) |
Derek Lamberti | 0028d1b | 2019-02-20 13:57:42 +0000 | [diff] [blame] | 13 | #include "armnnDeserializer/IDeserializer.hpp" |
Aron Virginas-Tar | 64e4ccb | 2019-02-12 11:27:53 +0000 | [diff] [blame] | 14 | #endif |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 15 | #if defined(ARMNN_TF_LITE_PARSER) |
David Beck | f0b4845 | 2018-10-19 15:20:56 +0100 | [diff] [blame] | 16 | #include <armnnTfLiteParser/ITfLiteParser.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 17 | #endif |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 18 | #if defined(ARMNN_ONNX_PARSER) |
David Beck | f0b4845 | 2018-10-19 15:20:56 +0100 | [diff] [blame] | 19 | #include <armnnOnnxParser/IOnnxParser.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 20 | #endif |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 21 | |
Aron Virginas-Tar | 64e4ccb | 2019-02-12 11:27:53 +0000 | [diff] [blame] | 22 | #include <HeapProfiling.hpp> |
Jim Flynn | 2fd6100 | 2019-05-03 12:54:26 +0100 | [diff] [blame] | 23 | #include <TensorIOUtils.hpp> |
Aron Virginas-Tar | 64e4ccb | 2019-02-12 11:27:53 +0000 | [diff] [blame] | 24 | |
Aron Virginas-Tar | 339bcae | 2019-01-31 16:44:26 +0000 | [diff] [blame] | 25 | #include <boost/algorithm/string/join.hpp> |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 26 | #include <boost/exception/exception.hpp> |
| 27 | #include <boost/exception/diagnostic_information.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 28 | #include <boost/format.hpp> |
| 29 | #include <boost/program_options.hpp> |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 30 | #include <boost/filesystem.hpp> |
Ferran Balaguer | c602f29 | 2019-02-08 17:09:55 +0000 | [diff] [blame] | 31 | #include <boost/variant.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 32 | |
Aron Virginas-Tar | 339bcae | 2019-01-31 16:44:26 +0000 | [diff] [blame] | 33 | #include <algorithm> |
James Conroy | 7b4886f | 2019-04-11 10:23:58 +0100 | [diff] [blame] | 34 | #include <chrono> |
Aron Virginas-Tar | 339bcae | 2019-01-31 16:44:26 +0000 | [diff] [blame] | 35 | #include <iterator> |
Aron Virginas-Tar | 5cc8e56 | 2018-10-23 15:14:46 +0100 | [diff] [blame] | 36 | #include <fstream> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 37 | #include <map> |
| 38 | #include <string> |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 39 | #include <vector> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 40 | #include <type_traits> |
| 41 | |
Aron Virginas-Tar | 5cc8e56 | 2018-10-23 15:14:46 +0100 | [diff] [blame] | 42 | namespace |
| 43 | { |
| 44 | |
| 45 | inline bool CheckRequestedBackendsAreValid(const std::vector<armnn::BackendId>& backendIds, |
| 46 | armnn::Optional<std::string&> invalidBackendIds = armnn::EmptyOptional()) |
| 47 | { |
| 48 | if (backendIds.empty()) |
| 49 | { |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | armnn::BackendIdSet validBackendIds = armnn::BackendRegistryInstance().GetBackendIds(); |
| 54 | |
| 55 | bool allValid = true; |
| 56 | for (const auto& backendId : backendIds) |
| 57 | { |
| 58 | if (std::find(validBackendIds.begin(), validBackendIds.end(), backendId) == validBackendIds.end()) |
| 59 | { |
| 60 | allValid = false; |
| 61 | if (invalidBackendIds) |
| 62 | { |
| 63 | if (!invalidBackendIds.value().empty()) |
| 64 | { |
| 65 | invalidBackendIds.value() += ", "; |
| 66 | } |
| 67 | invalidBackendIds.value() += backendId; |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | return allValid; |
| 72 | } |
| 73 | |
| 74 | } // anonymous namespace |
| 75 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 76 | namespace InferenceModelInternal |
| 77 | { |
Jim Flynn | b4d7eae | 2019-05-01 14:44:27 +0100 | [diff] [blame] | 78 | using BindingPointInfo = armnn::BindingPointInfo; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 79 | |
| 80 | using QuantizationParams = std::pair<float,int32_t>; |
| 81 | |
| 82 | struct Params |
| 83 | { |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 84 | std::string m_ModelPath; |
| 85 | std::vector<std::string> m_InputBindings; |
| 86 | std::vector<armnn::TensorShape> m_InputShapes; |
| 87 | std::vector<std::string> m_OutputBindings; |
Aron Virginas-Tar | 339bcae | 2019-01-31 16:44:26 +0000 | [diff] [blame] | 88 | std::vector<armnn::BackendId> m_ComputeDevices; |
Matteo Martincigh | 00dda4a | 2019-08-14 11:42:30 +0100 | [diff] [blame] | 89 | std::string m_DynamicBackendsPath; |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 90 | size_t m_SubgraphId; |
| 91 | bool m_IsModelBinary; |
| 92 | bool m_VisualizePostOptimizationModel; |
| 93 | bool m_EnableFp16TurboMode; |
Narumol Prangnawarat | d8cc811 | 2020-03-24 13:54:05 +0000 | [diff] [blame] | 94 | bool m_EnableBf16TurboMode; |
Matthew Jackson | 54658b9 | 2019-08-27 15:35:59 +0100 | [diff] [blame] | 95 | bool m_PrintIntermediateLayers; |
Derek Lamberti | 132563c | 2019-12-02 16:06:40 +0000 | [diff] [blame] | 96 | bool m_ParseUnsupported; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 97 | |
| 98 | Params() |
Matteo Martincigh | 00dda4a | 2019-08-14 11:42:30 +0100 | [diff] [blame] | 99 | : m_ComputeDevices{} |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 100 | , m_SubgraphId(0) |
| 101 | , m_IsModelBinary(true) |
| 102 | , m_VisualizePostOptimizationModel(false) |
| 103 | , m_EnableFp16TurboMode(false) |
Narumol Prangnawarat | d8cc811 | 2020-03-24 13:54:05 +0000 | [diff] [blame] | 104 | , m_EnableBf16TurboMode(false) |
Matthew Jackson | 54658b9 | 2019-08-27 15:35:59 +0100 | [diff] [blame] | 105 | , m_PrintIntermediateLayers(false) |
Derek Lamberti | 132563c | 2019-12-02 16:06:40 +0000 | [diff] [blame] | 106 | , m_ParseUnsupported(false) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 107 | {} |
| 108 | }; |
| 109 | |
| 110 | } // namespace InferenceModelInternal |
| 111 | |
| 112 | template <typename IParser> |
| 113 | struct CreateNetworkImpl |
| 114 | { |
| 115 | public: |
| 116 | using Params = InferenceModelInternal::Params; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 117 | |
| 118 | static armnn::INetworkPtr Create(const Params& params, |
Jim Flynn | b4d7eae | 2019-05-01 14:44:27 +0100 | [diff] [blame] | 119 | std::vector<armnn::BindingPointInfo>& inputBindings, |
| 120 | std::vector<armnn::BindingPointInfo>& outputBindings) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 121 | { |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 122 | const std::string& modelPath = params.m_ModelPath; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 123 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 124 | // Create a network from a file on disk |
| 125 | auto parser(IParser::Create()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 126 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 127 | std::map<std::string, armnn::TensorShape> inputShapes; |
| 128 | if (!params.m_InputShapes.empty()) |
| 129 | { |
| 130 | const size_t numInputShapes = params.m_InputShapes.size(); |
| 131 | const size_t numInputBindings = params.m_InputBindings.size(); |
| 132 | if (numInputShapes < numInputBindings) |
| 133 | { |
| 134 | throw armnn::Exception(boost::str(boost::format( |
| 135 | "Not every input has its tensor shape specified: expected=%1%, got=%2%") |
| 136 | % numInputBindings % numInputShapes)); |
| 137 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 138 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 139 | for (size_t i = 0; i < numInputShapes; i++) |
| 140 | { |
| 141 | inputShapes[params.m_InputBindings[i]] = params.m_InputShapes[i]; |
| 142 | } |
| 143 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 144 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 145 | std::vector<std::string> requestedOutputs = params.m_OutputBindings; |
| 146 | armnn::INetworkPtr network{nullptr, [](armnn::INetwork *){}}; |
| 147 | |
| 148 | { |
| 149 | ARMNN_SCOPED_HEAP_PROFILING("Parsing"); |
| 150 | // Handle text and binary input differently by calling the corresponding parser function |
| 151 | network = (params.m_IsModelBinary ? |
| 152 | parser->CreateNetworkFromBinaryFile(modelPath.c_str(), inputShapes, requestedOutputs) : |
| 153 | parser->CreateNetworkFromTextFile(modelPath.c_str(), inputShapes, requestedOutputs)); |
| 154 | } |
| 155 | |
| 156 | for (const std::string& inputLayerName : params.m_InputBindings) |
| 157 | { |
| 158 | inputBindings.push_back(parser->GetNetworkInputBindingInfo(inputLayerName)); |
| 159 | } |
| 160 | |
| 161 | for (const std::string& outputLayerName : params.m_OutputBindings) |
| 162 | { |
| 163 | outputBindings.push_back(parser->GetNetworkOutputBindingInfo(outputLayerName)); |
| 164 | } |
| 165 | |
| 166 | return network; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 167 | } |
| 168 | }; |
| 169 | |
Aron Virginas-Tar | 64e4ccb | 2019-02-12 11:27:53 +0000 | [diff] [blame] | 170 | #if defined(ARMNN_SERIALIZER) |
| 171 | template <> |
Derek Lamberti | 0028d1b | 2019-02-20 13:57:42 +0000 | [diff] [blame] | 172 | struct CreateNetworkImpl<armnnDeserializer::IDeserializer> |
Aron Virginas-Tar | 64e4ccb | 2019-02-12 11:27:53 +0000 | [diff] [blame] | 173 | { |
| 174 | public: |
Derek Lamberti | 0028d1b | 2019-02-20 13:57:42 +0000 | [diff] [blame] | 175 | using IParser = armnnDeserializer::IDeserializer; |
Aron Virginas-Tar | 64e4ccb | 2019-02-12 11:27:53 +0000 | [diff] [blame] | 176 | using Params = InferenceModelInternal::Params; |
Aron Virginas-Tar | 64e4ccb | 2019-02-12 11:27:53 +0000 | [diff] [blame] | 177 | |
| 178 | static armnn::INetworkPtr Create(const Params& params, |
Jim Flynn | b4d7eae | 2019-05-01 14:44:27 +0100 | [diff] [blame] | 179 | std::vector<armnn::BindingPointInfo>& inputBindings, |
| 180 | std::vector<armnn::BindingPointInfo>& outputBindings) |
Aron Virginas-Tar | 64e4ccb | 2019-02-12 11:27:53 +0000 | [diff] [blame] | 181 | { |
| 182 | auto parser(IParser::Create()); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 183 | ARMNN_ASSERT(parser); |
Aron Virginas-Tar | 64e4ccb | 2019-02-12 11:27:53 +0000 | [diff] [blame] | 184 | |
| 185 | armnn::INetworkPtr network{nullptr, [](armnn::INetwork *){}}; |
| 186 | |
| 187 | { |
| 188 | ARMNN_SCOPED_HEAP_PROFILING("Parsing"); |
Derek Lamberti | 2b183fb | 2019-02-18 16:36:57 +0000 | [diff] [blame] | 189 | |
| 190 | boost::system::error_code errorCode; |
| 191 | boost::filesystem::path pathToFile(params.m_ModelPath); |
| 192 | if (!boost::filesystem::exists(pathToFile, errorCode)) |
| 193 | { |
| 194 | throw armnn::FileNotFoundException(boost::str( |
| 195 | boost::format("Cannot find the file (%1%) errorCode: %2% %3%") % |
| 196 | params.m_ModelPath % |
| 197 | errorCode % |
| 198 | CHECK_LOCATION().AsString())); |
| 199 | } |
| 200 | std::ifstream file(params.m_ModelPath, std::ios::binary); |
| 201 | |
| 202 | network = parser->CreateNetworkFromBinary(file); |
Aron Virginas-Tar | 64e4ccb | 2019-02-12 11:27:53 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 205 | unsigned int subgraphId = boost::numeric_cast<unsigned int>(params.m_SubgraphId); |
Aron Virginas-Tar | 64e4ccb | 2019-02-12 11:27:53 +0000 | [diff] [blame] | 206 | |
| 207 | for (const std::string& inputLayerName : params.m_InputBindings) |
| 208 | { |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 209 | armnnDeserializer::BindingPointInfo inputBinding = |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 210 | parser->GetNetworkInputBindingInfo(subgraphId, inputLayerName); |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 211 | inputBindings.push_back(std::make_pair(inputBinding.m_BindingId, inputBinding.m_TensorInfo)); |
Aron Virginas-Tar | 64e4ccb | 2019-02-12 11:27:53 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | for (const std::string& outputLayerName : params.m_OutputBindings) |
| 215 | { |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 216 | armnnDeserializer::BindingPointInfo outputBinding = |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 217 | parser->GetNetworkOutputBindingInfo(subgraphId, outputLayerName); |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 218 | outputBindings.push_back(std::make_pair(outputBinding.m_BindingId, outputBinding.m_TensorInfo)); |
Aron Virginas-Tar | 64e4ccb | 2019-02-12 11:27:53 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | return network; |
| 222 | } |
| 223 | }; |
| 224 | #endif |
| 225 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 226 | #if defined(ARMNN_TF_LITE_PARSER) |
| 227 | template <> |
| 228 | struct CreateNetworkImpl<armnnTfLiteParser::ITfLiteParser> |
| 229 | { |
| 230 | public: |
| 231 | using IParser = armnnTfLiteParser::ITfLiteParser; |
| 232 | using Params = InferenceModelInternal::Params; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 233 | |
| 234 | static armnn::INetworkPtr Create(const Params& params, |
Jim Flynn | b4d7eae | 2019-05-01 14:44:27 +0100 | [diff] [blame] | 235 | std::vector<armnn::BindingPointInfo>& inputBindings, |
| 236 | std::vector<armnn::BindingPointInfo>& outputBindings) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 237 | { |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 238 | const std::string& modelPath = params.m_ModelPath; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 239 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 240 | // Create a network from a file on disk |
Derek Lamberti | 132563c | 2019-12-02 16:06:40 +0000 | [diff] [blame] | 241 | IParser::TfLiteParserOptions options; |
| 242 | options.m_StandInLayerForUnsupported = params.m_ParseUnsupported; |
| 243 | auto parser(IParser::Create(options)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 244 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 245 | armnn::INetworkPtr network{nullptr, [](armnn::INetwork *){}}; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 246 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 247 | { |
| 248 | ARMNN_SCOPED_HEAP_PROFILING("Parsing"); |
| 249 | network = parser->CreateNetworkFromBinaryFile(modelPath.c_str()); |
| 250 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 251 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 252 | for (const std::string& inputLayerName : params.m_InputBindings) |
| 253 | { |
Jim Flynn | b4d7eae | 2019-05-01 14:44:27 +0100 | [diff] [blame] | 254 | armnn::BindingPointInfo inputBinding = |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 255 | parser->GetNetworkInputBindingInfo(params.m_SubgraphId, inputLayerName); |
| 256 | inputBindings.push_back(inputBinding); |
| 257 | } |
| 258 | |
| 259 | for (const std::string& outputLayerName : params.m_OutputBindings) |
| 260 | { |
Jim Flynn | b4d7eae | 2019-05-01 14:44:27 +0100 | [diff] [blame] | 261 | armnn::BindingPointInfo outputBinding = |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 262 | parser->GetNetworkOutputBindingInfo(params.m_SubgraphId, outputLayerName); |
| 263 | outputBindings.push_back(outputBinding); |
| 264 | } |
| 265 | |
| 266 | return network; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 267 | } |
| 268 | }; |
| 269 | #endif |
| 270 | |
| 271 | #if defined(ARMNN_ONNX_PARSER) |
| 272 | template <> |
| 273 | struct CreateNetworkImpl<armnnOnnxParser::IOnnxParser> |
| 274 | { |
| 275 | public: |
| 276 | using IParser = armnnOnnxParser::IOnnxParser; |
| 277 | using Params = InferenceModelInternal::Params; |
| 278 | using BindingPointInfo = InferenceModelInternal::BindingPointInfo; |
| 279 | |
| 280 | static armnn::INetworkPtr Create(const Params& params, |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 281 | std::vector<BindingPointInfo>& inputBindings, |
| 282 | std::vector<BindingPointInfo>& outputBindings) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 283 | { |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 284 | const std::string& modelPath = params.m_ModelPath; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 285 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 286 | // Create a network from a file on disk |
| 287 | auto parser(IParser::Create()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 288 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 289 | armnn::INetworkPtr network{nullptr, [](armnn::INetwork *){}}; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 290 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 291 | { |
| 292 | ARMNN_SCOPED_HEAP_PROFILING("Parsing"); |
| 293 | network = (params.m_IsModelBinary ? |
| 294 | parser->CreateNetworkFromBinaryFile(modelPath.c_str()) : |
| 295 | parser->CreateNetworkFromTextFile(modelPath.c_str())); |
| 296 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 297 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 298 | for (const std::string& inputLayerName : params.m_InputBindings) |
| 299 | { |
| 300 | BindingPointInfo inputBinding = parser->GetNetworkInputBindingInfo(inputLayerName); |
| 301 | inputBindings.push_back(inputBinding); |
| 302 | } |
| 303 | |
| 304 | for (const std::string& outputLayerName : params.m_OutputBindings) |
| 305 | { |
| 306 | BindingPointInfo outputBinding = parser->GetNetworkOutputBindingInfo(outputLayerName); |
| 307 | outputBindings.push_back(outputBinding); |
| 308 | } |
| 309 | |
| 310 | return network; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 311 | } |
| 312 | }; |
| 313 | #endif |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 314 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 315 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 316 | |
| 317 | template <typename IParser, typename TDataType> |
| 318 | class InferenceModel |
| 319 | { |
| 320 | public: |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 321 | using DataType = TDataType; |
| 322 | using Params = InferenceModelInternal::Params; |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 323 | using QuantizationParams = InferenceModelInternal::QuantizationParams; |
Ferran Balaguer | c602f29 | 2019-02-08 17:09:55 +0000 | [diff] [blame] | 324 | using TContainer = boost::variant<std::vector<float>, std::vector<int>, std::vector<unsigned char>>; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 325 | |
| 326 | struct CommandLineOptions |
| 327 | { |
| 328 | std::string m_ModelDir; |
Aron Virginas-Tar | 339bcae | 2019-01-31 16:44:26 +0000 | [diff] [blame] | 329 | std::vector<std::string> m_ComputeDevices; |
Matteo Martincigh | 00dda4a | 2019-08-14 11:42:30 +0100 | [diff] [blame] | 330 | std::string m_DynamicBackendsPath; |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 331 | bool m_VisualizePostOptimizationModel; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 332 | bool m_EnableFp16TurboMode; |
Narumol Prangnawarat | d8cc811 | 2020-03-24 13:54:05 +0000 | [diff] [blame] | 333 | bool m_EnableBf16TurboMode; |
Pablo Tello | 507f39d | 2019-04-15 15:44:39 +0100 | [diff] [blame] | 334 | std::string m_Labels; |
Aron Virginas-Tar | 339bcae | 2019-01-31 16:44:26 +0000 | [diff] [blame] | 335 | |
| 336 | std::vector<armnn::BackendId> GetComputeDevicesAsBackendIds() |
| 337 | { |
| 338 | std::vector<armnn::BackendId> backendIds; |
| 339 | std::copy(m_ComputeDevices.begin(), m_ComputeDevices.end(), std::back_inserter(backendIds)); |
| 340 | return backendIds; |
| 341 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 342 | }; |
| 343 | |
| 344 | static void AddCommandLineOptions(boost::program_options::options_description& desc, CommandLineOptions& options) |
| 345 | { |
| 346 | namespace po = boost::program_options; |
| 347 | |
Aron Virginas-Tar | 339bcae | 2019-01-31 16:44:26 +0000 | [diff] [blame] | 348 | const std::vector<std::string> defaultComputes = { "CpuAcc", "CpuRef" }; |
David Beck | f0b4845 | 2018-10-19 15:20:56 +0100 | [diff] [blame] | 349 | |
Aron Virginas-Tar | 5cc8e56 | 2018-10-23 15:14:46 +0100 | [diff] [blame] | 350 | const std::string backendsMessage = "Which device to run layers on by default. Possible choices: " |
| 351 | + armnn::BackendRegistryInstance().GetBackendIdsAsString(); |
| 352 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 353 | desc.add_options() |
| 354 | ("model-dir,m", po::value<std::string>(&options.m_ModelDir)->required(), |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 355 | "Path to directory containing model files (.caffemodel/.prototxt/.tflite)") |
Aron Virginas-Tar | 339bcae | 2019-01-31 16:44:26 +0000 | [diff] [blame] | 356 | ("compute,c", po::value<std::vector<std::string>>(&options.m_ComputeDevices)-> |
| 357 | default_value(defaultComputes, boost::algorithm::join(defaultComputes, ", "))-> |
| 358 | multitoken(), backendsMessage.c_str()) |
Matteo Martincigh | 00dda4a | 2019-08-14 11:42:30 +0100 | [diff] [blame] | 359 | ("dynamic-backends-path,b", po::value(&options.m_DynamicBackendsPath), |
| 360 | "Path where to load any available dynamic backend from. " |
| 361 | "If left empty (the default), dynamic backends will not be used.") |
Pablo Tello | 507f39d | 2019-04-15 15:44:39 +0100 | [diff] [blame] | 362 | ("labels,l", po::value<std::string>(&options.m_Labels), |
| 363 | "Text file containing one image filename - correct label pair per line, " |
| 364 | "used to test the accuracy of the network.") |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 365 | ("visualize-optimized-model,v", |
| 366 | po::value<bool>(&options.m_VisualizePostOptimizationModel)->default_value(false), |
| 367 | "Produce a dot file useful for visualizing the graph post optimization." |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 368 | "The file will have the same name as the model with the .dot extention.") |
| 369 | ("fp16-turbo-mode", po::value<bool>(&options.m_EnableFp16TurboMode)->default_value(false), |
| 370 | "If this option is enabled FP32 layers, weights and biases will be converted " |
Narumol Prangnawarat | d8cc811 | 2020-03-24 13:54:05 +0000 | [diff] [blame] | 371 | "to FP16 where the backend supports it.") |
| 372 | ("bf16-turbo-mode", po::value<bool>(&options.m_EnableBf16TurboMode)->default_value(false), |
| 373 | "If this option is enabled FP32 layers, weights and biases will be converted " |
| 374 | "to BF16 where the backend supports it."); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Matthew Bentham | 3e68b97 | 2019-04-09 13:10:46 +0100 | [diff] [blame] | 377 | InferenceModel(const Params& params, |
| 378 | bool enableProfiling, |
Matteo Martincigh | 00dda4a | 2019-08-14 11:42:30 +0100 | [diff] [blame] | 379 | const std::string& dynamicBackendsPath, |
Matthew Bentham | 3e68b97 | 2019-04-09 13:10:46 +0100 | [diff] [blame] | 380 | const std::shared_ptr<armnn::IRuntime>& runtime = nullptr) |
| 381 | : m_EnableProfiling(enableProfiling) |
Matteo Martincigh | 00dda4a | 2019-08-14 11:42:30 +0100 | [diff] [blame] | 382 | , m_DynamicBackendsPath(dynamicBackendsPath) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 383 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 384 | if (runtime) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 385 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 386 | m_Runtime = runtime; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 387 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 388 | else |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 389 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 390 | armnn::IRuntime::CreationOptions options; |
Nina Drozd | 549ae37 | 2018-09-10 14:26:44 +0100 | [diff] [blame] | 391 | options.m_EnableGpuProfiling = m_EnableProfiling; |
Matteo Martincigh | 00dda4a | 2019-08-14 11:42:30 +0100 | [diff] [blame] | 392 | options.m_DynamicBackendsPath = m_DynamicBackendsPath; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 393 | m_Runtime = std::move(armnn::IRuntime::Create(options)); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 394 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 395 | |
Aron Virginas-Tar | 5cc8e56 | 2018-10-23 15:14:46 +0100 | [diff] [blame] | 396 | std::string invalidBackends; |
Aron Virginas-Tar | 339bcae | 2019-01-31 16:44:26 +0000 | [diff] [blame] | 397 | if (!CheckRequestedBackendsAreValid(params.m_ComputeDevices, armnn::Optional<std::string&>(invalidBackends))) |
Aron Virginas-Tar | 5cc8e56 | 2018-10-23 15:14:46 +0100 | [diff] [blame] | 398 | { |
| 399 | throw armnn::Exception("Some backend IDs are invalid: " + invalidBackends); |
| 400 | } |
| 401 | |
Matteo Martincigh | 00dda4a | 2019-08-14 11:42:30 +0100 | [diff] [blame] | 402 | armnn::INetworkPtr network = CreateNetworkImpl<IParser>::Create(params, m_InputBindings, m_OutputBindings); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 403 | |
Matteo Martincigh | 00dda4a | 2019-08-14 11:42:30 +0100 | [diff] [blame] | 404 | armnn::IOptimizedNetworkPtr optNet{nullptr, [](armnn::IOptimizedNetwork*){}}; |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 405 | { |
| 406 | ARMNN_SCOPED_HEAP_PROFILING("Optimizing"); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 407 | |
| 408 | armnn::OptimizerOptions options; |
| 409 | options.m_ReduceFp32ToFp16 = params.m_EnableFp16TurboMode; |
Narumol Prangnawarat | d8cc811 | 2020-03-24 13:54:05 +0000 | [diff] [blame] | 410 | options.m_ReduceFp32ToBf16 = params.m_EnableBf16TurboMode; |
Matthew Jackson | 54658b9 | 2019-08-27 15:35:59 +0100 | [diff] [blame] | 411 | options.m_Debug = params.m_PrintIntermediateLayers; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 412 | |
Aron Virginas-Tar | 339bcae | 2019-01-31 16:44:26 +0000 | [diff] [blame] | 413 | optNet = armnn::Optimize(*network, params.m_ComputeDevices, m_Runtime->GetDeviceSpec(), options); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 414 | if (!optNet) |
| 415 | { |
| 416 | throw armnn::Exception("Optimize returned nullptr"); |
| 417 | } |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 418 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 419 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 420 | if (params.m_VisualizePostOptimizationModel) |
| 421 | { |
| 422 | boost::filesystem::path filename = params.m_ModelPath; |
| 423 | filename.replace_extension("dot"); |
Rob Hughes | 9e10c2b | 2019-07-23 15:37:19 +0100 | [diff] [blame] | 424 | std::fstream file(filename.c_str(), std::ios_base::out); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 425 | optNet->SerializeToDot(file); |
| 426 | } |
| 427 | |
| 428 | armnn::Status ret; |
| 429 | { |
| 430 | ARMNN_SCOPED_HEAP_PROFILING("LoadNetwork"); |
| 431 | ret = m_Runtime->LoadNetwork(m_NetworkIdentifier, std::move(optNet)); |
| 432 | } |
| 433 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 434 | if (ret == armnn::Status::Failure) |
| 435 | { |
| 436 | throw armnn::Exception("IRuntime::LoadNetwork failed"); |
| 437 | } |
| 438 | } |
| 439 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 440 | void CheckInputIndexIsValid(unsigned int inputIndex) const |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 441 | { |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 442 | if (m_InputBindings.size() < inputIndex + 1) |
| 443 | { |
| 444 | throw armnn::Exception(boost::str(boost::format("Input index out of range: %1%") % inputIndex)); |
| 445 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 448 | void CheckOutputIndexIsValid(unsigned int outputIndex) const |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 449 | { |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 450 | if (m_OutputBindings.size() < outputIndex + 1) |
| 451 | { |
| 452 | throw armnn::Exception(boost::str(boost::format("Output index out of range: %1%") % outputIndex)); |
| 453 | } |
| 454 | } |
| 455 | |
Aron Virginas-Tar | c82c873 | 2019-10-24 17:07:43 +0100 | [diff] [blame] | 456 | unsigned int GetInputSize(unsigned int inputIndex = 0u) const |
| 457 | { |
| 458 | CheckInputIndexIsValid(inputIndex); |
| 459 | return m_InputBindings[inputIndex].second.GetNumElements(); |
| 460 | } |
| 461 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 462 | unsigned int GetOutputSize(unsigned int outputIndex = 0u) const |
| 463 | { |
| 464 | CheckOutputIndexIsValid(outputIndex); |
| 465 | return m_OutputBindings[outputIndex].second.GetNumElements(); |
| 466 | } |
| 467 | |
James Conroy | 7b4886f | 2019-04-11 10:23:58 +0100 | [diff] [blame] | 468 | std::chrono::duration<double, std::milli> Run( |
| 469 | const std::vector<TContainer>& inputContainers, |
| 470 | std::vector<TContainer>& outputContainers) |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 471 | { |
Ferran Balaguer | c602f29 | 2019-02-08 17:09:55 +0000 | [diff] [blame] | 472 | for (unsigned int i = 0; i < outputContainers.size(); ++i) |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 473 | { |
| 474 | const unsigned int expectedOutputDataSize = GetOutputSize(i); |
Ferran Balaguer | c602f29 | 2019-02-08 17:09:55 +0000 | [diff] [blame] | 475 | |
| 476 | boost::apply_visitor([expectedOutputDataSize, i](auto&& value) |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 477 | { |
Ferran Balaguer | c602f29 | 2019-02-08 17:09:55 +0000 | [diff] [blame] | 478 | const unsigned int actualOutputDataSize = boost::numeric_cast<unsigned int>(value.size()); |
| 479 | if (actualOutputDataSize < expectedOutputDataSize) |
| 480 | { |
| 481 | unsigned int outputIndex = boost::numeric_cast<unsigned int>(i); |
| 482 | throw armnn::Exception( |
| 483 | boost::str(boost::format("Not enough data for output #%1%: expected " |
| 484 | "%2% elements, got %3%") % outputIndex % expectedOutputDataSize % actualOutputDataSize)); |
| 485 | } |
| 486 | }, |
| 487 | outputContainers[i]); |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 488 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 489 | |
| 490 | std::shared_ptr<armnn::IProfiler> profiler = m_Runtime->GetProfiler(m_NetworkIdentifier); |
| 491 | if (profiler) |
| 492 | { |
| 493 | profiler->EnableProfiling(m_EnableProfiling); |
| 494 | } |
| 495 | |
James Conroy | 7b4886f | 2019-04-11 10:23:58 +0100 | [diff] [blame] | 496 | // Start timer to record inference time in EnqueueWorkload (in milliseconds) |
| 497 | const auto start_time = GetCurrentTime(); |
| 498 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 499 | armnn::Status ret = m_Runtime->EnqueueWorkload(m_NetworkIdentifier, |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 500 | MakeInputTensors(inputContainers), |
| 501 | MakeOutputTensors(outputContainers)); |
Sadik Armagan | 2b7a158 | 2018-09-05 16:33:58 +0100 | [diff] [blame] | 502 | |
James Conroy | 7b4886f | 2019-04-11 10:23:58 +0100 | [diff] [blame] | 503 | const auto end_time = GetCurrentTime(); |
| 504 | |
Sadik Armagan | 2b7a158 | 2018-09-05 16:33:58 +0100 | [diff] [blame] | 505 | // if profiling is enabled print out the results |
| 506 | if (profiler && profiler->IsProfilingEnabled()) |
| 507 | { |
| 508 | profiler->Print(std::cout); |
| 509 | } |
| 510 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 511 | if (ret == armnn::Status::Failure) |
| 512 | { |
| 513 | throw armnn::Exception("IRuntime::EnqueueWorkload failed"); |
| 514 | } |
James Conroy | 7b4886f | 2019-04-11 10:23:58 +0100 | [diff] [blame] | 515 | else |
| 516 | { |
| 517 | return std::chrono::duration<double, std::milli>(end_time - start_time); |
| 518 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Jim Flynn | b4d7eae | 2019-05-01 14:44:27 +0100 | [diff] [blame] | 521 | const armnn::BindingPointInfo& GetInputBindingInfo(unsigned int inputIndex = 0u) const |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 522 | { |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 523 | CheckInputIndexIsValid(inputIndex); |
| 524 | return m_InputBindings[inputIndex]; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 525 | } |
| 526 | |
Jim Flynn | b4d7eae | 2019-05-01 14:44:27 +0100 | [diff] [blame] | 527 | const std::vector<armnn::BindingPointInfo>& GetInputBindingInfos() const |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 528 | { |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 529 | return m_InputBindings; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 530 | } |
| 531 | |
Jim Flynn | b4d7eae | 2019-05-01 14:44:27 +0100 | [diff] [blame] | 532 | const armnn::BindingPointInfo& GetOutputBindingInfo(unsigned int outputIndex = 0u) const |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 533 | { |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 534 | CheckOutputIndexIsValid(outputIndex); |
| 535 | return m_OutputBindings[outputIndex]; |
| 536 | } |
| 537 | |
Jim Flynn | b4d7eae | 2019-05-01 14:44:27 +0100 | [diff] [blame] | 538 | const std::vector<armnn::BindingPointInfo>& GetOutputBindingInfos() const |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 539 | { |
| 540 | return m_OutputBindings; |
| 541 | } |
| 542 | |
| 543 | QuantizationParams GetQuantizationParams(unsigned int outputIndex = 0u) const |
| 544 | { |
| 545 | CheckOutputIndexIsValid(outputIndex); |
| 546 | return std::make_pair(m_OutputBindings[outputIndex].second.GetQuantizationScale(), |
| 547 | m_OutputBindings[outputIndex].second.GetQuantizationOffset()); |
| 548 | } |
| 549 | |
Narumol Prangnawarat | 4628d05 | 2019-02-25 17:26:05 +0000 | [diff] [blame] | 550 | QuantizationParams GetInputQuantizationParams(unsigned int inputIndex = 0u) const |
| 551 | { |
| 552 | CheckInputIndexIsValid(inputIndex); |
| 553 | return std::make_pair(m_InputBindings[inputIndex].second.GetQuantizationScale(), |
| 554 | m_InputBindings[inputIndex].second.GetQuantizationOffset()); |
| 555 | } |
| 556 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 557 | std::vector<QuantizationParams> GetAllQuantizationParams() const |
| 558 | { |
| 559 | std::vector<QuantizationParams> quantizationParams; |
| 560 | for (unsigned int i = 0u; i < m_OutputBindings.size(); i++) |
| 561 | { |
| 562 | quantizationParams.push_back(GetQuantizationParams(i)); |
| 563 | } |
| 564 | return quantizationParams; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 565 | } |
| 566 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 567 | private: |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 568 | armnn::NetworkId m_NetworkIdentifier; |
| 569 | std::shared_ptr<armnn::IRuntime> m_Runtime; |
| 570 | |
Jim Flynn | b4d7eae | 2019-05-01 14:44:27 +0100 | [diff] [blame] | 571 | std::vector<armnn::BindingPointInfo> m_InputBindings; |
| 572 | std::vector<armnn::BindingPointInfo> m_OutputBindings; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 573 | bool m_EnableProfiling; |
Matteo Martincigh | 00dda4a | 2019-08-14 11:42:30 +0100 | [diff] [blame] | 574 | std::string m_DynamicBackendsPath; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 575 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 576 | template<typename TContainer> |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 577 | armnn::InputTensors MakeInputTensors(const std::vector<TContainer>& inputDataContainers) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 578 | { |
Jim Flynn | 2fd6100 | 2019-05-03 12:54:26 +0100 | [diff] [blame] | 579 | return armnnUtils::MakeInputTensors(m_InputBindings, inputDataContainers); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | template<typename TContainer> |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 583 | armnn::OutputTensors MakeOutputTensors(std::vector<TContainer>& outputDataContainers) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 584 | { |
Jim Flynn | 2fd6100 | 2019-05-03 12:54:26 +0100 | [diff] [blame] | 585 | return armnnUtils::MakeOutputTensors(m_OutputBindings, outputDataContainers); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 586 | } |
James Conroy | 7b4886f | 2019-04-11 10:23:58 +0100 | [diff] [blame] | 587 | |
| 588 | std::chrono::high_resolution_clock::time_point GetCurrentTime() |
| 589 | { |
| 590 | return std::chrono::high_resolution_clock::now(); |
| 591 | } |
| 592 | |
| 593 | std::chrono::duration<double, std::milli> GetTimeDuration( |
| 594 | std::chrono::high_resolution_clock::time_point& start_time, |
| 595 | std::chrono::high_resolution_clock::time_point& end_time) |
| 596 | { |
| 597 | return std::chrono::duration<double, std::milli>(end_time - start_time); |
| 598 | } |
| 599 | |
Ferran Balaguer | c602f29 | 2019-02-08 17:09:55 +0000 | [diff] [blame] | 600 | }; |