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