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) |
| 9 | #include "armnnDeserializeParser/IDeserializeParser.hpp" |
| 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 <> |
| 168 | struct CreateNetworkImpl<armnnDeserializeParser::IDeserializeParser> |
| 169 | { |
| 170 | public: |
| 171 | using IParser = armnnDeserializeParser::IDeserializeParser; |
| 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"); |
| 186 | const std::string& modelPath = params.m_ModelPath; |
| 187 | network = parser->CreateNetworkFromBinaryFile(modelPath.c_str()); |
| 188 | } |
| 189 | |
| 190 | unsigned int subGraphId = boost::numeric_cast<unsigned int>(params.m_SubgraphId); |
| 191 | |
| 192 | for (const std::string& inputLayerName : params.m_InputBindings) |
| 193 | { |
| 194 | BindingPointInfo inputBinding = parser->GetNetworkInputBindingInfo(subGraphId, inputLayerName); |
| 195 | inputBindings.push_back(inputBinding); |
| 196 | } |
| 197 | |
| 198 | for (const std::string& outputLayerName : params.m_OutputBindings) |
| 199 | { |
| 200 | BindingPointInfo outputBinding = parser->GetNetworkOutputBindingInfo(subGraphId, outputLayerName); |
| 201 | outputBindings.push_back(outputBinding); |
| 202 | } |
| 203 | |
| 204 | return network; |
| 205 | } |
| 206 | }; |
| 207 | #endif |
| 208 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 209 | #if defined(ARMNN_TF_LITE_PARSER) |
| 210 | template <> |
| 211 | struct CreateNetworkImpl<armnnTfLiteParser::ITfLiteParser> |
| 212 | { |
| 213 | public: |
| 214 | using IParser = armnnTfLiteParser::ITfLiteParser; |
| 215 | using Params = InferenceModelInternal::Params; |
| 216 | using BindingPointInfo = InferenceModelInternal::BindingPointInfo; |
| 217 | |
| 218 | static armnn::INetworkPtr Create(const Params& params, |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 219 | std::vector<BindingPointInfo>& inputBindings, |
| 220 | std::vector<BindingPointInfo>& outputBindings) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 221 | { |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 222 | const std::string& modelPath = params.m_ModelPath; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 223 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 224 | // Create a network from a file on disk |
| 225 | auto parser(IParser::Create()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 226 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 227 | armnn::INetworkPtr network{nullptr, [](armnn::INetwork *){}}; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 228 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 229 | { |
| 230 | ARMNN_SCOPED_HEAP_PROFILING("Parsing"); |
| 231 | network = parser->CreateNetworkFromBinaryFile(modelPath.c_str()); |
| 232 | } |
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 | for (const std::string& inputLayerName : params.m_InputBindings) |
| 235 | { |
| 236 | BindingPointInfo inputBinding = |
| 237 | parser->GetNetworkInputBindingInfo(params.m_SubgraphId, inputLayerName); |
| 238 | inputBindings.push_back(inputBinding); |
| 239 | } |
| 240 | |
| 241 | for (const std::string& outputLayerName : params.m_OutputBindings) |
| 242 | { |
| 243 | BindingPointInfo outputBinding = |
| 244 | parser->GetNetworkOutputBindingInfo(params.m_SubgraphId, outputLayerName); |
| 245 | outputBindings.push_back(outputBinding); |
| 246 | } |
| 247 | |
| 248 | return network; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 249 | } |
| 250 | }; |
| 251 | #endif |
| 252 | |
| 253 | #if defined(ARMNN_ONNX_PARSER) |
| 254 | template <> |
| 255 | struct CreateNetworkImpl<armnnOnnxParser::IOnnxParser> |
| 256 | { |
| 257 | public: |
| 258 | using IParser = armnnOnnxParser::IOnnxParser; |
| 259 | using Params = InferenceModelInternal::Params; |
| 260 | using BindingPointInfo = InferenceModelInternal::BindingPointInfo; |
| 261 | |
| 262 | static armnn::INetworkPtr Create(const Params& params, |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 263 | std::vector<BindingPointInfo>& inputBindings, |
| 264 | std::vector<BindingPointInfo>& outputBindings) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 265 | { |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 266 | const std::string& modelPath = params.m_ModelPath; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 267 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 268 | // Create a network from a file on disk |
| 269 | auto parser(IParser::Create()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 270 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 271 | armnn::INetworkPtr network{nullptr, [](armnn::INetwork *){}}; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 272 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 273 | { |
| 274 | ARMNN_SCOPED_HEAP_PROFILING("Parsing"); |
| 275 | network = (params.m_IsModelBinary ? |
| 276 | parser->CreateNetworkFromBinaryFile(modelPath.c_str()) : |
| 277 | parser->CreateNetworkFromTextFile(modelPath.c_str())); |
| 278 | } |
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 | for (const std::string& inputLayerName : params.m_InputBindings) |
| 281 | { |
| 282 | BindingPointInfo inputBinding = parser->GetNetworkInputBindingInfo(inputLayerName); |
| 283 | inputBindings.push_back(inputBinding); |
| 284 | } |
| 285 | |
| 286 | for (const std::string& outputLayerName : params.m_OutputBindings) |
| 287 | { |
| 288 | BindingPointInfo outputBinding = parser->GetNetworkOutputBindingInfo(outputLayerName); |
| 289 | outputBindings.push_back(outputBinding); |
| 290 | } |
| 291 | |
| 292 | return network; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 293 | } |
| 294 | }; |
| 295 | #endif |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 296 | |
| 297 | template<typename TContainer> |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 298 | inline armnn::InputTensors MakeInputTensors( |
| 299 | const std::vector<InferenceModelInternal::BindingPointInfo>& inputBindings, |
| 300 | const std::vector<TContainer>& inputDataContainers) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 301 | { |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 302 | armnn::InputTensors inputTensors; |
| 303 | |
| 304 | const size_t numInputs = inputBindings.size(); |
| 305 | if (numInputs != inputDataContainers.size()) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 306 | { |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 307 | throw armnn::Exception(boost::str(boost::format("Number of inputs does not match number of " |
| 308 | "tensor data containers: %1% != %2%") % numInputs % inputDataContainers.size())); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 309 | } |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 310 | |
| 311 | for (size_t i = 0; i < numInputs; i++) |
| 312 | { |
| 313 | const InferenceModelInternal::BindingPointInfo& inputBinding = inputBindings[i]; |
| 314 | const TContainer& inputData = inputDataContainers[i]; |
| 315 | |
Ferran Balaguer | c602f29 | 2019-02-08 17:09:55 +0000 | [diff] [blame] | 316 | boost::apply_visitor([&](auto&& value) |
| 317 | { |
| 318 | if (value.size() != inputBinding.second.GetNumElements()) |
| 319 | { |
| 320 | throw armnn::Exception("Input tensor has incorrect size"); |
| 321 | } |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 322 | |
Ferran Balaguer | c602f29 | 2019-02-08 17:09:55 +0000 | [diff] [blame] | 323 | armnn::ConstTensor inputTensor(inputBinding.second, value.data()); |
| 324 | inputTensors.push_back(std::make_pair(inputBinding.first, inputTensor)); |
| 325 | }, |
| 326 | inputData); |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | return inputTensors; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | template<typename TContainer> |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 333 | inline armnn::OutputTensors MakeOutputTensors( |
| 334 | const std::vector<InferenceModelInternal::BindingPointInfo>& outputBindings, |
| 335 | std::vector<TContainer>& outputDataContainers) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 336 | { |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 337 | armnn::OutputTensors outputTensors; |
| 338 | |
| 339 | const size_t numOutputs = outputBindings.size(); |
| 340 | if (numOutputs != outputDataContainers.size()) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 341 | { |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 342 | throw armnn::Exception(boost::str(boost::format("Number of outputs does not match number of " |
| 343 | "tensor data containers: %1% != %2%") % numOutputs % outputDataContainers.size())); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 344 | } |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 345 | |
| 346 | for (size_t i = 0; i < numOutputs; i++) |
| 347 | { |
| 348 | const InferenceModelInternal::BindingPointInfo& outputBinding = outputBindings[i]; |
| 349 | TContainer& outputData = outputDataContainers[i]; |
| 350 | |
Ferran Balaguer | c602f29 | 2019-02-08 17:09:55 +0000 | [diff] [blame] | 351 | boost::apply_visitor([&](auto&& value) |
| 352 | { |
| 353 | if (value.size() != outputBinding.second.GetNumElements()) |
| 354 | { |
| 355 | throw armnn::Exception("Output tensor has incorrect size"); |
| 356 | } |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 357 | |
Ferran Balaguer | c602f29 | 2019-02-08 17:09:55 +0000 | [diff] [blame] | 358 | armnn::Tensor outputTensor(outputBinding.second, value.data()); |
| 359 | outputTensors.push_back(std::make_pair(outputBinding.first, outputTensor)); |
| 360 | }, |
| 361 | outputData); |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | return outputTensors; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | template <typename IParser, typename TDataType> |
| 368 | class InferenceModel |
| 369 | { |
| 370 | public: |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 371 | using DataType = TDataType; |
| 372 | using Params = InferenceModelInternal::Params; |
| 373 | using BindingPointInfo = InferenceModelInternal::BindingPointInfo; |
| 374 | using QuantizationParams = InferenceModelInternal::QuantizationParams; |
Ferran Balaguer | c602f29 | 2019-02-08 17:09:55 +0000 | [diff] [blame] | 375 | 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] | 376 | |
| 377 | struct CommandLineOptions |
| 378 | { |
| 379 | std::string m_ModelDir; |
Aron Virginas-Tar | 339bcae | 2019-01-31 16:44:26 +0000 | [diff] [blame] | 380 | std::vector<std::string> m_ComputeDevices; |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 381 | bool m_VisualizePostOptimizationModel; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 382 | bool m_EnableFp16TurboMode; |
Aron Virginas-Tar | 339bcae | 2019-01-31 16:44:26 +0000 | [diff] [blame] | 383 | |
| 384 | std::vector<armnn::BackendId> GetComputeDevicesAsBackendIds() |
| 385 | { |
| 386 | std::vector<armnn::BackendId> backendIds; |
| 387 | std::copy(m_ComputeDevices.begin(), m_ComputeDevices.end(), std::back_inserter(backendIds)); |
| 388 | return backendIds; |
| 389 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 390 | }; |
| 391 | |
| 392 | static void AddCommandLineOptions(boost::program_options::options_description& desc, CommandLineOptions& options) |
| 393 | { |
| 394 | namespace po = boost::program_options; |
| 395 | |
Aron Virginas-Tar | 339bcae | 2019-01-31 16:44:26 +0000 | [diff] [blame] | 396 | const std::vector<std::string> defaultComputes = { "CpuAcc", "CpuRef" }; |
David Beck | f0b4845 | 2018-10-19 15:20:56 +0100 | [diff] [blame] | 397 | |
Aron Virginas-Tar | 5cc8e56 | 2018-10-23 15:14:46 +0100 | [diff] [blame] | 398 | const std::string backendsMessage = "Which device to run layers on by default. Possible choices: " |
| 399 | + armnn::BackendRegistryInstance().GetBackendIdsAsString(); |
| 400 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 401 | desc.add_options() |
| 402 | ("model-dir,m", po::value<std::string>(&options.m_ModelDir)->required(), |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 403 | "Path to directory containing model files (.caffemodel/.prototxt/.tflite)") |
Aron Virginas-Tar | 339bcae | 2019-01-31 16:44:26 +0000 | [diff] [blame] | 404 | ("compute,c", po::value<std::vector<std::string>>(&options.m_ComputeDevices)-> |
| 405 | default_value(defaultComputes, boost::algorithm::join(defaultComputes, ", "))-> |
| 406 | multitoken(), backendsMessage.c_str()) |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 407 | ("visualize-optimized-model,v", |
| 408 | po::value<bool>(&options.m_VisualizePostOptimizationModel)->default_value(false), |
| 409 | "Produce a dot file useful for visualizing the graph post optimization." |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 410 | "The file will have the same name as the model with the .dot extention.") |
| 411 | ("fp16-turbo-mode", po::value<bool>(&options.m_EnableFp16TurboMode)->default_value(false), |
| 412 | "If this option is enabled FP32 layers, weights and biases will be converted " |
| 413 | "to FP16 where the backend supports it."); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 414 | } |
| 415 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 416 | InferenceModel(const Params& params, const std::shared_ptr<armnn::IRuntime>& runtime = nullptr) |
| 417 | : m_EnableProfiling(params.m_EnableProfiling) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 418 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 419 | if (runtime) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 420 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 421 | m_Runtime = runtime; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 422 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 423 | else |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 424 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 425 | armnn::IRuntime::CreationOptions options; |
Nina Drozd | 549ae37 | 2018-09-10 14:26:44 +0100 | [diff] [blame] | 426 | options.m_EnableGpuProfiling = m_EnableProfiling; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 427 | m_Runtime = std::move(armnn::IRuntime::Create(options)); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 428 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 429 | |
Aron Virginas-Tar | 5cc8e56 | 2018-10-23 15:14:46 +0100 | [diff] [blame] | 430 | std::string invalidBackends; |
Aron Virginas-Tar | 339bcae | 2019-01-31 16:44:26 +0000 | [diff] [blame] | 431 | if (!CheckRequestedBackendsAreValid(params.m_ComputeDevices, armnn::Optional<std::string&>(invalidBackends))) |
Aron Virginas-Tar | 5cc8e56 | 2018-10-23 15:14:46 +0100 | [diff] [blame] | 432 | { |
| 433 | throw armnn::Exception("Some backend IDs are invalid: " + invalidBackends); |
| 434 | } |
| 435 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 436 | armnn::INetworkPtr network = |
| 437 | CreateNetworkImpl<IParser>::Create(params, m_InputBindings, m_OutputBindings); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 438 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 439 | armnn::IOptimizedNetworkPtr optNet{nullptr, [](armnn::IOptimizedNetwork *){}}; |
| 440 | { |
| 441 | ARMNN_SCOPED_HEAP_PROFILING("Optimizing"); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 442 | |
| 443 | armnn::OptimizerOptions options; |
| 444 | options.m_ReduceFp32ToFp16 = params.m_EnableFp16TurboMode; |
| 445 | |
Aron Virginas-Tar | 339bcae | 2019-01-31 16:44:26 +0000 | [diff] [blame] | 446 | optNet = armnn::Optimize(*network, params.m_ComputeDevices, m_Runtime->GetDeviceSpec(), options); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 447 | if (!optNet) |
| 448 | { |
| 449 | throw armnn::Exception("Optimize returned nullptr"); |
| 450 | } |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 451 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 452 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 453 | if (params.m_VisualizePostOptimizationModel) |
| 454 | { |
| 455 | boost::filesystem::path filename = params.m_ModelPath; |
| 456 | filename.replace_extension("dot"); |
| 457 | std::fstream file(filename.c_str(),file.out); |
| 458 | optNet->SerializeToDot(file); |
| 459 | } |
| 460 | |
| 461 | armnn::Status ret; |
| 462 | { |
| 463 | ARMNN_SCOPED_HEAP_PROFILING("LoadNetwork"); |
| 464 | ret = m_Runtime->LoadNetwork(m_NetworkIdentifier, std::move(optNet)); |
| 465 | } |
| 466 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 467 | if (ret == armnn::Status::Failure) |
| 468 | { |
| 469 | throw armnn::Exception("IRuntime::LoadNetwork failed"); |
| 470 | } |
| 471 | } |
| 472 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 473 | void CheckInputIndexIsValid(unsigned int inputIndex) const |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 474 | { |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 475 | if (m_InputBindings.size() < inputIndex + 1) |
| 476 | { |
| 477 | throw armnn::Exception(boost::str(boost::format("Input index out of range: %1%") % inputIndex)); |
| 478 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 481 | void CheckOutputIndexIsValid(unsigned int outputIndex) const |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 482 | { |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 483 | if (m_OutputBindings.size() < outputIndex + 1) |
| 484 | { |
| 485 | throw armnn::Exception(boost::str(boost::format("Output index out of range: %1%") % outputIndex)); |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | unsigned int GetOutputSize(unsigned int outputIndex = 0u) const |
| 490 | { |
| 491 | CheckOutputIndexIsValid(outputIndex); |
| 492 | return m_OutputBindings[outputIndex].second.GetNumElements(); |
| 493 | } |
| 494 | |
| 495 | void Run(const std::vector<TContainer>& inputContainers, std::vector<TContainer>& outputContainers) |
| 496 | { |
Ferran Balaguer | c602f29 | 2019-02-08 17:09:55 +0000 | [diff] [blame] | 497 | for (unsigned int i = 0; i < outputContainers.size(); ++i) |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 498 | { |
| 499 | const unsigned int expectedOutputDataSize = GetOutputSize(i); |
Ferran Balaguer | c602f29 | 2019-02-08 17:09:55 +0000 | [diff] [blame] | 500 | |
| 501 | boost::apply_visitor([expectedOutputDataSize, i](auto&& value) |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 502 | { |
Ferran Balaguer | c602f29 | 2019-02-08 17:09:55 +0000 | [diff] [blame] | 503 | const unsigned int actualOutputDataSize = boost::numeric_cast<unsigned int>(value.size()); |
| 504 | if (actualOutputDataSize < expectedOutputDataSize) |
| 505 | { |
| 506 | unsigned int outputIndex = boost::numeric_cast<unsigned int>(i); |
| 507 | throw armnn::Exception( |
| 508 | boost::str(boost::format("Not enough data for output #%1%: expected " |
| 509 | "%2% elements, got %3%") % outputIndex % expectedOutputDataSize % actualOutputDataSize)); |
| 510 | } |
| 511 | }, |
| 512 | outputContainers[i]); |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 513 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 514 | |
| 515 | std::shared_ptr<armnn::IProfiler> profiler = m_Runtime->GetProfiler(m_NetworkIdentifier); |
| 516 | if (profiler) |
| 517 | { |
| 518 | profiler->EnableProfiling(m_EnableProfiling); |
| 519 | } |
| 520 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 521 | armnn::Status ret = m_Runtime->EnqueueWorkload(m_NetworkIdentifier, |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 522 | MakeInputTensors(inputContainers), |
| 523 | MakeOutputTensors(outputContainers)); |
Sadik Armagan | 2b7a158 | 2018-09-05 16:33:58 +0100 | [diff] [blame] | 524 | |
| 525 | // if profiling is enabled print out the results |
| 526 | if (profiler && profiler->IsProfilingEnabled()) |
| 527 | { |
| 528 | profiler->Print(std::cout); |
| 529 | } |
| 530 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 531 | if (ret == armnn::Status::Failure) |
| 532 | { |
| 533 | throw armnn::Exception("IRuntime::EnqueueWorkload failed"); |
| 534 | } |
| 535 | } |
| 536 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 537 | const BindingPointInfo& GetInputBindingInfo(unsigned int inputIndex = 0u) const |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 538 | { |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 539 | CheckInputIndexIsValid(inputIndex); |
| 540 | return m_InputBindings[inputIndex]; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 541 | } |
| 542 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 543 | const std::vector<BindingPointInfo>& GetInputBindingInfos() const |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 544 | { |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 545 | return m_InputBindings; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 546 | } |
| 547 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 548 | const BindingPointInfo& GetOutputBindingInfo(unsigned int outputIndex = 0u) const |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 549 | { |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 550 | CheckOutputIndexIsValid(outputIndex); |
| 551 | return m_OutputBindings[outputIndex]; |
| 552 | } |
| 553 | |
| 554 | const std::vector<BindingPointInfo>& GetOutputBindingInfos() const |
| 555 | { |
| 556 | return m_OutputBindings; |
| 557 | } |
| 558 | |
| 559 | QuantizationParams GetQuantizationParams(unsigned int outputIndex = 0u) const |
| 560 | { |
| 561 | CheckOutputIndexIsValid(outputIndex); |
| 562 | return std::make_pair(m_OutputBindings[outputIndex].second.GetQuantizationScale(), |
| 563 | m_OutputBindings[outputIndex].second.GetQuantizationOffset()); |
| 564 | } |
| 565 | |
| 566 | std::vector<QuantizationParams> GetAllQuantizationParams() const |
| 567 | { |
| 568 | std::vector<QuantizationParams> quantizationParams; |
| 569 | for (unsigned int i = 0u; i < m_OutputBindings.size(); i++) |
| 570 | { |
| 571 | quantizationParams.push_back(GetQuantizationParams(i)); |
| 572 | } |
| 573 | return quantizationParams; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 574 | } |
| 575 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 576 | private: |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 577 | armnn::NetworkId m_NetworkIdentifier; |
| 578 | std::shared_ptr<armnn::IRuntime> m_Runtime; |
| 579 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 580 | std::vector<InferenceModelInternal::BindingPointInfo> m_InputBindings; |
| 581 | std::vector<InferenceModelInternal::BindingPointInfo> m_OutputBindings; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 582 | bool m_EnableProfiling; |
| 583 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 584 | template<typename TContainer> |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 585 | armnn::InputTensors MakeInputTensors(const std::vector<TContainer>& inputDataContainers) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 586 | { |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 587 | return ::MakeInputTensors(m_InputBindings, inputDataContainers); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | template<typename TContainer> |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 591 | armnn::OutputTensors MakeOutputTensors(std::vector<TContainer>& outputDataContainers) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 592 | { |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 593 | return ::MakeOutputTensors(m_OutputBindings, outputDataContainers); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 594 | } |
Ferran Balaguer | c602f29 | 2019-02-08 17:09:55 +0000 | [diff] [blame] | 595 | }; |