blob: c3875740e1d44207d425ed202acea0d73ba6623a [file] [log] [blame]
Jan Eilers2cd18472020-12-15 10:42:38 +00001//
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 Sloyanc2b99a872021-04-27 17:16:12 +01007#include <armnn/utility/NumericCast.hpp>
Jan Eilers2cd18472020-12-15 10:42:38 +00008
9#include <iostream>
10#include <tensorflow/lite/minimal_logging.h>
11
12namespace 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
27extern "C"
28{
Jan Eilers2cd18472020-12-15 10:42:38 +000029
30/**
Jan Eilersf39f8d82021-10-26 16:57:34 +010031 * 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 */
Jan Eilers2cd18472020-12-15 10:42:38 +000036TfLiteDelegate* tflite_plugin_create_delegate(char** options_keys,
37 char** options_values,
38 size_t num_options,
39 void (*report_error)(const char*))
40{
Jan Eilersf39f8d82021-10-26 16:57:34 +010041 // Returning null indicates an error during delegate creation, we initialize with that
Jan Eilers2cd18472020-12-15 10:42:38 +000042 TfLiteDelegate* delegate = nullptr;
43 try
44 {
Jan Eilersf39f8d82021-10-26 16:57:34 +010045 armnnDelegate::DelegateOptions options (options_keys, options_values, num_options, (*report_error));
Jan Eilers2cd18472020-12-15 10:42:38 +000046 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 */
62void tflite_plugin_destroy_delegate(TfLiteDelegate* delegate)
63{
64 armnnDelegate::TfLiteArmnnDelegateDelete(delegate);
65}
66
67} // extern "C"
68} // namespace tflite