Sadik Armagan | 3c24f43 | 2020-10-19 17:35:30 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2020 Arm Ltd and Contributors. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include <DelegateOptions.hpp> |
Jan Eilers | f39f8d8 | 2021-10-26 16:57:34 +0100 | [diff] [blame] | 7 | #include <armnn/utility/NumericCast.hpp> |
| 8 | #include <armnn/utility/StringUtils.hpp> |
Sadik Armagan | 3c24f43 | 2020-10-19 17:35:30 +0100 | [diff] [blame] | 9 | |
| 10 | namespace armnnDelegate |
| 11 | { |
| 12 | |
Sadik Armagan | 4189cc5 | 2020-11-11 18:01:48 +0000 | [diff] [blame] | 13 | DelegateOptions::DelegateOptions(armnn::Compute computeDevice, |
Jan Eilers | 2cd1847 | 2020-12-15 10:42:38 +0000 | [diff] [blame] | 14 | const std::vector<armnn::BackendOptions>& backendOptions, |
| 15 | const armnn::Optional<armnn::LogSeverity> logSeverityLevel) |
Jan Eilers | b1c62f1 | 2021-10-26 14:56:47 +0100 | [diff] [blame] | 16 | : m_Backends({computeDevice}), m_RuntimeOptions(), m_LoggingSeverity(logSeverityLevel) |
Sadik Armagan | 3c24f43 | 2020-10-19 17:35:30 +0100 | [diff] [blame] | 17 | { |
Jan Eilers | b1c62f1 | 2021-10-26 14:56:47 +0100 | [diff] [blame] | 18 | m_RuntimeOptions.m_BackendOptions = backendOptions; |
Sadik Armagan | 3c24f43 | 2020-10-19 17:35:30 +0100 | [diff] [blame] | 19 | } |
| 20 | |
Sadik Armagan | 4189cc5 | 2020-11-11 18:01:48 +0000 | [diff] [blame] | 21 | DelegateOptions::DelegateOptions(const std::vector<armnn::BackendId>& backends, |
Jan Eilers | 2cd1847 | 2020-12-15 10:42:38 +0000 | [diff] [blame] | 22 | const std::vector<armnn::BackendOptions>& backendOptions, |
| 23 | const armnn::Optional<armnn::LogSeverity> logSeverityLevel) |
Jan Eilers | b1c62f1 | 2021-10-26 14:56:47 +0100 | [diff] [blame] | 24 | : m_Backends(backends), m_RuntimeOptions(), m_LoggingSeverity(logSeverityLevel) |
Sadik Armagan | 3c24f43 | 2020-10-19 17:35:30 +0100 | [diff] [blame] | 25 | { |
Jan Eilers | b1c62f1 | 2021-10-26 14:56:47 +0100 | [diff] [blame] | 26 | m_RuntimeOptions.m_BackendOptions = backendOptions; |
Sadik Armagan | 3c24f43 | 2020-10-19 17:35:30 +0100 | [diff] [blame] | 27 | } |
| 28 | |
Narumol Prangnawarat | 0b51d5a | 2021-01-20 15:58:29 +0000 | [diff] [blame] | 29 | DelegateOptions::DelegateOptions(armnn::Compute computeDevice, |
| 30 | const armnn::OptimizerOptions& optimizerOptions, |
Narumol Prangnawarat | 0b51d5a | 2021-01-20 15:58:29 +0000 | [diff] [blame] | 31 | const armnn::Optional<armnn::LogSeverity>& logSeverityLevel, |
| 32 | const armnn::Optional<armnn::DebugCallbackFunction>& func) |
| 33 | : m_Backends({computeDevice}), |
Jan Eilers | b1c62f1 | 2021-10-26 14:56:47 +0100 | [diff] [blame] | 34 | m_RuntimeOptions(), |
Narumol Prangnawarat | 0b51d5a | 2021-01-20 15:58:29 +0000 | [diff] [blame] | 35 | m_OptimizerOptions(optimizerOptions), |
Narumol Prangnawarat | 0b51d5a | 2021-01-20 15:58:29 +0000 | [diff] [blame] | 36 | m_LoggingSeverity(logSeverityLevel), |
| 37 | m_DebugCallbackFunc(func) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | DelegateOptions::DelegateOptions(const std::vector<armnn::BackendId>& backends, |
| 42 | const armnn::OptimizerOptions& optimizerOptions, |
Narumol Prangnawarat | 0b51d5a | 2021-01-20 15:58:29 +0000 | [diff] [blame] | 43 | const armnn::Optional<armnn::LogSeverity>& logSeverityLevel, |
| 44 | const armnn::Optional<armnn::DebugCallbackFunction>& func) |
| 45 | : m_Backends(backends), |
Jan Eilers | b1c62f1 | 2021-10-26 14:56:47 +0100 | [diff] [blame] | 46 | m_RuntimeOptions(), |
Narumol Prangnawarat | 0b51d5a | 2021-01-20 15:58:29 +0000 | [diff] [blame] | 47 | m_OptimizerOptions(optimizerOptions), |
Narumol Prangnawarat | 0b51d5a | 2021-01-20 15:58:29 +0000 | [diff] [blame] | 48 | m_LoggingSeverity(logSeverityLevel), |
| 49 | m_DebugCallbackFunc(func) |
| 50 | { |
| 51 | } |
| 52 | |
Jan Eilers | f39f8d8 | 2021-10-26 16:57:34 +0100 | [diff] [blame] | 53 | DelegateOptions::DelegateOptions(char const* const* options_keys, |
| 54 | char const* const* options_values, |
| 55 | size_t num_options, |
| 56 | void (*report_error)(const char*)) |
| 57 | { |
| 58 | armnn::IRuntime::CreationOptions runtimeOptions; |
| 59 | armnn::OptimizerOptions optimizerOptions; |
| 60 | bool internalProfilingState = false; |
| 61 | armnn::ProfilingDetailsMethod internalProfilingDetail = armnn::ProfilingDetailsMethod::DetailsWithEvents; |
| 62 | armnn::IRuntime::CreationOptions::ExternalProfilingOptions extProfilingParams; |
| 63 | for (size_t i = 0; i < num_options; ++i) |
| 64 | { |
| 65 | // Process backends |
| 66 | if (std::string(options_keys[i]) == std::string("backends")) |
| 67 | { |
| 68 | // The backend option is a comma separated string of backendIDs that needs to be split |
| 69 | std::vector<armnn::BackendId> backends; |
| 70 | char* dup = strdup(options_values[i]); |
| 71 | char* pch = std::strtok(dup, ","); |
| 72 | while (pch != NULL) |
| 73 | { |
| 74 | backends.push_back(pch); |
| 75 | pch = strtok (NULL, ","); |
| 76 | } |
| 77 | this->SetBackends(backends); |
| 78 | } |
| 79 | // Process dynamic-backends-path |
| 80 | else if (std::string(options_keys[i]) == std::string("dynamic-backends-path")) |
| 81 | { |
| 82 | runtimeOptions.m_DynamicBackendsPath = std::string(options_values[i]); |
| 83 | } |
| 84 | // Process logging level |
| 85 | else if (std::string(options_keys[i]) == std::string("logging-severity")) |
| 86 | { |
| 87 | this->SetLoggingSeverity(options_values[i]); |
| 88 | } |
| 89 | // Process GPU backend options |
| 90 | else if (std::string(options_keys[i]) == std::string("gpu-tuning-level")) |
| 91 | { |
| 92 | armnn::BackendOptions option("GpuAcc", {{"TuningLevel", atoi(options_values[i])}}); |
| 93 | runtimeOptions.m_BackendOptions.push_back(option); |
| 94 | } |
| 95 | else if (std::string(options_keys[i]) == std::string("gpu-mlgo-tuning-file")) |
| 96 | { |
| 97 | armnn::BackendOptions option("GpuAcc", {{"MLGOTuningFilePath", std::string(options_values[i])}}); |
| 98 | optimizerOptions.m_ModelOptions.push_back(option); |
| 99 | } |
| 100 | else if (std::string(options_keys[i]) == std::string("gpu-tuning-file")) |
| 101 | { |
| 102 | armnn::BackendOptions option("GpuAcc", {{"TuningFile", std::string(options_values[i])}}); |
| 103 | runtimeOptions.m_BackendOptions.push_back(option); |
| 104 | } |
| 105 | else if (std::string(options_keys[i]) == std::string("gpu-enable-profiling")) |
| 106 | { |
| 107 | runtimeOptions.m_EnableGpuProfiling = (*options_values[i] != '0'); |
| 108 | } |
| 109 | else if (std::string(options_keys[i]) == std::string("gpu-kernel-profiling-enabled")) |
| 110 | { |
| 111 | armnn::BackendOptions option("GpuAcc", {{"KernelProfilingEnabled", |
| 112 | armnn::stringUtils::StringToBool(options_values[i])}}); |
| 113 | runtimeOptions.m_BackendOptions.push_back(option); |
| 114 | } |
| 115 | else if (std::string(options_keys[i]) == std::string("save-cached-network")) |
| 116 | { |
| 117 | armnn::BackendOptions option("GpuAcc", {{"SaveCachedNetwork", |
| 118 | armnn::stringUtils::StringToBool(options_values[i])}}); |
| 119 | optimizerOptions.m_ModelOptions.push_back(option); |
| 120 | } |
| 121 | else if (std::string(options_keys[i]) == std::string("cached-network-filepath")) |
| 122 | { |
| 123 | armnn::BackendOptions option("GpuAcc", {{"CachedNetworkFilePath", std::string(options_values[i])}}); |
| 124 | optimizerOptions.m_ModelOptions.push_back(option); |
| 125 | } |
| 126 | // Process GPU & CPU backend options |
| 127 | else if (std::string(options_keys[i]) == std::string("enable-fast-math")) |
| 128 | { |
| 129 | armnn::BackendOptions modelOptionGpu("GpuAcc", {{"FastMathEnabled", |
| 130 | armnn::stringUtils::StringToBool(options_values[i])}}); |
| 131 | optimizerOptions.m_ModelOptions.push_back(modelOptionGpu); |
| 132 | |
| 133 | armnn::BackendOptions modelOptionCpu("CpuAcc", {{"FastMathEnabled", |
| 134 | armnn::stringUtils::StringToBool(options_values[i])}}); |
| 135 | optimizerOptions.m_ModelOptions.push_back(modelOptionCpu); |
| 136 | } |
| 137 | // Process CPU backend options |
| 138 | else if (std::string(options_keys[i]) == std::string("number-of-threads")) |
| 139 | { |
| 140 | unsigned int numberOfThreads = armnn::numeric_cast<unsigned int>(atoi(options_values[i])); |
| 141 | armnn::BackendOptions modelOption("CpuAcc", {{"NumberOfThreads", numberOfThreads}}); |
| 142 | optimizerOptions.m_ModelOptions.push_back(modelOption); |
| 143 | } |
| 144 | // Process reduce-fp32-to-fp16 option |
| 145 | else if (std::string(options_keys[i]) == std::string("reduce-fp32-to-fp16")) |
| 146 | { |
| 147 | optimizerOptions.m_ReduceFp32ToFp16 = armnn::stringUtils::StringToBool(options_values[i]); |
| 148 | } |
| 149 | // Process reduce-fp32-to-bf16 option |
| 150 | else if (std::string(options_keys[i]) == std::string("reduce-fp32-to-bf16")) |
| 151 | { |
| 152 | optimizerOptions.m_ReduceFp32ToBf16 = armnn::stringUtils::StringToBool(options_values[i]); |
| 153 | } |
| 154 | // Process debug-data |
| 155 | else if (std::string(options_keys[i]) == std::string("debug-data")) |
| 156 | { |
| 157 | optimizerOptions.m_Debug = armnn::stringUtils::StringToBool(options_values[i]); |
| 158 | } |
| 159 | // Process memory-import |
| 160 | else if (std::string(options_keys[i]) == std::string("memory-import")) |
| 161 | { |
| 162 | optimizerOptions.m_ImportEnabled = armnn::stringUtils::StringToBool(options_values[i]); |
| 163 | } |
| 164 | // Process enable-internal-profiling |
| 165 | else if (std::string(options_keys[i]) == std::string("enable-internal-profiling")) |
| 166 | { |
| 167 | internalProfilingState = *options_values[i] != '0'; |
| 168 | optimizerOptions.m_ProfilingEnabled = internalProfilingState; |
| 169 | } |
| 170 | // Process internal-profiling-detail |
| 171 | else if (std::string(options_keys[i]) == std::string("internal-profiling-detail")) |
| 172 | { |
| 173 | uint32_t detailLevel = static_cast<uint32_t>(std::stoul(options_values[i])); |
| 174 | switch (detailLevel) |
| 175 | { |
| 176 | case 1: |
| 177 | internalProfilingDetail = armnn::ProfilingDetailsMethod::DetailsWithEvents; |
| 178 | break; |
| 179 | case 2: |
| 180 | internalProfilingDetail = armnn::ProfilingDetailsMethod::DetailsOnly; |
| 181 | break; |
| 182 | default: |
| 183 | internalProfilingDetail = armnn::ProfilingDetailsMethod::Undefined; |
| 184 | break; |
| 185 | } |
| 186 | } |
| 187 | // Process enable-external-profiling |
| 188 | else if (std::string(options_keys[i]) == std::string("enable-external-profiling")) |
| 189 | { |
| 190 | extProfilingParams.m_EnableProfiling = armnn::stringUtils::StringToBool(options_values[i]); |
| 191 | } |
| 192 | // Process timeline-profiling |
| 193 | else if (std::string(options_keys[i]) == std::string("timeline-profiling")) |
| 194 | { |
| 195 | extProfilingParams.m_TimelineEnabled = armnn::stringUtils::StringToBool(options_values[i]); |
| 196 | } |
| 197 | // Process outgoing-capture-file |
| 198 | else if (std::string(options_keys[i]) == std::string("outgoing-capture-file")) |
| 199 | { |
| 200 | extProfilingParams.m_OutgoingCaptureFile = options_values[i]; |
| 201 | } |
| 202 | // Process incoming-capture-file |
| 203 | else if (std::string(options_keys[i]) == std::string("incoming-capture-file")) |
| 204 | { |
| 205 | extProfilingParams.m_IncomingCaptureFile = options_values[i]; |
| 206 | } |
| 207 | // Process file-only-external-profiling |
| 208 | else if (std::string(options_keys[i]) == std::string("file-only-external-profiling")) |
| 209 | { |
| 210 | extProfilingParams.m_FileOnly = armnn::stringUtils::StringToBool(options_values[i]); |
| 211 | } |
| 212 | // Process counter-capture-period |
| 213 | else if (std::string(options_keys[i]) == std::string("counter-capture-period")) |
| 214 | { |
| 215 | extProfilingParams.m_CapturePeriod = static_cast<uint32_t>(std::stoul(options_values[i])); |
| 216 | } |
| 217 | // Process profiling-file-format |
| 218 | else if (std::string(options_keys[i]) == std::string("profiling-file-format")) |
| 219 | { |
| 220 | extProfilingParams.m_FileFormat = options_values[i]; |
| 221 | } |
| 222 | // Process serialize-to-dot |
| 223 | else if (std::string(options_keys[i]) == std::string("serialize-to-dot")) |
| 224 | { |
| 225 | this->SetSerializeToDot(options_values[i]); |
| 226 | } |
| 227 | else |
| 228 | { |
| 229 | throw armnn::Exception("Unknown option for the ArmNN Delegate given: " + std::string(options_keys[i])); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | this->SetRuntimeOptions(runtimeOptions); |
| 234 | this->SetOptimizerOptions(optimizerOptions); |
| 235 | this->SetInternalProfilingParams(internalProfilingState, internalProfilingDetail); |
| 236 | this->SetExternalProfilingParams(extProfilingParams); |
| 237 | } |
Sadik Armagan | 3c24f43 | 2020-10-19 17:35:30 +0100 | [diff] [blame] | 238 | } // namespace armnnDelegate |