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