1 //
2 // Copyright © 2020, 2023 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #include "armnn_delegate.hpp"
6 #include <armnn/Logging.hpp>
7 #include <armnn/utility/NumericCast.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
30 /**
31 * Implementation of the TfLite external delegate plugin
32 *
33 * For details about what options_keys and option_values are supported please see:
34 * armnnDelegate::DelegateOptions::DelegateOptions(char const* const*, char const* const*,size_t,void (*)(const char*))
35 */
tflite_plugin_create_delegate(char ** options_keys,char ** options_values,size_t num_options,void (* report_error)(const char *))36 TfLiteDelegate* tflite_plugin_create_delegate(char** options_keys,
37 char** options_values,
38 size_t num_options,
39 void (*report_error)(const char*))
40 {
41 // Returning null indicates an error during delegate creation, we initialize with that
42 TfLiteDelegate* delegate = nullptr;
43 try
44 {
45 armnnDelegate::DelegateOptions options (options_keys, options_values, num_options, (*report_error));
46 delegate = TfLiteArmnnDelegateCreate(options);
47 }
48 catch (const std::exception& ex)
49 {
50 if(report_error)
51 {
52 report_error(ex.what());
53 }
54 }
55 return delegate;
56 }
57
58 /** Destroy a given delegate plugin
59 *
60 * @param[in] delegate Delegate to destruct
61 */
tflite_plugin_destroy_delegate(TfLiteDelegate * delegate)62 void tflite_plugin_destroy_delegate(TfLiteDelegate* delegate)
63 {
64 armnnDelegate::TfLiteArmnnDelegateDelete(delegate);
65 }
66
67 } // extern "C"
68 } // namespace tflite