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