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 | { |
Jan Eilers | 2cd1847 | 2020-12-15 10:42:38 +0000 | [diff] [blame] | 29 | |
| 30 | /** |
Jan Eilers | f39f8d8 | 2021-10-26 16:57:34 +0100 | [diff] [blame] | 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 | */ |
Jan Eilers | 2cd1847 | 2020-12-15 10:42:38 +0000 | [diff] [blame] | 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 | { |
Jan Eilers | f39f8d8 | 2021-10-26 16:57:34 +0100 | [diff] [blame] | 41 | // Returning null indicates an error during delegate creation, we initialize with that |
Jan Eilers | 2cd1847 | 2020-12-15 10:42:38 +0000 | [diff] [blame] | 42 | TfLiteDelegate* delegate = nullptr; |
| 43 | try |
| 44 | { |
Jan Eilers | f39f8d8 | 2021-10-26 16:57:34 +0100 | [diff] [blame] | 45 | armnnDelegate::DelegateOptions options (options_keys, options_values, num_options, (*report_error)); |
Jan Eilers | 2cd1847 | 2020-12-15 10:42:38 +0000 | [diff] [blame] | 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 | */ |
| 62 | void tflite_plugin_destroy_delegate(TfLiteDelegate* delegate) |
| 63 | { |
| 64 | armnnDelegate::TfLiteArmnnDelegateDelete(delegate); |
| 65 | } |
| 66 | |
| 67 | } // extern "C" |
| 68 | } // namespace tflite |