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(); |
Narumol Prangnawarat | 74a3cf5 | 2021-01-29 15:38:54 +0000 | [diff] [blame] | 174 | armnn::OptimizerOptions optimizerOptions; |
Colm Donelan | 3e32a87 | 2021-10-04 22:55:37 +0100 | [diff] [blame] | 175 | bool internalProfilingState = false; |
| 176 | armnn::ProfilingDetailsMethod internalProfilingDetail = armnn::ProfilingDetailsMethod::DetailsWithEvents; |
| 177 | armnn::IRuntime::CreationOptions::ExternalProfilingOptions extProfilingParams; |
Jan Eilers | 2cd1847 | 2020-12-15 10:42:38 +0000 | [diff] [blame] | 178 | for (size_t i = 0; i < num_options; ++i) |
| 179 | { |
| 180 | // Process backends |
| 181 | if (std::string(options_keys[i]) == std::string("backends")) |
| 182 | { |
| 183 | // The backend option is a comma separated string of backendIDs that needs to be split |
| 184 | std::vector<armnn::BackendId> backends; |
| 185 | char* pch; |
| 186 | pch = strtok(options_values[i],","); |
| 187 | while (pch != NULL) |
| 188 | { |
| 189 | backends.push_back(pch); |
| 190 | pch = strtok (NULL, ","); |
| 191 | } |
| 192 | options.SetBackends(backends); |
| 193 | } |
Colm Donelan | 3e32a87 | 2021-10-04 22:55:37 +0100 | [diff] [blame] | 194 | // Process dynamic-backends-path |
| 195 | else if (std::string(options_keys[i]) == std::string("dynamic-backends-path")) |
| 196 | { |
| 197 | options.SetDynamicBackendsPath(std::string(options_values[i])); |
| 198 | } |
Jan Eilers | 2cd1847 | 2020-12-15 10:42:38 +0000 | [diff] [blame] | 199 | // Process logging level |
| 200 | else if (std::string(options_keys[i]) == std::string("logging-severity")) |
| 201 | { |
| 202 | options.SetLoggingSeverity(options_values[i]); |
| 203 | } |
| 204 | // Process GPU backend options |
| 205 | else if (std::string(options_keys[i]) == std::string("gpu-tuning-level")) |
| 206 | { |
| 207 | armnn::BackendOptions option("GpuAcc", {{"TuningLevel", atoi(options_values[i])}}); |
| 208 | options.AddBackendOption(option); |
| 209 | } |
Finn Williams | 4064632 | 2021-02-11 16:16:42 +0000 | [diff] [blame] | 210 | else if (std::string(options_keys[i]) == std::string("gpu-mlgo-tuning-file")) |
| 211 | { |
| 212 | armnn::BackendOptions option("GpuAcc", {{"MLGOTuningFilePath", std::string(options_values[i])}}); |
| 213 | options.AddBackendOption(option); |
| 214 | } |
Jan Eilers | 2cd1847 | 2020-12-15 10:42:38 +0000 | [diff] [blame] | 215 | else if (std::string(options_keys[i]) == std::string("gpu-tuning-file")) |
| 216 | { |
| 217 | armnn::BackendOptions option("GpuAcc", {{"TuningFile", std::string(options_values[i])}}); |
| 218 | options.AddBackendOption(option); |
| 219 | } |
Colm Donelan | 3e32a87 | 2021-10-04 22:55:37 +0100 | [diff] [blame] | 220 | else if (std::string(options_keys[i]) == std::string("gpu-enable-profiling")) |
| 221 | { |
| 222 | options.SetGpuProfilingState(*options_values[i] != '0'); |
| 223 | } |
Jan Eilers | 2cd1847 | 2020-12-15 10:42:38 +0000 | [diff] [blame] | 224 | else if (std::string(options_keys[i]) == std::string("gpu-kernel-profiling-enabled")) |
| 225 | { |
| 226 | armnn::BackendOptions option("GpuAcc", {{"KernelProfilingEnabled", (*options_values[i] != '0')}}); |
| 227 | options.AddBackendOption(option); |
| 228 | } |
Matthew Sloyan | c2b99a87 | 2021-04-27 17:16:12 +0100 | [diff] [blame] | 229 | else if (std::string(options_keys[i]) == std::string("save-cached-network")) |
| 230 | { |
| 231 | armnn::BackendOptions option("GpuAcc", {{"SaveCachedNetwork", (*options_values[i] != '0')}}); |
| 232 | optimizerOptions.m_ModelOptions.push_back(option); |
| 233 | } |
| 234 | else if (std::string(options_keys[i]) == std::string("cached-network-filepath")) |
| 235 | { |
| 236 | armnn::BackendOptions option("GpuAcc", {{"CachedNetworkFilePath", std::string(options_values[i])}}); |
| 237 | optimizerOptions.m_ModelOptions.push_back(option); |
| 238 | } |
| 239 | // Process GPU & CPU backend options |
| 240 | else if (std::string(options_keys[i]) == std::string("enable-fast-math")) |
| 241 | { |
| 242 | armnn::BackendOptions modelOptionGpu("GpuAcc", {{"FastMathEnabled", (*options_values[i] != '0')}}); |
| 243 | optimizerOptions.m_ModelOptions.push_back(modelOptionGpu); |
| 244 | |
| 245 | armnn::BackendOptions modelOptionCpu("CpuAcc", {{"FastMathEnabled", (*options_values[i] != '0')}}); |
| 246 | optimizerOptions.m_ModelOptions.push_back(modelOptionCpu); |
| 247 | } |
| 248 | // Process CPU backend options |
| 249 | else if (std::string(options_keys[i]) == std::string("number-of-threads")) |
| 250 | { |
| 251 | unsigned int numberOfThreads = armnn::numeric_cast<unsigned int>(atoi(options_values[i])); |
| 252 | armnn::BackendOptions modelOption("CpuAcc", {{"NumberOfThreads", numberOfThreads}}); |
| 253 | optimizerOptions.m_ModelOptions.push_back(modelOption); |
| 254 | } |
Narumol Prangnawarat | 74a3cf5 | 2021-01-29 15:38:54 +0000 | [diff] [blame] | 255 | // Process reduce-fp32-to-fp16 option |
| 256 | else if (std::string(options_keys[i]) == std::string("reduce-fp32-to-fp16")) |
| 257 | { |
| 258 | optimizerOptions.m_ReduceFp32ToFp16 = *options_values[i] != '0'; |
| 259 | } |
| 260 | // Process reduce-fp32-to-bf16 option |
| 261 | else if (std::string(options_keys[i]) == std::string("reduce-fp32-to-bf16")) |
| 262 | { |
| 263 | optimizerOptions.m_ReduceFp32ToBf16 = *options_values[i] != '0'; |
| 264 | } |
| 265 | // Process debug-data |
| 266 | else if (std::string(options_keys[i]) == std::string("debug-data")) |
| 267 | { |
| 268 | optimizerOptions.m_Debug = *options_values[i] != '0'; |
| 269 | } |
| 270 | // Process memory-import |
| 271 | else if (std::string(options_keys[i]) == std::string("memory-import")) |
| 272 | { |
| 273 | optimizerOptions.m_ImportEnabled = *options_values[i] != '0'; |
| 274 | } |
Colm Donelan | 3e32a87 | 2021-10-04 22:55:37 +0100 | [diff] [blame] | 275 | // Process enable-internal-profiling |
| 276 | else if (std::string(options_keys[i]) == std::string("enable-internal-profiling")) |
| 277 | { |
| 278 | internalProfilingState = *options_values[i] != '0'; |
| 279 | } |
| 280 | // Process internal-profiling-detail |
| 281 | else if (std::string(options_keys[i]) == std::string("internal-profiling-detail")) |
| 282 | { |
| 283 | uint32_t detailLevel = static_cast<uint32_t>(std::stoul(options_values[i])); |
| 284 | switch (detailLevel) |
| 285 | { |
| 286 | case 1: |
| 287 | internalProfilingDetail = armnn::ProfilingDetailsMethod::DetailsWithEvents; |
| 288 | break; |
| 289 | case 2: |
| 290 | internalProfilingDetail = armnn::ProfilingDetailsMethod::DetailsOnly; |
| 291 | break; |
| 292 | default: |
| 293 | internalProfilingDetail = armnn::ProfilingDetailsMethod::Undefined; |
| 294 | break; |
| 295 | } |
| 296 | } |
| 297 | // Process enable-external-profiling |
| 298 | else if (std::string(options_keys[i]) == std::string("enable-external-profiling")) |
| 299 | { |
| 300 | extProfilingParams.m_EnableProfiling = *options_values[i] != '0'; |
| 301 | } |
| 302 | // Process timeline-profiling |
| 303 | else if (std::string(options_keys[i]) == std::string("timeline-profiling")) |
| 304 | { |
| 305 | extProfilingParams.m_TimelineEnabled = *options_values[i] != '0'; |
| 306 | } |
| 307 | // Process outgoing-capture-file |
| 308 | else if (std::string(options_keys[i]) == std::string("outgoing-capture-file")) |
| 309 | { |
| 310 | extProfilingParams.m_OutgoingCaptureFile = options_values[i]; |
| 311 | } |
| 312 | // Process incoming-capture-file |
| 313 | else if (std::string(options_keys[i]) == std::string("incoming-capture-file")) |
| 314 | { |
| 315 | extProfilingParams.m_IncomingCaptureFile = options_values[i]; |
| 316 | } |
| 317 | // Process file-only-external-profiling |
| 318 | else if (std::string(options_keys[i]) == std::string("file-only-external-profiling")) |
| 319 | { |
| 320 | extProfilingParams.m_FileOnly = *options_values[i] != '0'; |
| 321 | } |
| 322 | // Process counter-capture-period |
| 323 | else if (std::string(options_keys[i]) == std::string("counter-capture-period")) |
| 324 | { |
| 325 | extProfilingParams.m_CapturePeriod = static_cast<uint32_t>(std::stoul(options_values[i])); |
| 326 | } |
| 327 | // Process profiling-file-format |
| 328 | else if (std::string(options_keys[i]) == std::string("profiling-file-format")) |
| 329 | { |
| 330 | extProfilingParams.m_FileFormat = options_values[i]; |
| 331 | } |
| 332 | // Process serialize-to-dot |
| 333 | else if (std::string(options_keys[i]) == std::string("serialize-to-dot")) |
| 334 | { |
| 335 | options.SetSerializeToDot(options_values[i]); |
| 336 | } |
Jan Eilers | 2cd1847 | 2020-12-15 10:42:38 +0000 | [diff] [blame] | 337 | else |
| 338 | { |
| 339 | throw armnn::Exception("Unknown option for the ArmNN Delegate given: " + std::string(options_keys[i])); |
| 340 | } |
| 341 | } |
Narumol Prangnawarat | 74a3cf5 | 2021-01-29 15:38:54 +0000 | [diff] [blame] | 342 | options.SetOptimizerOptions(optimizerOptions); |
Colm Donelan | 3e32a87 | 2021-10-04 22:55:37 +0100 | [diff] [blame] | 343 | options.SetInternalProfilingParams(internalProfilingState, internalProfilingDetail); |
| 344 | options.SetExternalProfilingParams(extProfilingParams); |
Jan Eilers | 2cd1847 | 2020-12-15 10:42:38 +0000 | [diff] [blame] | 345 | delegate = TfLiteArmnnDelegateCreate(options); |
| 346 | } |
| 347 | catch (const std::exception& ex) |
| 348 | { |
| 349 | if(report_error) |
| 350 | { |
| 351 | report_error(ex.what()); |
| 352 | } |
| 353 | } |
| 354 | return delegate; |
| 355 | } |
| 356 | |
| 357 | /** Destroy a given delegate plugin |
| 358 | * |
| 359 | * @param[in] delegate Delegate to destruct |
| 360 | */ |
| 361 | void tflite_plugin_destroy_delegate(TfLiteDelegate* delegate) |
| 362 | { |
| 363 | armnnDelegate::TfLiteArmnnDelegateDelete(delegate); |
| 364 | } |
| 365 | |
| 366 | } // extern "C" |
| 367 | } // namespace tflite |