Jan Eilers | 2cd1847 | 2020-12-15 10:42:38 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2020 Arm Ltd and Contributors. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | #include "armnn_delegate.hpp" |
| 6 | #include <armnn/Logging.hpp> |
Matthew Sloyan | c2b99a87 | 2021-04-27 17:16:12 +0100 | [diff] [blame] | 7 | #include <armnn/utility/NumericCast.hpp> |
Jan Eilers | 2cd1847 | 2020-12-15 10:42:38 +0000 | [diff] [blame] | 8 | |
| 9 | #include <iostream> |
| 10 | #include <tensorflow/lite/minimal_logging.h> |
| 11 | |
| 12 | namespace tflite |
| 13 | { |
| 14 | |
| 15 | /** |
| 16 | * This file defines two symbols that need to be exported to use the TFLite external delegate provider. This is a plugin |
| 17 | * that can be used for fast integration of delegates into benchmark tests and other tools. It allows loading of |
| 18 | * a dynamic delegate library at runtime. |
| 19 | * |
| 20 | * The external delegate also has Tensorflow Lite Python bindings. Therefore the dynamic external delegate |
| 21 | * can be directly used with Tensorflow Lite Python APIs. |
| 22 | * |
| 23 | * See tensorflow/lite/delegates/external for details or visit the tensorflow guide |
| 24 | * [here](https://www.tensorflow.org/lite/performance/implementing_delegate#option_2_leverage_external_delegate) |
| 25 | */ |
| 26 | |
| 27 | extern "C" |
| 28 | { |
| 29 | std::vector<std::string> gpu_options {"gpu-tuning-level", |
| 30 | "gpu-tuning-file", |
| 31 | "gpu-kernel-profiling-enabled"}; |
| 32 | |
| 33 | |
| 34 | /** |
| 35 | * Create an ArmNN delegate plugin |
| 36 | * |
| 37 | * Available options: |
| 38 | * |
| 39 | * Option key: "backends" \n |
| 40 | * Possible values: ["EthosNPU"/"GpuAcc"/"CpuAcc"/"CpuRef"] \n |
| 41 | * Descriptions: A comma separated list without whitespaces of |
| 42 | * backends which should be used for execution. Falls |
| 43 | * back to next backend in list if previous doesn't |
| 44 | * provide support for operation. e.g. "GpuAcc,CpuAcc" |
| 45 | * |
Colm Donelan | 3e32a87 | 2021-10-04 22:55:37 +0100 | [diff] [blame] | 46 | * Option key: "dynamic-backends-path" \n |
| 47 | * Possible values: [filenameString] \n |
| 48 | * Descriptions: This is the directory that will be searched for any dynamic backends. |
| 49 | * |
Jan Eilers | 2cd1847 | 2020-12-15 10:42:38 +0000 | [diff] [blame] | 50 | * Option key: "logging-severity" \n |
| 51 | * Possible values: ["trace"/"debug"/"info"/"warning"/"error"/"fatal"] \n |
| 52 | * Description: Sets the logging severity level for ArmNN. Logging |
| 53 | * is turned off if this option is not provided. |
| 54 | * |
| 55 | * Option key: "gpu-tuning-level" \n |
| 56 | * Possible values: ["0"/"1"/"2"/"3"] \n |
| 57 | * Description: 0=UseOnly(default), 1=RapidTuning, 2=NormalTuning, |
| 58 | * 3=ExhaustiveTuning. Requires option gpu-tuning-file. |
| 59 | * 1,2 and 3 will create a tuning-file, 0 will apply the |
| 60 | * tunings from an existing file |
| 61 | * |
Matthew Sloyan | c2b99a87 | 2021-04-27 17:16:12 +0100 | [diff] [blame] | 62 | * Option key: "gpu-mlgo-tuning-file" \n |
| 63 | * Possible values: [filenameString] \n |
| 64 | * Description: File name for the MLGO tuning file |
| 65 | * |
Jan Eilers | 2cd1847 | 2020-12-15 10:42:38 +0000 | [diff] [blame] | 66 | * Option key: "gpu-tuning-file" \n |
| 67 | * Possible values: [filenameString] \n |
| 68 | * Description: File name for the tuning file. |
| 69 | * |
Colm Donelan | 3e32a87 | 2021-10-04 22:55:37 +0100 | [diff] [blame] | 70 | * Option key: "gpu-enable-profiling" \n |
| 71 | * Possible values: ["true"/"false"] \n |
| 72 | * Description: Enables GPU profiling |
| 73 | * |
Jan Eilers | 2cd1847 | 2020-12-15 10:42:38 +0000 | [diff] [blame] | 74 | * Option key: "gpu-kernel-profiling-enabled" \n |
| 75 | * Possible values: ["true"/"false"] \n |
| 76 | * Description: Enables GPU kernel profiling |
| 77 | * |
Matthew Sloyan | c2b99a87 | 2021-04-27 17:16:12 +0100 | [diff] [blame] | 78 | * Option key: "save-cached-network" \n |
| 79 | * Possible values: ["true"/"false"] \n |
| 80 | * Description: Enables saving of the cached network to a file, |
| 81 | * specified with the cached-network-filepath option |
| 82 | * |
| 83 | * Option key: "cached-network-filepath" \n |
| 84 | * Possible values: [filenameString] \n |
| 85 | * Description: If non-empty, the given file will be used to load/save the cached network. |
| 86 | * If save-cached-network is given then the cached network will be saved to the given file. |
| 87 | * To save the cached network a file must already exist. |
| 88 | * If save-cached-network is not given then the cached network will be loaded from the given file. |
| 89 | * This will remove initial compilation time of kernels and speed up the first execution. |
| 90 | * |
| 91 | * Option key: "enable-fast-math" \n |
| 92 | * Possible values: ["true"/"false"] \n |
| 93 | * Description: Enables fast_math options in backends that support it |
| 94 | * |
| 95 | * Option key: "number-of-threads" \n |
| 96 | * Possible values: ["1"-"64"] \n |
| 97 | * Description: Assign the number of threads used by the CpuAcc backend. |
| 98 | * Default is set to 0 (Backend will decide number of threads to use). |
| 99 | * |
Narumol Prangnawarat | 74a3cf5 | 2021-01-29 15:38:54 +0000 | [diff] [blame] | 100 | * Option key: "reduce-fp32-to-fp16" \n |
| 101 | * Possible values: ["true"/"false"] \n |
| 102 | * Description: Reduce Fp32 data to Fp16 for faster processing |
| 103 | * |
| 104 | * Option key: "reduce-fp32-to-bf16" \n |
| 105 | * Possible values: ["true"/"false"] \n |
| 106 | * Description: Reduce Fp32 data to Bf16 for faster processing |
| 107 | * |
| 108 | * Option key: "debug-data" \n |
| 109 | * Possible values: ["true"/"false"] \n |
| 110 | * Description: Add debug data for easier troubleshooting |
| 111 | * |
| 112 | * Option key: "memory-import" \n |
| 113 | * Possible values: ["true"/"false"] \n |
| 114 | * Description: Enable memory import |
| 115 | * |
Colm Donelan | 3e32a87 | 2021-10-04 22:55:37 +0100 | [diff] [blame] | 116 | * Option key: "enable-internal-profiling" \n |
| 117 | * Possible values: ["true"/"false"] \n |
| 118 | * Description: Enable the internal profiling feature. |
| 119 | * |
| 120 | * Option key: "internal-profiling-detail" \n |
| 121 | * Possible values: [1/2] \n |
| 122 | * Description: Set the detail on the internal profiling. 1 = DetailsWithEvents, 2 = DetailsOnly. |
| 123 | * |
| 124 | * Option key: "enable-external-profiling" \n |
| 125 | * Possible values: ["true"/"false"] \n |
| 126 | * Description: Enable the external profiling feature. |
| 127 | * |
| 128 | * Option key: "timeline-profiling" \n |
| 129 | * Possible values: ["true"/"false"] \n |
| 130 | * Description: Indicates whether external timeline profiling is enabled or not. |
| 131 | * |
| 132 | * Option key: "outgoing-capture-file" \n |
| 133 | * Possible values: [filenameString] \n |
| 134 | * Description: Path to a file in which outgoing timeline profiling messages will be stored. |
| 135 | * |
| 136 | * Option key: "incoming-capture-file" \n |
| 137 | * Possible values: [filenameString] \n |
| 138 | * Description: Path to a file in which incoming timeline profiling messages will be stored. |
| 139 | * |
| 140 | * Option key: "file-only-external-profiling" \n |
| 141 | * Possible values: ["true"/"false"] \n |
| 142 | * Description: Enable profiling output to file only. |
| 143 | * |
| 144 | * Option key: "counter-capture-period" \n |
| 145 | * Possible values: Integer, Default is 10000u |
| 146 | * Description: Value in microseconds of the profiling capture period. \n |
| 147 | * |
| 148 | * Option key: "profiling-file-format" \n |
| 149 | * Possible values: String of ["binary"] \n |
| 150 | * Description: The format of the file used for outputting profiling data. Currently on "binary" is supported. |
| 151 | * |
| 152 | * Option key: "serialize-to-dot" \n |
| 153 | * Possible values: [filenameString] \n |
| 154 | * Description: Serialize the optimized network to the file specified in "dot" format. |
Jan Eilers | 2cd1847 | 2020-12-15 10:42:38 +0000 | [diff] [blame] | 155 | * |
| 156 | * @param[in] option_keys Delegate option names |
| 157 | * @param[in] options_values Delegate option values |
| 158 | * @param[in] num_options Number of delegate options |
| 159 | * @param[in,out] report_error Error callback function |
| 160 | * |
| 161 | * @return An ArmNN delegate if it succeeds else NULL |
| 162 | */ |
| 163 | TfLiteDelegate* tflite_plugin_create_delegate(char** options_keys, |
| 164 | char** options_values, |
| 165 | size_t num_options, |
| 166 | void (*report_error)(const char*)) |
| 167 | { |
| 168 | // Returning null indicates an error during delegate creation so we initialize with that |
| 169 | TfLiteDelegate* delegate = nullptr; |
| 170 | try |
| 171 | { |
| 172 | // (Initializes with CpuRef backend) |
| 173 | armnnDelegate::DelegateOptions options = armnnDelegate::TfLiteArmnnDelegateOptionsDefault(); |
Jan Eilers | b1c62f1 | 2021-10-26 14:56:47 +0100 | [diff] [blame] | 174 | |
| 175 | armnn::IRuntime::CreationOptions runtimeOptions; |
Narumol Prangnawarat | 74a3cf5 | 2021-01-29 15:38:54 +0000 | [diff] [blame] | 176 | armnn::OptimizerOptions optimizerOptions; |
Colm Donelan | 3e32a87 | 2021-10-04 22:55:37 +0100 | [diff] [blame] | 177 | bool internalProfilingState = false; |
| 178 | armnn::ProfilingDetailsMethod internalProfilingDetail = armnn::ProfilingDetailsMethod::DetailsWithEvents; |
| 179 | armnn::IRuntime::CreationOptions::ExternalProfilingOptions extProfilingParams; |
Jan Eilers | 2cd1847 | 2020-12-15 10:42:38 +0000 | [diff] [blame] | 180 | for (size_t i = 0; i < num_options; ++i) |
| 181 | { |
| 182 | // Process backends |
| 183 | if (std::string(options_keys[i]) == std::string("backends")) |
| 184 | { |
| 185 | // The backend option is a comma separated string of backendIDs that needs to be split |
| 186 | std::vector<armnn::BackendId> backends; |
| 187 | char* pch; |
| 188 | pch = strtok(options_values[i],","); |
| 189 | while (pch != NULL) |
| 190 | { |
| 191 | backends.push_back(pch); |
| 192 | pch = strtok (NULL, ","); |
| 193 | } |
| 194 | options.SetBackends(backends); |
| 195 | } |
Colm Donelan | 3e32a87 | 2021-10-04 22:55:37 +0100 | [diff] [blame] | 196 | // Process dynamic-backends-path |
| 197 | else if (std::string(options_keys[i]) == std::string("dynamic-backends-path")) |
| 198 | { |
Jan Eilers | b1c62f1 | 2021-10-26 14:56:47 +0100 | [diff] [blame] | 199 | runtimeOptions.m_DynamicBackendsPath = std::string(options_values[i]); |
Colm Donelan | 3e32a87 | 2021-10-04 22:55:37 +0100 | [diff] [blame] | 200 | } |
Jan Eilers | 2cd1847 | 2020-12-15 10:42:38 +0000 | [diff] [blame] | 201 | // Process logging level |
| 202 | else if (std::string(options_keys[i]) == std::string("logging-severity")) |
| 203 | { |
| 204 | options.SetLoggingSeverity(options_values[i]); |
| 205 | } |
| 206 | // Process GPU backend options |
| 207 | else if (std::string(options_keys[i]) == std::string("gpu-tuning-level")) |
| 208 | { |
| 209 | armnn::BackendOptions option("GpuAcc", {{"TuningLevel", atoi(options_values[i])}}); |
Jan Eilers | b1c62f1 | 2021-10-26 14:56:47 +0100 | [diff] [blame] | 210 | runtimeOptions.m_BackendOptions.push_back(option); |
Jan Eilers | 2cd1847 | 2020-12-15 10:42:38 +0000 | [diff] [blame] | 211 | } |
Finn Williams | 4064632 | 2021-02-11 16:16:42 +0000 | [diff] [blame] | 212 | else if (std::string(options_keys[i]) == std::string("gpu-mlgo-tuning-file")) |
| 213 | { |
| 214 | armnn::BackendOptions option("GpuAcc", {{"MLGOTuningFilePath", std::string(options_values[i])}}); |
Jan Eilers | b1c62f1 | 2021-10-26 14:56:47 +0100 | [diff] [blame] | 215 | optimizerOptions.m_ModelOptions.push_back(option); |
Finn Williams | 4064632 | 2021-02-11 16:16:42 +0000 | [diff] [blame] | 216 | } |
Jan Eilers | 2cd1847 | 2020-12-15 10:42:38 +0000 | [diff] [blame] | 217 | else if (std::string(options_keys[i]) == std::string("gpu-tuning-file")) |
| 218 | { |
| 219 | armnn::BackendOptions option("GpuAcc", {{"TuningFile", std::string(options_values[i])}}); |
Jan Eilers | b1c62f1 | 2021-10-26 14:56:47 +0100 | [diff] [blame] | 220 | runtimeOptions.m_BackendOptions.push_back(option); |
Jan Eilers | 2cd1847 | 2020-12-15 10:42:38 +0000 | [diff] [blame] | 221 | } |
Colm Donelan | 3e32a87 | 2021-10-04 22:55:37 +0100 | [diff] [blame] | 222 | else if (std::string(options_keys[i]) == std::string("gpu-enable-profiling")) |
| 223 | { |
Jan Eilers | b1c62f1 | 2021-10-26 14:56:47 +0100 | [diff] [blame] | 224 | runtimeOptions.m_EnableGpuProfiling = (*options_values[i] != '0'); |
Colm Donelan | 3e32a87 | 2021-10-04 22:55:37 +0100 | [diff] [blame] | 225 | } |
Jan Eilers | 2cd1847 | 2020-12-15 10:42:38 +0000 | [diff] [blame] | 226 | else if (std::string(options_keys[i]) == std::string("gpu-kernel-profiling-enabled")) |
| 227 | { |
| 228 | armnn::BackendOptions option("GpuAcc", {{"KernelProfilingEnabled", (*options_values[i] != '0')}}); |
Jan Eilers | b1c62f1 | 2021-10-26 14:56:47 +0100 | [diff] [blame] | 229 | runtimeOptions.m_BackendOptions.push_back(option); |
Jan Eilers | 2cd1847 | 2020-12-15 10:42:38 +0000 | [diff] [blame] | 230 | } |
Matthew Sloyan | c2b99a87 | 2021-04-27 17:16:12 +0100 | [diff] [blame] | 231 | else if (std::string(options_keys[i]) == std::string("save-cached-network")) |
| 232 | { |
| 233 | armnn::BackendOptions option("GpuAcc", {{"SaveCachedNetwork", (*options_values[i] != '0')}}); |
| 234 | optimizerOptions.m_ModelOptions.push_back(option); |
| 235 | } |
| 236 | else if (std::string(options_keys[i]) == std::string("cached-network-filepath")) |
| 237 | { |
| 238 | armnn::BackendOptions option("GpuAcc", {{"CachedNetworkFilePath", std::string(options_values[i])}}); |
| 239 | optimizerOptions.m_ModelOptions.push_back(option); |
| 240 | } |
| 241 | // Process GPU & CPU backend options |
| 242 | else if (std::string(options_keys[i]) == std::string("enable-fast-math")) |
| 243 | { |
| 244 | armnn::BackendOptions modelOptionGpu("GpuAcc", {{"FastMathEnabled", (*options_values[i] != '0')}}); |
| 245 | optimizerOptions.m_ModelOptions.push_back(modelOptionGpu); |
| 246 | |
| 247 | armnn::BackendOptions modelOptionCpu("CpuAcc", {{"FastMathEnabled", (*options_values[i] != '0')}}); |
| 248 | optimizerOptions.m_ModelOptions.push_back(modelOptionCpu); |
| 249 | } |
| 250 | // Process CPU backend options |
| 251 | else if (std::string(options_keys[i]) == std::string("number-of-threads")) |
| 252 | { |
| 253 | unsigned int numberOfThreads = armnn::numeric_cast<unsigned int>(atoi(options_values[i])); |
| 254 | armnn::BackendOptions modelOption("CpuAcc", {{"NumberOfThreads", numberOfThreads}}); |
| 255 | optimizerOptions.m_ModelOptions.push_back(modelOption); |
| 256 | } |
Narumol Prangnawarat | 74a3cf5 | 2021-01-29 15:38:54 +0000 | [diff] [blame] | 257 | // Process reduce-fp32-to-fp16 option |
| 258 | else if (std::string(options_keys[i]) == std::string("reduce-fp32-to-fp16")) |
| 259 | { |
| 260 | optimizerOptions.m_ReduceFp32ToFp16 = *options_values[i] != '0'; |
| 261 | } |
| 262 | // Process reduce-fp32-to-bf16 option |
| 263 | else if (std::string(options_keys[i]) == std::string("reduce-fp32-to-bf16")) |
| 264 | { |
| 265 | optimizerOptions.m_ReduceFp32ToBf16 = *options_values[i] != '0'; |
| 266 | } |
| 267 | // Process debug-data |
| 268 | else if (std::string(options_keys[i]) == std::string("debug-data")) |
| 269 | { |
| 270 | optimizerOptions.m_Debug = *options_values[i] != '0'; |
| 271 | } |
| 272 | // Process memory-import |
| 273 | else if (std::string(options_keys[i]) == std::string("memory-import")) |
| 274 | { |
| 275 | optimizerOptions.m_ImportEnabled = *options_values[i] != '0'; |
| 276 | } |
Colm Donelan | 3e32a87 | 2021-10-04 22:55:37 +0100 | [diff] [blame] | 277 | // Process enable-internal-profiling |
| 278 | else if (std::string(options_keys[i]) == std::string("enable-internal-profiling")) |
| 279 | { |
| 280 | internalProfilingState = *options_values[i] != '0'; |
Derek Lamberti | 827e4bf | 2021-10-14 00:38:35 +0100 | [diff] [blame] | 281 | optimizerOptions.m_ProfilingEnabled = internalProfilingState; |
Colm Donelan | 3e32a87 | 2021-10-04 22:55:37 +0100 | [diff] [blame] | 282 | } |
| 283 | // Process internal-profiling-detail |
| 284 | else if (std::string(options_keys[i]) == std::string("internal-profiling-detail")) |
| 285 | { |
| 286 | uint32_t detailLevel = static_cast<uint32_t>(std::stoul(options_values[i])); |
| 287 | switch (detailLevel) |
| 288 | { |
| 289 | case 1: |
| 290 | internalProfilingDetail = armnn::ProfilingDetailsMethod::DetailsWithEvents; |
| 291 | break; |
| 292 | case 2: |
| 293 | internalProfilingDetail = armnn::ProfilingDetailsMethod::DetailsOnly; |
| 294 | break; |
| 295 | default: |
| 296 | internalProfilingDetail = armnn::ProfilingDetailsMethod::Undefined; |
| 297 | break; |
| 298 | } |
| 299 | } |
| 300 | // Process enable-external-profiling |
| 301 | else if (std::string(options_keys[i]) == std::string("enable-external-profiling")) |
| 302 | { |
| 303 | extProfilingParams.m_EnableProfiling = *options_values[i] != '0'; |
| 304 | } |
| 305 | // Process timeline-profiling |
| 306 | else if (std::string(options_keys[i]) == std::string("timeline-profiling")) |
| 307 | { |
| 308 | extProfilingParams.m_TimelineEnabled = *options_values[i] != '0'; |
| 309 | } |
| 310 | // Process outgoing-capture-file |
| 311 | else if (std::string(options_keys[i]) == std::string("outgoing-capture-file")) |
| 312 | { |
| 313 | extProfilingParams.m_OutgoingCaptureFile = options_values[i]; |
| 314 | } |
| 315 | // Process incoming-capture-file |
| 316 | else if (std::string(options_keys[i]) == std::string("incoming-capture-file")) |
| 317 | { |
| 318 | extProfilingParams.m_IncomingCaptureFile = options_values[i]; |
| 319 | } |
| 320 | // Process file-only-external-profiling |
| 321 | else if (std::string(options_keys[i]) == std::string("file-only-external-profiling")) |
| 322 | { |
| 323 | extProfilingParams.m_FileOnly = *options_values[i] != '0'; |
| 324 | } |
| 325 | // Process counter-capture-period |
| 326 | else if (std::string(options_keys[i]) == std::string("counter-capture-period")) |
| 327 | { |
| 328 | extProfilingParams.m_CapturePeriod = static_cast<uint32_t>(std::stoul(options_values[i])); |
| 329 | } |
| 330 | // Process profiling-file-format |
| 331 | else if (std::string(options_keys[i]) == std::string("profiling-file-format")) |
| 332 | { |
| 333 | extProfilingParams.m_FileFormat = options_values[i]; |
| 334 | } |
| 335 | // Process serialize-to-dot |
| 336 | else if (std::string(options_keys[i]) == std::string("serialize-to-dot")) |
| 337 | { |
| 338 | options.SetSerializeToDot(options_values[i]); |
| 339 | } |
Jan Eilers | 2cd1847 | 2020-12-15 10:42:38 +0000 | [diff] [blame] | 340 | else |
| 341 | { |
| 342 | throw armnn::Exception("Unknown option for the ArmNN Delegate given: " + std::string(options_keys[i])); |
| 343 | } |
| 344 | } |
Jan Eilers | b1c62f1 | 2021-10-26 14:56:47 +0100 | [diff] [blame] | 345 | |
| 346 | options.SetRuntimeOptions(runtimeOptions); |
Narumol Prangnawarat | 74a3cf5 | 2021-01-29 15:38:54 +0000 | [diff] [blame] | 347 | options.SetOptimizerOptions(optimizerOptions); |
Colm Donelan | 3e32a87 | 2021-10-04 22:55:37 +0100 | [diff] [blame] | 348 | options.SetInternalProfilingParams(internalProfilingState, internalProfilingDetail); |
| 349 | options.SetExternalProfilingParams(extProfilingParams); |
Jan Eilers | 2cd1847 | 2020-12-15 10:42:38 +0000 | [diff] [blame] | 350 | delegate = TfLiteArmnnDelegateCreate(options); |
| 351 | } |
| 352 | catch (const std::exception& ex) |
| 353 | { |
| 354 | if(report_error) |
| 355 | { |
| 356 | report_error(ex.what()); |
| 357 | } |
| 358 | } |
| 359 | return delegate; |
| 360 | } |
| 361 | |
| 362 | /** Destroy a given delegate plugin |
| 363 | * |
| 364 | * @param[in] delegate Delegate to destruct |
| 365 | */ |
| 366 | void tflite_plugin_destroy_delegate(TfLiteDelegate* delegate) |
| 367 | { |
| 368 | armnnDelegate::TfLiteArmnnDelegateDelete(delegate); |
| 369 | } |
| 370 | |
| 371 | } // extern "C" |
| 372 | } // namespace tflite |