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