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