telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1 | // |
Sadik Armagan | a9c2ce1 | 2020-07-14 10:02:22 +0100 | [diff] [blame] | 2 | // Copyright © 2017 Arm Ltd and Contributors. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 4 | // |
Matteo Martincigh | c601aa6 | 2019-10-29 15:03:22 +0000 | [diff] [blame] | 5 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 6 | #pragma once |
Matteo Martincigh | c601aa6 | 2019-10-29 15:03:22 +0000 | [diff] [blame] | 7 | |
David Beck | f0b4845 | 2018-10-19 15:20:56 +0100 | [diff] [blame] | 8 | #include <armnn/ArmNN.hpp> |
alered01 | a7227ac | 2020-05-07 14:58:29 +0100 | [diff] [blame] | 9 | #include <armnn/Logging.hpp> |
| 10 | #include <armnn/utility/Timer.hpp> |
Matteo Martincigh | c601aa6 | 2019-10-29 15:03:22 +0000 | [diff] [blame] | 11 | #include <armnn/BackendRegistry.hpp> |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 12 | #include <armnn/utility/Assert.hpp> |
Matthew Sloyan | 80c6b14 | 2020-09-08 12:00:32 +0100 | [diff] [blame] | 13 | #include <armnn/utility/NumericCast.hpp> |
Nikhil Raj | 7dcc697 | 2021-04-30 15:44:24 +0100 | [diff] [blame] | 14 | #include <common/include/ProfilingGuid.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 15 | |
Aron Virginas-Tar | 64e4ccb | 2019-02-12 11:27:53 +0000 | [diff] [blame] | 16 | #if defined(ARMNN_SERIALIZER) |
Derek Lamberti | 0028d1b | 2019-02-20 13:57:42 +0000 | [diff] [blame] | 17 | #include "armnnDeserializer/IDeserializer.hpp" |
Aron Virginas-Tar | 64e4ccb | 2019-02-12 11:27:53 +0000 | [diff] [blame] | 18 | #endif |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 19 | #if defined(ARMNN_TF_LITE_PARSER) |
David Beck | f0b4845 | 2018-10-19 15:20:56 +0100 | [diff] [blame] | 20 | #include <armnnTfLiteParser/ITfLiteParser.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 21 | #endif |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 22 | #if defined(ARMNN_ONNX_PARSER) |
David Beck | f0b4845 | 2018-10-19 15:20:56 +0100 | [diff] [blame] | 23 | #include <armnnOnnxParser/IOnnxParser.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 24 | #endif |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 25 | |
Francis Murtagh | 532a29d | 2020-06-29 11:50:01 +0100 | [diff] [blame] | 26 | #include <Filesystem.hpp> |
Aron Virginas-Tar | 64e4ccb | 2019-02-12 11:27:53 +0000 | [diff] [blame] | 27 | #include <HeapProfiling.hpp> |
Jim Flynn | 2fd6100 | 2019-05-03 12:54:26 +0100 | [diff] [blame] | 28 | #include <TensorIOUtils.hpp> |
Aron Virginas-Tar | 64e4ccb | 2019-02-12 11:27:53 +0000 | [diff] [blame] | 29 | |
David Monahan | a8837bf | 2020-04-16 10:01:56 +0100 | [diff] [blame] | 30 | #include "armnn/utility/StringUtils.hpp" |
James Ward | c89829f | 2020-10-12 14:17:36 +0100 | [diff] [blame] | 31 | #include <cxxopts/cxxopts.hpp> |
| 32 | #include "CxxoptsUtils.hpp" |
James Ward | 08f4016 | 2020-09-07 16:45:07 +0100 | [diff] [blame] | 33 | #include <fmt/format.h> |
James Ward | 6d9f5c5 | 2020-09-28 11:56:35 +0100 | [diff] [blame] | 34 | #include <mapbox/variant.hpp> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 35 | |
Aron Virginas-Tar | 339bcae | 2019-01-31 16:44:26 +0000 | [diff] [blame] | 36 | #include <algorithm> |
| 37 | #include <iterator> |
Aron Virginas-Tar | 5cc8e56 | 2018-10-23 15:14:46 +0100 | [diff] [blame] | 38 | #include <fstream> |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 39 | #include <map> |
| 40 | #include <string> |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 41 | #include <vector> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 42 | #include <type_traits> |
| 43 | |
Aron Virginas-Tar | 5cc8e56 | 2018-10-23 15:14:46 +0100 | [diff] [blame] | 44 | namespace |
| 45 | { |
| 46 | |
| 47 | inline bool CheckRequestedBackendsAreValid(const std::vector<armnn::BackendId>& backendIds, |
| 48 | armnn::Optional<std::string&> invalidBackendIds = armnn::EmptyOptional()) |
| 49 | { |
| 50 | if (backendIds.empty()) |
| 51 | { |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | armnn::BackendIdSet validBackendIds = armnn::BackendRegistryInstance().GetBackendIds(); |
| 56 | |
| 57 | bool allValid = true; |
| 58 | for (const auto& backendId : backendIds) |
| 59 | { |
| 60 | if (std::find(validBackendIds.begin(), validBackendIds.end(), backendId) == validBackendIds.end()) |
| 61 | { |
| 62 | allValid = false; |
| 63 | if (invalidBackendIds) |
| 64 | { |
| 65 | if (!invalidBackendIds.value().empty()) |
| 66 | { |
| 67 | invalidBackendIds.value() += ", "; |
| 68 | } |
| 69 | invalidBackendIds.value() += backendId; |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | return allValid; |
| 74 | } |
| 75 | |
| 76 | } // anonymous namespace |
| 77 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 78 | namespace InferenceModelInternal |
| 79 | { |
Jim Flynn | b4d7eae | 2019-05-01 14:44:27 +0100 | [diff] [blame] | 80 | using BindingPointInfo = armnn::BindingPointInfo; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 81 | |
| 82 | using QuantizationParams = std::pair<float,int32_t>; |
| 83 | |
| 84 | struct Params |
| 85 | { |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 86 | std::string m_ModelPath; |
| 87 | std::vector<std::string> m_InputBindings; |
| 88 | std::vector<armnn::TensorShape> m_InputShapes; |
| 89 | std::vector<std::string> m_OutputBindings; |
Aron Virginas-Tar | 339bcae | 2019-01-31 16:44:26 +0000 | [diff] [blame] | 90 | std::vector<armnn::BackendId> m_ComputeDevices; |
Matteo Martincigh | 00dda4a | 2019-08-14 11:42:30 +0100 | [diff] [blame] | 91 | std::string m_DynamicBackendsPath; |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 92 | size_t m_SubgraphId; |
| 93 | bool m_IsModelBinary; |
| 94 | bool m_VisualizePostOptimizationModel; |
| 95 | bool m_EnableFp16TurboMode; |
Narumol Prangnawarat | d8cc811 | 2020-03-24 13:54:05 +0000 | [diff] [blame] | 96 | bool m_EnableBf16TurboMode; |
Matthew Jackson | 54658b9 | 2019-08-27 15:35:59 +0100 | [diff] [blame] | 97 | bool m_PrintIntermediateLayers; |
Derek Lamberti | 132563c | 2019-12-02 16:06:40 +0000 | [diff] [blame] | 98 | bool m_ParseUnsupported; |
Sadik Armagan | a9c2ce1 | 2020-07-14 10:02:22 +0100 | [diff] [blame] | 99 | bool m_InferOutputShape; |
Sadik Armagan | a25886e | 2020-09-15 17:17:08 +0100 | [diff] [blame] | 100 | bool m_EnableFastMath; |
Matthew Sloyan | 4243211 | 2021-01-08 10:30:51 +0000 | [diff] [blame] | 101 | bool m_SaveCachedNetwork; |
| 102 | std::string m_CachedNetworkFilePath; |
Matthew Sloyan | 0a7dc6b | 2021-02-10 16:50:53 +0000 | [diff] [blame] | 103 | unsigned int m_NumberOfThreads; |
Finn Williams | 4064632 | 2021-02-11 16:16:42 +0000 | [diff] [blame] | 104 | std::string m_MLGOTuningFilePath; |
Sadik Armagan | a04a9d7 | 2021-04-27 10:02:10 +0100 | [diff] [blame] | 105 | bool m_AsyncEnabled; |
Kevin May | b4b3ac9 | 2021-05-21 16:42:21 +0100 | [diff] [blame] | 106 | size_t m_ThreadPoolSize; |
Finn Williams | 4064632 | 2021-02-11 16:16:42 +0000 | [diff] [blame] | 107 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 108 | |
| 109 | Params() |
Matteo Martincigh | 00dda4a | 2019-08-14 11:42:30 +0100 | [diff] [blame] | 110 | : m_ComputeDevices{} |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 111 | , m_SubgraphId(0) |
| 112 | , m_IsModelBinary(true) |
| 113 | , m_VisualizePostOptimizationModel(false) |
| 114 | , m_EnableFp16TurboMode(false) |
Narumol Prangnawarat | d8cc811 | 2020-03-24 13:54:05 +0000 | [diff] [blame] | 115 | , m_EnableBf16TurboMode(false) |
Matthew Jackson | 54658b9 | 2019-08-27 15:35:59 +0100 | [diff] [blame] | 116 | , m_PrintIntermediateLayers(false) |
Derek Lamberti | 132563c | 2019-12-02 16:06:40 +0000 | [diff] [blame] | 117 | , m_ParseUnsupported(false) |
Sadik Armagan | a9c2ce1 | 2020-07-14 10:02:22 +0100 | [diff] [blame] | 118 | , m_InferOutputShape(false) |
Sadik Armagan | a25886e | 2020-09-15 17:17:08 +0100 | [diff] [blame] | 119 | , m_EnableFastMath(false) |
Matthew Sloyan | 4243211 | 2021-01-08 10:30:51 +0000 | [diff] [blame] | 120 | , m_SaveCachedNetwork(false) |
| 121 | , m_CachedNetworkFilePath("") |
Matthew Sloyan | 0a7dc6b | 2021-02-10 16:50:53 +0000 | [diff] [blame] | 122 | , m_NumberOfThreads(0) |
Finn Williams | 4064632 | 2021-02-11 16:16:42 +0000 | [diff] [blame] | 123 | , m_MLGOTuningFilePath("") |
Sadik Armagan | a04a9d7 | 2021-04-27 10:02:10 +0100 | [diff] [blame] | 124 | , m_AsyncEnabled(false) |
Kevin May | 94dd4db | 2021-05-26 16:01:08 +0100 | [diff] [blame] | 125 | , m_ThreadPoolSize(0) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 126 | {} |
| 127 | }; |
| 128 | |
| 129 | } // namespace InferenceModelInternal |
| 130 | |
| 131 | template <typename IParser> |
| 132 | struct CreateNetworkImpl |
| 133 | { |
| 134 | public: |
| 135 | using Params = InferenceModelInternal::Params; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 136 | |
| 137 | static armnn::INetworkPtr Create(const Params& params, |
Jim Flynn | b4d7eae | 2019-05-01 14:44:27 +0100 | [diff] [blame] | 138 | std::vector<armnn::BindingPointInfo>& inputBindings, |
| 139 | std::vector<armnn::BindingPointInfo>& outputBindings) |
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 | const std::string& modelPath = params.m_ModelPath; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 142 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 143 | // Create a network from a file on disk |
| 144 | auto parser(IParser::Create()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 145 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 146 | std::map<std::string, armnn::TensorShape> inputShapes; |
| 147 | if (!params.m_InputShapes.empty()) |
| 148 | { |
| 149 | const size_t numInputShapes = params.m_InputShapes.size(); |
| 150 | const size_t numInputBindings = params.m_InputBindings.size(); |
| 151 | if (numInputShapes < numInputBindings) |
| 152 | { |
James Ward | 08f4016 | 2020-09-07 16:45:07 +0100 | [diff] [blame] | 153 | throw armnn::Exception(fmt::format( |
| 154 | "Not every input has its tensor shape specified: expected={0}, got={1}", |
| 155 | numInputBindings, numInputShapes)); |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 156 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 157 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 158 | for (size_t i = 0; i < numInputShapes; i++) |
| 159 | { |
| 160 | inputShapes[params.m_InputBindings[i]] = params.m_InputShapes[i]; |
| 161 | } |
| 162 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 163 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 164 | std::vector<std::string> requestedOutputs = params.m_OutputBindings; |
| 165 | armnn::INetworkPtr network{nullptr, [](armnn::INetwork *){}}; |
| 166 | |
| 167 | { |
| 168 | ARMNN_SCOPED_HEAP_PROFILING("Parsing"); |
| 169 | // Handle text and binary input differently by calling the corresponding parser function |
| 170 | network = (params.m_IsModelBinary ? |
| 171 | parser->CreateNetworkFromBinaryFile(modelPath.c_str(), inputShapes, requestedOutputs) : |
| 172 | parser->CreateNetworkFromTextFile(modelPath.c_str(), inputShapes, requestedOutputs)); |
| 173 | } |
| 174 | |
| 175 | for (const std::string& inputLayerName : params.m_InputBindings) |
| 176 | { |
| 177 | inputBindings.push_back(parser->GetNetworkInputBindingInfo(inputLayerName)); |
| 178 | } |
| 179 | |
| 180 | for (const std::string& outputLayerName : params.m_OutputBindings) |
| 181 | { |
| 182 | outputBindings.push_back(parser->GetNetworkOutputBindingInfo(outputLayerName)); |
| 183 | } |
| 184 | |
| 185 | return network; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 186 | } |
| 187 | }; |
| 188 | |
Aron Virginas-Tar | 64e4ccb | 2019-02-12 11:27:53 +0000 | [diff] [blame] | 189 | #if defined(ARMNN_SERIALIZER) |
| 190 | template <> |
Derek Lamberti | 0028d1b | 2019-02-20 13:57:42 +0000 | [diff] [blame] | 191 | struct CreateNetworkImpl<armnnDeserializer::IDeserializer> |
Aron Virginas-Tar | 64e4ccb | 2019-02-12 11:27:53 +0000 | [diff] [blame] | 192 | { |
| 193 | public: |
Derek Lamberti | 0028d1b | 2019-02-20 13:57:42 +0000 | [diff] [blame] | 194 | using IParser = armnnDeserializer::IDeserializer; |
Aron Virginas-Tar | 64e4ccb | 2019-02-12 11:27:53 +0000 | [diff] [blame] | 195 | using Params = InferenceModelInternal::Params; |
Aron Virginas-Tar | 64e4ccb | 2019-02-12 11:27:53 +0000 | [diff] [blame] | 196 | |
| 197 | static armnn::INetworkPtr Create(const Params& params, |
Jim Flynn | b4d7eae | 2019-05-01 14:44:27 +0100 | [diff] [blame] | 198 | std::vector<armnn::BindingPointInfo>& inputBindings, |
| 199 | std::vector<armnn::BindingPointInfo>& outputBindings) |
Aron Virginas-Tar | 64e4ccb | 2019-02-12 11:27:53 +0000 | [diff] [blame] | 200 | { |
| 201 | auto parser(IParser::Create()); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 202 | ARMNN_ASSERT(parser); |
Aron Virginas-Tar | 64e4ccb | 2019-02-12 11:27:53 +0000 | [diff] [blame] | 203 | |
| 204 | armnn::INetworkPtr network{nullptr, [](armnn::INetwork *){}}; |
| 205 | |
| 206 | { |
| 207 | ARMNN_SCOPED_HEAP_PROFILING("Parsing"); |
Derek Lamberti | 2b183fb | 2019-02-18 16:36:57 +0000 | [diff] [blame] | 208 | |
Francis Murtagh | 532a29d | 2020-06-29 11:50:01 +0100 | [diff] [blame] | 209 | std::error_code errorCode; |
| 210 | fs::path pathToFile(params.m_ModelPath); |
| 211 | if (!fs::exists(pathToFile, errorCode)) |
Derek Lamberti | 2b183fb | 2019-02-18 16:36:57 +0000 | [diff] [blame] | 212 | { |
James Ward | 08f4016 | 2020-09-07 16:45:07 +0100 | [diff] [blame] | 213 | throw armnn::FileNotFoundException(fmt::format("Cannot find the file ({0}) errorCode: {1} {2}", |
| 214 | params.m_ModelPath, |
| 215 | errorCode.message(), |
Derek Lamberti | 2b183fb | 2019-02-18 16:36:57 +0000 | [diff] [blame] | 216 | CHECK_LOCATION().AsString())); |
| 217 | } |
| 218 | std::ifstream file(params.m_ModelPath, std::ios::binary); |
| 219 | |
| 220 | network = parser->CreateNetworkFromBinary(file); |
Aron Virginas-Tar | 64e4ccb | 2019-02-12 11:27:53 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Matthew Sloyan | 80c6b14 | 2020-09-08 12:00:32 +0100 | [diff] [blame] | 223 | unsigned int subgraphId = armnn::numeric_cast<unsigned int>(params.m_SubgraphId); |
Aron Virginas-Tar | 64e4ccb | 2019-02-12 11:27:53 +0000 | [diff] [blame] | 224 | |
| 225 | for (const std::string& inputLayerName : params.m_InputBindings) |
| 226 | { |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 227 | armnnDeserializer::BindingPointInfo inputBinding = |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 228 | parser->GetNetworkInputBindingInfo(subgraphId, inputLayerName); |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 229 | 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] | 230 | } |
| 231 | |
| 232 | for (const std::string& outputLayerName : params.m_OutputBindings) |
| 233 | { |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 234 | armnnDeserializer::BindingPointInfo outputBinding = |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 235 | parser->GetNetworkOutputBindingInfo(subgraphId, outputLayerName); |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 236 | 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] | 237 | } |
| 238 | |
| 239 | return network; |
| 240 | } |
| 241 | }; |
| 242 | #endif |
| 243 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 244 | #if defined(ARMNN_TF_LITE_PARSER) |
| 245 | template <> |
| 246 | struct CreateNetworkImpl<armnnTfLiteParser::ITfLiteParser> |
| 247 | { |
| 248 | public: |
| 249 | using IParser = armnnTfLiteParser::ITfLiteParser; |
| 250 | using Params = InferenceModelInternal::Params; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 251 | |
| 252 | static armnn::INetworkPtr Create(const Params& params, |
Jim Flynn | b4d7eae | 2019-05-01 14:44:27 +0100 | [diff] [blame] | 253 | std::vector<armnn::BindingPointInfo>& inputBindings, |
| 254 | std::vector<armnn::BindingPointInfo>& outputBindings) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 255 | { |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 256 | const std::string& modelPath = params.m_ModelPath; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 257 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 258 | // Create a network from a file on disk |
Derek Lamberti | 132563c | 2019-12-02 16:06:40 +0000 | [diff] [blame] | 259 | IParser::TfLiteParserOptions options; |
| 260 | options.m_StandInLayerForUnsupported = params.m_ParseUnsupported; |
Sadik Armagan | a9c2ce1 | 2020-07-14 10:02:22 +0100 | [diff] [blame] | 261 | options.m_InferAndValidate = params.m_InferOutputShape; |
Derek Lamberti | 132563c | 2019-12-02 16:06:40 +0000 | [diff] [blame] | 262 | auto parser(IParser::Create(options)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 263 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 264 | armnn::INetworkPtr network{nullptr, [](armnn::INetwork *){}}; |
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 | { |
| 267 | ARMNN_SCOPED_HEAP_PROFILING("Parsing"); |
| 268 | network = parser->CreateNetworkFromBinaryFile(modelPath.c_str()); |
| 269 | } |
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 | for (const std::string& inputLayerName : params.m_InputBindings) |
| 272 | { |
Jim Flynn | b4d7eae | 2019-05-01 14:44:27 +0100 | [diff] [blame] | 273 | armnn::BindingPointInfo inputBinding = |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 274 | parser->GetNetworkInputBindingInfo(params.m_SubgraphId, inputLayerName); |
| 275 | inputBindings.push_back(inputBinding); |
| 276 | } |
| 277 | |
| 278 | for (const std::string& outputLayerName : params.m_OutputBindings) |
| 279 | { |
Jim Flynn | b4d7eae | 2019-05-01 14:44:27 +0100 | [diff] [blame] | 280 | armnn::BindingPointInfo outputBinding = |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 281 | parser->GetNetworkOutputBindingInfo(params.m_SubgraphId, outputLayerName); |
| 282 | outputBindings.push_back(outputBinding); |
| 283 | } |
| 284 | |
| 285 | return network; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 286 | } |
| 287 | }; |
| 288 | #endif |
| 289 | |
| 290 | #if defined(ARMNN_ONNX_PARSER) |
| 291 | template <> |
| 292 | struct CreateNetworkImpl<armnnOnnxParser::IOnnxParser> |
| 293 | { |
| 294 | public: |
| 295 | using IParser = armnnOnnxParser::IOnnxParser; |
| 296 | using Params = InferenceModelInternal::Params; |
| 297 | using BindingPointInfo = InferenceModelInternal::BindingPointInfo; |
| 298 | |
| 299 | static armnn::INetworkPtr Create(const Params& params, |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 300 | std::vector<BindingPointInfo>& inputBindings, |
| 301 | std::vector<BindingPointInfo>& outputBindings) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 302 | { |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 303 | const std::string& modelPath = params.m_ModelPath; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 304 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 305 | // Create a network from a file on disk |
| 306 | auto parser(IParser::Create()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 307 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 308 | armnn::INetworkPtr network{nullptr, [](armnn::INetwork *){}}; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 309 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 310 | { |
| 311 | ARMNN_SCOPED_HEAP_PROFILING("Parsing"); |
| 312 | network = (params.m_IsModelBinary ? |
| 313 | parser->CreateNetworkFromBinaryFile(modelPath.c_str()) : |
| 314 | parser->CreateNetworkFromTextFile(modelPath.c_str())); |
| 315 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 316 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 317 | for (const std::string& inputLayerName : params.m_InputBindings) |
| 318 | { |
| 319 | BindingPointInfo inputBinding = parser->GetNetworkInputBindingInfo(inputLayerName); |
| 320 | inputBindings.push_back(inputBinding); |
| 321 | } |
| 322 | |
| 323 | for (const std::string& outputLayerName : params.m_OutputBindings) |
| 324 | { |
| 325 | BindingPointInfo outputBinding = parser->GetNetworkOutputBindingInfo(outputLayerName); |
| 326 | outputBindings.push_back(outputBinding); |
| 327 | } |
| 328 | |
| 329 | return network; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 330 | } |
| 331 | }; |
| 332 | #endif |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 333 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 334 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 335 | |
| 336 | template <typename IParser, typename TDataType> |
| 337 | class InferenceModel |
| 338 | { |
| 339 | public: |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 340 | using DataType = TDataType; |
| 341 | using Params = InferenceModelInternal::Params; |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 342 | using QuantizationParams = InferenceModelInternal::QuantizationParams; |
Finn Williams | f806c4d | 2021-02-22 15:13:12 +0000 | [diff] [blame] | 343 | using TContainer |
| 344 | = mapbox::util::variant<std::vector<float>, std::vector<int>, std::vector<unsigned char>, std::vector<int8_t>>; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 345 | |
| 346 | struct CommandLineOptions |
| 347 | { |
| 348 | std::string m_ModelDir; |
Aron Virginas-Tar | 339bcae | 2019-01-31 16:44:26 +0000 | [diff] [blame] | 349 | std::vector<std::string> m_ComputeDevices; |
Matteo Martincigh | 00dda4a | 2019-08-14 11:42:30 +0100 | [diff] [blame] | 350 | std::string m_DynamicBackendsPath; |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 351 | bool m_VisualizePostOptimizationModel; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 352 | bool m_EnableFp16TurboMode; |
Narumol Prangnawarat | d8cc811 | 2020-03-24 13:54:05 +0000 | [diff] [blame] | 353 | bool m_EnableBf16TurboMode; |
Pablo Tello | 507f39d | 2019-04-15 15:44:39 +0100 | [diff] [blame] | 354 | std::string m_Labels; |
Aron Virginas-Tar | 339bcae | 2019-01-31 16:44:26 +0000 | [diff] [blame] | 355 | |
| 356 | std::vector<armnn::BackendId> GetComputeDevicesAsBackendIds() |
| 357 | { |
| 358 | std::vector<armnn::BackendId> backendIds; |
| 359 | std::copy(m_ComputeDevices.begin(), m_ComputeDevices.end(), std::back_inserter(backendIds)); |
| 360 | return backendIds; |
| 361 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 362 | }; |
| 363 | |
James Ward | c89829f | 2020-10-12 14:17:36 +0100 | [diff] [blame] | 364 | static void AddCommandLineOptions(cxxopts::Options& options, |
| 365 | CommandLineOptions& cLineOptions, std::vector<std::string>& required) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 366 | { |
Aron Virginas-Tar | 339bcae | 2019-01-31 16:44:26 +0000 | [diff] [blame] | 367 | const std::vector<std::string> defaultComputes = { "CpuAcc", "CpuRef" }; |
David Beck | f0b4845 | 2018-10-19 15:20:56 +0100 | [diff] [blame] | 368 | |
Aron Virginas-Tar | 5cc8e56 | 2018-10-23 15:14:46 +0100 | [diff] [blame] | 369 | const std::string backendsMessage = "Which device to run layers on by default. Possible choices: " |
| 370 | + armnn::BackendRegistryInstance().GetBackendIdsAsString(); |
| 371 | |
James Ward | c89829f | 2020-10-12 14:17:36 +0100 | [diff] [blame] | 372 | options |
| 373 | .allow_unrecognised_options() |
| 374 | .add_options() |
Nikhil Raj | 6dd178f | 2021-04-02 22:04:39 +0100 | [diff] [blame] | 375 | ("m,model-dir", "Path to directory containing model files (.prototxt/.tflite)", |
James Ward | c89829f | 2020-10-12 14:17:36 +0100 | [diff] [blame] | 376 | cxxopts::value<std::string>(cLineOptions.m_ModelDir)) |
| 377 | ("c,compute", backendsMessage.c_str(), |
| 378 | cxxopts::value<std::vector<std::string>>(cLineOptions.m_ComputeDevices)->default_value("CpuRef")) |
| 379 | ("b,dynamic-backends-path", |
| 380 | "Path where to load any available dynamic backend from. " |
| 381 | "If left empty (the default), dynamic backends will not be used.", |
| 382 | cxxopts::value(cLineOptions.m_DynamicBackendsPath)) |
| 383 | ("l,labels", |
| 384 | "Text file containing one image filename - correct label pair per line, " |
| 385 | "used to test the accuracy of the network.", cxxopts::value<std::string>(cLineOptions.m_Labels)) |
| 386 | ("v,visualize-optimized-model", |
| 387 | "Produce a dot file useful for visualizing the graph post optimization." |
| 388 | "The file will have the same name as the model with the .dot extention.", |
| 389 | cxxopts::value<bool>(cLineOptions.m_VisualizePostOptimizationModel)->default_value("false")) |
| 390 | ("fp16-turbo-mode", |
| 391 | "If this option is enabled FP32 layers, weights and biases will be converted " |
| 392 | "to FP16 where the backend supports it.", |
| 393 | cxxopts::value<bool>(cLineOptions.m_EnableFp16TurboMode)->default_value("false")) |
| 394 | ("bf16-turbo-mode", |
| 395 | "If this option is enabled FP32 layers, weights and biases will be converted " |
| 396 | "to BF16 where the backend supports it.", |
| 397 | cxxopts::value<bool>(cLineOptions.m_EnableBf16TurboMode)->default_value("false")); |
| 398 | |
| 399 | required.emplace_back("model-dir"); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Matthew Bentham | 3e68b97 | 2019-04-09 13:10:46 +0100 | [diff] [blame] | 402 | InferenceModel(const Params& params, |
| 403 | bool enableProfiling, |
Matteo Martincigh | 00dda4a | 2019-08-14 11:42:30 +0100 | [diff] [blame] | 404 | const std::string& dynamicBackendsPath, |
Matthew Bentham | 3e68b97 | 2019-04-09 13:10:46 +0100 | [diff] [blame] | 405 | const std::shared_ptr<armnn::IRuntime>& runtime = nullptr) |
| 406 | : m_EnableProfiling(enableProfiling) |
Matteo Martincigh | 00dda4a | 2019-08-14 11:42:30 +0100 | [diff] [blame] | 407 | , m_DynamicBackendsPath(dynamicBackendsPath) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 408 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 409 | if (runtime) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 410 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 411 | m_Runtime = runtime; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 412 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 413 | else |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 414 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 415 | armnn::IRuntime::CreationOptions options; |
Nina Drozd | 549ae37 | 2018-09-10 14:26:44 +0100 | [diff] [blame] | 416 | options.m_EnableGpuProfiling = m_EnableProfiling; |
Matteo Martincigh | 00dda4a | 2019-08-14 11:42:30 +0100 | [diff] [blame] | 417 | options.m_DynamicBackendsPath = m_DynamicBackendsPath; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 418 | m_Runtime = std::move(armnn::IRuntime::Create(options)); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 419 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 420 | |
Aron Virginas-Tar | 5cc8e56 | 2018-10-23 15:14:46 +0100 | [diff] [blame] | 421 | std::string invalidBackends; |
Aron Virginas-Tar | 339bcae | 2019-01-31 16:44:26 +0000 | [diff] [blame] | 422 | if (!CheckRequestedBackendsAreValid(params.m_ComputeDevices, armnn::Optional<std::string&>(invalidBackends))) |
Aron Virginas-Tar | 5cc8e56 | 2018-10-23 15:14:46 +0100 | [diff] [blame] | 423 | { |
| 424 | throw armnn::Exception("Some backend IDs are invalid: " + invalidBackends); |
| 425 | } |
| 426 | |
Matteo Martincigh | 00dda4a | 2019-08-14 11:42:30 +0100 | [diff] [blame] | 427 | armnn::IOptimizedNetworkPtr optNet{nullptr, [](armnn::IOptimizedNetwork*){}}; |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 428 | { |
Finn Williams | 4422cec | 2021-03-22 17:51:06 +0000 | [diff] [blame] | 429 | const auto parsing_start_time = armnn::GetTimeNow(); |
| 430 | armnn::INetworkPtr network = CreateNetworkImpl<IParser>::Create(params, m_InputBindings, m_OutputBindings); |
| 431 | |
| 432 | ARMNN_LOG(info) << "Network parsing time: " << std::setprecision(2) |
| 433 | << std::fixed << armnn::GetTimeDuration(parsing_start_time).count() << " ms\n"; |
| 434 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 435 | ARMNN_SCOPED_HEAP_PROFILING("Optimizing"); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 436 | |
| 437 | armnn::OptimizerOptions options; |
| 438 | options.m_ReduceFp32ToFp16 = params.m_EnableFp16TurboMode; |
Narumol Prangnawarat | d8cc811 | 2020-03-24 13:54:05 +0000 | [diff] [blame] | 439 | options.m_ReduceFp32ToBf16 = params.m_EnableBf16TurboMode; |
Matthew Jackson | 54658b9 | 2019-08-27 15:35:59 +0100 | [diff] [blame] | 440 | options.m_Debug = params.m_PrintIntermediateLayers; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 441 | |
Sadik Armagan | a25886e | 2020-09-15 17:17:08 +0100 | [diff] [blame] | 442 | armnn::BackendOptions gpuAcc("GpuAcc", |
| 443 | { |
Matthew Sloyan | 4243211 | 2021-01-08 10:30:51 +0000 | [diff] [blame] | 444 | { "FastMathEnabled", params.m_EnableFastMath }, |
| 445 | { "SaveCachedNetwork", params.m_SaveCachedNetwork }, |
Finn Williams | 4064632 | 2021-02-11 16:16:42 +0000 | [diff] [blame] | 446 | { "CachedNetworkFilePath", params.m_CachedNetworkFilePath }, |
| 447 | { "MLGOTuningFilePath", params.m_MLGOTuningFilePath } |
Sadik Armagan | a25886e | 2020-09-15 17:17:08 +0100 | [diff] [blame] | 448 | }); |
Finn Williams | 4064632 | 2021-02-11 16:16:42 +0000 | [diff] [blame] | 449 | |
Sadik Armagan | a25886e | 2020-09-15 17:17:08 +0100 | [diff] [blame] | 450 | armnn::BackendOptions cpuAcc("CpuAcc", |
| 451 | { |
Matthew Sloyan | 0a7dc6b | 2021-02-10 16:50:53 +0000 | [diff] [blame] | 452 | { "FastMathEnabled", params.m_EnableFastMath }, |
| 453 | { "NumberOfThreads", params.m_NumberOfThreads } |
Sadik Armagan | a25886e | 2020-09-15 17:17:08 +0100 | [diff] [blame] | 454 | }); |
| 455 | options.m_ModelOptions.push_back(gpuAcc); |
| 456 | options.m_ModelOptions.push_back(cpuAcc); |
| 457 | |
alered01 | a7227ac | 2020-05-07 14:58:29 +0100 | [diff] [blame] | 458 | const auto optimization_start_time = armnn::GetTimeNow(); |
Aron Virginas-Tar | 339bcae | 2019-01-31 16:44:26 +0000 | [diff] [blame] | 459 | optNet = armnn::Optimize(*network, params.m_ComputeDevices, m_Runtime->GetDeviceSpec(), options); |
alered01 | a7227ac | 2020-05-07 14:58:29 +0100 | [diff] [blame] | 460 | |
| 461 | ARMNN_LOG(info) << "Optimization time: " << std::setprecision(2) |
| 462 | << std::fixed << armnn::GetTimeDuration(optimization_start_time).count() << " ms\n"; |
| 463 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 464 | if (!optNet) |
| 465 | { |
| 466 | throw armnn::Exception("Optimize returned nullptr"); |
| 467 | } |
Finn Williams | 4422cec | 2021-03-22 17:51:06 +0000 | [diff] [blame] | 468 | |
| 469 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 470 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 471 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 472 | if (params.m_VisualizePostOptimizationModel) |
| 473 | { |
Francis Murtagh | 532a29d | 2020-06-29 11:50:01 +0100 | [diff] [blame] | 474 | fs::path filename = params.m_ModelPath; |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 475 | filename.replace_extension("dot"); |
Rob Hughes | 9e10c2b | 2019-07-23 15:37:19 +0100 | [diff] [blame] | 476 | std::fstream file(filename.c_str(), std::ios_base::out); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 477 | optNet->SerializeToDot(file); |
| 478 | } |
| 479 | |
| 480 | armnn::Status ret; |
| 481 | { |
| 482 | ARMNN_SCOPED_HEAP_PROFILING("LoadNetwork"); |
Matthew Sloyan | 4f29f15 | 2021-01-18 16:10:20 +0000 | [diff] [blame] | 483 | |
| 484 | const auto loading_start_time = armnn::GetTimeNow(); |
Francis Murtagh | 73d3e2e | 2021-04-29 14:23:04 +0100 | [diff] [blame] | 485 | armnn::INetworkProperties networkProperties(params.m_AsyncEnabled, |
| 486 | armnn::MemorySource::Undefined, |
Kevin May | b4b3ac9 | 2021-05-21 16:42:21 +0100 | [diff] [blame] | 487 | armnn::MemorySource::Undefined, |
| 488 | params.m_ThreadPoolSize); |
Sadik Armagan | a04a9d7 | 2021-04-27 10:02:10 +0100 | [diff] [blame] | 489 | std::string errorMessage; |
| 490 | ret = m_Runtime->LoadNetwork(m_NetworkIdentifier, std::move(optNet), errorMessage, networkProperties); |
Matthew Sloyan | 4f29f15 | 2021-01-18 16:10:20 +0000 | [diff] [blame] | 491 | |
| 492 | ARMNN_LOG(info) << "Network loading time: " << std::setprecision(2) |
| 493 | << std::fixed << armnn::GetTimeDuration(loading_start_time).count() << " ms\n"; |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 494 | } |
| 495 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 496 | if (ret == armnn::Status::Failure) |
| 497 | { |
| 498 | throw armnn::Exception("IRuntime::LoadNetwork failed"); |
| 499 | } |
| 500 | } |
| 501 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 502 | void CheckInputIndexIsValid(unsigned int inputIndex) const |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 503 | { |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 504 | if (m_InputBindings.size() < inputIndex + 1) |
| 505 | { |
James Ward | 08f4016 | 2020-09-07 16:45:07 +0100 | [diff] [blame] | 506 | throw armnn::Exception(fmt::format("Input index out of range: {}", inputIndex)); |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 507 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 510 | void CheckOutputIndexIsValid(unsigned int outputIndex) const |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 511 | { |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 512 | if (m_OutputBindings.size() < outputIndex + 1) |
| 513 | { |
James Ward | 08f4016 | 2020-09-07 16:45:07 +0100 | [diff] [blame] | 514 | throw armnn::Exception(fmt::format("Output index out of range: {}", outputIndex)); |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 515 | } |
| 516 | } |
| 517 | |
Aron Virginas-Tar | c82c873 | 2019-10-24 17:07:43 +0100 | [diff] [blame] | 518 | unsigned int GetInputSize(unsigned int inputIndex = 0u) const |
| 519 | { |
| 520 | CheckInputIndexIsValid(inputIndex); |
| 521 | return m_InputBindings[inputIndex].second.GetNumElements(); |
| 522 | } |
| 523 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 524 | unsigned int GetOutputSize(unsigned int outputIndex = 0u) const |
| 525 | { |
| 526 | CheckOutputIndexIsValid(outputIndex); |
| 527 | return m_OutputBindings[outputIndex].second.GetNumElements(); |
| 528 | } |
| 529 | |
James Conroy | 7b4886f | 2019-04-11 10:23:58 +0100 | [diff] [blame] | 530 | std::chrono::duration<double, std::milli> Run( |
| 531 | const std::vector<TContainer>& inputContainers, |
| 532 | std::vector<TContainer>& outputContainers) |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 533 | { |
Ferran Balaguer | c602f29 | 2019-02-08 17:09:55 +0000 | [diff] [blame] | 534 | for (unsigned int i = 0; i < outputContainers.size(); ++i) |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 535 | { |
| 536 | const unsigned int expectedOutputDataSize = GetOutputSize(i); |
Ferran Balaguer | c602f29 | 2019-02-08 17:09:55 +0000 | [diff] [blame] | 537 | |
James Ward | 6d9f5c5 | 2020-09-28 11:56:35 +0100 | [diff] [blame] | 538 | mapbox::util::apply_visitor([expectedOutputDataSize, i](auto&& value) |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 539 | { |
Matthew Sloyan | 80c6b14 | 2020-09-08 12:00:32 +0100 | [diff] [blame] | 540 | const unsigned int actualOutputDataSize = armnn::numeric_cast<unsigned int>(value.size()); |
Ferran Balaguer | c602f29 | 2019-02-08 17:09:55 +0000 | [diff] [blame] | 541 | if (actualOutputDataSize < expectedOutputDataSize) |
| 542 | { |
Matthew Sloyan | 80c6b14 | 2020-09-08 12:00:32 +0100 | [diff] [blame] | 543 | unsigned int outputIndex = i; |
Ferran Balaguer | c602f29 | 2019-02-08 17:09:55 +0000 | [diff] [blame] | 544 | throw armnn::Exception( |
James Ward | 08f4016 | 2020-09-07 16:45:07 +0100 | [diff] [blame] | 545 | fmt::format("Not enough data for output #{0}: expected " |
| 546 | "{1} elements, got {2}", outputIndex, expectedOutputDataSize, actualOutputDataSize)); |
Ferran Balaguer | c602f29 | 2019-02-08 17:09:55 +0000 | [diff] [blame] | 547 | } |
| 548 | }, |
| 549 | outputContainers[i]); |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 550 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 551 | |
| 552 | std::shared_ptr<armnn::IProfiler> profiler = m_Runtime->GetProfiler(m_NetworkIdentifier); |
| 553 | if (profiler) |
| 554 | { |
| 555 | profiler->EnableProfiling(m_EnableProfiling); |
| 556 | } |
| 557 | |
James Conroy | 7b4886f | 2019-04-11 10:23:58 +0100 | [diff] [blame] | 558 | // Start timer to record inference time in EnqueueWorkload (in milliseconds) |
alered01 | a7227ac | 2020-05-07 14:58:29 +0100 | [diff] [blame] | 559 | const auto start_time = armnn::GetTimeNow(); |
James Conroy | 7b4886f | 2019-04-11 10:23:58 +0100 | [diff] [blame] | 560 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 561 | armnn::Status ret = m_Runtime->EnqueueWorkload(m_NetworkIdentifier, |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 562 | MakeInputTensors(inputContainers), |
| 563 | MakeOutputTensors(outputContainers)); |
alered01 | a7227ac | 2020-05-07 14:58:29 +0100 | [diff] [blame] | 564 | const auto duration = armnn::GetTimeDuration(start_time); |
James Conroy | 7b4886f | 2019-04-11 10:23:58 +0100 | [diff] [blame] | 565 | |
Sadik Armagan | 2b7a158 | 2018-09-05 16:33:58 +0100 | [diff] [blame] | 566 | // if profiling is enabled print out the results |
| 567 | if (profiler && profiler->IsProfilingEnabled()) |
| 568 | { |
| 569 | profiler->Print(std::cout); |
| 570 | } |
| 571 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 572 | if (ret == armnn::Status::Failure) |
| 573 | { |
| 574 | throw armnn::Exception("IRuntime::EnqueueWorkload failed"); |
| 575 | } |
James Conroy | 7b4886f | 2019-04-11 10:23:58 +0100 | [diff] [blame] | 576 | else |
| 577 | { |
alered01 | a7227ac | 2020-05-07 14:58:29 +0100 | [diff] [blame] | 578 | return duration; |
James Conroy | 7b4886f | 2019-04-11 10:23:58 +0100 | [diff] [blame] | 579 | } |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 580 | } |
| 581 | |
Sadik Armagan | a04a9d7 | 2021-04-27 10:02:10 +0100 | [diff] [blame] | 582 | std::tuple<armnn::profiling::ProfilingGuid, std::chrono::duration<double, std::milli>> RunAsync( |
| 583 | armnn::experimental::IWorkingMemHandle& workingMemHandleRef, |
| 584 | const std::vector<TContainer>& inputContainers, |
| 585 | std::vector<TContainer>& outputContainers) |
| 586 | { |
| 587 | for (unsigned int i = 0; i < outputContainers.size(); ++i) |
| 588 | { |
| 589 | const unsigned int expectedOutputDataSize = GetOutputSize(i); |
| 590 | |
| 591 | mapbox::util::apply_visitor([expectedOutputDataSize, i](auto&& value) |
| 592 | { |
| 593 | const unsigned int actualOutputDataSize = armnn::numeric_cast<unsigned int>(value.size()); |
| 594 | if (actualOutputDataSize < expectedOutputDataSize) |
| 595 | { |
| 596 | unsigned int outputIndex = i; |
| 597 | throw armnn::Exception( |
| 598 | fmt::format("Not enough data for output #{0}: expected " |
| 599 | "{1} elements, got {2}", outputIndex, expectedOutputDataSize, actualOutputDataSize)); |
| 600 | } |
| 601 | }, |
| 602 | outputContainers[i]); |
| 603 | } |
| 604 | |
| 605 | std::shared_ptr<armnn::IProfiler> profiler = m_Runtime->GetProfiler(m_NetworkIdentifier); |
| 606 | if (profiler) |
| 607 | { |
| 608 | profiler->EnableProfiling(m_EnableProfiling); |
| 609 | } |
| 610 | |
| 611 | // Start timer to record inference time in EnqueueWorkload (in milliseconds) |
| 612 | const auto start_time = armnn::GetTimeNow(); |
| 613 | |
| 614 | armnn::Status ret = m_Runtime->Execute(workingMemHandleRef, |
| 615 | MakeInputTensors(inputContainers), |
| 616 | MakeOutputTensors(outputContainers)); |
| 617 | auto inferenceID = workingMemHandleRef.GetInferenceId(); |
| 618 | |
| 619 | const auto duration = armnn::GetTimeDuration(start_time); |
| 620 | |
| 621 | // if profiling is enabled print out the results |
| 622 | if (profiler && profiler->IsProfilingEnabled()) |
| 623 | { |
| 624 | profiler->Print(std::cout); |
| 625 | } |
| 626 | |
| 627 | if (ret == armnn::Status::Failure) |
| 628 | { |
| 629 | throw armnn::Exception( |
| 630 | fmt::format("IRuntime::Execute asynchronously failed for network #{0} on inference #{1}", |
| 631 | m_NetworkIdentifier, inferenceID)); |
| 632 | } |
| 633 | else |
| 634 | { |
| 635 | return std::make_tuple(inferenceID, duration); |
| 636 | } |
| 637 | } |
| 638 | |
Kevin May | b4b3ac9 | 2021-05-21 16:42:21 +0100 | [diff] [blame] | 639 | void RunAsync(const std::vector<TContainer>& inputContainers, |
| 640 | std::vector<TContainer>& outputContainers, |
| 641 | armnn::experimental::IAsyncExecutionCallbackPtr cb) |
| 642 | { |
| 643 | for (unsigned int i = 0; i < outputContainers.size(); ++i) |
| 644 | { |
| 645 | const unsigned int expectedOutputDataSize = GetOutputSize(i); |
| 646 | |
| 647 | mapbox::util::apply_visitor([expectedOutputDataSize, i](auto&& value) |
| 648 | { |
| 649 | const unsigned int actualOutputDataSize = armnn::numeric_cast<unsigned int>(value.size()); |
| 650 | if (actualOutputDataSize < expectedOutputDataSize) |
| 651 | { |
| 652 | unsigned int outputIndex = i; |
| 653 | throw armnn::Exception( |
| 654 | fmt::format("Not enough data for output #{0}: expected " |
| 655 | "{1} elements, got {2}", outputIndex, expectedOutputDataSize, actualOutputDataSize)); |
| 656 | } |
| 657 | }, |
| 658 | outputContainers[i]); |
| 659 | } |
| 660 | |
| 661 | std::shared_ptr<armnn::IProfiler> profiler = m_Runtime->GetProfiler(m_NetworkIdentifier); |
| 662 | if (profiler) |
| 663 | { |
| 664 | profiler->EnableProfiling(m_EnableProfiling); |
| 665 | } |
| 666 | |
| 667 | m_Runtime->Schedule(m_NetworkIdentifier, |
| 668 | MakeInputTensors(inputContainers), |
| 669 | MakeOutputTensors(outputContainers), |
| 670 | armnn::QosExecPriority::Medium, |
| 671 | cb); |
| 672 | |
| 673 | // if profiling is enabled print out the results |
| 674 | if (profiler && profiler->IsProfilingEnabled()) |
| 675 | { |
| 676 | profiler->Print(std::cout); |
| 677 | } |
| 678 | } |
| 679 | |
Jim Flynn | b4d7eae | 2019-05-01 14:44:27 +0100 | [diff] [blame] | 680 | const armnn::BindingPointInfo& GetInputBindingInfo(unsigned int inputIndex = 0u) const |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 681 | { |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 682 | CheckInputIndexIsValid(inputIndex); |
| 683 | return m_InputBindings[inputIndex]; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 684 | } |
| 685 | |
Jim Flynn | b4d7eae | 2019-05-01 14:44:27 +0100 | [diff] [blame] | 686 | const std::vector<armnn::BindingPointInfo>& GetInputBindingInfos() const |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 687 | { |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 688 | return m_InputBindings; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 689 | } |
| 690 | |
Jim Flynn | b4d7eae | 2019-05-01 14:44:27 +0100 | [diff] [blame] | 691 | const armnn::BindingPointInfo& GetOutputBindingInfo(unsigned int outputIndex = 0u) const |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 692 | { |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 693 | CheckOutputIndexIsValid(outputIndex); |
| 694 | return m_OutputBindings[outputIndex]; |
| 695 | } |
| 696 | |
Jim Flynn | b4d7eae | 2019-05-01 14:44:27 +0100 | [diff] [blame] | 697 | const std::vector<armnn::BindingPointInfo>& GetOutputBindingInfos() const |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 698 | { |
| 699 | return m_OutputBindings; |
| 700 | } |
| 701 | |
| 702 | QuantizationParams GetQuantizationParams(unsigned int outputIndex = 0u) const |
| 703 | { |
| 704 | CheckOutputIndexIsValid(outputIndex); |
| 705 | return std::make_pair(m_OutputBindings[outputIndex].second.GetQuantizationScale(), |
| 706 | m_OutputBindings[outputIndex].second.GetQuantizationOffset()); |
| 707 | } |
| 708 | |
Narumol Prangnawarat | 4628d05 | 2019-02-25 17:26:05 +0000 | [diff] [blame] | 709 | QuantizationParams GetInputQuantizationParams(unsigned int inputIndex = 0u) const |
| 710 | { |
| 711 | CheckInputIndexIsValid(inputIndex); |
| 712 | return std::make_pair(m_InputBindings[inputIndex].second.GetQuantizationScale(), |
| 713 | m_InputBindings[inputIndex].second.GetQuantizationOffset()); |
| 714 | } |
| 715 | |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 716 | std::vector<QuantizationParams> GetAllQuantizationParams() const |
| 717 | { |
| 718 | std::vector<QuantizationParams> quantizationParams; |
| 719 | for (unsigned int i = 0u; i < m_OutputBindings.size(); i++) |
| 720 | { |
| 721 | quantizationParams.push_back(GetQuantizationParams(i)); |
| 722 | } |
| 723 | return quantizationParams; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 724 | } |
| 725 | |
Sadik Armagan | a04a9d7 | 2021-04-27 10:02:10 +0100 | [diff] [blame] | 726 | std::unique_ptr<armnn::experimental::IWorkingMemHandle> CreateWorkingMemHandle() |
| 727 | { |
| 728 | return m_Runtime->CreateWorkingMemHandle(m_NetworkIdentifier); |
| 729 | } |
| 730 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 731 | private: |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 732 | armnn::NetworkId m_NetworkIdentifier; |
| 733 | std::shared_ptr<armnn::IRuntime> m_Runtime; |
| 734 | |
Jim Flynn | b4d7eae | 2019-05-01 14:44:27 +0100 | [diff] [blame] | 735 | std::vector<armnn::BindingPointInfo> m_InputBindings; |
| 736 | std::vector<armnn::BindingPointInfo> m_OutputBindings; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 737 | bool m_EnableProfiling; |
Matteo Martincigh | 00dda4a | 2019-08-14 11:42:30 +0100 | [diff] [blame] | 738 | std::string m_DynamicBackendsPath; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 739 | |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 740 | template<typename TContainer> |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 741 | armnn::InputTensors MakeInputTensors(const std::vector<TContainer>& inputDataContainers) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 742 | { |
Jim Flynn | 2fd6100 | 2019-05-03 12:54:26 +0100 | [diff] [blame] | 743 | return armnnUtils::MakeInputTensors(m_InputBindings, inputDataContainers); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | template<typename TContainer> |
Aron Virginas-Tar | 7cf0eaa | 2019-01-24 17:05:36 +0000 | [diff] [blame] | 747 | armnn::OutputTensors MakeOutputTensors(std::vector<TContainer>& outputDataContainers) |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 748 | { |
Jim Flynn | 2fd6100 | 2019-05-03 12:54:26 +0100 | [diff] [blame] | 749 | return armnnUtils::MakeOutputTensors(m_OutputBindings, outputDataContainers); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 750 | } |
Ferran Balaguer | c602f29 | 2019-02-08 17:09:55 +0000 | [diff] [blame] | 751 | }; |