ArmNN
 21.05
armnn_external_delegate.cpp
Go to the documentation of this file.
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>
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  *
46  * Option key: "logging-severity" \n
47  * Possible values: ["trace"/"debug"/"info"/"warning"/"error"/"fatal"] \n
48  * Description: Sets the logging severity level for ArmNN. Logging
49  * is turned off if this option is not provided.
50  *
51  * Option key: "gpu-tuning-level" \n
52  * Possible values: ["0"/"1"/"2"/"3"] \n
53  * Description: 0=UseOnly(default), 1=RapidTuning, 2=NormalTuning,
54  * 3=ExhaustiveTuning. Requires option gpu-tuning-file.
55  * 1,2 and 3 will create a tuning-file, 0 will apply the
56  * tunings from an existing file
57  *
58  * Option key: "gpu-mlgo-tuning-file" \n
59  * Possible values: [filenameString] \n
60  * Description: File name for the MLGO tuning file
61  *
62  * Option key: "gpu-tuning-file" \n
63  * Possible values: [filenameString] \n
64  * Description: File name for the tuning file.
65  *
66  * Option key: "gpu-kernel-profiling-enabled" \n
67  * Possible values: ["true"/"false"] \n
68  * Description: Enables GPU kernel profiling
69  *
70  * Option key: "save-cached-network" \n
71  * Possible values: ["true"/"false"] \n
72  * Description: Enables saving of the cached network to a file,
73  * specified with the cached-network-filepath option
74  *
75  * Option key: "cached-network-filepath" \n
76  * Possible values: [filenameString] \n
77  * Description: If non-empty, the given file will be used to load/save the cached network.
78  * If save-cached-network is given then the cached network will be saved to the given file.
79  * To save the cached network a file must already exist.
80  * If save-cached-network is not given then the cached network will be loaded from the given file.
81  * This will remove initial compilation time of kernels and speed up the first execution.
82  *
83  * Option key: "enable-fast-math" \n
84  * Possible values: ["true"/"false"] \n
85  * Description: Enables fast_math options in backends that support it
86  *
87  * Option key: "number-of-threads" \n
88  * Possible values: ["1"-"64"] \n
89  * Description: Assign the number of threads used by the CpuAcc backend.
90  * Default is set to 0 (Backend will decide number of threads to use).
91  *
92  * Option key: "reduce-fp32-to-fp16" \n
93  * Possible values: ["true"/"false"] \n
94  * Description: Reduce Fp32 data to Fp16 for faster processing
95  *
96  * Option key: "reduce-fp32-to-bf16" \n
97  * Possible values: ["true"/"false"] \n
98  * Description: Reduce Fp32 data to Bf16 for faster processing
99  *
100  * Option key: "debug-data" \n
101  * Possible values: ["true"/"false"] \n
102  * Description: Add debug data for easier troubleshooting
103  *
104  * Option key: "memory-import" \n
105  * Possible values: ["true"/"false"] \n
106  * Description: Enable memory import
107  *
108  *
109  * @param[in] option_keys Delegate option names
110  * @param[in] options_values Delegate option values
111  * @param[in] num_options Number of delegate options
112  * @param[in,out] report_error Error callback function
113  *
114  * @return An ArmNN delegate if it succeeds else NULL
115  */
116 TfLiteDelegate* tflite_plugin_create_delegate(char** options_keys,
117  char** options_values,
118  size_t num_options,
119  void (*report_error)(const char*))
120 {
121  // Returning null indicates an error during delegate creation so we initialize with that
122  TfLiteDelegate* delegate = nullptr;
123  try
124  {
125  // (Initializes with CpuRef backend)
127  armnn::OptimizerOptions optimizerOptions;
128  for (size_t i = 0; i < num_options; ++i)
129  {
130  // Process backends
131  if (std::string(options_keys[i]) == std::string("backends"))
132  {
133  // The backend option is a comma separated string of backendIDs that needs to be split
134  std::vector<armnn::BackendId> backends;
135  char* pch;
136  pch = strtok(options_values[i],",");
137  while (pch != NULL)
138  {
139  backends.push_back(pch);
140  pch = strtok (NULL, ",");
141  }
142  options.SetBackends(backends);
143  }
144  // Process logging level
145  else if (std::string(options_keys[i]) == std::string("logging-severity"))
146  {
147  options.SetLoggingSeverity(options_values[i]);
148  }
149  // Process GPU backend options
150  else if (std::string(options_keys[i]) == std::string("gpu-tuning-level"))
151  {
152  armnn::BackendOptions option("GpuAcc", {{"TuningLevel", atoi(options_values[i])}});
153  options.AddBackendOption(option);
154  }
155  else if (std::string(options_keys[i]) == std::string("gpu-mlgo-tuning-file"))
156  {
157  armnn::BackendOptions option("GpuAcc", {{"MLGOTuningFilePath", std::string(options_values[i])}});
158  options.AddBackendOption(option);
159  }
160  else if (std::string(options_keys[i]) == std::string("gpu-tuning-file"))
161  {
162  armnn::BackendOptions option("GpuAcc", {{"TuningFile", std::string(options_values[i])}});
163  options.AddBackendOption(option);
164  }
165  else if (std::string(options_keys[i]) == std::string("gpu-kernel-profiling-enabled"))
166  {
167  armnn::BackendOptions option("GpuAcc", {{"KernelProfilingEnabled", (*options_values[i] != '0')}});
168  options.AddBackendOption(option);
169  }
170  else if (std::string(options_keys[i]) == std::string("save-cached-network"))
171  {
172  armnn::BackendOptions option("GpuAcc", {{"SaveCachedNetwork", (*options_values[i] != '0')}});
173  optimizerOptions.m_ModelOptions.push_back(option);
174  }
175  else if (std::string(options_keys[i]) == std::string("cached-network-filepath"))
176  {
177  armnn::BackendOptions option("GpuAcc", {{"CachedNetworkFilePath", std::string(options_values[i])}});
178  optimizerOptions.m_ModelOptions.push_back(option);
179  }
180  // Process GPU & CPU backend options
181  else if (std::string(options_keys[i]) == std::string("enable-fast-math"))
182  {
183  armnn::BackendOptions modelOptionGpu("GpuAcc", {{"FastMathEnabled", (*options_values[i] != '0')}});
184  optimizerOptions.m_ModelOptions.push_back(modelOptionGpu);
185 
186  armnn::BackendOptions modelOptionCpu("CpuAcc", {{"FastMathEnabled", (*options_values[i] != '0')}});
187  optimizerOptions.m_ModelOptions.push_back(modelOptionCpu);
188  }
189  // Process CPU backend options
190  else if (std::string(options_keys[i]) == std::string("number-of-threads"))
191  {
192  unsigned int numberOfThreads = armnn::numeric_cast<unsigned int>(atoi(options_values[i]));
193  armnn::BackendOptions modelOption("CpuAcc", {{"NumberOfThreads", numberOfThreads}});
194  optimizerOptions.m_ModelOptions.push_back(modelOption);
195  }
196  // Process reduce-fp32-to-fp16 option
197  else if (std::string(options_keys[i]) == std::string("reduce-fp32-to-fp16"))
198  {
199  optimizerOptions.m_ReduceFp32ToFp16 = *options_values[i] != '0';
200  }
201  // Process reduce-fp32-to-bf16 option
202  else if (std::string(options_keys[i]) == std::string("reduce-fp32-to-bf16"))
203  {
204  optimizerOptions.m_ReduceFp32ToBf16 = *options_values[i] != '0';
205  }
206  // Process debug-data
207  else if (std::string(options_keys[i]) == std::string("debug-data"))
208  {
209  optimizerOptions.m_Debug = *options_values[i] != '0';
210  }
211  // Process memory-import
212  else if (std::string(options_keys[i]) == std::string("memory-import"))
213  {
214  optimizerOptions.m_ImportEnabled = *options_values[i] != '0';
215  }
216  else
217  {
218  throw armnn::Exception("Unknown option for the ArmNN Delegate given: " + std::string(options_keys[i]));
219  }
220  }
221  options.SetOptimizerOptions(optimizerOptions);
222  delegate = TfLiteArmnnDelegateCreate(options);
223  }
224  catch (const std::exception& ex)
225  {
226  if(report_error)
227  {
228  report_error(ex.what());
229  }
230  }
231  return delegate;
232 }
233 
234 /** Destroy a given delegate plugin
235  *
236  * @param[in] delegate Delegate to destruct
237  */
238 void tflite_plugin_destroy_delegate(TfLiteDelegate* delegate)
239 {
241 }
242 
243 } // extern "C"
244 } // namespace tflite
ModelOptions m_ModelOptions
Definition: INetwork.hpp:168
TfLiteDelegate * tflite_plugin_create_delegate(char **options_keys, char **options_values, size_t num_options, void(*report_error)(const char *))
Create an ArmNN delegate plugin.
std::vector< std::string > gpu_options
This file defines two symbols that need to be exported to use the TFLite external delegate provider...
void SetLoggingSeverity(const armnn::LogSeverity &level)
Sets the severity level for logging within ArmNN that will be used on creation of the delegate...
Struct for the users to pass backend specific options.
TfLiteDelegate * TfLiteArmnnDelegateCreate(armnnDelegate::DelegateOptions options)
Base class for all ArmNN exceptions so that users can filter to just those.
Definition: Exceptions.hpp:46
void SetBackends(const std::vector< armnn::BackendId > &backends)
void TfLiteArmnnDelegateDelete(TfLiteDelegate *tfLiteDelegate)
DelegateOptions TfLiteArmnnDelegateOptionsDefault()
std::enable_if_t< std::is_unsigned< Source >::value &&std::is_unsigned< Dest >::value, Dest > numeric_cast(Source source)
Definition: NumericCast.hpp:35
void AddBackendOption(const armnn::BackendOptions &option)
Appends a backend option to the list of backend options.
void SetOptimizerOptions(const armnn::OptimizerOptions &optimizerOptions)
void tflite_plugin_destroy_delegate(TfLiteDelegate *delegate)
Destroy a given delegate plugin.