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