• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #include "tensorflow/lite/experimental/acceleration/configuration/delegate_plugin_converter.h"
16 
17 #include <functional>
18 #include <memory>
19 
20 #include "absl/memory/memory.h"
21 #include "tensorflow/lite/core/shims/c/common.h"
22 
23 namespace tflite {
24 namespace delegates {
25 
26 using ::tflite_shims::delegates::DelegatePluginInterface;
27 using ::tflite_shims::delegates::TfLiteOpaqueDelegatePtr;
28 
29 // This class implements the C++ DelegatePluginInterface using
30 // the equivalent C API, which is the TfLiteDelegatePlugin struct.
31 class DelegatePluginViaCApi : public DelegatePluginInterface {
32  public:
DelegatePluginViaCApi(const TfLiteOpaqueDelegatePlugin & plugin_c_api,const::tflite::TFLiteSettings & settings)33   explicit DelegatePluginViaCApi(const TfLiteOpaqueDelegatePlugin& plugin_c_api,
34                                  const ::tflite::TFLiteSettings& settings)
35       : plugin_c_api_(plugin_c_api), tflite_settings_(settings) {}
Create()36   TfLiteOpaqueDelegatePtr Create() override {
37     return TfLiteOpaqueDelegatePtr(plugin_c_api_.create(&tflite_settings_),
38                                    plugin_c_api_.destroy);
39   }
40 
GetDelegateErrno(TfLiteOpaqueDelegate * from_delegate)41   int GetDelegateErrno(TfLiteOpaqueDelegate* from_delegate) override {
42     return plugin_c_api_.get_delegate_errno(from_delegate);
43   }
44 
45  private:
46   TfLiteOpaqueDelegatePlugin plugin_c_api_;
47   const ::tflite::TFLiteSettings& tflite_settings_;
48 };
49 
50 std::function<
51     std::unique_ptr<DelegatePluginInterface>(const ::tflite::TFLiteSettings&)>
DelegatePluginConverter(const TfLiteOpaqueDelegatePlugin & plugin_c_api)52 DelegatePluginConverter(const TfLiteOpaqueDelegatePlugin& plugin_c_api) {
53   return [plugin_c_api](const ::tflite::TFLiteSettings& settings)
54              -> std::unique_ptr<DelegatePluginInterface> {
55     return std::make_unique<DelegatePluginViaCApi>(plugin_c_api, settings);
56   };
57 }
58 
59 }  // namespace delegates
60 }  // namespace tflite
61